wiki/list-editor/textarea.autosize.js

56 lines
1.7 KiB
JavaScript

function textareaAutosizeIinit($) {
var pluginName = "textareaAutoSize";
var pluginDataName = "plugin_" + pluginName;
var containsText = function (value) {
return (value.replace(/\s/g, '').length > 0);
};
function Plugin(element, options) {
this.element = element;
this.$element = $(element);
this.init();
}
Plugin.prototype = {
init: function () {
var diff = parseInt(this.$element.css('paddingBottom')) +
parseInt(this.$element.css('paddingTop')) +
parseInt(this.$element.css('borderTopWidth')) +
parseInt(this.$element.css('borderBottomWidth')) || 0;
if (containsText(this.element.value)) {
let height = this.element.scrollHeight - diff;
height = 24 * Math.round(height / 24)
this.$element.height(height);
}
// keyup is required for IE to properly reset height when deleting text
this.$element.on('input keyup', function (event) {
var $window = $(window);
var currentScrollPosition = $window.scrollTop();
let height = this.scrollHeight - diff;
height = 24 * Math.round(height / 24)
$(this)
.height(0)
.height(height);
$window.scrollTop(currentScrollPosition);
});
}
};
$.fn[pluginName] = function (options) {
this.each(function () {
if (!$.data(this, pluginDataName)) {
$.data(this, pluginDataName, new Plugin(this, options));
}
});
return this;
};
}
export default textareaAutosizeIinit;