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