forked from akaihola/jquery-autogrow
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.autogrow.js
179 lines (156 loc) · 4.2 KB
/
jquery.autogrow.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Auto Expanding Text Area (1.2.2)
* by Chrys Bader (www.chrysbader.com)
* chrysb@gmail.com
*
* Special thanks to:
* Jake Chapa - jake@hybridstudio.com
* John Resig - jeresig@gmail.com
*
* Copyright (c) 2008 Chrys Bader (www.chrysbader.com)
* Licensed under the GPL (GPL-LICENSE.txt) license.
*
*
* NOTE: This script requires jQuery to work. Download jQuery at www.jquery.com
*
*/
(function(jQuery) {
var self = null;
jQuery.fn.autogrow = function(o)
{
return this.each(function() {
new jQuery.autogrow(this, o);
});
};
/**
* The autogrow object.
*
* @constructor
* @name jQuery.autogrow
* @param Object e The textarea to create the autogrow for.
* @param Hash o A set of key/value pairs to set as configuration properties.
* @cat Plugins/autogrow
*/
jQuery.autogrow = function (e, o)
{
this.options = o || {};
this.dummy = null;
this.interval = null;
this.line_height = this.options.lineHeight || parseInt(jQuery(e).css('line-height'));
if (isNaN(this.line_height)) {
// "normal" could also be a property, which will fail later
throw "The line-height must be a number (either the option or the css property on the element)";
}
this.min_height = parseInt(this.options.minHeight || jQuery(e).css('min-height'));
this.max_height = this.options.maxHeight || parseInt(jQuery(e).css('max-height'));;
this.expand_callback = this.options.expandCallback;
this.resize_callback = this.options.resizeCallback || function() {};
this.textarea = jQuery(e);
this.even_numbered_height = this.options.evenNumberedHeight || false;
if(this.line_height == NaN)
this.line_height = 0;
// Only one textarea activated at a time, the one being used
this.init();
};
jQuery.autogrow.fn = jQuery.autogrow.prototype = {
autogrow: '1.2.2'
};
jQuery.autogrow.fn.extend = jQuery.autogrow.extend = jQuery.extend;
jQuery.autogrow.fn.extend({
init: function() {
var self = this;
this.textarea.css({overflow: 'hidden', display: 'block'});
this.textarea.bind(
'focus',
function() {
self.startExpand()
}
).bind(
'blur',
function() {
self.stopExpand()
}
);
this.checkExpand();
},
startExpand: function() {
var self = this;
this.interval = window.setInterval(
function() {
self.checkExpand()
},
400
);
},
stopExpand: function() {
clearInterval(this.interval);
},
checkExpand: function() {
if (this.dummy == null)
{
this.dummy = jQuery('<div></div>');
this.dummy.css({
'font-size' : this.textarea.css('font-size'),
'font-family': this.textarea.css('font-family'),
'width' : this.textarea.css('width'),
'padding' : this.textarea.css('padding'),
'line-height': this.line_height + 'px',
'overflow-x' : 'hidden',
'position' : 'absolute',
'top' : 0,
'left' : -9999
}).appendTo('body');
}
// Strip HTML tags
var html = this.textarea.val().replace(/(<|>)/g, '');
// IE is different, as per usual
if (jQuery.browser.msie)
{
html = html.replace(/\n/g, '<BR>new');
}
else
{
html = html.replace(/\n/g, '<br>new');
}
if (this.dummy.html() != html)
{
this.dummy.html(html);
if ( this.max_height > 0
&& (this.dummy.height() + this.line_height > this.max_height)
)
{
this.textarea.css('overflow-y', 'auto');
}
else
{
this.textarea.css('overflow-y', 'hidden');
if ( this.textarea.height() < this.dummy.height() + this.line_height
|| (this.dummy.height() < this.textarea.height())
)
{
var newHeight = (this.dummy.height() + this.line_height);
if (newHeight < this.min_height)
{
newHeight = this.min_height;
}
if (this.even_numbered_height) {
newHeight += newHeight % 2;
}
this.textarea.animate({height: newHeight + 'px'}, 100);
this.resize_callback(newHeight);
}
}
}
if (this.expand_callback)
{
var self = this;
window.setTimeout(
function() {
self.expand_callback()
},
500
);
}
}
});
})(jQuery);