PageSourceSearch

https://www.chuman.co.jp/common/js/autosize.js

js chuman.co.jp collected 2026-10-05 03:41:12 UTC 8,316 bytes, 270 lines download raw bytes

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

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.