| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 /** | 4 /** |
| 5 * @unrestricted | 5 * @unrestricted |
| 6 */ | 6 */ |
| 7 UI.ListWidget = class extends UI.VBox { | 7 UI.ListWidget = class extends UI.VBox { |
| 8 /** | 8 /** |
| 9 * @param {!UI.ListWidget.Delegate} delegate | 9 * @param {!UI.ListWidget.Delegate} delegate |
| 10 */ | 10 */ |
| 11 constructor(delegate) { | 11 constructor(delegate) { |
| 12 super(true); | 12 super(true); |
| 13 this.registerRequiredCSS('ui/listWidget.css'); | 13 this.registerRequiredCSS('ui/listWidget.css'); |
| 14 this._delegate = delegate; | 14 this._delegate = delegate; |
| 15 | 15 |
| 16 this._list = this.contentElement.createChild('div', 'list'); | 16 this._list = this.contentElement.createChild('div', 'list'); |
| 17 this.element.tabIndex = -1; |
| 17 | 18 |
| 18 /** @type {?UI.ListWidget.Editor} */ | 19 /** @type {?UI.ListWidget.Editor} */ |
| 19 this._editor = null; | 20 this._editor = null; |
| 20 /** @type {*|null} */ | 21 /** @type {*|null} */ |
| 21 this._editItem = null; | 22 this._editItem = null; |
| 22 /** @type {?Element} */ | 23 /** @type {?Element} */ |
| 23 this._editElement = null; | 24 this._editElement = null; |
| 24 | 25 |
| 25 /** @type {?Element} */ | 26 /** @type {?Element} */ |
| 26 this._emptyPlaceholder = null; | 27 this._emptyPlaceholder = null; |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 var index = this._elements.indexOf(element); | 138 var index = this._elements.indexOf(element); |
| 138 var insertionPoint = this._elements[index + 1] || null; | 139 var insertionPoint = this._elements[index + 1] || null; |
| 139 this._startEditing(item, element, insertionPoint); | 140 this._startEditing(item, element, insertionPoint); |
| 140 } | 141 } |
| 141 | 142 |
| 142 /** | 143 /** |
| 143 * @this {UI.ListWidget} | 144 * @this {UI.ListWidget} |
| 144 */ | 145 */ |
| 145 function onRemoveClicked() { | 146 function onRemoveClicked() { |
| 146 var index = this._elements.indexOf(element); | 147 var index = this._elements.indexOf(element); |
| 148 this.element.focus(); |
| 147 this._delegate.removeItemRequested(this._items[index], index); | 149 this._delegate.removeItemRequested(this._items[index], index); |
| 148 } | 150 } |
| 149 } | 151 } |
| 150 | 152 |
| 151 /** | 153 /** |
| 152 * @override | 154 * @override |
| 153 */ | 155 */ |
| 154 wasShown() { | 156 wasShown() { |
| 155 super.wasShown(); | 157 super.wasShown(); |
| 156 this._stopEditing(); | 158 this._stopEditing(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 169 /** | 171 /** |
| 170 * @param {*} item | 172 * @param {*} item |
| 171 * @param {?Element} element | 173 * @param {?Element} element |
| 172 * @param {?Element} insertionPoint | 174 * @param {?Element} insertionPoint |
| 173 */ | 175 */ |
| 174 _startEditing(item, element, insertionPoint) { | 176 _startEditing(item, element, insertionPoint) { |
| 175 if (element && this._editElement === element) | 177 if (element && this._editElement === element) |
| 176 return; | 178 return; |
| 177 | 179 |
| 178 this._stopEditing(); | 180 this._stopEditing(); |
| 181 this._focusRestorer = new UI.ElementFocusRestorer(this.element); |
| 179 | 182 |
| 180 this._list.classList.add('list-editing'); | 183 this._list.classList.add('list-editing'); |
| 181 this._editItem = item; | 184 this._editItem = item; |
| 182 this._editElement = element; | 185 this._editElement = element; |
| 183 if (element) | 186 if (element) |
| 184 element.classList.add('hidden'); | 187 element.classList.add('hidden'); |
| 185 | 188 |
| 186 var index = element ? this._elements.indexOf(element) : -1; | 189 var index = element ? this._elements.indexOf(element) : -1; |
| 187 this._editor = this._delegate.beginEdit(item); | 190 this._editor = this._delegate.beginEdit(item); |
| 188 this._updatePlaceholder(); | 191 this._updatePlaceholder(); |
| 189 this._list.insertBefore(this._editor.element, insertionPoint); | 192 this._list.insertBefore(this._editor.element, insertionPoint); |
| 190 this._editor.beginEdit( | 193 this._editor.beginEdit( |
| 191 item, index, element ? Common.UIString('Save') : Common.UIString('Add'),
this._commitEditing.bind(this), | 194 item, index, element ? Common.UIString('Save') : Common.UIString('Add'),
this._commitEditing.bind(this), |
| 192 this._stopEditing.bind(this)); | 195 this._stopEditing.bind(this)); |
| 193 } | 196 } |
| 194 | 197 |
| 195 _commitEditing() { | 198 _commitEditing() { |
| 196 var editItem = this._editItem; | 199 var editItem = this._editItem; |
| 197 var isNew = !this._editElement; | 200 var isNew = !this._editElement; |
| 198 var editor = /** @type {!UI.ListWidget.Editor} */ (this._editor); | 201 var editor = /** @type {!UI.ListWidget.Editor} */ (this._editor); |
| 199 this._stopEditing(); | 202 this._stopEditing(); |
| 200 this._delegate.commitEdit(editItem, editor, isNew); | 203 this._delegate.commitEdit(editItem, editor, isNew); |
| 201 } | 204 } |
| 202 | 205 |
| 203 _stopEditing() { | 206 _stopEditing() { |
| 204 this._list.classList.remove('list-editing'); | 207 this._list.classList.remove('list-editing'); |
| 208 if (this._focusRestorer) |
| 209 this._focusRestorer.restore(); |
| 205 if (this._editElement) | 210 if (this._editElement) |
| 206 this._editElement.classList.remove('hidden'); | 211 this._editElement.classList.remove('hidden'); |
| 207 if (this._editor && this._editor.element.parentElement) | 212 if (this._editor && this._editor.element.parentElement) |
| 208 this._editor.element.remove(); | 213 this._editor.element.remove(); |
| 209 | 214 |
| 210 this._editor = null; | 215 this._editor = null; |
| 211 this._editItem = null; | 216 this._editItem = null; |
| 212 this._editElement = null; | 217 this._editElement = null; |
| 213 this._updatePlaceholder(); | 218 this._updatePlaceholder(); |
| 214 } | 219 } |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 | 403 |
| 399 _cancelClicked() { | 404 _cancelClicked() { |
| 400 var cancel = this._cancel; | 405 var cancel = this._cancel; |
| 401 this._commit = null; | 406 this._commit = null; |
| 402 this._cancel = null; | 407 this._cancel = null; |
| 403 this._item = null; | 408 this._item = null; |
| 404 this._index = -1; | 409 this._index = -1; |
| 405 cancel(); | 410 cancel(); |
| 406 } | 411 } |
| 407 }; | 412 }; |
| OLD | NEW |