-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTextareaSurface.js
194 lines (178 loc) · 6.23 KB
/
TextareaSurface.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Owner: [email protected]
* @license MPL 2.0
* @copyright Famous Industries, Inc. 2014
*/
define(function(require, exports, module) {
var Surface = require('famous/core/Surface');
/**
* A Famo.us surface in the form of an HTML textarea element.
* This extends the Surface class.
*
* @class TextareaSurface
* @extends Surface
* @constructor
* @param {Object} [options] overrides of default options
* @param {string} [options.placeholder] placeholder text hint that describes the expected value of an textarea element
* @param {string} [options.value] value of text
* @param {string} [options.name] specifies the name of textarea
* @param {string} [options.wrap] specify 'hard' or 'soft' wrap for textarea
* @param {number} [options.cols] number of columns in textarea
* @param {number} [options.rows] number of rows in textarea
*/
function TextareaSurface(options) {
this._placeholder = options.placeholder || '';
this._value = options.value || '';
this._name = options.name || '';
this._wrap = options.wrap || '';
this._cols = options.cols || '';
this._rows = options.rows || '';
Surface.apply(this, arguments);
this.on('click', this.focus.bind(this));
}
TextareaSurface.prototype = Object.create(Surface.prototype);
TextareaSurface.prototype.constructor = TextareaSurface;
TextareaSurface.prototype.elementType = 'textarea';
TextareaSurface.prototype.elementClass = 'famous-surface';
/**
* Set placeholder text. Note: Triggers a repaint.
*
* @method setPlaceholder
* @param {string} str Value to set the placeholder to.
* @return {TextareaSurface} this, allowing method chaining.
*/
TextareaSurface.prototype.setPlaceholder = function setPlaceholder(str) {
this._placeholder = str;
this._contentDirty = true;
return this;
};
/**
* Focus on the current input, pulling up the keyboard on mobile.
*
* @method focus
* @return {TextareaSurface} this, allowing method chaining.
*/
TextareaSurface.prototype.focus = function focus() {
if (this._currTarget) this._currTarget.focus();
return this;
};
/**
* Blur the current input, hiding the keyboard on mobile.
*
* @method focus
* @return {TextareaSurface} this, allowing method chaining.
*/
TextareaSurface.prototype.blur = function blur() {
if (this._currTarget) this._currTarget.blur();
return this;
};
/**
* Set the value of textarea.
* Note: Triggers a repaint next tick.
*
* @method setValue
* @param {string} str Value to set the main textarea value to.
* @return {TextareaSurface} this, allowing method chaining.
*/
TextareaSurface.prototype.setValue = function setValue(str) {
this._value = str;
this._contentDirty = true;
return this;
};
/**
* Get the value of the inner content of the textarea (e.g. the entered text)
*
* @method getValue
* @return {string} value of element
*/
TextareaSurface.prototype.getValue = function getValue() {
if (this._currTarget) {
return this._currTarget.value;
}
else {
return this._value;
}
};
/**
* Set the name attribute of the element.
* Note: Triggers a repaint next tick.
*
* @method setName
* @param {string} str element name
* @return {TextareaSurface} this, allowing method chaining.
*/
TextareaSurface.prototype.setName = function setName(str) {
this._name = str;
this._contentDirty = true;
return this;
};
/**
* Get the name attribute of the element.
*
* @method getName
* @return {string} name of element
*/
TextareaSurface.prototype.getName = function getName() {
return this._name;
};
/**
* Set the wrap of textarea.
* Note: Triggers a repaint next tick.
*
* @method setWrap
* @param {string} str wrap of the textarea surface (e.g. 'soft', 'hard')
* @return {TextareaSurface} this, allowing method chaining.
*/
TextareaSurface.prototype.setWrap = function setWrap(str) {
this._wrap = str;
this._contentDirty = true;
return this;
};
/**
* Set the number of columns visible in the textarea.
* Note: Overridden by surface size; set width to true. (eg. size: [true, *])
* Triggers a repaint next tick.
*
* @method setColumns
* @param {number} num columns in textarea surface
* @return {TextareaSurface} this, allowing method chaining.
*/
TextareaSurface.prototype.setColumns = function setColumns(num) {
this._cols = num;
this._contentDirty = true;
return this;
};
/**
* Set the number of rows visible in the textarea.
* Note: Overridden by surface size; set height to true. (eg. size: [*, true])
* Triggers a repaint next tick.
*
* @method setRows
* @param {number} num rows in textarea surface
* @return {TextareaSurface} this, allowing method chaining.
*/
TextareaSurface.prototype.setRows = function setRows(num) {
this._rows = num;
this._contentDirty = true;
return this;
};
/**
* Place the document element this component manages into the document.
*
* @private
* @method deploy
* @param {Node} target document parent of this container
*/
TextareaSurface.prototype.deploy = function deploy(target) {
if (this._placeholder !== '') target.placeholder = this._placeholder;
if (this._value !== '') target.value = this._value;
if (this._name !== '') 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 = TextareaSurface;
});