Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 4 | |
Paul Lewis | 17e384e | 2020-01-08 15:46:51 | [diff] [blame^] | 5 | import * as Common from '../common/common.js'; |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 6 | import {Toolbar, ToolbarButton} from './Toolbar.js'; |
| 7 | import {createInput, createTextButton, ElementFocusRestorer} from './UIUtils.js'; |
| 8 | import {VBox} from './Widget.js'; |
| 9 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 10 | /** |
| 11 | * @template T |
| 12 | */ |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 13 | export class ListWidget extends VBox { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 14 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 15 | * @param {!Delegate<T>} delegate |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 16 | */ |
| 17 | constructor(delegate) { |
Joel Einbinder | 7fbe24c | 2019-01-24 05:19:01 | [diff] [blame] | 18 | super(true, true /* delegatesFocus */); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 19 | this.registerRequiredCSS('ui/listWidget.css'); |
| 20 | this._delegate = delegate; |
| 21 | |
| 22 | this._list = this.contentElement.createChild('div', 'list'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 23 | |
| 24 | this._lastSeparator = false; |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 25 | /** @type {?ElementFocusRestorer} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 26 | this._focusRestorer = null; |
| 27 | /** @type {!Array<T>} */ |
| 28 | this._items = []; |
| 29 | /** @type {!Array<boolean>} */ |
| 30 | this._editable = []; |
| 31 | /** @type {!Array<!Element>} */ |
| 32 | this._elements = []; |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 33 | /** @type {?Editor<T>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 34 | this._editor = null; |
| 35 | /** @type {?T} */ |
| 36 | this._editItem = null; |
| 37 | /** @type {?Element} */ |
| 38 | this._editElement = null; |
| 39 | |
| 40 | /** @type {?Element} */ |
| 41 | this._emptyPlaceholder = null; |
| 42 | |
| 43 | this._updatePlaceholder(); |
| 44 | } |
| 45 | |
| 46 | clear() { |
| 47 | this._items = []; |
| 48 | this._editable = []; |
| 49 | this._elements = []; |
| 50 | this._lastSeparator = false; |
| 51 | this._list.removeChildren(); |
| 52 | this._updatePlaceholder(); |
| 53 | this._stopEditing(); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @param {!T} item |
| 58 | * @param {boolean} editable |
| 59 | */ |
| 60 | appendItem(item, editable) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 61 | if (this._lastSeparator && this._items.length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 62 | this._list.appendChild(createElementWithClass('div', 'list-separator')); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 63 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 64 | this._lastSeparator = false; |
| 65 | |
| 66 | this._items.push(item); |
| 67 | this._editable.push(editable); |
| 68 | |
| 69 | const element = this._list.createChild('div', 'list-item'); |
| 70 | element.appendChild(this._delegate.renderItem(item, editable)); |
| 71 | if (editable) { |
| 72 | element.classList.add('editable'); |
| 73 | element.appendChild(this._createControls(item, element)); |
| 74 | } |
| 75 | this._elements.push(element); |
| 76 | this._updatePlaceholder(); |
| 77 | } |
| 78 | |
| 79 | appendSeparator() { |
| 80 | this._lastSeparator = true; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @param {number} index |
| 85 | */ |
| 86 | removeItem(index) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 87 | if (this._editItem === this._items[index]) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 88 | this._stopEditing(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 89 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 90 | |
| 91 | const element = this._elements[index]; |
| 92 | |
| 93 | const previous = element.previousElementSibling; |
| 94 | const previousIsSeparator = previous && previous.classList.contains('list-separator'); |
| 95 | |
| 96 | const next = element.nextElementSibling; |
| 97 | const nextIsSeparator = next && next.classList.contains('list-separator'); |
| 98 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 99 | if (previousIsSeparator && (nextIsSeparator || !next)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 100 | previous.remove(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 101 | } |
| 102 | if (nextIsSeparator && !previous) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 103 | next.remove(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 104 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 105 | element.remove(); |
| 106 | |
| 107 | this._elements.splice(index, 1); |
| 108 | this._items.splice(index, 1); |
| 109 | this._editable.splice(index, 1); |
| 110 | this._updatePlaceholder(); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @param {number} index |
| 115 | * @param {!T} item |
| 116 | */ |
| 117 | addNewItem(index, item) { |
| 118 | this._startEditing(item, null, this._elements[index] || null); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @param {?Element} element |
| 123 | */ |
| 124 | setEmptyPlaceholder(element) { |
| 125 | this._emptyPlaceholder = element; |
| 126 | this._updatePlaceholder(); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * @param {!T} item |
| 131 | * @param {!Element} element |
| 132 | * @return {!Element} |
| 133 | */ |
| 134 | _createControls(item, element) { |
| 135 | const controls = createElementWithClass('div', 'controls-container fill'); |
| 136 | controls.createChild('div', 'controls-gradient'); |
| 137 | |
| 138 | const buttons = controls.createChild('div', 'controls-buttons'); |
| 139 | |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 140 | const toolbar = new Toolbar('', buttons); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 141 | |
Paul Lewis | 17e384e | 2020-01-08 15:46:51 | [diff] [blame^] | 142 | const editButton = new ToolbarButton(Common.UIString.UIString('Edit'), 'largeicon-edit'); |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 143 | editButton.addEventListener(ToolbarButton.Events.Click, onEditClicked.bind(this)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 144 | toolbar.appendToolbarItem(editButton); |
| 145 | |
Paul Lewis | 17e384e | 2020-01-08 15:46:51 | [diff] [blame^] | 146 | const removeButton = new ToolbarButton(Common.UIString.UIString('Remove'), 'largeicon-trash-bin'); |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 147 | removeButton.addEventListener(ToolbarButton.Events.Click, onRemoveClicked.bind(this)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 148 | toolbar.appendToolbarItem(removeButton); |
| 149 | |
| 150 | return controls; |
| 151 | |
| 152 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 153 | * @this {ListWidget} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 154 | */ |
| 155 | function onEditClicked() { |
| 156 | const index = this._elements.indexOf(element); |
| 157 | const insertionPoint = this._elements[index + 1] || null; |
| 158 | this._startEditing(item, element, insertionPoint); |
| 159 | } |
| 160 | |
| 161 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 162 | * @this {ListWidget} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 163 | */ |
| 164 | function onRemoveClicked() { |
| 165 | const index = this._elements.indexOf(element); |
| 166 | this.element.focus(); |
| 167 | this._delegate.removeItemRequested(this._items[index], index); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * @override |
| 173 | */ |
| 174 | wasShown() { |
| 175 | super.wasShown(); |
| 176 | this._stopEditing(); |
| 177 | } |
| 178 | |
| 179 | _updatePlaceholder() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 180 | if (!this._emptyPlaceholder) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 181 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 182 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 183 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 184 | if (!this._elements.length && !this._editor) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 185 | this._list.appendChild(this._emptyPlaceholder); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 186 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 187 | this._emptyPlaceholder.remove(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 188 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /** |
| 192 | * @param {!T} item |
| 193 | * @param {?Element} element |
| 194 | * @param {?Element} insertionPoint |
| 195 | */ |
| 196 | _startEditing(item, element, insertionPoint) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 197 | if (element && this._editElement === element) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 198 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 199 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 200 | |
| 201 | this._stopEditing(); |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 202 | this._focusRestorer = new ElementFocusRestorer(this.element); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 203 | |
| 204 | this._list.classList.add('list-editing'); |
| 205 | this._editItem = item; |
| 206 | this._editElement = element; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 207 | if (element) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 208 | element.classList.add('hidden'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 209 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 210 | |
| 211 | const index = element ? this._elements.indexOf(element) : -1; |
| 212 | this._editor = this._delegate.beginEdit(item); |
| 213 | this._updatePlaceholder(); |
| 214 | this._list.insertBefore(this._editor.element, insertionPoint); |
| 215 | this._editor.beginEdit( |
Paul Lewis | 17e384e | 2020-01-08 15:46:51 | [diff] [blame^] | 216 | item, index, element ? Common.UIString.UIString('Save') : Common.UIString.UIString('Add'), |
| 217 | this._commitEditing.bind(this), this._stopEditing.bind(this)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | _commitEditing() { |
| 221 | const editItem = this._editItem; |
| 222 | const isNew = !this._editElement; |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 223 | const editor = /** @type {!Editor<T>} */ (this._editor); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 224 | this._stopEditing(); |
| 225 | this._delegate.commitEdit(editItem, editor, isNew); |
| 226 | } |
| 227 | |
| 228 | _stopEditing() { |
| 229 | this._list.classList.remove('list-editing'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 230 | if (this._focusRestorer) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 231 | this._focusRestorer.restore(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 232 | } |
| 233 | if (this._editElement) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 234 | this._editElement.classList.remove('hidden'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 235 | } |
| 236 | if (this._editor && this._editor.element.parentElement) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 237 | this._editor.element.remove(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 238 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 239 | |
| 240 | this._editor = null; |
| 241 | this._editItem = null; |
| 242 | this._editElement = null; |
| 243 | this._updatePlaceholder(); |
| 244 | } |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 245 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 246 | |
| 247 | /** |
| 248 | * @template T |
| 249 | * @interface |
| 250 | */ |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 251 | export class Delegate { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 252 | /** |
| 253 | * @param {!T} item |
| 254 | * @param {boolean} editable |
| 255 | * @return {!Element} |
| 256 | */ |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 257 | renderItem(item, editable) { |
| 258 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 259 | |
| 260 | /** |
| 261 | * @param {!T} item |
| 262 | * @param {number} index |
| 263 | */ |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 264 | removeItemRequested(item, index) { |
| 265 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 266 | |
| 267 | /** |
| 268 | * @param {!T} item |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 269 | * @return {!Editor<T>} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 270 | */ |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 271 | beginEdit(item) { |
| 272 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 273 | |
| 274 | /** |
| 275 | * @param {!T} item |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 276 | * @param {!Editor<T>} editor |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 277 | * @param {boolean} isNew |
| 278 | */ |
| 279 | commitEdit(item, editor, isNew) {} |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 280 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 281 | |
| 282 | /** |
| 283 | * @template T |
| 284 | */ |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 285 | export class Editor { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 286 | constructor() { |
| 287 | this.element = createElementWithClass('div', 'editor-container'); |
| 288 | this.element.addEventListener('keydown', onKeyDown.bind(null, isEscKey, this._cancelClicked.bind(this)), false); |
| 289 | this.element.addEventListener('keydown', onKeyDown.bind(null, isEnterKey, this._commitClicked.bind(this)), false); |
| 290 | |
| 291 | this._contentElement = this.element.createChild('div', 'editor-content'); |
| 292 | |
| 293 | const buttonsRow = this.element.createChild('div', 'editor-buttons'); |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 294 | this._commitButton = createTextButton('', this._commitClicked.bind(this), '', true /* primary */); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 295 | buttonsRow.appendChild(this._commitButton); |
Paul Lewis | 17e384e | 2020-01-08 15:46:51 | [diff] [blame^] | 296 | this._cancelButton = createTextButton(Common.UIString.UIString('Cancel'), this._cancelClicked.bind(this)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 297 | this._cancelButton.addEventListener( |
| 298 | 'keydown', onKeyDown.bind(null, isEnterKey, this._cancelClicked.bind(this)), false); |
| 299 | buttonsRow.appendChild(this._cancelButton); |
| 300 | |
Rob Paveza | c671db3 | 2019-09-20 01:54:50 | [diff] [blame] | 301 | this._errorMessageContainer = this.element.createChild('div', 'list-widget-input-validation-error'); |
| 302 | UI.ARIAUtils.markAsAlert(this._errorMessageContainer); |
| 303 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 304 | /** |
| 305 | * @param {function(!Event):boolean} predicate |
| 306 | * @param {function()} callback |
| 307 | * @param {!Event} event |
| 308 | */ |
| 309 | function onKeyDown(predicate, callback, event) { |
| 310 | if (predicate(event)) { |
| 311 | event.consume(true); |
| 312 | callback(); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | /** @type {!Array<!HTMLInputElement|!HTMLSelectElement>} */ |
| 317 | this._controls = []; |
| 318 | /** @type {!Map<string, !HTMLInputElement|!HTMLSelectElement>} */ |
| 319 | this._controlByName = new Map(); |
Amanda Baker | ca50282 | 2019-07-02 00:01:28 | [diff] [blame] | 320 | /** @type {!Array<function(!T, number, (!HTMLInputElement|!HTMLSelectElement)): !UI.ListWidget.ValidatorResult>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 321 | this._validators = []; |
| 322 | |
| 323 | /** @type {?function()} */ |
| 324 | this._commit = null; |
| 325 | /** @type {?function()} */ |
| 326 | this._cancel = null; |
| 327 | /** @type {?T} */ |
| 328 | this._item = null; |
| 329 | /** @type {number} */ |
| 330 | this._index = -1; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * @return {!Element} |
| 335 | */ |
| 336 | contentElement() { |
| 337 | return this._contentElement; |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * @param {string} name |
| 342 | * @param {string} type |
| 343 | * @param {string} title |
Amanda Baker | ca50282 | 2019-07-02 00:01:28 | [diff] [blame] | 344 | * @param {function(!T, number, (!HTMLInputElement|!HTMLSelectElement)): !UI.ListWidget.ValidatorResult} validator |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 345 | * @return {!HTMLInputElement} |
| 346 | */ |
| 347 | createInput(name, type, title, validator) { |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 348 | const input = /** @type {!HTMLInputElement} */ (createInput('', type)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 349 | input.placeholder = title; |
| 350 | input.addEventListener('input', this._validateControls.bind(this, false), false); |
| 351 | input.addEventListener('blur', this._validateControls.bind(this, false), false); |
Amanda Baker | ca50282 | 2019-07-02 00:01:28 | [diff] [blame] | 352 | UI.ARIAUtils.setAccessibleName(input, title); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 353 | this._controlByName.set(name, input); |
| 354 | this._controls.push(input); |
| 355 | this._validators.push(validator); |
| 356 | return input; |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * @param {string} name |
| 361 | * @param {!Array<string>} options |
Amanda Baker | ca50282 | 2019-07-02 00:01:28 | [diff] [blame] | 362 | * @param {function(!T, number, (!HTMLInputElement|!HTMLSelectElement)): !UI.ListWidget.ValidatorResult} validator |
| 363 | * @param {string=} title |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 364 | * @return {!HTMLSelectElement} |
| 365 | */ |
Amanda Baker | ca50282 | 2019-07-02 00:01:28 | [diff] [blame] | 366 | createSelect(name, options, validator, title) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 367 | const select = /** @type {!HTMLSelectElement} */ (createElementWithClass('select', 'chrome-select')); |
| 368 | for (let index = 0; index < options.length; ++index) { |
| 369 | const option = select.createChild('option'); |
| 370 | option.value = options[index]; |
| 371 | option.textContent = options[index]; |
| 372 | } |
Amanda Baker | ca50282 | 2019-07-02 00:01:28 | [diff] [blame] | 373 | if (title) { |
| 374 | select.title = title; |
| 375 | UI.ARIAUtils.setAccessibleName(select, title); |
| 376 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 377 | select.addEventListener('input', this._validateControls.bind(this, false), false); |
| 378 | select.addEventListener('blur', this._validateControls.bind(this, false), false); |
| 379 | this._controlByName.set(name, select); |
| 380 | this._controls.push(select); |
| 381 | this._validators.push(validator); |
| 382 | return select; |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * @param {string} name |
| 387 | * @return {!HTMLInputElement|!HTMLSelectElement} |
| 388 | */ |
| 389 | control(name) { |
| 390 | return /** @type {!HTMLInputElement|!HTMLSelectElement} */ (this._controlByName.get(name)); |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * @param {boolean} forceValid |
| 395 | */ |
| 396 | _validateControls(forceValid) { |
| 397 | let allValid = true; |
Amanda Baker | ca50282 | 2019-07-02 00:01:28 | [diff] [blame] | 398 | this._errorMessageContainer.textContent = ''; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 399 | for (let index = 0; index < this._controls.length; ++index) { |
| 400 | const input = this._controls[index]; |
Amanda Baker | ca50282 | 2019-07-02 00:01:28 | [diff] [blame] | 401 | const {valid, errorMessage} = this._validators[index].call(null, this._item, this._index, input); |
| 402 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 403 | input.classList.toggle('error-input', !valid && !forceValid); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 404 | if (valid || forceValid) { |
Amanda Baker | ca50282 | 2019-07-02 00:01:28 | [diff] [blame] | 405 | UI.ARIAUtils.setInvalid(input, false); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 406 | } else { |
Amanda Baker | ca50282 | 2019-07-02 00:01:28 | [diff] [blame] | 407 | UI.ARIAUtils.setInvalid(input, true); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 408 | } |
Amanda Baker | ca50282 | 2019-07-02 00:01:28 | [diff] [blame] | 409 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 410 | if (!forceValid && errorMessage && !this._errorMessageContainer.textContent) { |
Amanda Baker | ca50282 | 2019-07-02 00:01:28 | [diff] [blame] | 411 | this._errorMessageContainer.textContent = errorMessage; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 412 | } |
Amanda Baker | ca50282 | 2019-07-02 00:01:28 | [diff] [blame] | 413 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 414 | allValid &= valid; |
| 415 | } |
| 416 | this._commitButton.disabled = !allValid; |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * @param {!T} item |
| 421 | * @param {number} index |
| 422 | * @param {string} commitButtonTitle |
| 423 | * @param {function()} commit |
| 424 | * @param {function()} cancel |
| 425 | */ |
| 426 | beginEdit(item, index, commitButtonTitle, commit, cancel) { |
| 427 | this._commit = commit; |
| 428 | this._cancel = cancel; |
| 429 | this._item = item; |
| 430 | this._index = index; |
| 431 | |
| 432 | this._commitButton.textContent = commitButtonTitle; |
| 433 | this.element.scrollIntoViewIfNeeded(false); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 434 | if (this._controls.length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 435 | this._controls[0].focus(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 436 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 437 | this._validateControls(true); |
| 438 | } |
| 439 | |
| 440 | _commitClicked() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 441 | if (this._commitButton.disabled) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 442 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 443 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 444 | |
| 445 | const commit = this._commit; |
| 446 | this._commit = null; |
| 447 | this._cancel = null; |
| 448 | this._item = null; |
| 449 | this._index = -1; |
| 450 | commit(); |
| 451 | } |
| 452 | |
| 453 | _cancelClicked() { |
| 454 | const cancel = this._cancel; |
| 455 | this._commit = null; |
| 456 | this._cancel = null; |
| 457 | this._item = null; |
| 458 | this._index = -1; |
| 459 | cancel(); |
| 460 | } |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 461 | } |