forked from bgrins/ExpandingTextareas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expanding.js
134 lines (108 loc) · 4.01 KB
/
expanding.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// 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 || {});
var cloneCSSProperties = [
'lineHeight', 'textDecoration', 'letterSpacing',
'fontSize', 'fontFamily', 'fontStyle',
'fontWeight', 'textTransform', 'textAlign',
'direction', 'wordSpacing', 'fontSizeAdjust',
'wordWrap', 'word-break',
'borderLeftWidth', 'borderRightWidth',
'borderTopWidth','borderBottomWidth',
'paddingLeft', 'paddingRight',
'paddingTop','paddingBottom',
'marginLeft', 'marginRight',
'marginTop','marginBottom',
'boxSizing', 'webkitBoxSizing', 'mozBoxSizing', 'msBoxSizing'
];
var textareaCSS = {
position: "absolute",
height: "100%",
resize: "none"
};
var preCSS = {
visibility: "visible",
border: "0 solid",
whiteSpace: "pre-wrap"
};
var containerCSS = {
position: "relative"
};
function resize() {
var $this = $(this);
$this.closest('.expandingText').find("div").text(this.value + ' ');
//Handle width by checking box-sizing first.
if ($this.css('-moz-box-sizing') === 'content-box' || $this.css('box-sizing') === 'content-box'){
$this.closest('.expandingText').find("pre").width($(this).width());
}
else if ($this.css('-moz-box-sizing') === 'border-box') {
var paddingWidth = parseInt($this.css('padding-left')) + parseInt($this.css('padding-right')),
borderWidth = parseInt($this.css('border-left-width')) + parseInt($this.css('border-right-width'));
$this.closest('.expandingText').find("pre").width($this.outerWidth() - paddingWidth - borderWidth);
}
else {
$this.closest('.expandingText').find("pre").width($this.outerWidth());
}
$this.trigger("resize.expanding");
}
$.fn.expandingTextarea = function(o) {
var opts = $.extend({ }, $.expandingTextarea.opts, o);
if (o === "resize") {
return this.trigger("input.expanding");
}
if (o === "destroy") {
this.filter(".expanding-init").each(function() {
var textarea = $(this).removeClass('expanding-init');
var container = textarea.closest('.expandingText');
container.before(textarea).remove();
textarea
.attr('style', textarea.data('expanding-styles') || '')
.removeData('expanding-styles');
});
return this;
}
this.filter("textarea").not(".expanding-init").addClass("expanding-init").each(function() {
var textarea = $(this);
textarea.wrap("<div class='expandingText'></div>");
textarea.after("<pre class='textareaClone'><div></div></pre>");
var container = textarea.parent().css(containerCSS);
var pre = container.find("pre").css(preCSS);
// Store the original styles in case of destroying.
textarea.data('expanding-styles', textarea.attr('style'));
textarea.css(textareaCSS);
$.each(cloneCSSProperties, function(i, p) {
var val = textarea.css(p);
// Only set if different to prevent overriding percentage css values.
if (pre.css(p) !== val) {
pre.css(p, val);
}
});
textarea.bind("input.expanding propertychange.expanding keyup.expanding", resize);
resize.apply(this);
if (opts.resize) {
textarea.bind("resize.expanding", opts.resize);
}
});
return this;
};
$(function () {
if ($.expandingTextarea.autoInitialize) {
$($.expandingTextarea.initialSelector).expandingTextarea();
}
});
}));