autosize.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*!
  2. Autosize 3.0.8
  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. resize();
  61. }
  62. function resize() {
  63. var htmlTop = window.pageYOffset;
  64. var bodyTop = document.body.scrollTop;
  65. var originalHeight = ta.style.height;
  66. ta.style.height = 'auto';
  67. var endHeight = ta.scrollHeight + heightOffset;
  68. if (ta.scrollHeight === 0) {
  69. // If the scrollHeight is 0, then the element probably has display:none or is detached from the DOM.
  70. ta.style.height = originalHeight;
  71. return;
  72. }
  73. ta.style.height = endHeight + 'px';
  74. // prevents scroll-position jumping
  75. document.documentElement.scrollTop = htmlTop;
  76. document.body.scrollTop = bodyTop;
  77. }
  78. function update() {
  79. var startHeight = ta.style.height;
  80. resize();
  81. var style = window.getComputedStyle(ta, null);
  82. if (style.height !== ta.style.height) {
  83. if (overflowY !== 'visible') {
  84. changeOverflow('visible');
  85. }
  86. } else {
  87. if (overflowY !== 'hidden') {
  88. changeOverflow('hidden');
  89. }
  90. }
  91. if (startHeight !== ta.style.height) {
  92. var evt = document.createEvent('Event');
  93. evt.initEvent('autosize:resized', true, false);
  94. ta.dispatchEvent(evt);
  95. }
  96. }
  97. var destroy = (function (style) {
  98. window.removeEventListener('resize', update);
  99. ta.removeEventListener('input', update);
  100. ta.removeEventListener('keyup', update);
  101. ta.removeAttribute('data-autosize-on');
  102. ta.removeEventListener('autosize:destroy', destroy);
  103. Object.keys(style).forEach(function (key) {
  104. ta.style[key] = style[key];
  105. });
  106. }).bind(ta, {
  107. height: ta.style.height,
  108. resize: ta.style.resize,
  109. overflowY: ta.style.overflowY,
  110. overflowX: ta.style.overflowX,
  111. wordWrap: ta.style.wordWrap });
  112. ta.addEventListener('autosize:destroy', destroy);
  113. // IE9 does not fire onpropertychange or oninput for deletions,
  114. // so binding to onkeyup to catch most of those events.
  115. // There is no way that I know of to detect something like 'cut' in IE9.
  116. if ('onpropertychange' in ta && 'oninput' in ta) {
  117. ta.addEventListener('keyup', update);
  118. }
  119. window.addEventListener('resize', update);
  120. ta.addEventListener('input', update);
  121. ta.addEventListener('autosize:update', update);
  122. ta.setAttribute('data-autosize-on', true);
  123. if (setOverflowY) {
  124. ta.style.overflowY = 'hidden';
  125. }
  126. if (setOverflowX) {
  127. ta.style.overflowX = 'hidden';
  128. ta.style.wordWrap = 'break-word';
  129. }
  130. init();
  131. }
  132. function destroy(ta) {
  133. if (!(ta && ta.nodeName && ta.nodeName === 'TEXTAREA')) return;
  134. var evt = document.createEvent('Event');
  135. evt.initEvent('autosize:destroy', true, false);
  136. ta.dispatchEvent(evt);
  137. }
  138. function update(ta) {
  139. if (!(ta && ta.nodeName && ta.nodeName === 'TEXTAREA')) return;
  140. var evt = document.createEvent('Event');
  141. evt.initEvent('autosize:update', true, false);
  142. ta.dispatchEvent(evt);
  143. }
  144. var autosize = null;
  145. // Do nothing in Node.js environment and IE8 (or lower)
  146. if (typeof window === 'undefined' || typeof window.getComputedStyle !== 'function') {
  147. autosize = function (el) {
  148. return el;
  149. };
  150. autosize.destroy = function (el) {
  151. return el;
  152. };
  153. autosize.update = function (el) {
  154. return el;
  155. };
  156. } else {
  157. autosize = function (el, options) {
  158. if (el) {
  159. Array.prototype.forEach.call(el.length ? el : [el], function (x) {
  160. return assign(x, options);
  161. });
  162. }
  163. return el;
  164. };
  165. autosize.destroy = function (el) {
  166. if (el) {
  167. Array.prototype.forEach.call(el.length ? el : [el], destroy);
  168. }
  169. return el;
  170. };
  171. autosize.update = function (el) {
  172. if (el) {
  173. Array.prototype.forEach.call(el.length ? el : [el], update);
  174. }
  175. return el;
  176. };
  177. }
  178. module.exports = autosize;
  179. });