Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | // Copyright 2016 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. |
| 4 | |
Tim van der Lippe | aa76aa2 | 2020-02-14 14:38:24 | [diff] [blame^] | 5 | import * as ARIAUtils from './ARIAUtils.js'; |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 6 | import {Events as ListModelEvents, ListModel} from './ListModel.js'; // eslint-disable-line no-unused-vars |
| 7 | import {measurePreferredSize} from './UIUtils.js'; |
| 8 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 9 | /** |
| 10 | * @template T |
| 11 | * @interface |
| 12 | */ |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 13 | export class ListDelegate { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 14 | /** |
| 15 | * @param {T} item |
| 16 | * @return {!Element} |
| 17 | */ |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 18 | createElementForItem(item) { |
| 19 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 20 | |
| 21 | /** |
| 22 | * This method is not called in NonViewport mode. |
| 23 | * Return zero to make list measure the item (only works in SameHeight mode). |
| 24 | * @param {T} item |
| 25 | * @return {number} |
| 26 | */ |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 27 | heightForItem(item) { |
| 28 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * @param {T} item |
| 32 | * @return {boolean} |
| 33 | */ |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 34 | isItemSelectable(item) { |
| 35 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * @param {?T} from |
| 39 | * @param {?T} to |
| 40 | * @param {?Element} fromElement |
| 41 | * @param {?Element} toElement |
| 42 | */ |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 43 | selectedItemChanged(from, to, fromElement, toElement) { |
| 44 | } |
Jack Lynch | 805641c | 2019-12-07 00:05:39 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * @param {?Element} fromElement |
| 48 | * @param {?Element} toElement |
| 49 | * @return {boolean} |
| 50 | */ |
| 51 | updateSelectedItemARIA(fromElement, toElement) { |
| 52 | } |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 53 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 54 | |
| 55 | /** @enum {symbol} */ |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 56 | export const ListMode = { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 57 | NonViewport: Symbol('UI.ListMode.NonViewport'), |
| 58 | EqualHeightItems: Symbol('UI.ListMode.EqualHeightItems'), |
| 59 | VariousHeightItems: Symbol('UI.ListMode.VariousHeightItems') |
| 60 | }; |
| 61 | |
| 62 | /** |
| 63 | * @template T |
| 64 | */ |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 65 | export class ListControl { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 66 | /** |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 67 | * @param {!ListModel<T>} model |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 68 | * @param {!ListDelegate<T>} delegate |
| 69 | * @param {!ListMode=} mode |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 70 | */ |
| 71 | constructor(model, delegate, mode) { |
| 72 | this.element = createElement('div'); |
| 73 | this.element.style.overflowY = 'auto'; |
| 74 | this._topElement = this.element.createChild('div'); |
| 75 | this._bottomElement = this.element.createChild('div'); |
| 76 | this._firstIndex = 0; |
| 77 | this._lastIndex = 0; |
| 78 | this._renderedHeight = 0; |
| 79 | this._topHeight = 0; |
| 80 | this._bottomHeight = 0; |
| 81 | |
| 82 | this._model = model; |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 83 | this._model.addEventListener(ListModelEvents.ItemsReplaced, this._replacedItemsInRange, this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 84 | /** @type {!Map<T, !Element>} */ |
| 85 | this._itemToElement = new Map(); |
| 86 | this._selectedIndex = -1; |
| 87 | /** @type {?T} */ |
| 88 | this._selectedItem = null; |
| 89 | |
| 90 | this.element.tabIndex = -1; |
| 91 | this.element.addEventListener('click', this._onClick.bind(this), false); |
| 92 | this.element.addEventListener('keydown', this._onKeyDown.bind(this), false); |
Tim van der Lippe | aa76aa2 | 2020-02-14 14:38:24 | [diff] [blame^] | 93 | ARIAUtils.markAsListBox(this.element); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 94 | |
| 95 | this._delegate = delegate; |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 96 | this._mode = mode || ListMode.EqualHeightItems; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 97 | this._fixedHeight = 0; |
| 98 | this._variableOffsets = new Int32Array(0); |
| 99 | this._clearContents(); |
| 100 | |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 101 | if (this._mode !== ListMode.NonViewport) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 102 | this.element.addEventListener('scroll', () => { |
| 103 | this._updateViewport(this.element.scrollTop, this.element.offsetHeight); |
| 104 | }, false); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 109 | * @param {!ListModel<T>} model |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 110 | */ |
| 111 | setModel(model) { |
| 112 | this._itemToElement.clear(); |
| 113 | const length = this._model.length; |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 114 | this._model.removeEventListener(ListModelEvents.ItemsReplaced, this._replacedItemsInRange, this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 115 | this._model = model; |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 116 | this._model.addEventListener(ListModelEvents.ItemsReplaced, this._replacedItemsInRange, this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 117 | this.invalidateRange(0, length); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @param {!Common.Event} event |
| 122 | */ |
| 123 | _replacedItemsInRange(event) { |
| 124 | const data = /** @type {{index: number, removed: !Array<T>, inserted: number}} */ (event.data); |
| 125 | const from = data.index; |
| 126 | const to = from + data.removed.length; |
| 127 | |
| 128 | const oldSelectedItem = this._selectedItem; |
| 129 | const oldSelectedElement = oldSelectedItem ? (this._itemToElement.get(oldSelectedItem) || null) : null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 130 | for (let i = 0; i < data.removed.length; i++) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 131 | this._itemToElement.delete(data.removed[i]); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 132 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 133 | this._invalidate(from, to, data.inserted); |
| 134 | |
| 135 | if (this._selectedIndex >= to) { |
| 136 | this._selectedIndex += data.inserted - (to - from); |
| 137 | this._selectedItem = this._model.at(this._selectedIndex); |
| 138 | } else if (this._selectedIndex >= from) { |
| 139 | let index = this._findFirstSelectable(from + data.inserted, +1, false); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 140 | if (index === -1) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 141 | index = this._findFirstSelectable(from - 1, -1, false); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 142 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 143 | this._select(index, oldSelectedItem, oldSelectedElement); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * @param {T} item |
| 149 | */ |
| 150 | refreshItem(item) { |
| 151 | const index = this._model.indexOf(item); |
| 152 | if (index === -1) { |
| 153 | console.error('Item to refresh is not present'); |
| 154 | return; |
| 155 | } |
Alexey Kozyatinskiy | d0ece35 | 2018-08-14 02:04:17 | [diff] [blame] | 156 | this.refreshItemByIndex(index); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @param {number} index |
| 161 | */ |
| 162 | refreshItemByIndex(index) { |
| 163 | const item = this._model.at(index); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 164 | this._itemToElement.delete(item); |
| 165 | this.invalidateRange(index, index + 1); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 166 | if (this._selectedIndex !== -1) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 167 | this._select(this._selectedIndex, null, null); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 168 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /** |
| 172 | * @param {number} from |
| 173 | * @param {number} to |
| 174 | */ |
| 175 | invalidateRange(from, to) { |
| 176 | this._invalidate(from, to, to - from); |
| 177 | } |
| 178 | |
| 179 | viewportResized() { |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 180 | if (this._mode === ListMode.NonViewport) { |
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 | // TODO(dgozman): try to keep visible scrollTop the same. |
| 184 | const scrollTop = this.element.scrollTop; |
| 185 | const viewportHeight = this.element.offsetHeight; |
| 186 | this._clearViewport(); |
| 187 | this._updateViewport(Number.constrain(scrollTop, 0, this._totalHeight() - viewportHeight), viewportHeight); |
| 188 | } |
| 189 | |
| 190 | invalidateItemHeight() { |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 191 | if (this._mode !== ListMode.EqualHeightItems) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 192 | console.error('Only supported in equal height items mode'); |
| 193 | return; |
| 194 | } |
| 195 | this._fixedHeight = 0; |
| 196 | if (this._model.length) { |
| 197 | this._itemToElement.clear(); |
| 198 | this._invalidate(0, this._model.length, this._model.length); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * @param {?Node} node |
| 204 | * @return {?T} |
| 205 | */ |
| 206 | itemForNode(node) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 207 | while (node && node.parentNodeOrShadowHost() !== this.element) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 208 | node = node.parentNodeOrShadowHost(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 209 | } |
| 210 | if (!node) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 211 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 212 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 213 | const element = /** @type {!Element} */ (node); |
| 214 | const index = this._model.findIndex(item => this._itemToElement.get(item) === element); |
| 215 | return index !== -1 ? this._model.at(index) : null; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * @param {T} item |
| 220 | * @param {boolean=} center |
| 221 | */ |
| 222 | scrollItemIntoView(item, center) { |
| 223 | const index = this._model.indexOf(item); |
| 224 | if (index === -1) { |
| 225 | console.error('Attempt to scroll onto missing item'); |
| 226 | return; |
| 227 | } |
| 228 | this._scrollIntoView(index, center); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * @return {?T} |
| 233 | */ |
| 234 | selectedItem() { |
| 235 | return this._selectedItem; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * @return {number} |
| 240 | */ |
| 241 | selectedIndex() { |
| 242 | return this._selectedIndex; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * @param {?T} item |
| 247 | * @param {boolean=} center |
| 248 | * @param {boolean=} dontScroll |
| 249 | */ |
| 250 | selectItem(item, center, dontScroll) { |
| 251 | let index = -1; |
| 252 | if (item !== null) { |
| 253 | index = this._model.indexOf(item); |
| 254 | if (index === -1) { |
| 255 | console.error('Attempt to select missing item'); |
| 256 | return; |
| 257 | } |
| 258 | if (!this._delegate.isItemSelectable(item)) { |
| 259 | console.error('Attempt to select non-selectable item'); |
| 260 | return; |
| 261 | } |
| 262 | } |
Junyi Xiao | 27d1807 | 2019-07-27 01:10:10 | [diff] [blame] | 263 | // Scrolling the item before selection ensures it is in the DOM. |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 264 | if (index !== -1 && !dontScroll) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 265 | this._scrollIntoView(index, center); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 266 | } |
| 267 | if (this._selectedIndex !== index) { |
Junyi Xiao | 27d1807 | 2019-07-27 01:10:10 | [diff] [blame] | 268 | this._select(index); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 269 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /** |
| 273 | * @param {boolean=} canWrap |
| 274 | * @param {boolean=} center |
| 275 | * @return {boolean} |
| 276 | */ |
| 277 | selectPreviousItem(canWrap, center) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 278 | if (this._selectedIndex === -1 && !canWrap) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 279 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 280 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 281 | let index = this._selectedIndex === -1 ? this._model.length - 1 : this._selectedIndex - 1; |
| 282 | index = this._findFirstSelectable(index, -1, !!canWrap); |
| 283 | if (index !== -1) { |
| 284 | this._scrollIntoView(index, center); |
| 285 | this._select(index); |
| 286 | return true; |
| 287 | } |
| 288 | return false; |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * @param {boolean=} canWrap |
| 293 | * @param {boolean=} center |
| 294 | * @return {boolean} |
| 295 | */ |
| 296 | selectNextItem(canWrap, center) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 297 | if (this._selectedIndex === -1 && !canWrap) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 298 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 299 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 300 | let index = this._selectedIndex === -1 ? 0 : this._selectedIndex + 1; |
| 301 | index = this._findFirstSelectable(index, +1, !!canWrap); |
| 302 | if (index !== -1) { |
| 303 | this._scrollIntoView(index, center); |
| 304 | this._select(index); |
| 305 | return true; |
| 306 | } |
| 307 | return false; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * @param {boolean=} center |
| 312 | * @return {boolean} |
| 313 | */ |
| 314 | selectItemPreviousPage(center) { |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 315 | if (this._mode === ListMode.NonViewport) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 316 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 317 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 318 | let index = this._selectedIndex === -1 ? this._model.length - 1 : this._selectedIndex; |
| 319 | index = this._findPageSelectable(index, -1); |
| 320 | if (index !== -1) { |
| 321 | this._scrollIntoView(index, center); |
| 322 | this._select(index); |
| 323 | return true; |
| 324 | } |
| 325 | return false; |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * @param {boolean=} center |
| 330 | * @return {boolean} |
| 331 | */ |
| 332 | selectItemNextPage(center) { |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 333 | if (this._mode === ListMode.NonViewport) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 334 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 335 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 336 | let index = this._selectedIndex === -1 ? 0 : this._selectedIndex; |
| 337 | index = this._findPageSelectable(index, +1); |
| 338 | if (index !== -1) { |
| 339 | this._scrollIntoView(index, center); |
| 340 | this._select(index); |
| 341 | return true; |
| 342 | } |
| 343 | return false; |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * @param {number} index |
| 348 | * @param {boolean=} center |
| 349 | */ |
| 350 | _scrollIntoView(index, center) { |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 351 | if (this._mode === ListMode.NonViewport) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 352 | this._elementAtIndex(index).scrollIntoViewIfNeeded(!!center); |
| 353 | return; |
| 354 | } |
| 355 | |
| 356 | const top = this._offsetAtIndex(index); |
| 357 | const bottom = this._offsetAtIndex(index + 1); |
| 358 | const viewportHeight = this.element.offsetHeight; |
| 359 | if (center) { |
| 360 | const scrollTo = (top + bottom) / 2 - viewportHeight / 2; |
| 361 | this._updateViewport(Number.constrain(scrollTo, 0, this._totalHeight() - viewportHeight), viewportHeight); |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | const scrollTop = this.element.scrollTop; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 366 | if (top < scrollTop) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 367 | this._updateViewport(top, viewportHeight); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 368 | } else if (bottom > scrollTop + viewportHeight) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 369 | this._updateViewport(bottom - viewportHeight, viewportHeight); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 370 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | /** |
| 374 | * @param {!Event} event |
| 375 | */ |
| 376 | _onClick(event) { |
| 377 | const item = this.itemForNode(/** @type {?Node} */ (event.target)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 378 | if (item && this._delegate.isItemSelectable(item)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 379 | this.selectItem(item); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 380 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | /** |
| 384 | * @param {!Event} event |
| 385 | */ |
| 386 | _onKeyDown(event) { |
| 387 | let selected = false; |
| 388 | switch (event.key) { |
| 389 | case 'ArrowUp': |
| 390 | selected = this.selectPreviousItem(true, false); |
| 391 | break; |
| 392 | case 'ArrowDown': |
| 393 | selected = this.selectNextItem(true, false); |
| 394 | break; |
| 395 | case 'PageUp': |
| 396 | selected = this.selectItemPreviousPage(false); |
| 397 | break; |
| 398 | case 'PageDown': |
| 399 | selected = this.selectItemNextPage(false); |
| 400 | break; |
| 401 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 402 | if (selected) { |
Jack Lynch | 805641c | 2019-12-07 00:05:39 | [diff] [blame] | 403 | event.consume(true); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 404 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | /** |
| 408 | * @return {number} |
| 409 | */ |
| 410 | _totalHeight() { |
| 411 | return this._offsetAtIndex(this._model.length); |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * @param {number} offset |
| 416 | * @return {number} |
| 417 | */ |
| 418 | _indexAtOffset(offset) { |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 419 | if (this._mode === ListMode.NonViewport) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 420 | throw 'There should be no offset conversions in non-viewport mode'; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 421 | } |
| 422 | if (!this._model.length || offset < 0) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 423 | return 0; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 424 | } |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 425 | if (this._mode === ListMode.VariousHeightItems) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 426 | return Math.min( |
| 427 | this._model.length - 1, this._variableOffsets.lowerBound(offset, undefined, 0, this._model.length)); |
| 428 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 429 | if (!this._fixedHeight) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 430 | this._measureHeight(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 431 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 432 | return Math.min(this._model.length - 1, Math.floor(offset / this._fixedHeight)); |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * @param {number} index |
| 437 | * @return {!Element} |
| 438 | */ |
| 439 | _elementAtIndex(index) { |
| 440 | const item = this._model.at(index); |
| 441 | let element = this._itemToElement.get(item); |
| 442 | if (!element) { |
| 443 | element = this._delegate.createElementForItem(item); |
Tim van der Lippe | aa76aa2 | 2020-02-14 14:38:24 | [diff] [blame^] | 444 | if (!ARIAUtils.hasRole(element)) { |
| 445 | ARIAUtils.markAsOption(element); |
Jack Lynch | 805641c | 2019-12-07 00:05:39 | [diff] [blame] | 446 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 447 | this._itemToElement.set(item, element); |
| 448 | } |
| 449 | return element; |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * @param {number} index |
| 454 | * @return {number} |
| 455 | */ |
| 456 | _offsetAtIndex(index) { |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 457 | if (this._mode === ListMode.NonViewport) { |
Jack Lynch | 805641c | 2019-12-07 00:05:39 | [diff] [blame] | 458 | throw new Error('There should be no offset conversions in non-viewport mode'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 459 | } |
| 460 | if (!this._model.length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 461 | return 0; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 462 | } |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 463 | if (this._mode === ListMode.VariousHeightItems) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 464 | return this._variableOffsets[index]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 465 | } |
| 466 | if (!this._fixedHeight) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 467 | this._measureHeight(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 468 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 469 | return index * this._fixedHeight; |
| 470 | } |
| 471 | |
| 472 | _measureHeight() { |
| 473 | this._fixedHeight = this._delegate.heightForItem(this._model.at(0)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 474 | if (!this._fixedHeight) { |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 475 | this._fixedHeight = measurePreferredSize(this._elementAtIndex(0), this.element).height; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 476 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /** |
| 480 | * @param {number} index |
| 481 | * @param {?T=} oldItem |
| 482 | * @param {?Element=} oldElement |
| 483 | */ |
| 484 | _select(index, oldItem, oldElement) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 485 | if (oldItem === undefined) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 486 | oldItem = this._selectedItem; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 487 | } |
| 488 | if (oldElement === undefined) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 489 | oldElement = this._itemToElement.get(oldItem) || null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 490 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 491 | this._selectedIndex = index; |
| 492 | this._selectedItem = index === -1 ? null : this._model.at(index); |
| 493 | const newItem = this._selectedItem; |
| 494 | const newElement = this._selectedIndex !== -1 ? this._elementAtIndex(index) : null; |
Jack Lynch | 805641c | 2019-12-07 00:05:39 | [diff] [blame] | 495 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 496 | this._delegate.selectedItemChanged(oldItem, newItem, /** @type {?Element} */ (oldElement), newElement); |
Jack Lynch | 805641c | 2019-12-07 00:05:39 | [diff] [blame] | 497 | if (!this._delegate.updateSelectedItemARIA(/** @type {?Element} */ (oldElement), newElement)) { |
| 498 | if (oldElement) { |
Tim van der Lippe | aa76aa2 | 2020-02-14 14:38:24 | [diff] [blame^] | 499 | ARIAUtils.setSelected(oldElement, false); |
Jack Lynch | 805641c | 2019-12-07 00:05:39 | [diff] [blame] | 500 | } |
| 501 | if (newElement) { |
Tim van der Lippe | aa76aa2 | 2020-02-14 14:38:24 | [diff] [blame^] | 502 | ARIAUtils.setSelected(newElement, true); |
Jack Lynch | 805641c | 2019-12-07 00:05:39 | [diff] [blame] | 503 | } |
Tim van der Lippe | aa76aa2 | 2020-02-14 14:38:24 | [diff] [blame^] | 504 | ARIAUtils.setActiveDescendant(this.element, newElement); |
Jack Lynch | 805641c | 2019-12-07 00:05:39 | [diff] [blame] | 505 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | /** |
| 509 | * @param {number} index |
| 510 | * @param {number} direction |
| 511 | * @param {boolean} canWrap |
| 512 | * @return {number} |
| 513 | */ |
| 514 | _findFirstSelectable(index, direction, canWrap) { |
| 515 | const length = this._model.length; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 516 | if (!length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 517 | return -1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 518 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 519 | for (let step = 0; step <= length; step++) { |
| 520 | if (index < 0 || index >= length) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 521 | if (!canWrap) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 522 | return -1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 523 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 524 | index = (index + length) % length; |
| 525 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 526 | if (this._delegate.isItemSelectable(this._model.at(index))) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 527 | return index; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 528 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 529 | index += direction; |
| 530 | } |
| 531 | return -1; |
| 532 | } |
| 533 | |
| 534 | /** |
| 535 | * @param {number} index |
| 536 | * @param {number} direction |
| 537 | * @return {number} |
| 538 | */ |
| 539 | _findPageSelectable(index, direction) { |
| 540 | let lastSelectable = -1; |
| 541 | const startOffset = this._offsetAtIndex(index); |
| 542 | // Compensate for zoom rounding errors with -1. |
| 543 | const viewportHeight = this.element.offsetHeight - 1; |
| 544 | while (index >= 0 && index < this._model.length) { |
| 545 | if (this._delegate.isItemSelectable(this._model.at(index))) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 546 | if (Math.abs(this._offsetAtIndex(index) - startOffset) >= viewportHeight) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 547 | return index; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 548 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 549 | lastSelectable = index; |
| 550 | } |
| 551 | index += direction; |
| 552 | } |
| 553 | return lastSelectable; |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * @param {number} length |
| 558 | * @param {number} copyTo |
| 559 | */ |
| 560 | _reallocateVariableOffsets(length, copyTo) { |
| 561 | if (this._variableOffsets.length < length) { |
| 562 | const variableOffsets = new Int32Array(Math.max(length, this._variableOffsets.length * 2)); |
| 563 | variableOffsets.set(this._variableOffsets.slice(0, copyTo), 0); |
| 564 | this._variableOffsets = variableOffsets; |
| 565 | } else if (this._variableOffsets.length >= 2 * length) { |
| 566 | const variableOffsets = new Int32Array(length); |
| 567 | variableOffsets.set(this._variableOffsets.slice(0, copyTo), 0); |
| 568 | this._variableOffsets = variableOffsets; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | /** |
| 573 | * @param {number} from |
| 574 | * @param {number} to |
| 575 | * @param {number} inserted |
| 576 | */ |
| 577 | _invalidate(from, to, inserted) { |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 578 | if (this._mode === ListMode.NonViewport) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 579 | this._invalidateNonViewportMode(from, to - from, inserted); |
| 580 | return; |
| 581 | } |
| 582 | |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 583 | if (this._mode === ListMode.VariousHeightItems) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 584 | this._reallocateVariableOffsets(this._model.length + 1, from + 1); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 585 | for (let i = from + 1; i <= this._model.length; i++) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 586 | this._variableOffsets[i] = this._variableOffsets[i - 1] + this._delegate.heightForItem(this._model.at(i - 1)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 587 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | const viewportHeight = this.element.offsetHeight; |
| 591 | const totalHeight = this._totalHeight(); |
| 592 | const scrollTop = this.element.scrollTop; |
| 593 | |
| 594 | if (this._renderedHeight < viewportHeight || totalHeight < viewportHeight) { |
| 595 | this._clearViewport(); |
| 596 | this._updateViewport(Number.constrain(scrollTop, 0, totalHeight - viewportHeight), viewportHeight); |
| 597 | return; |
| 598 | } |
| 599 | |
| 600 | const heightDelta = totalHeight - this._renderedHeight; |
| 601 | if (to <= this._firstIndex) { |
| 602 | const topHeight = this._topHeight + heightDelta; |
| 603 | this._topElement.style.height = topHeight + 'px'; |
| 604 | this.element.scrollTop = scrollTop + heightDelta; |
| 605 | this._topHeight = topHeight; |
| 606 | this._renderedHeight = totalHeight; |
| 607 | const indexDelta = inserted - (to - from); |
| 608 | this._firstIndex += indexDelta; |
| 609 | this._lastIndex += indexDelta; |
| 610 | return; |
| 611 | } |
| 612 | |
| 613 | if (from >= this._lastIndex) { |
| 614 | const bottomHeight = this._bottomHeight + heightDelta; |
| 615 | this._bottomElement.style.height = bottomHeight + 'px'; |
| 616 | this._bottomHeight = bottomHeight; |
| 617 | this._renderedHeight = totalHeight; |
| 618 | return; |
| 619 | } |
| 620 | |
| 621 | // TODO(dgozman): try to keep visible scrollTop the same |
| 622 | // when invalidating after firstIndex but before first visible element. |
| 623 | this._clearViewport(); |
| 624 | this._updateViewport(Number.constrain(scrollTop, 0, totalHeight - viewportHeight), viewportHeight); |
| 625 | } |
| 626 | |
| 627 | /** |
| 628 | * @param {number} start |
| 629 | * @param {number} remove |
| 630 | * @param {number} add |
| 631 | */ |
| 632 | _invalidateNonViewportMode(start, remove, add) { |
| 633 | let startElement = this._topElement; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 634 | for (let index = 0; index < start; index++) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 635 | startElement = startElement.nextElementSibling; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 636 | } |
| 637 | while (remove--) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 638 | startElement.nextElementSibling.remove(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 639 | } |
| 640 | while (add--) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 641 | this.element.insertBefore(this._elementAtIndex(start + add), startElement.nextElementSibling); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 642 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | _clearViewport() { |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 646 | if (this._mode === ListMode.NonViewport) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 647 | console.error('There should be no viewport updates in non-viewport mode'); |
| 648 | return; |
| 649 | } |
| 650 | this._firstIndex = 0; |
| 651 | this._lastIndex = 0; |
| 652 | this._renderedHeight = 0; |
| 653 | this._topHeight = 0; |
| 654 | this._bottomHeight = 0; |
| 655 | this._clearContents(); |
| 656 | } |
| 657 | |
| 658 | _clearContents() { |
| 659 | // Note: this method should not force layout. Be careful. |
| 660 | this._topElement.style.height = '0'; |
| 661 | this._bottomElement.style.height = '0'; |
| 662 | this.element.removeChildren(); |
| 663 | this.element.appendChild(this._topElement); |
| 664 | this.element.appendChild(this._bottomElement); |
| 665 | } |
| 666 | |
| 667 | /** |
| 668 | * @param {number} scrollTop |
| 669 | * @param {number} viewportHeight |
| 670 | */ |
| 671 | _updateViewport(scrollTop, viewportHeight) { |
| 672 | // Note: this method should not force layout. Be careful. |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 673 | if (this._mode === ListMode.NonViewport) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 674 | console.error('There should be no viewport updates in non-viewport mode'); |
| 675 | return; |
| 676 | } |
| 677 | const totalHeight = this._totalHeight(); |
| 678 | if (!totalHeight) { |
| 679 | this._firstIndex = 0; |
| 680 | this._lastIndex = 0; |
| 681 | this._topHeight = 0; |
| 682 | this._bottomHeight = 0; |
| 683 | this._renderedHeight = 0; |
| 684 | this._topElement.style.height = '0'; |
| 685 | this._bottomElement.style.height = '0'; |
| 686 | return; |
| 687 | } |
| 688 | |
| 689 | const firstIndex = this._indexAtOffset(scrollTop - viewportHeight); |
| 690 | const lastIndex = this._indexAtOffset(scrollTop + 2 * viewportHeight) + 1; |
| 691 | |
| 692 | while (this._firstIndex < Math.min(firstIndex, this._lastIndex)) { |
| 693 | this._elementAtIndex(this._firstIndex).remove(); |
| 694 | this._firstIndex++; |
| 695 | } |
| 696 | while (this._lastIndex > Math.max(lastIndex, this._firstIndex)) { |
| 697 | this._elementAtIndex(this._lastIndex - 1).remove(); |
| 698 | this._lastIndex--; |
| 699 | } |
| 700 | |
| 701 | this._firstIndex = Math.min(this._firstIndex, lastIndex); |
| 702 | this._lastIndex = Math.max(this._lastIndex, firstIndex); |
| 703 | for (let index = this._firstIndex - 1; index >= firstIndex; index--) { |
| 704 | const element = this._elementAtIndex(index); |
| 705 | this.element.insertBefore(element, this._topElement.nextSibling); |
| 706 | } |
| 707 | for (let index = this._lastIndex; index < lastIndex; index++) { |
| 708 | const element = this._elementAtIndex(index); |
| 709 | this.element.insertBefore(element, this._bottomElement); |
| 710 | } |
| 711 | |
| 712 | this._firstIndex = firstIndex; |
| 713 | this._lastIndex = lastIndex; |
| 714 | this._topHeight = this._offsetAtIndex(firstIndex); |
| 715 | this._topElement.style.height = this._topHeight + 'px'; |
| 716 | this._bottomHeight = totalHeight - this._offsetAtIndex(lastIndex); |
| 717 | this._bottomElement.style.height = this._bottomHeight + 'px'; |
| 718 | this._renderedHeight = totalHeight; |
| 719 | this.element.scrollTop = scrollTop; |
| 720 | } |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 721 | } |