OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // require: array_data_model.js | 5 // require: array_data_model.js |
6 // require: list_selection_model.js | 6 // require: list_selection_model.js |
7 // require: list_selection_controller.js | 7 // require: list_selection_controller.js |
8 // require: list_item.js | 8 // require: list_item.js |
9 | 9 |
10 /** | 10 /** |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
84 remainingSpace_: true, | 84 remainingSpace_: true, |
85 | 85 |
86 /** | 86 /** |
87 * Function used to create grid items. | 87 * Function used to create grid items. |
88 * @type {function(): !ListItem} | 88 * @type {function(): !ListItem} |
89 * @private | 89 * @private |
90 */ | 90 */ |
91 itemConstructor_: cr.ui.ListItem, | 91 itemConstructor_: cr.ui.ListItem, |
92 | 92 |
93 /** | 93 /** |
94 * The prefix to use when giving each item an unique id. | |
95 * @type {string} | |
96 * @private | |
97 */ | |
98 uniqueIdPrefix_: 'list', | |
99 | |
100 /** | |
101 * The next id suffix to use when giving each item an unique id. | |
102 * @type {number} | |
103 * @private | |
104 */ | |
105 nextUniqueIdSuffix_: 0, | |
106 | |
107 /** | |
94 * Function used to create grid items. | 108 * Function used to create grid items. |
95 * @type {function(): !ListItem} | 109 * @type {function(): !ListItem} |
96 */ | 110 */ |
97 get itemConstructor() { | 111 get itemConstructor() { |
98 return this.itemConstructor_; | 112 return this.itemConstructor_; |
99 }, | 113 }, |
100 set itemConstructor(func) { | 114 set itemConstructor(func) { |
101 if (func != this.itemConstructor_) { | 115 if (func != this.itemConstructor_) { |
102 this.itemConstructor_ = func; | 116 this.itemConstructor_ = func; |
103 this.cachedItems_ = {}; | 117 this.cachedItems_ = {}; |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
325 this.addEventListener(cr.ui.TouchHandler.EventType.TOUCH_START, | 339 this.addEventListener(cr.ui.TouchHandler.EventType.TOUCH_START, |
326 this.handlePointerDownUp_); | 340 this.handlePointerDownUp_); |
327 this.addEventListener(cr.ui.TouchHandler.EventType.TOUCH_END, | 341 this.addEventListener(cr.ui.TouchHandler.EventType.TOUCH_END, |
328 this.handlePointerDownUp_); | 342 this.handlePointerDownUp_); |
329 this.addEventListener(cr.ui.TouchHandler.EventType.TAP, | 343 this.addEventListener(cr.ui.TouchHandler.EventType.TAP, |
330 this.handleDoubleClickOrTap_); | 344 this.handleDoubleClickOrTap_); |
331 this.touchHandler_.enable(false, false); | 345 this.touchHandler_.enable(false, false); |
332 // Make list focusable | 346 // Make list focusable |
333 if (!this.hasAttribute('tabindex')) | 347 if (!this.hasAttribute('tabindex')) |
334 this.tabIndex = 0; | 348 this.tabIndex = 0; |
349 | |
350 // Try to get an unique id prefix from the id of this element or the | |
351 // nearest ancestor with an id. | |
352 var element = this; | |
353 while (element && !element.id) | |
354 element = element.parentElement; | |
355 if (element && element.id) | |
356 this.uniqueIdPrefix_ = element.id; | |
335 }, | 357 }, |
336 | 358 |
337 /** | 359 /** |
338 * @param {ListItem=} item The list item to measure. | 360 * @param {ListItem=} item The list item to measure. |
339 * @return {number} The height of the given item. If the fixed height on CSS | 361 * @return {number} The height of the given item. If the fixed height on CSS |
340 * is set by 'px', uses that value as height. Otherwise, measures the size. | 362 * is set by 'px', uses that value as height. Otherwise, measures the size. |
341 * @private | 363 * @private |
342 */ | 364 */ |
343 measureItemHeight_: function(item) { | 365 measureItemHeight_: function(item) { |
344 var height = item.style.height; | 366 var height = item.style.height; |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
566 | 588 |
567 /** | 589 /** |
568 * Callback from the selection model. We dispatch {@code change} events | 590 * Callback from the selection model. We dispatch {@code change} events |
569 * when the selection changes. | 591 * when the selection changes. |
570 * @param {!cr.Event} e Event with change info. | 592 * @param {!cr.Event} e Event with change info. |
571 * @private | 593 * @private |
572 */ | 594 */ |
573 handleOnChange_: function(ce) { | 595 handleOnChange_: function(ce) { |
574 ce.changes.forEach(function(change) { | 596 ce.changes.forEach(function(change) { |
575 var listItem = this.getListItemByIndex(change.index); | 597 var listItem = this.getListItemByIndex(change.index); |
576 if (listItem) | 598 if (listItem) { |
577 listItem.selected = change.selected; | 599 listItem.selected = change.selected; |
600 if (change.selected) | |
601 this.setAttribute('aria-activedescendant', listItem.id); | |
602 } | |
578 }, this); | 603 }, this); |
579 | 604 |
580 cr.dispatchSimpleEvent(this, 'change'); | 605 cr.dispatchSimpleEvent(this, 'change'); |
581 }, | 606 }, |
582 | 607 |
583 /** | 608 /** |
584 * Handles a change of the lead item from the selection model. | 609 * Handles a change of the lead item from the selection model. |
585 * @param {Event} pe The property change event. | 610 * @param {Event} pe The property change event. |
586 * @private | 611 * @private |
587 */ | 612 */ |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
800 }, | 825 }, |
801 | 826 |
802 /** | 827 /** |
803 * Creates a new list item. | 828 * Creates a new list item. |
804 * @param {*} value The value to use for the item. | 829 * @param {*} value The value to use for the item. |
805 * @return {!ListItem} The newly created list item. | 830 * @return {!ListItem} The newly created list item. |
806 */ | 831 */ |
807 createItem: function(value) { | 832 createItem: function(value) { |
808 var item = new this.itemConstructor_(value); | 833 var item = new this.itemConstructor_(value); |
809 item.label = value; | 834 item.label = value; |
835 item.id = this.uniqueIdPrefix_ + '-' + this.nextUniqueIdSuffix_; | |
Ivan Korotkov
2012/05/04 17:02:27
Please make assigning unique IDs to each element o
dmazzoni
2012/05/04 17:48:00
I don't want to make it optional, then we'll have
Ivan Korotkov
2012/05/04 22:03:33
Ok, I guess these IDs won't hurt.
| |
836 this.nextUniqueIdSuffix_++; | |
837 | |
810 if (typeof item.decorate == 'function') | 838 if (typeof item.decorate == 'function') |
811 item.decorate(); | 839 item.decorate(); |
812 return item; | 840 return item; |
813 }, | 841 }, |
814 | 842 |
815 /** | 843 /** |
816 * Creates the selection controller to use internally. | 844 * Creates the selection controller to use internally. |
817 * @param {cr.ui.ListSelectionModel} sm The underlying selection model. | 845 * @param {cr.ui.ListSelectionModel} sm The underlying selection model. |
818 * @return {!cr.ui.ListSelectionController} The newly created selection | 846 * @return {!cr.ui.ListSelectionController} The newly created selection |
819 * controller. | 847 * controller. |
(...skipping 436 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1256 * because list items can contain controls that can be focused, and for some | 1284 * because list items can contain controls that can be focused, and for some |
1257 * purposes (e.g., styling), the list can still be conceptually focused at | 1285 * purposes (e.g., styling), the list can still be conceptually focused at |
1258 * that point even though it doesn't actually have the page focus. | 1286 * that point even though it doesn't actually have the page focus. |
1259 */ | 1287 */ |
1260 cr.defineProperty(List, 'hasElementFocus', cr.PropertyKind.BOOL_ATTR); | 1288 cr.defineProperty(List, 'hasElementFocus', cr.PropertyKind.BOOL_ATTR); |
1261 | 1289 |
1262 return { | 1290 return { |
1263 List: List | 1291 List: List |
1264 }; | 1292 }; |
1265 }); | 1293 }); |
OLD | NEW |