OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 |
| 5 // require: listselectionmodel.js |
| 6 |
| 7 /** |
| 8 * @fileoverview This implements a list control. |
| 9 */ |
| 10 |
| 11 cr.define('cr.ui', function() { |
| 12 const ListSelectionModel = cr.ui.ListSelectionModel; |
| 13 |
| 14 /** |
| 15 * Creates a new list element. |
| 16 * @param {Object=} opt_propertyBag Optional properties. |
| 17 * @constructor |
| 18 * @extends {HTMLUListElement} |
| 19 */ |
| 20 var List = cr.ui.define('list'); |
| 21 |
| 22 List.prototype = { |
| 23 __proto__: HTMLUListElement.prototype, |
| 24 |
| 25 /** |
| 26 * The selection model to use. |
| 27 * @type {cr.ui.ListSelectionModel} |
| 28 */ |
| 29 get selectionModel() { |
| 30 return this.selectionModel_; |
| 31 }, |
| 32 set selectionModel(sm) { |
| 33 var oldSm = this.selectionModel_; |
| 34 if (oldSm == sm) |
| 35 return; |
| 36 |
| 37 if (!this.boundHandleOnChange_) { |
| 38 this.boundHandleOnChange_ = cr.bind(this.handleOnChange_, this); |
| 39 this.boundHandleLeadChange_ = cr.bind(this.handleLeadChange_, this); |
| 40 } |
| 41 |
| 42 if (oldSm) { |
| 43 oldSm.removeEventListener('change', this.boundHandleOnChange_); |
| 44 oldSm.removeEventListener('leadItemChange', this.boundHandleLeadChange_)
; |
| 45 } |
| 46 |
| 47 this.selectionModel_ = sm; |
| 48 |
| 49 if (sm) { |
| 50 sm.addEventListener('change', this.boundHandleOnChange_); |
| 51 sm.addEventListener('leadItemChange', this.boundHandleLeadChange_); |
| 52 } |
| 53 }, |
| 54 |
| 55 /** |
| 56 * Convenience alias for selectionModel.selectedItem |
| 57 * @type {cr.ui.ListItem} |
| 58 */ |
| 59 get selectedItem() { |
| 60 return this.selectionModel.selectedItem; |
| 61 }, |
| 62 set selectedItem(selectedItem) { |
| 63 this.selectionModel.selectedItem = selectedItem; |
| 64 }, |
| 65 |
| 66 /** |
| 67 * Convenience alias for selectionModel.selectedItems |
| 68 * @type {!Array<cr.ui.ListItem>} |
| 69 */ |
| 70 get selectedItems() { |
| 71 return this.selectionModel.selectedItems; |
| 72 }, |
| 73 |
| 74 /** |
| 75 * The HTML elements representing the items. This is just all the element |
| 76 * children but subclasses may override this to filter out certain elements. |
| 77 * @type {HTMLCollection} |
| 78 */ |
| 79 get items() { |
| 80 return this.children; |
| 81 }, |
| 82 |
| 83 add: function(listItem) { |
| 84 this.appendChild(listItem); |
| 85 |
| 86 var uid = cr.getUid(listItem); |
| 87 this.uidToListItem_[uid] = listItem; |
| 88 |
| 89 this.selectionModel.add(listItem); |
| 90 }, |
| 91 |
| 92 addAt: function(listItem, index) { |
| 93 this.insertBefore(listItem, this.items[index]); |
| 94 |
| 95 var uid = cr.getUid(listItem); |
| 96 this.uidToListItem_[uid] = listItem; |
| 97 |
| 98 this.selectionModel.add(listItem); |
| 99 }, |
| 100 |
| 101 remove: function(listItem) { |
| 102 this.selectionModel.remove(listItem); |
| 103 |
| 104 this.removeChild(listItem); |
| 105 |
| 106 var uid = cr.getUid(listItem); |
| 107 delete this.uidToListItem_[uid]; |
| 108 }, |
| 109 |
| 110 clear: function() { |
| 111 this.innerHTML = ''; |
| 112 this.selectionModel.clear(); |
| 113 }, |
| 114 |
| 115 /** |
| 116 * Initializes the element. |
| 117 */ |
| 118 decorate: function() { |
| 119 this.uidToListItem_ = {}; |
| 120 |
| 121 this.selectionModel = new ListSelectionModel(this); |
| 122 |
| 123 this.addEventListener('mousedown', this.handleMouseDownUp_); |
| 124 this.addEventListener('mouseup', this.handleMouseDownUp_); |
| 125 this.addEventListener('keydown', this.handleKeyDown); |
| 126 this.addEventListener('dblclick', this.handleDoubleClick_); |
| 127 |
| 128 // Make list focusable |
| 129 if (!this.hasAttribute('tabindex')) |
| 130 this.tabIndex = 0; |
| 131 }, |
| 132 |
| 133 /** |
| 134 * Callback for mousedown and mouseup events. |
| 135 * @param {Event} e The mouse event object. |
| 136 * @private |
| 137 */ |
| 138 handleMouseDownUp_: function(e) { |
| 139 var target = e.target; |
| 140 while (target && target.parentNode != this) { |
| 141 target = target.parentNode; |
| 142 } |
| 143 this.selectionModel.handleMouseDownUp(e, target); |
| 144 }, |
| 145 |
| 146 /** |
| 147 * Callback for mousedown events. |
| 148 * @param {Event} e The mouse event object. |
| 149 * @private |
| 150 */ |
| 151 handleMouseUp_: function(e) { |
| 152 var target = e.target; |
| 153 while (target && target.parentNode != this) { |
| 154 target = target.parentNode; |
| 155 } |
| 156 if (target) { |
| 157 this.selectionModel.handleMouseDown(e, target); |
| 158 } else { |
| 159 this.selectionModel.clear(); |
| 160 } |
| 161 }, |
| 162 |
| 163 |
| 164 /** |
| 165 * Handle a keydown event. |
| 166 * @param {Event} e The keydown event. |
| 167 * @return {boolean} Whether the key event was handled. |
| 168 */ |
| 169 handleKeyDown: function(e) { |
| 170 if (this.selectionModel.handleKeyDown(e)) |
| 171 return true; |
| 172 if (e.keyIdentifier == 'Enter' && this.selectionModel.selectedItem) { |
| 173 cr.dispatchSimpleEvent(this, 'activate'); |
| 174 return true; |
| 175 } |
| 176 return false; |
| 177 }, |
| 178 |
| 179 /** |
| 180 * Handler for double clicking. When the user double clicks on a selected |
| 181 * item we dispatch an {@code activate} event. |
| 182 * @param {Event} e The mouse event object. |
| 183 * @private |
| 184 */ |
| 185 handleDoubleClick_: function(e) { |
| 186 if (e.button == 0 && this.selectionModel.selectedItem) { |
| 187 cr.dispatchSimpleEvent(this, 'activate'); |
| 188 } |
| 189 }, |
| 190 |
| 191 /** |
| 192 * Callback from the selection model. We dispatch {@code change} events |
| 193 * when the selection changes. |
| 194 * @param {!cr.Event} e Event with change info. |
| 195 * @private |
| 196 */ |
| 197 handleOnChange_: function(ce) { |
| 198 ce.changes.forEach(function(change) { |
| 199 var listItem = this.uidToListItem_[change.uid]; |
| 200 listItem.selected = change.selected; |
| 201 }, this); |
| 202 |
| 203 cr.dispatchSimpleEvent(this, 'change'); |
| 204 }, |
| 205 |
| 206 /** |
| 207 * Handles a change of the lead item from the selection model. |
| 208 * @property {Event} pe The property change event. |
| 209 * @private |
| 210 */ |
| 211 handleLeadChange_: function(pe) { |
| 212 if (pe.oldValue) { |
| 213 pe.oldValue.lead = false; |
| 214 } |
| 215 if (pe.newValue) { |
| 216 pe.newValue.lead = true; |
| 217 } |
| 218 }, |
| 219 |
| 220 /** |
| 221 * Gets a unique ID for an item. This needs to be unique to the list but |
| 222 * does not have to be gloabally unique. This uses {@code cr.getUid} by |
| 223 * default. Override to provide a more efficient way to get the unique ID. |
| 224 * @param {cr.ui.ListItem} item The item to get the unique ID for. |
| 225 * @return |
| 226 */ |
| 227 itemToUid: function(item) { |
| 228 return cr.getUid(item); |
| 229 } |
| 230 }; |
| 231 |
| 232 return { |
| 233 List: List |
| 234 } |
| 235 }); |
OLD | NEW |