123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. (function($) {
  2. /*
  3. * Auto-growing textareas; technique ripped from Facebook
  4. */
  5. $.fn.autogrow = function(options) {
  6. this.filter('textarea').each(function() {
  7. var $this = $(this),
  8. minHeight = $this.height(),
  9. lineHeight = $this.css('lineHeight');
  10. var shadow = $('<div></div>').css({
  11. position: 'absolute',
  12. top: -10000,
  13. left: -10000,
  14. width: $(this).width() - parseInt($this.css('paddingLeft')) - parseInt($this.css('paddingRight')),
  15. fontSize: $this.css('fontSize'),
  16. fontFamily: $this.css('fontFamily'),
  17. lineHeight: $this.css('lineHeight'),
  18. resize: 'none'
  19. }).appendTo(document.body);
  20. var update = function() {
  21. var times = function(string, number) {
  22. for (var i = 0, r = ''; i < number; i ++) r += string;
  23. return r;
  24. };
  25. var val = this.value.replace(/</g, '&lt;')
  26. .replace(/>/g, '&gt;')
  27. .replace(/&/g, '&amp;')
  28. .replace(/\n$/, '<br/>&nbsp;')
  29. .replace(/\n/g, '<br/>')
  30. .replace(/ {2,}/g, function(space) { return times('&nbsp;', space.length -1) + ' ' });
  31. shadow.html(val);
  32. $(this).css('height', Math.max(shadow.height() + 20, minHeight));
  33. }
  34. $(this).change(update).keyup(update).keydown(update);
  35. update.apply(this);
  36. });
  37. return this;
  38. }
  39. })(jQuery);