autosize.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*!
  2. Autosize 3.0.5
  3. license: MIT
  4. http://www.jacklmoore.com/autosize
  5. */
  6. (function (global, factory) {
  7. if (typeof define === 'function' && define.amd) {
  8. define(['exports', 'module'], factory);
  9. } else if (typeof exports !== 'undefined' && typeof module !== 'undefined') {
  10. factory(exports, module);
  11. } else {
  12. var mod = {
  13. exports: {}
  14. };
  15. factory(mod.exports, mod);
  16. global.autosize = mod.exports;
  17. }
  18. })(this, function (exports, module) {
  19. 'use strict';
  20. function assign(ta) {
  21. var _ref = arguments[1] === undefined ? {} : arguments[1];
  22. var _ref$setOverflowX = _ref.setOverflowX;
  23. var setOverflowX = _ref$setOverflowX === undefined ? true : _ref$setOverflowX;
  24. var _ref$setOverflowY = _ref.setOverflowY;
  25. var setOverflowY = _ref$setOverflowY === undefined ? true : _ref$setOverflowY;
  26. if (!ta || !ta.nodeName || ta.nodeName !== 'TEXTAREA' || ta.hasAttribute('data-autosize-on')) return;
  27. var heightOffset = null;
  28. var overflowY = 'hidden';
  29. function init() {
  30. var style = window.getComputedStyle(ta, null);
  31. if (style.resize === 'vertical') {
  32. ta.style.resize = 'none';
  33. } else if (style.resize === 'both') {
  34. ta.style.resize = 'horizontal';
  35. }
  36. if (style.boxSizing === 'content-box') {
  37. heightOffset = -(parseFloat(style.paddingTop) + parseFloat(style.paddingBottom));
  38. } else {
  39. heightOffset = parseFloat(style.borderTopWidth) + parseFloat(style.borderBottomWidth);
  40. }
  41. update();
  42. }
  43. function changeOverflow(value) {
  44. {
  45. // Chrome/Safari-specific fix:
  46. // When the textarea y-overflow is hidden, Chrome/Safari do not reflow the text to account for the space
  47. // made available by removing the scrollbar. The following forces the necessary text reflow.
  48. var width = ta.style.width;
  49. ta.style.width = '0px';
  50. // Force reflow:
  51. /* jshint ignore:start */
  52. ta.offsetWidth;
  53. /* jshint ignore:end */
  54. ta.style.width = width;
  55. }
  56. overflowY = value;
  57. if (setOverflowY) {
  58. ta.style.overflowY = value;
  59. }
  60. update();
  61. }
  62. function update() {
  63. var startHeight = ta.style.height;
  64. var htmlTop = document.documentElement.scrollTop;
  65. var bodyTop = document.body.scrollTop;
  66. var originalHeight = ta.style.height;
  67. ta.style.height = 'auto';
  68. var endHeight = ta.scrollHeight + heightOffset;
  69. if (ta.scrollHeight === 0) {
  70. // If the scrollHeight is 0, then the element probably has display:none or is detached from the DOM.
  71. ta.style.height = originalHeight;
  72. return;
  73. }
  74. ta.style.height = endHeight + 'px';
  75. // prevents scroll-position jumping
  76. document.documentElement.scrollTop = htmlTop;
  77. document.body.scrollTop = bodyTop;
  78. var style = window.getComputedStyle(ta, null);
  79. if (style.height !== ta.style.height) {
  80. if (overflowY !== 'visible') {
  81. changeOverflow('visible');
  82. return;
  83. }
  84. } else {
  85. if (overflowY !== 'hidden') {
  86. changeOverflow('hidden');
  87. return;
  88. }
  89. }
  90. if (startHeight !== ta.style.height) {
  91. var evt = document.createEvent('Event');
  92. evt.initEvent('autosize:resized', true, false);
  93. ta.dispatchEvent(evt);
  94. }
  95. }
  96. var destroy = (function (style) {
  97. window.removeEventListener('resize', update);
  98. ta.removeEventListener('input', update);
  99. ta.removeEventListener('keyup', update);
  100. ta.removeAttribute('data-autosize-on');
  101. ta.removeEventListener('autosize:destroy', destroy);
  102. Object.keys(style).forEach(function (key) {
  103. ta.style[key] = style[key];
  104. });
  105. }).bind(ta, {
  106. height: ta.style.height,
  107. resize: ta.style.resize,
  108. overflowY: ta.style.overflowY,
  109. overflowX: ta.style.overflowX,
  110. wordWrap: ta.style.wordWrap });
  111. ta.addEventListener('autosize:destroy', destroy);
  112. // IE9 does not fire onpropertychange or oninput for deletions,
  113. // so binding to onkeyup to catch most of those events.
  114. // There is no way that I know of to detect something like 'cut' in IE9.
  115. if ('onpropertychange' in ta && 'oninput' in ta) {
  116. ta.addEventListener('keyup', update);
  117. }
  118. window.addEventListener('resize', update);
  119. ta.addEventListener('input', update);
  120. ta.addEventListener('autosize:update', update);
  121. ta.setAttribute('data-autosize-on', true);
  122. if (setOverflowY) {
  123. ta.style.overflowY = 'hidden';
  124. }
  125. if (setOverflowX) {
  126. ta.style.overflowX = 'hidden';
  127. ta.style.wordWrap = 'break-word';
  128. }
  129. init();
  130. }
  131. function destroy(ta) {
  132. if (!(ta && ta.nodeName && ta.nodeName === 'TEXTAREA')) return;
  133. var evt = document.createEvent('Event');
  134. evt.initEvent('autosize:destroy', true, false);
  135. ta.dispatchEvent(evt);
  136. }
  137. function update(ta) {
  138. if (!(ta && ta.nodeName && ta.nodeName === 'TEXTAREA')) return;
  139. var evt = document.createEvent('Event');
  140. evt.initEvent('autosize:update', true, false);
  141. ta.dispatchEvent(evt);
  142. }
  143. var autosize = null;
  144. // Do nothing in Node.js environment and IE8 (or lower)
  145. if (typeof window === 'undefined' || typeof window.getComputedStyle !== 'function') {
  146. autosize = function (el) {
  147. return el;
  148. };
  149. autosize.destroy = function (el) {
  150. return el;
  151. };
  152. autosize.update = function (el) {
  153. return el;
  154. };
  155. } else {
  156. autosize = function (el, options) {
  157. if (el) {
  158. Array.prototype.forEach.call(el.length ? el : [el], function (x) {
  159. return assign(x, options);
  160. });
  161. }
  162. return el;
  163. };
  164. autosize.destroy = function (el) {
  165. if (el) {
  166. Array.prototype.forEach.call(el.length ? el : [el], destroy);
  167. }
  168. return el;
  169. };
  170. autosize.update = function (el) {
  171. if (el) {
  172. Array.prototype.forEach.call(el.length ? el : [el], update);
  173. }
  174. return el;
  175. };
  176. }
  177. module.exports = autosize;
  178. });