2013-09-05 18:13:11 +02:00
|
|
|
/** @preserve
|
2013-09-05 18:09:51 +02:00
|
|
|
Autosize v1.17.7 - 2013-09-03
|
|
|
|
Automatically adjust textarea height based on user input.
|
|
|
|
(c) 2013 Jack Moore - http://www.jacklmoore.com/autosize
|
|
|
|
license: http://www.opensource.org/licenses/mit-license.php
|
2013-09-05 18:13:11 +02:00
|
|
|
---
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
2013-05-30 16:40:05 +02:00
|
|
|
*/
|
2013-09-05 18:09:51 +02:00
|
|
|
(function (factory) {
|
|
|
|
if (typeof define === 'function' && define.amd) {
|
|
|
|
// AMD. Register as an anonymous module.
|
|
|
|
define(['jquery'], factory);
|
|
|
|
} else {
|
|
|
|
// Browser globals: jQuery or jQuery-like library, such as Zepto
|
|
|
|
factory(window.jQuery || window.$);
|
|
|
|
}
|
|
|
|
}(function ($) {
|
2012-10-24 03:48:08 +02:00
|
|
|
var
|
|
|
|
defaults = {
|
|
|
|
className: 'autosizejs',
|
2013-09-05 18:09:51 +02:00
|
|
|
append: '',
|
|
|
|
callback: false,
|
|
|
|
resizeDelay: 10
|
2012-10-24 03:48:08 +02:00
|
|
|
},
|
2013-09-05 18:09:51 +02:00
|
|
|
|
|
|
|
// border:0 is unnecessary, but avoids a bug in FireFox on OSX
|
|
|
|
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;"/>',
|
|
|
|
|
|
|
|
// line-height is conditionally included because IE7/IE8/old Opera do not return the correct value.
|
|
|
|
typographyStyles = [
|
2012-10-24 03:48:08 +02:00
|
|
|
'fontFamily',
|
|
|
|
'fontSize',
|
|
|
|
'fontWeight',
|
|
|
|
'fontStyle',
|
|
|
|
'letterSpacing',
|
|
|
|
'textTransform',
|
|
|
|
'wordSpacing',
|
|
|
|
'textIndent'
|
|
|
|
],
|
|
|
|
|
2013-09-05 18:09:51 +02:00
|
|
|
// to keep track which textarea is being mirrored when adjust() is called.
|
|
|
|
mirrored,
|
2012-10-24 03:48:08 +02:00
|
|
|
|
2013-09-05 18:09:51 +02:00
|
|
|
// the mirror element, which is used to calculate what size the mirrored element should be.
|
|
|
|
mirror = $(copy).data('autosize', true)[0];
|
2012-10-24 03:48:08 +02:00
|
|
|
|
2013-09-05 18:09:51 +02:00
|
|
|
// test that line-height can be accurately copied.
|
|
|
|
mirror.style.lineHeight = '99px';
|
|
|
|
if ($(mirror).css('lineHeight') === '99px') {
|
|
|
|
typographyStyles.push('lineHeight');
|
|
|
|
}
|
|
|
|
mirror.style.lineHeight = '';
|
2012-10-24 03:48:08 +02:00
|
|
|
|
2013-09-05 18:09:51 +02:00
|
|
|
$.fn.autosize = function (options) {
|
|
|
|
options = $.extend({}, defaults, options || {});
|
2012-10-24 03:48:08 +02:00
|
|
|
|
2013-09-05 18:09:51 +02:00
|
|
|
if (mirror.parentNode !== document.body) {
|
|
|
|
$(document.body).append(mirror);
|
|
|
|
}
|
2012-10-24 03:48:08 +02:00
|
|
|
|
2013-09-05 18:09:51 +02:00
|
|
|
return this.each(function () {
|
|
|
|
var
|
|
|
|
ta = this,
|
|
|
|
$ta = $(ta),
|
|
|
|
maxHeight,
|
|
|
|
minHeight,
|
|
|
|
boxOffset = 0,
|
|
|
|
callback = $.isFunction(options.callback),
|
|
|
|
originalStyles = {
|
|
|
|
height: ta.style.height,
|
|
|
|
overflow: ta.style.overflow,
|
|
|
|
overflowY: ta.style.overflowY,
|
|
|
|
wordWrap: ta.style.wordWrap,
|
|
|
|
resize: ta.style.resize
|
|
|
|
},
|
|
|
|
timeout,
|
|
|
|
width = $ta.width();
|
|
|
|
|
|
|
|
if ($ta.data('autosize')) {
|
|
|
|
// exit if autosize has already been applied, or if the textarea is the mirror element.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$ta.data('autosize', true);
|
|
|
|
|
|
|
|
if ($ta.css('box-sizing') === 'border-box' || $ta.css('-moz-box-sizing') === 'border-box' || $ta.css('-webkit-box-sizing') === 'border-box'){
|
|
|
|
boxOffset = $ta.outerHeight() - $ta.height();
|
|
|
|
}
|
|
|
|
|
|
|
|
// IE8 and lower return 'auto', which parses to NaN, if no min-height is set.
|
|
|
|
minHeight = Math.max(parseInt($ta.css('minHeight'), 10) - boxOffset || 0, $ta.height());
|
|
|
|
|
|
|
|
$ta.css({
|
|
|
|
overflow: 'hidden',
|
|
|
|
overflowY: 'hidden',
|
|
|
|
wordWrap: 'break-word', // horizontal overflow is hidden, so break-word is necessary for handling words longer than the textarea width
|
|
|
|
resize: ($ta.css('resize') === 'none' || $ta.css('resize') === 'vertical') ? 'none' : 'horizontal'
|
|
|
|
});
|
|
|
|
|
|
|
|
// The mirror width must exactly match the textarea width, so using getBoundingClientRect because it doesn't round the sub-pixel value.
|
|
|
|
function setWidth() {
|
|
|
|
var style, width;
|
2013-09-05 18:13:11 +02:00
|
|
|
|
2013-09-05 18:09:51 +02:00
|
|
|
if ('getComputedStyle' in window) {
|
|
|
|
style = window.getComputedStyle(ta);
|
|
|
|
width = ta.getBoundingClientRect().width;
|
2012-10-24 03:48:08 +02:00
|
|
|
|
2013-09-05 18:09:51 +02:00
|
|
|
$.each(['paddingLeft', 'paddingRight', 'borderLeftWidth', 'borderRightWidth'], function(i,val){
|
|
|
|
width -= parseInt(style[val],10);
|
2012-10-24 03:48:08 +02:00
|
|
|
});
|
|
|
|
|
2013-09-05 18:09:51 +02:00
|
|
|
mirror.style.width = width + 'px';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// window.getComputedStyle, getBoundingClientRect returning a width are unsupported and unneeded in IE8 and lower.
|
|
|
|
mirror.style.width = Math.max($ta.width(), 0) + 'px';
|
2012-10-24 03:48:08 +02:00
|
|
|
}
|
2013-09-05 18:09:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function initMirror() {
|
|
|
|
var styles = {};
|
|
|
|
|
|
|
|
mirrored = ta;
|
|
|
|
mirror.className = options.className;
|
|
|
|
maxHeight = parseInt($ta.css('maxHeight'), 10);
|
2012-10-24 03:48:08 +02:00
|
|
|
|
|
|
|
// mirror is a duplicate textarea located off-screen that
|
|
|
|
// is automatically updated to contain the same text as the
|
|
|
|
// original textarea. mirror always has a height of 0.
|
|
|
|
// This gives a cross-browser supported way getting the actual
|
|
|
|
// height of the text, through the scrollTop property.
|
2013-09-05 18:09:51 +02:00
|
|
|
$.each(typographyStyles, function(i,val){
|
|
|
|
styles[val] = $ta.css(val);
|
|
|
|
});
|
|
|
|
$(mirror).css(styles);
|
|
|
|
|
|
|
|
setWidth();
|
|
|
|
|
|
|
|
// Chrome-specific fix:
|
|
|
|
// When the textarea y-overflow is hidden, Chrome doesn't reflow the text to account for the space
|
|
|
|
// made available by removing the scrollbar. This workaround triggers the reflow for Chrome.
|
|
|
|
if (window.chrome) {
|
|
|
|
var width = ta.style.width;
|
|
|
|
ta.style.width = '0px';
|
|
|
|
var ignore = ta.offsetWidth;
|
|
|
|
ta.style.width = width;
|
2012-10-24 03:48:08 +02:00
|
|
|
}
|
2013-09-05 18:09:51 +02:00
|
|
|
}
|
2012-10-24 03:48:08 +02:00
|
|
|
|
2013-09-05 18:09:51 +02:00
|
|
|
// Using mainly bare JS in this function because it is going
|
|
|
|
// to fire very often while typing, and needs to very efficient.
|
|
|
|
function adjust() {
|
|
|
|
var height, original;
|
|
|
|
|
|
|
|
if (mirrored !== ta) {
|
|
|
|
initMirror();
|
2012-10-24 03:48:08 +02:00
|
|
|
} else {
|
2013-09-05 18:09:51 +02:00
|
|
|
setWidth();
|
|
|
|
}
|
|
|
|
|
|
|
|
mirror.value = ta.value + options.append;
|
|
|
|
mirror.style.overflowY = ta.style.overflowY;
|
|
|
|
original = parseInt(ta.style.height,10);
|
|
|
|
|
|
|
|
// Setting scrollTop to zero is needed in IE8 and lower for the next step to be accurately applied
|
|
|
|
mirror.scrollTop = 0;
|
|
|
|
|
|
|
|
mirror.scrollTop = 9e4;
|
2012-10-24 03:48:08 +02:00
|
|
|
|
2013-09-05 18:09:51 +02:00
|
|
|
// Using scrollTop rather than scrollHeight because scrollHeight is non-standard and includes padding.
|
|
|
|
height = mirror.scrollTop;
|
|
|
|
|
|
|
|
if (maxHeight && height > maxHeight) {
|
|
|
|
ta.style.overflowY = 'scroll';
|
|
|
|
height = maxHeight;
|
|
|
|
} else {
|
|
|
|
ta.style.overflowY = 'hidden';
|
|
|
|
if (height < minHeight) {
|
|
|
|
height = minHeight;
|
|
|
|
}
|
2012-10-24 03:48:08 +02:00
|
|
|
}
|
|
|
|
|
2013-09-05 18:09:51 +02:00
|
|
|
height += boxOffset;
|
|
|
|
|
|
|
|
if (original !== height) {
|
|
|
|
ta.style.height = height + 'px';
|
|
|
|
if (callback) {
|
|
|
|
options.callback.call(ta,ta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-24 03:48:08 +02:00
|
|
|
|
2013-09-05 18:09:51 +02:00
|
|
|
function resize () {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
timeout = setTimeout(function(){
|
|
|
|
var newWidth = $ta.width();
|
2012-10-24 03:48:08 +02:00
|
|
|
|
2013-09-05 18:09:51 +02:00
|
|
|
if (newWidth !== width) {
|
|
|
|
width = newWidth;
|
|
|
|
adjust();
|
|
|
|
}
|
|
|
|
}, parseInt(options.resizeDelay,10));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('onpropertychange' in ta) {
|
|
|
|
if ('oninput' in ta) {
|
|
|
|
// Detects IE9. IE9 does not fire onpropertychange or oninput for deletions,
|
|
|
|
// so binding to onkeyup to catch most of those occasions. There is no way that I
|
|
|
|
// know of to detect something like 'cut' in IE9.
|
|
|
|
$ta.on('input.autosize keyup.autosize', adjust);
|
|
|
|
} else {
|
|
|
|
// IE7 / IE8
|
|
|
|
$ta.on('propertychange.autosize', function(){
|
|
|
|
if(event.propertyName === 'value'){
|
|
|
|
adjust();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Modern Browsers
|
|
|
|
$ta.on('input.autosize', adjust);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set options.resizeDelay to false if using fixed-width textarea elements.
|
|
|
|
// Uses a timeout and width check to reduce the amount of times adjust needs to be called after window resize.
|
|
|
|
|
|
|
|
if (options.resizeDelay !== false) {
|
|
|
|
$(window).on('resize.autosize', resize);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Event for manual triggering if needed.
|
|
|
|
// Should only be needed when the value of the textarea is changed through JavaScript rather than user input.
|
|
|
|
$ta.on('autosize.resize', adjust);
|
|
|
|
|
|
|
|
// Event for manual triggering that also forces the styles to update as well.
|
|
|
|
// Should only be needed if one of typography styles of the textarea change, and the textarea is already the target of the adjust method.
|
|
|
|
$ta.on('autosize.resizeIncludeStyle', function() {
|
|
|
|
mirrored = null;
|
2012-10-24 03:48:08 +02:00
|
|
|
adjust();
|
|
|
|
});
|
|
|
|
|
2013-09-05 18:09:51 +02:00
|
|
|
$ta.on('autosize.destroy', function(){
|
|
|
|
mirrored = null;
|
|
|
|
clearTimeout(timeout);
|
|
|
|
$(window).off('resize', resize);
|
|
|
|
$ta
|
|
|
|
.off('autosize')
|
|
|
|
.off('.autosize')
|
|
|
|
.css(originalStyles)
|
|
|
|
.removeData('autosize');
|
|
|
|
});
|
|
|
|
|
|
|
|
// Call adjust in case the textarea already contains text.
|
|
|
|
adjust();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}));
|