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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
cr.define('options', function() {
var EditableTextField = cr.ui.define('div');
/**
* Decorates an element as an editable text field.
* @param {!HTMLElement} el The element to decorate.
*/
EditableTextField.decorate = function(el) {
el.__proto__ = EditableTextField.prototype;
el.decorate();
};
EditableTextField.prototype = {
__proto__: HTMLDivElement.prototype,
/**
* The actual input element in this field.
* @type {?HTMLElement}
* @private
*/
editField_: null,
/**
* The static text displayed when this field isn't editable.
* @type {?HTMLElement}
* @private
*/
staticText_: null,
/**
* The data model for this field.
* @type {?Object}
* @private
*/
model_: null,
/**
* Whether or not the current edit should be considered canceled, rather
* than committed, when editing ends.
* @type {boolean}
* @private
*/
editCanceled_: true,
/** @override */
decorate: function() {
this.classList.add('editable-text-field');
this.createEditableTextCell();
if (this.hasAttribute('i18n-placeholder-text')) {
var identifier = this.getAttribute('i18n-placeholder-text');
var localizedText = loadTimeData.getString(identifier);
if (localizedText)
this.setAttribute('placeholder-text', localizedText);
}
this.addEventListener('keydown', this.handleKeyDown_);
this.editField_.addEventListener('focus', this.handleFocus_.bind(this));
this.editField_.addEventListener('blur', this.handleBlur_.bind(this));
this.checkForEmpty_();
},
/**
* Indicates that this field has no value in the model, and the placeholder
* text (if any) should be shown.
* @type {boolean}
*/
get empty() {
return this.hasAttribute('empty');
},
/**
* The placeholder text to be used when the model or its value is empty.
* @type {string}
*/
get placeholderText() {
return this.getAttribute('placeholder-text');
},
set placeholderText(text) {
if (text)
this.setAttribute('placeholder-text', text);
else
this.removeAttribute('placeholder-text');
this.checkForEmpty_();
},
/**
* Returns the input element in this text field.
* @type {HTMLElement} The element that is the actual input field.
*/
get editField() {
return this.editField_;
},
/**
* Whether the user is currently editing the list item.
* @type {boolean}
*/
get editing() {
return this.hasAttribute('editing');
},
set editing(editing) {
if (this.editing == editing)
return;
if (editing)
this.setAttribute('editing', '');
else
this.removeAttribute('editing');
if (editing) {
this.editCanceled_ = false;
if (this.empty) {
this.removeAttribute('empty');
if (this.editField)
this.editField.value = '';
}
if (this.editField) {
this.editField.focus();
this.editField.select();
}
} else {
if (!this.editCanceled_ && this.hasBeenEdited &&
this.currentInputIsValid) {
this.updateStaticValues_();
cr.dispatchSimpleEvent(this, 'commitedit', true);
} else {
this.resetEditableValues_();
cr.dispatchSimpleEvent(this, 'canceledit', true);
}
this.checkForEmpty_();
}
},
/**
* Whether the item is editable.
* @type {boolean}
*/
get editable() {
return this.hasAttribute('editable');
},
set editable(editable) {
if (this.editable == editable)
return;
if (editable)
this.setAttribute('editable', '');
else
this.removeAttribute('editable');
this.editable_ = editable;
},
/**
* The data model for this field.
* @type {Object}
*/
get model() {
return this.model_;
},
set model(model) {
this.model_ = model;
this.checkForEmpty_(); // This also updates the editField value.
this.updateStaticValues_();
},
/**
* The HTML element that should have focus initially when editing starts,
* if a specific element wasn't clicked. Defaults to the first <input>
* element; can be overridden by subclasses if a different element should be
* focused.
* @type {?HTMLElement}
*/
get initialFocusElement() {
return this.querySelector('input');
},
/**
* Whether the input in currently valid to submit. If this returns false
* when editing would be submitted, either editing will not be ended,
* or it will be cancelled, depending on the context. Can be overridden by
* subclasses to perform input validation.
* @type {boolean}
*/
get currentInputIsValid() {
return true;
},
/**
* Returns true if the item has been changed by an edit. Can be overridden
* by subclasses to return false when nothing has changed to avoid
* unnecessary commits.
* @type {boolean}
*/
get hasBeenEdited() {
return true;
},
/**
* Mutates the input during a successful commit. Can be overridden to
* provide a way to "clean up" valid input so that it conforms to a
* desired format. Will only be called when commit succeeds for valid
* input, or when the model is set.
* @param {string} value Input text to be mutated.
* @return {string} mutated text.
*/
mutateInput: function(value) {
return value;
},
/**
* Creates a div containing an <input>, as well as static text, keeping
* references to them so they can be manipulated.
* @param {string} text The text of the cell.
* @private
*/
createEditableTextCell: function(text) {
// This function should only be called once.
if (this.editField_)
return;
var container = this.ownerDocument.createElement('div');
var textEl = this.ownerDocument.createElement('div');
textEl.className = 'static-text';
textEl.textContent = text;
textEl.setAttribute('displaymode', 'static');
this.appendChild(textEl);
this.staticText_ = textEl;
var inputEl = this.ownerDocument.createElement('input');
inputEl.className = 'editable-text';
inputEl.type = 'text';
inputEl.value = text;
inputEl.setAttribute('displaymode', 'edit');
inputEl.staticVersion = textEl;
this.appendChild(inputEl);
this.editField_ = inputEl;
},
/**
* Resets the editable version of any controls created by
* createEditableTextCell to match the static text.
* @private
*/
resetEditableValues_: function() {
var editField = this.editField_;
var staticLabel = editField.staticVersion;
if (!staticLabel)
return;
if (editField instanceof HTMLInputElement)
editField.value = staticLabel.textContent;
editField.setCustomValidity('');
},
/**
* Sets the static version of any controls created by createEditableTextCell
* to match the current value of the editable version. Called on commit so
* that there's no flicker of the old value before the model updates. Also
* updates the model's value with the mutated value of the edit field.
* @private
*/
updateStaticValues_: function() {
var editField = this.editField_;
var staticLabel = editField.staticVersion;
if (!staticLabel)
return;
if (editField instanceof HTMLInputElement) {
staticLabel.textContent = editField.value;
this.model_.value = this.mutateInput(editField.value);
}
},
/**
* Checks to see if the model or its value are empty. If they are, then set
* the edit field to the placeholder text, if any, and if not, set it to the
* model's value.
* @private
*/
checkForEmpty_: function() {
var editField = this.editField_;
if (!editField)
return;
if (!this.model_ || !this.model_.value) {
this.setAttribute('empty', '');
editField.value = this.placeholderText || '';
} else {
this.removeAttribute('empty');
editField.value = this.model_.value;
}
},
/**
* Called when this widget receives focus.
* @param {Event} e the focus event.
* @private
*/
handleFocus_: function(e) {
if (this.editing)
return;
this.editing = true;
if (this.editField_)
this.editField_.focus();
},
/**
* Called when this widget loses focus.
* @param {Event} e the blur event.
* @private
*/
handleBlur_: function(e) {
if (!this.editing)
return;
this.editing = false;
},
/**
* Called when a key is pressed. Handles committing and canceling edits.
* @param {Event} e The key down event.
* @private
*/
handleKeyDown_: function(e) {
if (!this.editing)
return;
var endEdit;
switch (e.keyIdentifier) {
case 'U+001B': // Esc
this.editCanceled_ = true;
endEdit = true;
break;
case 'Enter':
if (this.currentInputIsValid)
endEdit = true;
break;
}
if (endEdit) {
// Blurring will trigger the edit to end.
this.ownerDocument.activeElement.blur();
// Make sure that handled keys aren't passed on and double-handled.
// (e.g., esc shouldn't both cancel an edit and close a subpage)
e.stopPropagation();
}
},
};
/**
* Takes care of committing changes to EditableTextField items when the
* window loses focus.
*/
window.addEventListener('blur', function(e) {
var itemAncestor = findAncestor(document.activeElement, function(node) {
return node instanceof EditableTextField;
});
if (itemAncestor)
document.activeElement.blur();
});
return {
EditableTextField: EditableTextField,
};
});
|