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.js < prev    next >
Encoding:
JavaScript  |  2009-07-10  |  2.2 KB  |  86 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.             
  36.             var v = $t.val().replace(/\n/g, "<br />");
  37.                 
  38.             if (!$("#" + id).length) {
  39.                     
  40.                 $("<div/>").attr("id", id).css({
  41.                     "border": $t.css("border"),
  42.                     "font-family": $t.css("font-family"),
  43.                     "font-size": $t.css("font-size"),
  44.                     "font-weight": $t.css("font-weight"),
  45.                     "left": "-999px",
  46.                     "overflow": "auto",
  47.                     "padding": $t.css("padding"),
  48.                     "position": "absolute",
  49.                     "top": 0,
  50.                     "width": $t.css("width")
  51.                 }).html(v).appendTo("body");
  52.                     
  53.             } else {
  54.                 $("#" + id).html(v);
  55.             }
  56.             
  57.             var n_h = parseInt($("#" + id).css("height")) + l;
  58.                 
  59.             if (n_h > parseInt(settings.max_height)) {
  60.                 $t.css({
  61.                     overflow: "auto", "height": (parseInt(settings.max_height) + l) + "px"
  62.                 });
  63.             } else if (n_h > settings.cache_height) {
  64.                 $t.css("height", n_h + "px");
  65.             } else {
  66.                 $t.css("height", settings.cache_height + "px");
  67.             }
  68.             
  69.             $("#debug").html(n_h + " - " + h + " - " + settings.cache_height);
  70.         }
  71.     };
  72.     
  73.     //Default configuration:
  74.     $.fn.jGrow.defaults = {
  75.         max_height  : "700px",
  76.         resize      : "none",
  77.         overflow    : "hidden",
  78.         cache_height: 0
  79.     };
  80.     
  81.     //Current version:
  82.     $.fn.jGrow.version = 0.3;
  83.  
  84. })(jQuery);
  85.