PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/autosize.js/1.16.13/jquery.autosize.js

https://gitlab.com/Blueprint-Marketing/cdnjs
JavaScript | 188 lines | 124 code | 31 blank | 33 comment | 24 complexity | 72fd0f8a4d8eebc716f1b3ef71fcdbce MD5 | raw file
  1. /*!
  2. jQuery Autosize v1.16.13
  3. (c) 2013 Jack Moore - jacklmoore.com
  4. updated: 2013-06-04
  5. license: http://www.opensource.org/licenses/mit-license.php
  6. */
  7. (function ($) {
  8. var
  9. defaults = {
  10. className: 'autosizejs',
  11. append: '',
  12. callback: false
  13. },
  14. hidden = 'hidden',
  15. borderBox = 'border-box',
  16. lineHeight = 'lineHeight',
  17. // border:0 is unnecessary, but avoids a bug in FireFox on OSX
  18. copy = '<textarea tabindex="-1" style="position:absolute; top:-999px; left:0; right:auto; bottom:auto; border: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;"/>',
  19. // line-height is conditionally included because IE7/IE8/old Opera do not return the correct value.
  20. copyStyle = [
  21. 'fontFamily',
  22. 'fontSize',
  23. 'fontWeight',
  24. 'fontStyle',
  25. 'letterSpacing',
  26. 'textTransform',
  27. 'wordSpacing',
  28. 'textIndent'
  29. ],
  30. oninput = 'oninput',
  31. onpropertychange = 'onpropertychange',
  32. // to keep track which textarea is being mirrored when adjust() is called.
  33. mirrored,
  34. // the mirror element, which is used to calculate what size the mirrored element should be.
  35. mirror = $(copy).data('autosize', true)[0];
  36. // test that line-height can be accurately copied.
  37. mirror.style.lineHeight = '99px';
  38. if ($(mirror).css(lineHeight) === '99px') {
  39. copyStyle.push(lineHeight);
  40. }
  41. mirror.style.lineHeight = '';
  42. $.fn.autosize = function (options) {
  43. options = $.extend({}, defaults, options || {});
  44. if (mirror.parentNode !== document.body) {
  45. $(document.body).append(mirror);
  46. }
  47. return this.each(function () {
  48. var
  49. ta = this,
  50. $ta = $(ta),
  51. minHeight,
  52. maxHeight,
  53. resize,
  54. boxOffset = 0,
  55. callback = $.isFunction(options.callback);
  56. if ($ta.data('autosize')) {
  57. // exit if autosize has already been applied, or if the textarea is the mirror element.
  58. return;
  59. }
  60. if ($ta.css('box-sizing') === borderBox || $ta.css('-moz-box-sizing') === borderBox || $ta.css('-webkit-box-sizing') === borderBox){
  61. boxOffset = $ta.outerHeight() - $ta.height();
  62. }
  63. // IE8 and lower return 'auto', which parses to NaN, if no min-height is set.
  64. minHeight = Math.max(parseInt($ta.css('minHeight'), 10) - boxOffset || 0, $ta.height());
  65. resize = ($ta.css('resize') === 'none' || $ta.css('resize') === 'vertical') ? 'none' : 'horizontal';
  66. $ta.css({
  67. overflow: hidden,
  68. overflowY: hidden,
  69. wordWrap: 'break-word',
  70. resize: resize
  71. }).data('autosize', true);
  72. function initMirror() {
  73. mirrored = ta;
  74. mirror.className = options.className;
  75. maxHeight = parseInt($ta.css('maxHeight'), 10);
  76. // mirror is a duplicate textarea located off-screen that
  77. // is automatically updated to contain the same text as the
  78. // original textarea. mirror always has a height of 0.
  79. // This gives a cross-browser supported way getting the actual
  80. // height of the text, through the scrollTop property.
  81. $.each(copyStyle, function(i, val){
  82. mirror.style[val] = $ta.css(val);
  83. });
  84. // The textarea overflow is probably now hidden, but Chrome doesn't reflow the text to account for the
  85. // new space made available by removing the scrollbars. This workaround causes Chrome to reflow the text.
  86. if (oninput in ta) {
  87. var width = ta.style.width;
  88. ta.style.width = '0px';
  89. var discard = ta.offsetWidth; // trigger a reflow
  90. ta.style.width = width;
  91. }
  92. }
  93. // Using mainly bare JS in this function because it is going
  94. // to fire very often while typing, and needs to very efficient.
  95. function adjust() {
  96. var height, overflow, original;
  97. if (mirrored !== ta) {
  98. initMirror();
  99. }
  100. mirror.value = ta.value + options.append;
  101. mirror.style.overflowY = ta.style.overflowY;
  102. original = parseInt(ta.style.height,10);
  103. // Update the width in case the original textarea width has changed
  104. // A floor of 0 is needed because IE8 returns a negative value for hidden textareas, raising an error.
  105. mirror.style.width = Math.max($ta.width(), 0) + 'px';
  106. // Needed for IE8 and lower to reliably return the correct scrollTop
  107. mirror.scrollTop = 0;
  108. mirror.scrollTop = 9e4;
  109. // Using scrollTop rather than scrollHeight because scrollHeight is non-standard and includes padding.
  110. height = mirror.scrollTop;
  111. if (maxHeight && height > maxHeight) {
  112. height = maxHeight;
  113. overflow = 'scroll';
  114. } else if (height < minHeight) {
  115. height = minHeight;
  116. }
  117. height += boxOffset;
  118. ta.style.overflowY = overflow || hidden;
  119. if (original !== height) {
  120. ta.style.height = height + 'px';
  121. if (callback) {
  122. options.callback.call(ta,ta);
  123. }
  124. }
  125. }
  126. if (onpropertychange in ta) {
  127. if (oninput in ta) {
  128. // Detects IE9. IE9 does not fire onpropertychange or oninput for deletions,
  129. // so binding to onkeyup to catch most of those occassions. There is no way that I
  130. // know of to detect something like 'cut' in IE9.
  131. ta[oninput] = ta.onkeyup = adjust;
  132. } else {
  133. // IE7 / IE8
  134. ta[onpropertychange] = function(){
  135. if(event.propertyName === 'value'){
  136. adjust();
  137. }
  138. };
  139. }
  140. } else {
  141. // Modern Browsers
  142. ta[oninput] = adjust;
  143. }
  144. $(window).on('resize', function(){
  145. active = false;
  146. adjust();
  147. });
  148. // Allow for manual triggering if needed.
  149. $ta.on('autosize', function(){
  150. active = false;
  151. adjust();
  152. });
  153. // Call adjust in case the textarea already contains text.
  154. adjust();
  155. });
  156. };
  157. }(window.jQuery || window.Zepto));