4fa579031ca3d6ae5267a77ac1ed148678cdb6e5
[Packages/TYPO3.CMS.git] / typo3 / contrib / jquery / jquery.autosize.js
1 /*!
2 Autosize 1.18.17 as UMD module
3 license: MIT
4 http://www.jacklmoore.com/autosize
5 derived: https://github.com/bmack/autosize
6 */
7 (function (factory) {
8 if (typeof define === 'function' && define.amd) {
9 // AMD
10 define(['jquery'], factory);
11 } else if (typeof exports === 'object') {
12 // CommonJS
13 factory(require('jquery'));
14 } else {
15 // Browser globals
16 factory(jQuery);
17 }
18 }(function ($) {
19 var
20 defaults = {
21 className: 'autosizejs',
22 id: 'autosizejs',
23 append: '\n',
24 callback: false,
25 resizeDelay: 10,
26 placeholder: true
27 },
28
29 // border:0 is unnecessary, but avoids a bug in Firefox on OSX
30 copy = '<textarea tabindex="-1" style="position:absolute; top:-999px; left:0; right:auto; bottom:auto; border:0; padding: 0; -moz-box-sizing:content-box; -webkit-box-sizing:content-box; box-sizing:content-box; word-wrap:break-word; height:0 !important; min-height:0 !important; overflow:hidden; transition:none; -webkit-transition:none; -moz-transition:none;"/>',
31
32 // line-height is conditionally included because IE7/IE8/old Opera do not return the correct value.
33 typographyStyles = [
34 'fontFamily',
35 'fontSize',
36 'fontWeight',
37 'fontStyle',
38 'letterSpacing',
39 'textTransform',
40 'wordSpacing',
41 'textIndent',
42 'whiteSpace'
43 ],
44
45 // to keep track which textarea is being mirrored when adjust() is called.
46 mirrored,
47
48 // the mirror element, which is used to calculate what size the mirrored element should be.
49 mirror = $(copy).data('autosize', true)[0];
50
51 // test that line-height can be accurately copied.
52 mirror.style.lineHeight = '99px';
53 if ($(mirror).css('lineHeight') === '99px') {
54 typographyStyles.push('lineHeight');
55 }
56 mirror.style.lineHeight = '';
57
58 $.fn.autosize = function (options) {
59 if (!this.length) {
60 return this;
61 }
62
63 options = $.extend({}, defaults, options || {});
64
65 if (mirror.parentNode !== document.body) {
66 $(document.body).append(mirror);
67 }
68
69 return this.each(function () {
70 var
71 ta = this,
72 $ta = $(ta),
73 maxHeight,
74 minHeight,
75 boxOffset = 0,
76 callback = $.isFunction(options.callback),
77 originalStyles = {
78 height: ta.style.height,
79 overflow: ta.style.overflow,
80 overflowY: ta.style.overflowY,
81 wordWrap: ta.style.wordWrap,
82 resize: ta.style.resize
83 },
84 timeout,
85 width = $ta.width(),
86 taResize = $ta.css('resize');
87
88 if ($ta.data('autosize')) {
89 // exit if autosize has already been applied, or if the textarea is the mirror element.
90 return;
91 }
92 $ta.data('autosize', true);
93
94 if ($ta.css('box-sizing') === 'border-box' || $ta.css('-moz-box-sizing') === 'border-box' || $ta.css('-webkit-box-sizing') === 'border-box'){
95 boxOffset = $ta.outerHeight() - $ta.height();
96 }
97
98 // IE8 and lower return 'auto', which parses to NaN, if no min-height is set.
99 minHeight = Math.max(parseFloat($ta.css('minHeight')) - boxOffset || 0, $ta.height());
100
101 $ta.css({
102 overflow: 'hidden',
103 overflowY: 'hidden',
104 wordWrap: 'break-word' // horizontal overflow is hidden, so break-word is necessary for handling words longer than the textarea width
105 });
106
107 if (taResize === 'vertical') {
108 $ta.css('resize','none');
109 } else if (taResize === 'both') {
110 $ta.css('resize', 'horizontal');
111 }
112
113 // getComputedStyle is preferred here because it preserves sub-pixel values, while jQuery's .width() rounds to an integer.
114 function setWidth() {
115 var width;
116 var style = window.getComputedStyle ? window.getComputedStyle(ta, null) : null;
117
118 if (style) {
119 width = parseFloat(style.width);
120 if (style.boxSizing === 'border-box' || style.webkitBoxSizing === 'border-box' || style.mozBoxSizing === 'border-box') {
121 $.each(['paddingLeft', 'paddingRight', 'borderLeftWidth', 'borderRightWidth'], function(i,val){
122 width -= parseFloat(style[val]);
123 });
124 }
125 } else {
126 width = $ta.width();
127 }
128
129 mirror.style.width = Math.max(width,0) + 'px';
130 }
131
132 function initMirror() {
133 var styles = {};
134
135 mirrored = ta;
136 mirror.className = options.className;
137 mirror.id = options.id;
138 maxHeight = parseFloat($ta.css('maxHeight'));
139
140 // mirror is a duplicate textarea located off-screen that
141 // is automatically updated to contain the same text as the
142 // original textarea. mirror always has a height of 0.
143 // This gives a cross-browser supported way getting the actual
144 // height of the text, through the scrollTop property.
145 $.each(typographyStyles, function(i,val){
146 styles[val] = $ta.css(val);
147 });
148
149 $(mirror).css(styles).attr('wrap', $ta.attr('wrap'));
150
151 setWidth();
152
153 // Chrome-specific fix:
154 // When the textarea y-overflow is hidden, Chrome doesn't reflow the text to account for the space
155 // made available by removing the scrollbar. This workaround triggers the reflow for Chrome.
156 if (window.chrome) {
157 var width = ta.style.width;
158 ta.style.width = '0px';
159 var ignore = ta.offsetWidth;
160 ta.style.width = width;
161 }
162 }
163
164 // Using mainly bare JS in this function because it is going
165 // to fire very often while typing, and needs to very efficient.
166 function adjust() {
167 var height, originalHeight;
168
169 if (mirrored !== ta) {
170 initMirror();
171 } else {
172 setWidth();
173 }
174
175 if (!ta.value && options.placeholder) {
176 // If the textarea is empty, copy the placeholder text into
177 // the mirror control and use that for sizing so that we
178 // don't end up with placeholder getting trimmed.
179 mirror.value = ($ta.attr("placeholder") || '');
180 } else {
181 mirror.value = ta.value;
182 }
183
184 mirror.value += options.append || '';
185 mirror.style.overflowY = ta.style.overflowY;
186 originalHeight = parseFloat(ta.style.height) || 0;
187
188 // Setting scrollTop to zero is needed in IE8 and lower for the next step to be accurately applied
189 mirror.scrollTop = 0;
190
191 mirror.scrollTop = 9e4;
192
193 // Using scrollTop rather than scrollHeight because scrollHeight is non-standard and includes padding.
194 height = mirror.scrollTop;
195
196 if (maxHeight && height > maxHeight) {
197 ta.style.overflowY = 'scroll';
198 height = maxHeight;
199 } else {
200 ta.style.overflowY = 'hidden';
201 if (height < minHeight) {
202 height = minHeight;
203 }
204 }
205
206 height += boxOffset;
207
208 if (Math.abs(originalHeight - height) > 1/100) {
209 ta.style.height = height + 'px';
210
211 // Trigger a repaint for IE8 for when ta is nested 2 or more levels inside an inline-block
212 mirror.className = mirror.className;
213
214 if (callback) {
215 options.callback.call(ta,ta);
216 }
217 $ta.trigger('autosize.resized');
218 }
219 }
220
221 function resize () {
222 clearTimeout(timeout);
223 timeout = setTimeout(function(){
224 var newWidth = $ta.width();
225
226 if (newWidth !== width) {
227 width = newWidth;
228 adjust();
229 }
230 }, parseInt(options.resizeDelay,10));
231 }
232
233 if ('onpropertychange' in ta) {
234 if ('oninput' in ta) {
235 // Detects IE9. IE9 does not fire onpropertychange or oninput for deletions,
236 // so binding to onkeyup to catch most of those occasions. There is no way that I
237 // know of to detect something like 'cut' in IE9.
238 $ta.on('input.autosize keyup.autosize', adjust);
239 } else {
240 // IE7 / IE8
241 $ta.on('propertychange.autosize', function(){
242 if(event.propertyName === 'value'){
243 adjust();
244 }
245 });
246 }
247 } else {
248 // Modern Browsers
249 $ta.on('input.autosize', adjust);
250 }
251
252 // Set options.resizeDelay to false if using fixed-width textarea elements.
253 // Uses a timeout and width check to reduce the amount of times adjust needs to be called after window resize.
254
255 if (options.resizeDelay !== false) {
256 $(window).on('resize.autosize', resize);
257 }
258
259 // Event for manual triggering if needed.
260 // Should only be needed when the value of the textarea is changed through JavaScript rather than user input.
261 $ta.on('autosize.resize', adjust);
262
263 // Event for manual triggering that also forces the styles to update as well.
264 // Should only be needed if one of typography styles of the textarea change, and the textarea is already the target of the adjust method.
265 $ta.on('autosize.resizeIncludeStyle', function() {
266 mirrored = null;
267 adjust();
268 });
269
270 $ta.on('autosize.destroy', function(){
271 mirrored = null;
272 clearTimeout(timeout);
273 $(window).off('resize', resize);
274 $ta
275 .off('autosize')
276 .off('.autosize')
277 .css(originalStyles)
278 .removeData('autosize');
279 });
280
281 // Call adjust in case the textarea already contains text.
282 adjust();
283 });
284 };
285 }));