home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2012 January / maximum-cd-2012-01.iso / DiscContents / digsby_setup.exe / res / html / jquery.jgrow-0.3-singleline-mod.js < prev    next >
Encoding:
JavaScript  |  2010-06-25  |  2.5 KB  |  90 lines

  1. /*
  2. * jGrow
  3. * jGrow is a jQuery plug-in that makes the textarea adjust its size according to the length of the text.
  4. * @requires jQuery v1.2.3 or later
  5. * @version 0.3
  6. * @author Berker Peksag http://lab.berkerpeksag.com/jGrow
  7. * Dual licensed under the MIT and GPL licenses:
  8. * http://www.opensource.org/licenses/mit-license.php
  9. * http://www.gnu.org/licenses/gpl.html
  10. */
  11.  
  12. (function($) {
  13.  
  14.     //jGrow:
  15.     $.fn.jGrow = function(settings) {
  16.         var settings = $.extend({}, $.fn.jGrow.defaults, settings);
  17.     
  18.         this.each(function() {
  19.             var $t = $(this);
  20.             $t.css(settings);
  21.             var c_h = parseInt($t.css("height"));
  22.             settings.cache_height = c_h;
  23.             init($(this), settings);
  24.         }).keyup(function() {
  25.             init($(this), settings);
  26.         });
  27.         
  28.         function init(k, o) {
  29.             var $t = k;
  30.             var id = "jgrow-" + $t.attr("name");
  31.             var h = $t.css("height");
  32.             h = parseInt(h == "auto" ? "50px" : h);
  33.             var l = $t.css("line-height");
  34.             l = parseInt(l == "normal" ? "16px" : l);
  35.             var v = $('<div/>').text($t.val()).html().replace(/\n/g, "<br />");
  36.                 
  37.             if (!$("#" + id).length) {
  38.                     
  39.                 $("<div/>").attr("id", id).css({
  40.                     "border": $t.css("border"),
  41.                     "font-family": $t.css("font-family"),
  42.                     "font-size": $t.css("font-size"),
  43.                     "font-weight": $t.css("font-weight"),
  44.                     //was -999px
  45.                     "left": "-9999px",
  46.                     "overflow": "auto",
  47.                     "padding": $t.css("padding"),
  48.                     "position": "absolute",
  49.                     "top": 0,
  50.                     "width": $t.css("width"),
  51.                     //follow similar wrapping characteristics to the textarea
  52.                     "white-space": "pre-wrap"
  53.                 }).html(v).appendTo("body");
  54.                     
  55.             } else {
  56.                 //in case the page has changed width.
  57.                 $("#" + id).css({"width": $t.css("width")});
  58.                 $("#" + id).html(v + "a"); //+ "a" = grow when the next char typed won't fit on this line.
  59.             }
  60.             
  61.             var n_h = parseInt($("#" + id).css("height"));
  62.                 
  63.             if (n_h > parseInt(settings.max_height)) {
  64.                 $t.css({
  65.                     overflow: "auto", "height": (parseInt(settings.max_height) + l) + "px"
  66.                 });
  67.             } else if (n_h > 0 && n_h != settings.cache_height) { //shrink, too (mostly for resizing the page)
  68.                 $t.css("height", n_h + "px");
  69.             } else {
  70.                 $t.css("height", settings.cache_height + "px");
  71.             }
  72.             
  73.             $("#debug").html(n_h + " - " + h + " - " + settings.cache_height);
  74.         }
  75.     };
  76.     
  77.     //Default configuration:
  78.     $.fn.jGrow.defaults = {
  79.         max_height  : "700px",
  80.         resize      : "none",
  81.         overflow    : "hidden",
  82.         cache_height: 0
  83.     };
  84.     
  85.     //Current version:
  86.     $.fn.jGrow.version = 0.3;
  87.  
  88. })(jQuery);
  89.