Нет описания

autosize.js 7.6KB

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