jqueryag.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Auto Expanding Text Area (1.2.2)
  3. * by Chrys Bader (www.chrysbader.com)
  4. * chrysb@gmail.com
  5. *
  6. * Special thanks to:
  7. * Jake Chapa - jake@hybridstudio.com
  8. * John Resig - jeresig@gmail.com
  9. *
  10. * Copyright (c) 2008 Chrys Bader (www.chrysbader.com)
  11. * Dual licensed under the MIT (MIT-LICENSE.txt)
  12. * and GPL (GPL-LICENSE.txt) licenses.
  13. *
  14. *
  15. * NOTE: This script requires jQuery to work. Download jQuery at www.jquery.com
  16. *
  17. */
  18. (function(jQuery) {
  19. var self = null;
  20. jQuery.fn.autogrow = function(o)
  21. {
  22. return this.each(function() {
  23. new jQuery.autogrow(this, o);
  24. });
  25. };
  26. /**
  27. * The autogrow object.
  28. *
  29. * @constructor
  30. * @name jQuery.autogrow
  31. * @param Object e The textarea to create the autogrow for.
  32. * @param Hash o A set of key/value pairs to set as configuration properties.
  33. * @cat Plugins/autogrow
  34. */
  35. jQuery.autogrow = function (e, o)
  36. {
  37. this.options = o || {};
  38. this.dummy = null;
  39. this.interval = null;
  40. this.line_height = this.options.lineHeight || parseInt(jQuery(e).css('line-height'));
  41. this.min_height = this.options.minHeight || parseInt(jQuery(e).css('min-height'));
  42. this.max_height = this.options.maxHeight || parseInt(jQuery(e).css('max-height'));;
  43. this.textarea = jQuery(e);
  44. if(this.line_height == NaN)
  45. this.line_height = 0;
  46. // Only one textarea activated at a time, the one being used
  47. this.init();
  48. };
  49. jQuery.autogrow.fn = jQuery.autogrow.prototype = {
  50. autogrow: '1.2.2'
  51. };
  52. jQuery.autogrow.fn.extend = jQuery.autogrow.extend = jQuery.extend;
  53. jQuery.autogrow.fn.extend({
  54. init: function() {
  55. var self = this;
  56. this.textarea.css({overflow: 'hidden', display: 'block'});
  57. this.textarea.bind('focus', function() { self.startExpand() } ).bind('blur', function() { self.stopExpand() });
  58. this.checkExpand();
  59. },
  60. startExpand: function() {
  61. var self = this;
  62. this.interval = window.setInterval(function() {self.checkExpand()}, 400);
  63. },
  64. stopExpand: function() {
  65. clearInterval(this.interval);
  66. },
  67. checkExpand: function() {
  68. if (this.dummy == null)
  69. {
  70. this.dummy = jQuery('<div></div>');
  71. this.dummy.css({
  72. 'font-size' : this.textarea.css('font-size'),
  73. 'font-family': this.textarea.css('font-family'),
  74. 'width' : this.textarea.css('width'),
  75. 'padding' : this.textarea.css('padding'),
  76. 'line-height': this.line_height + 'px',
  77. 'overflow-x' : 'hidden',
  78. 'position' : 'absolute',
  79. 'top' : 0,
  80. 'left' : -9999
  81. }).appendTo('body');
  82. }
  83. // Strip HTML tags
  84. var html = this.textarea.val().replace(/(<|>)/g, '');
  85. // IE is different, as per usual
  86. if (navigator.userAgent
  87. .search(/MSIE/i) > -1)
  88. {
  89. html = html.replace(/\n/g, '<BR>new');
  90. }
  91. else
  92. {
  93. html = html.replace(/\n/g, '<br>new');
  94. }
  95. if (this.dummy.html() != html)
  96. {
  97. this.dummy.html(html);
  98. if (this.max_height > 0 && (this.dummy.height() + this.line_height > this.max_height))
  99. {
  100. this.textarea.css('overflow-y', 'auto');
  101. }
  102. else
  103. {
  104. this.textarea.css('overflow-y', 'hidden');
  105. if (this.textarea.height() < this.dummy.height() + this.line_height || (this.dummy.height() < this.textarea.height()))
  106. {
  107. this.textarea.animate({height: (this.dummy.height() + this.line_height) + 'px'}, 100);
  108. }
  109. }
  110. }
  111. }
  112. });
  113. })(jQuery);