123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- (function (factory) {
- if (typeof define === 'function' && define.amd) {
-
- define(['jquery'], factory);
- } else {
-
- factory(window.jQuery || window.$);
- }
- }(function ($) {
- var
- defaults = {
- className: 'autosizejs',
- append: '',
- callback: false,
- resizeDelay: 10
- },
-
- 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;"/>',
-
- typographyStyles = [
- 'fontFamily',
- 'fontSize',
- 'fontWeight',
- 'fontStyle',
- 'letterSpacing',
- 'textTransform',
- 'wordSpacing',
- 'textIndent'
- ],
-
- mirrored,
-
- mirror = $(copy).data('autosize', true)[0];
-
- mirror.style.lineHeight = '99px';
- if ($(mirror).css('lineHeight') === '99px') {
- typographyStyles.push('lineHeight');
- }
- mirror.style.lineHeight = '';
- $.fn.autosize = function (options) {
- if (!this.length) {
- return this;
- }
- options = $.extend({}, defaults, options || {});
- if (mirror.parentNode !== document.body) {
- $(document.body).append(mirror);
- }
- return this.each(function () {
- var
- ta = this,
- $ta = $(ta),
- maxHeight,
- minHeight,
- boxOffset = 0,
- callback = $.isFunction(options.callback),
- originalStyles = {
- height: options.defaultStyles!=undefined && options.defaultStyles['height']!=undefined ? options.defaultStyles['height'] : ta.style.height,
- overflow: options.defaultStyles!=undefined && options.defaultStyles['overflow']!=undefined ? options.defaultStyles['overflow'] : ta.style.overflow,
- overflowY: options.defaultStyles!=undefined && options.defaultStyles['overflow-y']!=undefined ? options.defaultStyles['overflow-y'] : ta.style.overflowY,
- wordWrap: options.defaultStyles!=undefined && options.defaultStyles['word-wrap']!=undefined ? options.defaultStyles['word-wrap'] : ta.style.wordWrap,
- resize: options.defaultStyles!=undefined && options.defaultStyles['resize']!=undefined ? options.defaultStyles['resize'] : ta.style.resize
- },
- timeout,
- width = $ta.width();
- if ($ta.data('autosize')) {
-
- 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();
- }
-
- minHeight = Math.max(parseInt($ta.css('minHeight'), 10) - boxOffset || 0, originalStyles.height);
- $ta.css({
- overflow: 'hidden',
- overflowY: 'hidden',
- wordWrap: 'break-word',
- resize: (originalStyles.resize === 'none' || originalStyles.resize === 'vertical') ? 'none' : 'horizontal'
- });
-
- function setWidth() {
- var style, width;
-
- if ('getComputedStyle' in window) {
- style = window.getComputedStyle(ta, null);
- width = ta.getBoundingClientRect().width;
- $.each(['paddingLeft', 'paddingRight', 'borderLeftWidth', 'borderRightWidth'], function(i,val){
- width -= parseInt(style[val],10);
- });
- mirror.style.width = width + 'px';
- }
- else {
-
- mirror.style.width = Math.max($ta.width(), 0) + 'px';
- }
- }
- function initMirror() {
- var styles = {};
- mirrored = ta;
- mirror.className = options.className;
- maxHeight = parseInt($ta.css('maxHeight'), 10);
-
-
-
-
-
- $.each(typographyStyles, function(i,val){
- styles[val] = $ta.css(val);
- });
- $(mirror).css(styles);
- setWidth();
-
-
-
- if (window.chrome) {
- var width = ta.style.width;
- ta.style.width = '0px';
- var ignore = ta.offsetWidth;
- ta.style.width = width;
- }
- }
-
-
- function adjust() {
- var height, original;
- if (mirrored !== ta) {
- initMirror();
- } else {
- setWidth();
- }
- mirror.value = ta.value + options.append;
- mirror.style.overflowY = ta.style.overflowY;
- original = parseInt(ta.style.height,10);
-
- mirror.scrollTop = 0;
- mirror.scrollTop = 9e4;
-
- height = mirror.scrollTop;
- if (maxHeight && height > maxHeight) {
- ta.style.overflowY = 'scroll';
- height = maxHeight;
- } else {
- ta.style.overflowY = 'hidden';
- if (height < minHeight) {
- height = minHeight;
- }
- }
- height += boxOffset;
- if (original !== height) {
- ta.style.height = height + 'px';
- if (callback) {
- options.callback.call(ta,ta);
- }
- }
- }
- function resize () {
- clearTimeout(timeout);
- timeout = setTimeout(function(){
- var newWidth = $ta.width();
- if (newWidth !== width) {
- width = newWidth;
- adjust();
- }
- }, parseInt(options.resizeDelay,10));
- }
- if ('onpropertychange' in ta) {
- if ('oninput' in ta) {
-
-
-
- $ta.on('input.autosize keyup.autosize', adjust);
- } else {
-
- $ta.on('propertychange.autosize', function(){
- if(event.propertyName === 'value'){
- adjust();
- }
- });
- }
- } else {
-
- $ta.on('input.autosize', adjust);
- }
-
-
- if (options.resizeDelay !== false) {
- $(window).on('resize.autosize', resize);
- }
-
-
- $ta.on('autosize.resize', adjust);
-
-
- $ta.on('autosize.resizeIncludeStyle', function() {
- mirrored = null;
- adjust();
- });
- $ta.on('autosize.destroy', function(){
- mirrored = null;
- clearTimeout(timeout);
- $(window).off('resize', resize);
- $ta
- .off('autosize')
- .off('.autosize')
- .css(originalStyles)
- .removeData('autosize');
- });
-
- adjust();
- });
- };
- }));
|