1 // Autosize 1.10 - jQuery plugin for textareas
\r
2 // (c) 2012 Jack Moore - jacklmoore.com
\r
3 // license: www.opensource.org/licenses/mit-license.php
\r
8 borderBox = 'border-box',
\r
9 lineHeight = 'lineHeight',
\r
10 copy = '<textarea tabindex="-1" style="position:absolute; top:-9999px; left:-9999px; right:auto; bottom:auto; -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">',
\r
11 // line-height is omitted because IE7/IE8 doesn't return the correct value.
\r
22 oninput = 'oninput',
\r
23 onpropertychange = 'onpropertychange',
\r
26 // For testing support in old FireFox
\r
27 test.setAttribute(oninput, "return");
\r
29 if ($.isFunction(test[oninput]) || onpropertychange in test) {
\r
31 // test that line-height can be accurately copied to avoid
\r
32 // incorrect value reporting in old IE and old Opera
\r
33 $(test).css(lineHeight, '99px');
\r
34 if ($(test).css(lineHeight) === '99px') {
\r
35 copyStyle.push(lineHeight);
\r
38 $.fn.autosize = function (className) {
\r
39 return this.each(function () {
\r
44 minHeight = $ta.height(),
\r
45 maxHeight = parseInt($ta.css('maxHeight'), 10),
\r
47 i = copyStyle.length,
\r
51 if ($ta.css('box-sizing') === borderBox || $ta.css('-moz-box-sizing') === borderBox || $ta.css('-webkit-box-sizing') === borderBox){
\r
52 boxOffset = $ta.outerHeight() - $ta.height();
\r
55 if ($ta.data('mirror') || $ta.data('ismirror')) {
\r
56 // if autosize has already been applied, exit.
\r
57 // if autosize is being applied to a mirror element, exit.
\r
60 mirror = $(copy).data('ismirror', true).addClass(className || 'autosizejs')[0];
\r
62 // Changed allowed resize only in edit mode by VTA (31/8/2012)
\r
63 if ($ta.attr('disabled')) {
\r
64 $ta.css('resize', 'none');
\r
66 // Changed horizontal to vertical by VTA (31/8/2012)
\r
67 resize = $ta.css('resize') === 'none' ? 'none' : 'vertical';
\r
69 $ta.data('mirror', $(mirror)).css({
\r
72 wordWrap: 'break-word',
\r
77 // Opera returns '-1px' when max-height is set to 'none'.
\r
78 maxHeight = maxHeight && maxHeight > 0 ? maxHeight : 9e4;
\r
80 // Using mainly bare JS in this function because it is going
\r
81 // to fire very often while typing, and needs to very efficient.
\r
83 var height, overflow;
\r
84 // the active flag keeps IE from tripping all over itself. Otherwise
\r
85 // actions in the adjust function will cause IE to call adjust again.
\r
89 mirror.value = ta.value;
\r
91 mirror.style.overflowY = ta.style.overflowY;
\r
93 // Update the width in case the original textarea width has changed
\r
94 mirror.style.width = $ta.css('width');
\r
96 // Needed for IE to reliably return the correct scrollHeight
\r
97 mirror.scrollTop = 0;
\r
99 // Set a very high value for scrollTop to be sure the
\r
100 // mirror is scrolled all the way to the bottom.
\r
101 mirror.scrollTop = 9e4;
\r
103 height = mirror.scrollTop;
\r
105 if (height > maxHeight) {
\r
106 height = maxHeight;
\r
107 overflow = 'scroll';
\r
108 } else if (height < minHeight) {
\r
109 height = minHeight;
\r
111 ta.style.overflowY = overflow;
\r
113 ta.style.height = height + boxOffset + 'px';
\r
115 // This small timeout gives IE a chance to draw it's scrollbar
\r
116 // before adjust can be run again (prevents an infinite loop).
\r
117 setTimeout(function () {
\r
123 // mirror is a duplicate textarea located off-screen that
\r
124 // is automatically updated to contain the same text as the
\r
125 // original textarea. mirror always has a height of 0.
\r
126 // This gives a cross-browser supported way getting the actual
\r
127 // height of the text, through the scrollTop property.
\r
129 mirror.style[copyStyle[i]] = $ta.css(copyStyle[i]);
\r
132 $('body').append(mirror);
\r
134 if (onpropertychange in ta) {
\r
135 if (oninput in ta) {
\r
136 // Detects IE9. IE9 does not fire onpropertychange or oninput for deletions,
\r
137 // so binding to onkeyup to catch most of those occassions. There is no way that I
\r
138 // know of to detect something like 'cut' in IE9.
\r
139 ta[oninput] = ta.onkeyup = adjust;
\r
142 ta[onpropertychange] = adjust;
\r
146 ta[oninput] = adjust;
\r
149 $(window).resize(adjust);
\r
151 // Allow for manual triggering if needed.
\r
152 $ta.bind('autosize', adjust);
\r
154 // Call adjust in case the textarea already contains text.
\r
159 // Makes no changes for older browsers (FireFox3- and Safari4-)
\r
160 $.fn.autosize = function () {
\r