2012-05-04 20:17:15 +02:00
|
|
|
// Expanding Textareas
|
|
|
|
// https://github.com/bgrins/ExpandingTextareas
|
|
|
|
|
|
|
|
(function(factory) {
|
|
|
|
// Add jQuery via AMD registration or browser globals
|
|
|
|
if (typeof define === 'function' && define.amd) {
|
|
|
|
define([ 'jquery' ], factory);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
factory(jQuery);
|
|
|
|
}
|
|
|
|
}(function ($) {
|
|
|
|
$.expandingTextarea = $.extend({
|
|
|
|
autoInitialize: true,
|
|
|
|
initialSelector: "textarea.expanding",
|
|
|
|
opts: {
|
|
|
|
resize: function() { }
|
|
|
|
}
|
|
|
|
}, $.expandingTextarea || {});
|
2012-09-07 14:32:45 +02:00
|
|
|
|
2012-05-04 20:17:15 +02:00
|
|
|
var cloneCSSProperties = [
|
|
|
|
'lineHeight', 'textDecoration', 'letterSpacing',
|
2012-09-07 14:32:45 +02:00
|
|
|
'fontSize', 'fontFamily', 'fontStyle',
|
|
|
|
'fontWeight', 'textTransform', 'textAlign',
|
|
|
|
'direction', 'wordSpacing', 'fontSizeAdjust',
|
|
|
|
'wordWrap',
|
2012-05-04 20:17:15 +02:00
|
|
|
'borderLeftWidth', 'borderRightWidth',
|
|
|
|
'borderTopWidth','borderBottomWidth',
|
|
|
|
'paddingLeft', 'paddingRight',
|
|
|
|
'paddingTop','paddingBottom',
|
|
|
|
'marginLeft', 'marginRight',
|
|
|
|
'marginTop','marginBottom',
|
|
|
|
'boxSizing', 'webkitBoxSizing', 'mozBoxSizing', 'msBoxSizing'
|
|
|
|
];
|
2012-09-07 14:32:45 +02:00
|
|
|
|
2012-05-04 20:17:15 +02:00
|
|
|
var textareaCSS = {
|
|
|
|
position: "absolute",
|
|
|
|
height: "100%",
|
|
|
|
resize: "none"
|
|
|
|
};
|
2012-09-07 14:32:45 +02:00
|
|
|
|
2012-05-04 20:17:15 +02:00
|
|
|
var preCSS = {
|
|
|
|
visibility: "hidden",
|
|
|
|
border: "0 solid",
|
2012-09-07 14:32:45 +02:00
|
|
|
whiteSpace: "pre-wrap"
|
2012-05-04 20:17:15 +02:00
|
|
|
};
|
2012-09-07 14:32:45 +02:00
|
|
|
|
2012-05-04 20:17:15 +02:00
|
|
|
var containerCSS = {
|
|
|
|
position: "relative"
|
|
|
|
};
|
2012-09-07 14:32:45 +02:00
|
|
|
|
2012-05-04 20:17:15 +02:00
|
|
|
function resize() {
|
|
|
|
$(this).closest('.expandingText').find("div").text(this.value + ' ');
|
|
|
|
$(this).trigger("resize.expanding");
|
|
|
|
}
|
2012-09-07 14:32:45 +02:00
|
|
|
|
2012-05-04 20:17:15 +02:00
|
|
|
$.fn.expandingTextarea = function(o) {
|
2012-09-07 14:32:45 +02:00
|
|
|
|
2012-05-04 20:17:15 +02:00
|
|
|
var opts = $.extend({ }, $.expandingTextarea.opts, o);
|
2012-09-07 14:32:45 +02:00
|
|
|
|
2012-05-04 20:17:15 +02:00
|
|
|
if (o === "resize") {
|
|
|
|
return this.trigger("input.expanding");
|
|
|
|
}
|
2012-09-07 14:32:45 +02:00
|
|
|
|
2012-05-04 20:17:15 +02:00
|
|
|
if (o === "destroy") {
|
|
|
|
this.filter(".expanding-init").each(function() {
|
|
|
|
var textarea = $(this).removeClass('expanding-init');
|
|
|
|
var container = textarea.closest('.expandingText');
|
2012-09-07 14:32:45 +02:00
|
|
|
|
2012-05-04 20:17:15 +02:00
|
|
|
container.before(textarea).remove();
|
|
|
|
textarea
|
|
|
|
.attr('style', textarea.data('expanding-styles') || '')
|
|
|
|
.removeData('expanding-styles');
|
|
|
|
});
|
2012-09-07 14:32:45 +02:00
|
|
|
|
2012-05-04 20:17:15 +02:00
|
|
|
return this;
|
|
|
|
}
|
2012-09-07 14:32:45 +02:00
|
|
|
|
2012-05-04 20:17:15 +02:00
|
|
|
this.filter("textarea").not(".expanding-init").addClass("expanding-init").each(function() {
|
|
|
|
var textarea = $(this);
|
2012-09-07 14:32:45 +02:00
|
|
|
|
2012-05-04 20:17:15 +02:00
|
|
|
textarea.wrap("<div class='expandingText'></div>");
|
|
|
|
textarea.after("<pre class='textareaClone'><div></div></pre>");
|
2012-09-07 14:32:45 +02:00
|
|
|
|
2012-05-04 20:17:15 +02:00
|
|
|
var container = textarea.parent().css(containerCSS);
|
|
|
|
var pre = container.find("pre").css(preCSS);
|
2012-09-07 14:32:45 +02:00
|
|
|
|
2012-05-04 20:17:15 +02:00
|
|
|
// Store the original styles in case of destroying.
|
|
|
|
textarea.data('expanding-styles', textarea.attr('style'));
|
|
|
|
textarea.css(textareaCSS);
|
2012-09-07 14:32:45 +02:00
|
|
|
|
2012-05-04 20:17:15 +02:00
|
|
|
$.each(cloneCSSProperties, function(i, p) {
|
|
|
|
var val = textarea.css(p);
|
2012-09-07 14:32:45 +02:00
|
|
|
|
2012-05-04 20:17:15 +02:00
|
|
|
// Only set if different to prevent overriding percentage css values.
|
|
|
|
if (pre.css(p) !== val) {
|
|
|
|
pre.css(p, val);
|
|
|
|
}
|
|
|
|
});
|
2012-09-07 14:32:45 +02:00
|
|
|
|
2012-05-04 20:17:15 +02:00
|
|
|
textarea.bind("input.expanding propertychange.expanding", resize);
|
|
|
|
resize.apply(this);
|
2012-09-07 14:32:45 +02:00
|
|
|
|
2012-05-04 20:17:15 +02:00
|
|
|
if (opts.resize) {
|
|
|
|
textarea.bind("resize.expanding", opts.resize);
|
|
|
|
}
|
|
|
|
});
|
2012-09-07 14:32:45 +02:00
|
|
|
|
2012-05-04 20:17:15 +02:00
|
|
|
return this;
|
|
|
|
};
|
2012-09-07 14:32:45 +02:00
|
|
|
|
2012-05-04 20:17:15 +02:00
|
|
|
$(function () {
|
|
|
|
if ($.expandingTextarea.autoInitialize) {
|
|
|
|
$($.expandingTextarea.initialSelector).expandingTextarea();
|
|
|
|
}
|
|
|
|
});
|
2012-09-07 14:32:45 +02:00
|
|
|
|
2012-05-04 20:17:15 +02:00
|
|
|
}));
|