-
Notifications
You must be signed in to change notification settings - Fork 2
/
AutosizeTextareaSurface.js
232 lines (215 loc) · 8.8 KB
/
AutosizeTextareaSurface.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/**
* This Source Code is licensed under the MIT license. If a copy of the
* MIT-license was not distributed with this file, You can obtain one at:
* http://opensource.org/licenses/mit-license.html.
*
* @author: Hein Rutjes (IjzerenHein)
* @license MIT
* @copyright Gloey Apps, 2014 - 2016
*/
/**
* @module
*/
define(function(require, exports, module) {
'use strict';
// import dependencies
var TextareaSurface = require('famous/surfaces/TextareaSurface');
/**
* @class
* @extends TextareaSurface
* @param {Object} [options] Configuration options
*/
function AutosizeTextareaSurface(options) {
this._heightInvalidated = true;
this._oldCachedSize = [0, 0];
_createHiddenSurface.call(this);
TextareaSurface.apply(this, arguments);
this.on('change', _onValueChanged.bind(this));
this.on('keyup', _onValueChanged.bind(this));
this.on('keydown', _onValueChanged.bind(this));
}
AutosizeTextareaSurface.prototype = Object.create(TextareaSurface.prototype);
AutosizeTextareaSurface.prototype.constructor = AutosizeTextareaSurface;
//
// Called whenever the value is changed, copies the value to the
// hidden TextArea surface.
//
function _onValueChanged(event) {
this._heightInvalidated = true;
}
/**
* Create the hidden text-area surface
*/
function _createHiddenSurface() {
this._preferredScrollHeight = 0;
this._hiddenTextarea = new TextareaSurface({});
this.setProperties({});
}
/**
* Checks whether the scroll-height has changed and when so
* emits an event about the preferred height.
*/
AutosizeTextareaSurface.prototype.render = function render() {
// Return render-spec of both this textArea and the hidden
// text-area so that they are both rendered.
return [this._hiddenTextarea.id, this.id];
};
var oldCommit = AutosizeTextareaSurface.prototype.commit;
/**
* Apply changes from this component to the corresponding document element.
* This includes changes to classes, styles, size, content, opacity, origin,
* and matrix transforms.
*
* @private
* @method commit
* @param {Context} context commit context
*/
AutosizeTextareaSurface.prototype.commit = function commit(context) {
// Call base class
oldCommit.apply(this, arguments);
// Check if height has been changed
if ((this._oldCachedSize[0] !== context.size[0]) ||
(this._oldCachedSize[1] !== context.size[1])) {
this._oldCachedSize[0] = context.size[0];
this._oldCachedSize[1] = context.size[1];
this._heightInvalidated = true;
}
// Caluclate preferred height
if (this._currentTarget && this._hiddenTextarea._currentTarget && this._heightInvalidated) {
this._hiddenTextarea._currentTarget.value = this._currentTarget.value;
this._heightInvalidated = false;
// Calculate ideal scrollheight
this._hiddenTextarea._currentTarget.rows = 1;
this._hiddenTextarea._currentTarget.style.height = '';
var scrollHeight = this._hiddenTextarea._currentTarget.scrollHeight;
if (scrollHeight !== this._preferredScrollHeight) {
this._preferredScrollHeight = scrollHeight;
//console.log('scrollHeight changed: ' + this._preferredScrollHeight);
this._eventOutput.emit('scrollHeightChanged', this._preferredScrollHeight);
}
}
};
/**
* Get the height of the scrollable content.
*
* @return {Number} Ideal height that would fit all the content.
*/
AutosizeTextareaSurface.prototype.getScrollHeight = function() {
return this._preferredScrollHeight;
};
/**
* Copy set properties to hidden text-area and ensure that it stays hidden.
*/
var oldSetProperties = AutosizeTextareaSurface.prototype.setProperties;
AutosizeTextareaSurface.prototype.setProperties = function setProperties(properties) {
properties = properties || {};
var hiddenProperties = {};
for (var key in properties) {
hiddenProperties[key] = properties[key];
}
hiddenProperties.visibility = 'hidden';
this._hiddenTextarea.setProperties(hiddenProperties);
this._heightInvalidated = true;
return oldSetProperties.call(this, properties);
};
/**
* Override methods and forward to hidden text-area, so that they use the
* same settings.
*/
var oldSetAttributes = AutosizeTextareaSurface.prototype.setAttributes;
AutosizeTextareaSurface.prototype.setAttributes = function setAttributes(attributes) {
this._heightInvalidated = true;
this._hiddenTextarea.setAttributes(attributes);
return oldSetAttributes.call(this, attributes);
};
var oldAddClass = AutosizeTextareaSurface.prototype.addClass;
AutosizeTextareaSurface.prototype.addClass = function addClass(className) {
this._heightInvalidated = true;
this._hiddenTextarea.addClass(className);
return oldAddClass.call(this, className);
};
var oldRemoveClass = AutosizeTextareaSurface.prototype.removeClass;
AutosizeTextareaSurface.prototype.removeClass = function removeClass(className) {
this._heightInvalidated = true;
this._hiddenTextarea.removeClass(className);
return oldRemoveClass.call(this, className);
};
var oldToggleClass = AutosizeTextareaSurface.prototype.toggleClass;
AutosizeTextareaSurface.prototype.toggleClass = function toggleClass(className) {
this._heightInvalidated = true;
this._hiddenTextarea.toggleClass(className);
return oldToggleClass.call(this, className);
};
var oldSetClasses = AutosizeTextareaSurface.prototype.setClasses;
AutosizeTextareaSurface.prototype.setClasses = function setClasses(classList) {
this._heightInvalidated = true;
this._hiddenTextarea.setClasses(classList);
return oldSetClasses.call(this, classList);
};
var oldSetContent = AutosizeTextareaSurface.prototype.setContent;
AutosizeTextareaSurface.prototype.setContent = function setContent(content) {
this._heightInvalidated = true;
this._hiddenTextarea.setContent(content);
return oldSetContent.call(this, content);
};
var oldSetOptions = AutosizeTextareaSurface.prototype.setOptions;
AutosizeTextareaSurface.prototype.setOptions = function setOptions(options) {
this._heightInvalidated = true;
this._hiddenTextarea.setOptions(options);
return oldSetOptions.call(this, options);
};
var oldSetValue = AutosizeTextareaSurface.prototype.setValue;
AutosizeTextareaSurface.prototype.setValue = function setValue(str) {
this._heightInvalidated = true;
return oldSetValue.call(this, str);
};
var oldSetWrap = AutosizeTextareaSurface.prototype.setWrap;
AutosizeTextareaSurface.prototype.setWrap = function setWrap(str) {
this._heightInvalidated = true;
this._hiddenTextarea.setWrap(str);
return oldSetWrap.call(this, str);
};
var oldSetColumns = AutosizeTextareaSurface.prototype.setColumns;
AutosizeTextareaSurface.prototype.setColumns = function setColumns(num) {
this._heightInvalidated = true;
this._hiddenTextarea.setColumns(num);
return oldSetColumns.call(this, num);
};
var oldSetRows = AutosizeTextareaSurface.prototype.setRows;
AutosizeTextareaSurface.prototype.setRows = function setRows(num) {
this._heightInvalidated = true;
this._hiddenTextarea.setRows(num);
return oldSetRows.call(this, num);
};
var oldSetPlaceholder = AutosizeTextareaSurface.prototype.setPlaceholder;
AutosizeTextareaSurface.prototype.setPlaceholder = function setPlaceholder(str) {
this._heightInvalidated = true;
this._hiddenTextarea.setPlaceholder(str);
return oldSetPlaceholder.call(this, str);
};
/**
* Place the document element this component manages into the document.
*
* This fixes the issue that the value cannot be set to an empty string:
* https://github.com/Famous/famous/issues/414
*
* @private
* @method deploy
* @param {Node} target document parent of this container
*/
AutosizeTextareaSurface.prototype.deploy = function deploy(target) {
target.placeholder = this._placeholder || '';
target.value = this._value;
target.name = this._name;
if (this._wrap !== ''){
target.wrap = this._wrap;
}
if (this._cols !== ''){
target.cols = this._cols;
}
if (this._rows !== ''){
target.rows = this._rows;
}
};
module.exports = AutosizeTextareaSurface;
});