autosize.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*!
  2. Autosize 4.0.0
  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. var map = typeof Map === "function" ? new Map() : (function () {
  21. var keys = [];
  22. var values = [];
  23. return {
  24. has: function has(key) {
  25. return keys.indexOf(key) > -1;
  26. },
  27. get: function get(key) {
  28. return values[keys.indexOf(key)];
  29. },
  30. set: function set(key, value) {
  31. if (keys.indexOf(key) === -1) {
  32. keys.push(key);
  33. values.push(value);
  34. }
  35. },
  36. 'delete': function _delete(key) {
  37. var index = keys.indexOf(key);
  38. if (index > -1) {
  39. keys.splice(index, 1);
  40. values.splice(index, 1);
  41. }
  42. }
  43. };
  44. })();
  45. var createEvent = function createEvent(name) {
  46. return new Event(name, { bubbles: true });
  47. };
  48. try {
  49. new Event('test');
  50. } catch (e) {
  51. // IE does not support `new Event()`
  52. createEvent = function (name) {
  53. var evt = document.createEvent('Event');
  54. evt.initEvent(name, true, false);
  55. return evt;
  56. };
  57. }
  58. function assign(ta) {
  59. if (!ta || !ta.nodeName || ta.nodeName !== 'TEXTAREA' || map.has(ta)) return;
  60. var heightOffset = null;
  61. var clientWidth = ta.clientWidth;
  62. var cachedHeight = null;
  63. function init() {
  64. var style = window.getComputedStyle(ta, null);
  65. if (style.resize === 'vertical') {
  66. ta.style.resize = 'none';
  67. } else if (style.resize === 'both') {
  68. ta.style.resize = 'horizontal';
  69. }
  70. if (style.boxSizing === 'content-box') {
  71. heightOffset = -(parseFloat(style.paddingTop) + parseFloat(style.paddingBottom));
  72. } else {
  73. heightOffset = parseFloat(style.borderTopWidth) + parseFloat(style.borderBottomWidth);
  74. }
  75. // Fix when a textarea is not on document body and heightOffset is Not a Number
  76. if (isNaN(heightOffset)) {
  77. heightOffset = 0;
  78. }
  79. update();
  80. }
  81. function changeOverflow(value) {
  82. {
  83. // Chrome/Safari-specific fix:
  84. // When the textarea y-overflow is hidden, Chrome/Safari do not reflow the text to account for the space
  85. // made available by removing the scrollbar. The following forces the necessary text reflow.
  86. var width = ta.style.width;
  87. ta.style.width = '0px';
  88. // Force reflow:
  89. /* jshint ignore:start */
  90. ta.offsetWidth;
  91. /* jshint ignore:end */
  92. ta.style.width = width;
  93. }
  94. ta.style.overflowY = value;
  95. }
  96. function getParentOverflows(el) {
  97. var arr = [];
  98. while (el && el.parentNode && el.parentNode instanceof Element) {
  99. if (el.parentNode.scrollTop) {
  100. arr.push({
  101. node: el.parentNode,
  102. scrollTop: el.parentNode.scrollTop
  103. });
  104. }
  105. el = el.parentNode;
  106. }
  107. return arr;
  108. }
  109. function resize() {
  110. var originalHeight = ta.style.height;
  111. var overflows = getParentOverflows(ta);
  112. var docTop = document.documentElement && document.documentElement.scrollTop; // Needed for Mobile IE (ticket #240)
  113. ta.style.height = '';
  114. var endHeight = ta.scrollHeight + heightOffset;
  115. if (ta.scrollHeight === 0) {
  116. // If the scrollHeight is 0, then the element probably has display:none or is detached from the DOM.
  117. ta.style.height = originalHeight;
  118. return;
  119. }
  120. ta.style.height = endHeight + 'px';
  121. // used to check if an update is actually necessary on window.resize
  122. clientWidth = ta.clientWidth;
  123. // prevents scroll-position jumping
  124. overflows.forEach(function (el) {
  125. el.node.scrollTop = el.scrollTop;
  126. });
  127. if (docTop) {
  128. document.documentElement.scrollTop = docTop;
  129. }
  130. }
  131. function update() {
  132. resize();
  133. var styleHeight = Math.round(parseFloat(ta.style.height));
  134. var computed = window.getComputedStyle(ta, null);
  135. // Using offsetHeight as a replacement for computed.height in IE, because IE does not account use of border-box
  136. var actualHeight = computed.boxSizing === 'content-box' ? Math.round(parseFloat(computed.height)) : ta.offsetHeight;
  137. // The actual height not matching the style height (set via the resize method) indicates that
  138. // the max-height has been exceeded, in which case the overflow should be allowed.
  139. if (actualHeight !== styleHeight) {
  140. if (computed.overflowY === 'hidden') {
  141. changeOverflow('scroll');
  142. resize();
  143. actualHeight = computed.boxSizing === 'content-box' ? Math.round(parseFloat(window.getComputedStyle(ta, null).height)) : ta.offsetHeight;
  144. }
  145. } else {
  146. // Normally keep overflow set to hidden, to avoid flash of scrollbar as the textarea expands.
  147. if (computed.overflowY !== 'hidden') {
  148. changeOverflow('hidden');
  149. resize();
  150. actualHeight = computed.boxSizing === 'content-box' ? Math.round(parseFloat(window.getComputedStyle(ta, null).height)) : ta.offsetHeight;
  151. }
  152. }
  153. if (cachedHeight !== actualHeight) {
  154. cachedHeight = actualHeight;
  155. var evt = createEvent('autosize:resized');
  156. try {
  157. ta.dispatchEvent(evt);
  158. } catch (err) {
  159. // Firefox will throw an error on dispatchEvent for a detached element
  160. // https://bugzilla.mozilla.org/show_bug.cgi?id=889376
  161. }
  162. }
  163. }
  164. var pageResize = function pageResize() {
  165. if (ta.clientWidth !== clientWidth) {
  166. update();
  167. }
  168. };
  169. var destroy = (function (style) {
  170. window.removeEventListener('resize', pageResize, false);
  171. ta.removeEventListener('input', update, false);
  172. ta.removeEventListener('keyup', update, false);
  173. ta.removeEventListener('autosize:destroy', destroy, false);
  174. ta.removeEventListener('autosize:update', update, false);
  175. Object.keys(style).forEach(function (key) {
  176. ta.style[key] = style[key];
  177. });
  178. map['delete'](ta);
  179. }).bind(ta, {
  180. height: ta.style.height,
  181. resize: ta.style.resize,
  182. overflowY: ta.style.overflowY,
  183. overflowX: ta.style.overflowX,
  184. wordWrap: ta.style.wordWrap
  185. });
  186. ta.addEventListener('autosize:destroy', destroy, false);
  187. // IE9 does not fire onpropertychange or oninput for deletions,
  188. // so binding to onkeyup to catch most of those events.
  189. // There is no way that I know of to detect something like 'cut' in IE9.
  190. if ('onpropertychange' in ta && 'oninput' in ta) {
  191. ta.addEventListener('keyup', update, false);
  192. }
  193. window.addEventListener('resize', pageResize, false);
  194. ta.addEventListener('input', update, false);
  195. ta.addEventListener('autosize:update', update, false);
  196. ta.style.overflowX = 'hidden';
  197. ta.style.wordWrap = 'break-word';
  198. map.set(ta, {
  199. destroy: destroy,
  200. update: update
  201. });
  202. init();
  203. }
  204. function destroy(ta) {
  205. var methods = map.get(ta);
  206. if (methods) {
  207. methods.destroy();
  208. }
  209. }
  210. function update(ta) {
  211. var methods = map.get(ta);
  212. if (methods) {
  213. methods.update();
  214. }
  215. }
  216. var autosize = null;
  217. // Do nothing in Node.js environment and IE8 (or lower)
  218. if (typeof window === 'undefined' || typeof window.getComputedStyle !== 'function') {
  219. autosize = function (el) {
  220. return el;
  221. };
  222. autosize.destroy = function (el) {
  223. return el;
  224. };
  225. autosize.update = function (el) {
  226. return el;
  227. };
  228. } else {
  229. autosize = function (el, options) {
  230. if (el) {
  231. Array.prototype.forEach.call(el.length ? el : [el], function (x) {
  232. return assign(x, options);
  233. });
  234. }
  235. return el;
  236. };
  237. autosize.destroy = function (el) {
  238. if (el) {
  239. Array.prototype.forEach.call(el.length ? el : [el], destroy);
  240. }
  241. return el;
  242. };
  243. autosize.update = function (el) {
  244. if (el) {
  245. Array.prototype.forEach.call(el.length ? el : [el], update);
  246. }
  247. return el;
  248. };
  249. }
  250. module.exports = autosize;
  251. });