12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- (function($)
- {
- /**
- * Auto-growing textareas; technique ripped from Facebook
- *
- * http://github.com/jaz303/jquery-grab-bag/tree/master/javascripts/jquery.autogrow-textarea.js
- */
- $.fn.autogrow = function(options)
- {
- return this.filter('textarea').each(function()
- {
- var self = this;
- var $self = $(self);
- var minHeight = $self.height();
- var noFlickerPad = $self.hasClass('autogrow-short') ? 0 : parseInt($self.css('lineHeight'));
- var shadow = $('<div></div>').css({
- position: 'absolute',
- top: -10000,
- left: -10000,
- width: $self.width(),
- fontSize: $self.css('fontSize'),
- fontFamily: $self.css('fontFamily'),
- fontWeight: $self.css('fontWeight'),
- lineHeight: $self.css('lineHeight'),
- resize: 'none'
- }).appendTo(document.body);
- var update = function()
- {
- var times = function(string, number)
- {
- for (var i=0, r=''; i<number; i++) r += string;
- return r;
- };
- var val = self.value.replace(/</g, '<')
- .replace(/>/g, '>')
- .replace(/&/g, '&')
- .replace(/\n$/, '<br/> ')
- .replace(/\n/g, '<br/>')
- .replace(/ {2,}/g, function(space){ return times(' ', space.length - 1) + ' ' });
- shadow.css('width', $self.width());
- shadow.html(val);
- $self.css('height', Math.max(shadow.height() + noFlickerPad, minHeight));
- }
- $self.change(update).keyup(update).keydown(update);
- $(window).resize(update);
- update();
- });
- };
- })(jQuery);
|