1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 cr.define('options', function() {
6 const DeletableItem = options.DeletableItem;
7 const DeletableItemList = options.DeletableItemList;
10 * Creates a new list item with support for inline editing.
12 * @extends {options.DeletableListItem}
14 function InlineEditableItem() {
15 var el = cr.doc.createElement('div');
16 InlineEditableItem.decorate(el);
21 * Decorates an element as a inline-editable list item. Note that this is
22 * a subclass of DeletableItem.
23 * @param {!HTMLElement} el The element to decorate.
25 InlineEditableItem.decorate = function(el) {
26 el.__proto__ = InlineEditableItem.prototype;
30 InlineEditableItem.prototype = {
31 __proto__: DeletableItem.prototype,
34 * Whether or not this item can be edited.
41 * Whether or not this is a placeholder for adding a new item.
45 isPlaceholder_: false,
48 * Fields associated with edit mode.
55 * Whether or not the current edit should be considered cancelled, rather
56 * than committed, when editing ends.
63 * The editable item corresponding to the last click, if any. Used to decide
64 * initial focus when entering edit mode.
68 editClickTarget_: null,
71 decorate: function() {
72 DeletableItem.prototype.decorate.call(this);
74 this.editFields_ = [];
75 this.addEventListener('mousedown', this.handleMouseDown_);
76 this.addEventListener('keydown', this.handleKeyDown_);
77 this.addEventListener('leadChange', this.handleLeadChange_);
81 selectionChanged: function() {
82 this.updateEditState();
86 * Called when this element gains or loses 'lead' status. Updates editing
90 handleLeadChange_: function() {
91 this.updateEditState();
95 * Updates the edit state based on the current selected and lead states.
97 updateEditState: function() {
99 this.editing = this.selected && this.lead;
103 * Whether the user is currently editing the list item.
107 return this.hasAttribute('editing');
109 set editing(editing) {
110 if (this.editing == editing)
114 this.setAttribute('editing', '');
116 this.removeAttribute('editing');
119 this.editCancelled_ = false;
121 cr.dispatchSimpleEvent(this, 'edit', true);
123 var focusElement = this.editClickTarget_ || this.initialFocusElement;
124 this.editClickTarget_ = null;
126 // When this is called in response to the selectedChange event,
127 // the list grabs focus immediately afterwards. Thus we must delay
131 window.setTimeout(function() {
132 // Make sure we are still in edit mode by the time we execute.
134 focusElement.focus();
135 focusElement.select();
140 if (!this.editCancelled_ && this.hasBeenEdited &&
141 this.currentInputIsValid) {
142 if (this.isPlaceholder)
143 this.parentNode.focusPlaceholder = true;
145 this.updateStaticValues_();
146 cr.dispatchSimpleEvent(this, 'commitedit', true);
148 this.resetEditableValues_();
149 cr.dispatchSimpleEvent(this, 'canceledit', true);
155 * Whether the item is editable.
159 return this.editable_;
161 set editable(editable) {
162 this.editable_ = editable;
164 this.editing = false;
168 * Whether the item is a new item placeholder.
171 get isPlaceholder() {
172 return this.isPlaceholder_;
174 set isPlaceholder(isPlaceholder) {
175 this.isPlaceholder_ = isPlaceholder;
177 this.deletable = false;
181 * The HTML element that should have focus initially when editing starts,
182 * if a specific element wasn't clicked.
183 * Defaults to the first <input> element; can be overriden by subclasses if
184 * a different element should be focused.
185 * @type {HTMLElement}
187 get initialFocusElement() {
188 return this.contentElement.querySelector('input');
192 * Whether the input in currently valid to submit. If this returns false
193 * when editing would be submitted, either editing will not be ended,
194 * or it will be cancelled, depending on the context.
195 * Can be overrided by subclasses to perform input validation.
198 get currentInputIsValid() {
203 * Returns true if the item has been changed by an edit.
204 * Can be overrided by subclasses to return false when nothing has changed
205 * to avoid unnecessary commits.
208 get hasBeenEdited() {
213 * Returns a div containing an <input>, as well as static text if
214 * isPlaceholder is not true.
215 * @param {string} text The text of the cell.
216 * @return {HTMLElement} The HTML element for the cell.
219 createEditableTextCell: function(text) {
220 var container = this.ownerDocument.createElement('div');
222 if (!this.isPlaceholder) {
223 var textEl = this.ownerDocument.createElement('div');
224 textEl.className = 'static-text';
225 textEl.textContent = text;
226 textEl.setAttribute('displaymode', 'static');
227 container.appendChild(textEl);
230 var inputEl = this.ownerDocument.createElement('input');
231 inputEl.type = 'text';
232 inputEl.value = text;
233 if (!this.isPlaceholder) {
234 inputEl.setAttribute('displaymode', 'edit');
235 inputEl.staticVersion = textEl;
237 // At this point |this| is not attached to the parent list yet, so give
238 // a short timeout in order for the attachment to occur.
240 window.setTimeout(function() {
241 var list = self.parentNode;
242 if (list && list.focusPlaceholder) {
243 list.focusPlaceholder = false;
244 if (list.shouldFocusPlaceholder())
250 inputEl.addEventListener('focus', this.handleFocus_.bind(this));
251 container.appendChild(inputEl);
252 this.editFields_.push(inputEl);
258 * Resets the editable version of any controls created by createEditable*
259 * to match the static text.
262 resetEditableValues_: function() {
263 var editFields = this.editFields_;
264 for (var i = 0; i < editFields.length; i++) {
265 var staticLabel = editFields[i].staticVersion;
266 if (!staticLabel && !this.isPlaceholder)
269 if (editFields[i].tagName == 'INPUT') {
270 editFields[i].value =
271 this.isPlaceholder ? '' : staticLabel.textContent;
273 // Add more tag types here as new createEditable* methods are added.
275 editFields[i].setCustomValidity('');
280 * Sets the static version of any controls created by createEditable*
281 * to match the current value of the editable version. Called on commit so
282 * that there's no flicker of the old value before the model updates.
285 updateStaticValues_: function() {
286 var editFields = this.editFields_;
287 for (var i = 0; i < editFields.length; i++) {
288 var staticLabel = editFields[i].staticVersion;
292 if (editFields[i].tagName == 'INPUT')
293 staticLabel.textContent = editFields[i].value;
294 // Add more tag types here as new createEditable* methods are added.
299 * Called a key is pressed. Handles committing and cancelling edits.
300 * @param {Event} e The key down event.
303 handleKeyDown_: function(e) {
308 switch (e.keyIdentifier) {
309 case 'U+001B': // Esc
310 this.editCancelled_ = true;
314 if (this.currentInputIsValid)
320 // Blurring will trigger the edit to end; see InlineEditableItemList.
321 this.ownerDocument.activeElement.blur();
322 // Make sure that handled keys aren't passed on and double-handled.
323 // (e.g., esc shouldn't both cancel an edit and close a subpage)
329 * Called when the list item is clicked. If the click target corresponds to
330 * an editable item, stores that item to focus when edit mode is started.
331 * @param {Event} e The mouse down event.
334 handleMouseDown_: function(e) {
335 if (!this.editable || this.editing)
338 var clickTarget = e.target;
339 var editFields = this.editFields_;
340 for (var i = 0; i < editFields.length; i++) {
341 if (editFields[i] == clickTarget ||
342 editFields[i].staticVersion == clickTarget) {
343 this.editClickTarget_ = editFields[i];
351 * Takes care of committing changes to inline editable list items when the
352 * window loses focus.
354 function handleWindowBlurs() {
355 window.addEventListener('blur', function(e) {
356 var itemAncestor = findAncestor(document.activeElement, function(node) {
357 return node instanceof InlineEditableItem;
360 document.activeElement.blur();
365 var InlineEditableItemList = cr.ui.define('list');
367 InlineEditableItemList.prototype = {
368 __proto__: DeletableItemList.prototype,
371 * Focuses the input element of the placeholder if true.
374 focusPlaceholder: false,
377 decorate: function() {
378 DeletableItemList.prototype.decorate.call(this);
379 this.setAttribute('inlineeditable', '');
380 this.addEventListener('hasElementFocusChange',
381 this.handleListFocusChange_);
385 * Called when the list hierarchy as a whole loses or gains focus; starts
386 * or ends editing for the lead item if necessary.
387 * @param {Event} e The change event.
390 handleListFocusChange_: function(e) {
391 var leadItem = this.getListItemByIndex(this.selectionModel.leadIndex);
394 leadItem.updateEditState();
396 leadItem.editing = false;
401 * May be overridden by subclasses to disable focusing the placeholder.
402 * @return true if the placeholder element should be focused on edit commit.
404 shouldFocusPlaceholder: function() {
411 InlineEditableItem: InlineEditableItem,
412 InlineEditableItemList: InlineEditableItemList,