jquery.autoGrow.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*!
  2. * ----------------------------------------------------------------------------
  3. * "THE BEER-WARE LICENSE" (Revision 42):
  4. * <jevin9@gmail.com> wrote this file. As long as you retain this notice you
  5. * can do whatever you want with this stuff. If we meet some day, and you think
  6. * this stuff is worth it, you can buy me a beer in return. Jevin O. Sewaruth
  7. * ----------------------------------------------------------------------------
  8. *
  9. * Autogrow Textarea Plugin Version v3.0
  10. * http://www.technoreply.com/autogrow-textarea-plugin-3-0
  11. *
  12. * THIS PLUGIN IS DELIVERD ON A PAY WHAT YOU WHANT BASIS. IF THE PLUGIN WAS USEFUL TO YOU, PLEASE CONSIDER BUYING THE PLUGIN HERE :
  13. * https://sites.fastspring.com/technoreply/instant/autogrowtextareaplugin
  14. *
  15. * Date: October 15, 2012
  16. *
  17. * Zammad modification: remove overflow:hidden when maximum height is reached
  18. *
  19. */
  20. jQuery.fn.autoGrow = function(options) {
  21. return this.each(function() {
  22. var settings = jQuery.extend({
  23. extraLine: true,
  24. }, options);
  25. var createMirror = function(textarea) {
  26. jQuery(textarea).after('<div class="autogrow-textarea-mirror"></div>');
  27. return jQuery(textarea).next('.autogrow-textarea-mirror')[0];
  28. }
  29. var sendContentToMirror = function (textarea) {
  30. mirror.innerHTML = String(textarea.value)
  31. .replace(/&/g, '&amp;')
  32. .replace(/"/g, '&quot;')
  33. .replace(/'/g, '&#39;')
  34. .replace(/</g, '&lt;')
  35. .replace(/>/g, '&gt;')
  36. .replace(/ /g, '&nbsp;')
  37. .replace(/\n/g, '<br />') +
  38. (settings.extraLine? '.<br/>.' : '')
  39. ;
  40. if (jQuery(textarea).height() != jQuery(mirror).height()) {
  41. jQuery(textarea).height(jQuery(mirror).height());
  42. var maxHeight = parseInt(jQuery(textarea).css('max-height'), 10);
  43. var overflow = jQuery(mirror).height() > maxHeight ? '' : 'hidden'
  44. jQuery(textarea).css('overflow', overflow);
  45. }
  46. }
  47. var growTextarea = function () {
  48. sendContentToMirror(this);
  49. }
  50. // Create a mirror
  51. var mirror = createMirror(this);
  52. // Style the mirror
  53. mirror.style.display = 'none';
  54. mirror.style.wordWrap = 'break-word';
  55. mirror.style.whiteSpace = 'normal';
  56. mirror.style.padding = jQuery(this).css('paddingTop') + ' ' +
  57. jQuery(this).css('paddingRight') + ' ' +
  58. jQuery(this).css('paddingBottom') + ' ' +
  59. jQuery(this).css('paddingLeft');
  60. mirror.style.width = jQuery(this).css('width');
  61. mirror.style.fontFamily = jQuery(this).css('font-family');
  62. mirror.style.fontSize = jQuery(this).css('font-size');
  63. mirror.style.lineHeight = jQuery(this).css('line-height');
  64. // Style the textarea
  65. this.style.overflow = "hidden";
  66. this.style.minHeight = this.rows+"em";
  67. // Bind the textarea's event
  68. this.onkeyup = growTextarea;
  69. // Fire the event for text already present
  70. sendContentToMirror(this);
  71. });
  72. };