| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 /** | 5 /** |
| 6 * @fileoverview This implements a table control. | 6 * @fileoverview This implements a table control. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 cr.define('cr.ui', function() { | 9 cr.define('cr.ui', function() { |
| 10 const ListSelectionModel = cr.ui.ListSelectionModel; | 10 const TableSelectionModel = cr.ui.table.TableSelectionModel; |
| 11 const ListSelectionController = cr.ui.ListSelectionController; | 11 const ListSelectionController = cr.ui.ListSelectionController; |
| 12 const ArrayDataModel = cr.ui.ArrayDataModel; | 12 const ArrayDataModel = cr.ui.ArrayDataModel; |
| 13 const TableColumnModel = cr.ui.table.TableColumnModel; | 13 const TableColumnModel = cr.ui.table.TableColumnModel; |
| 14 const TableList = cr.ui.table.TableList; | 14 const TableList = cr.ui.table.TableList; |
| 15 const TableHeader = cr.ui.table.TableHeader; | 15 const TableHeader = cr.ui.table.TableHeader; |
| 16 | 16 |
| 17 /** | 17 /** |
| 18 * Creates a new table element. | 18 * Creates a new table element. |
| 19 * @param {Object=} opt_propertyBag Optional properties. | 19 * @param {Object=} opt_propertyBag Optional properties. |
| 20 * @constructor | 20 * @constructor |
| 21 * @extends {HTMLDivElement} | 21 * @extends {HTMLDivElement} |
| 22 */ | 22 */ |
| 23 var Table = cr.ui.define('div'); | 23 var Table = cr.ui.define('div'); |
| 24 | 24 |
| 25 Table.prototype = { | 25 Table.prototype = { |
| 26 __proto__: HTMLDivElement.prototype, | 26 __proto__: HTMLDivElement.prototype, |
| 27 | 27 |
| 28 columnModel_: new TableColumnModel([]), | 28 columnModel_: new TableColumnModel([]), |
| 29 | 29 |
| 30 /** | 30 /** |
| 31 * The table data model. | 31 * The table data model. |
| 32 * | 32 * |
| 33 * @type {cr.ui.ArrayDataModel} | 33 * @type {cr.ui.table.TableDataModel} |
| 34 */ | 34 */ |
| 35 get dataModel() { | 35 get dataModel() { |
| 36 return this.list_.dataModel; | 36 return this.list_.dataModel; |
| 37 }, | 37 }, |
| 38 set dataModel(dataModel) { | 38 set dataModel(dataModel) { |
| 39 if (this.list_.dataModel != dataModel) { | 39 if (this.list_.dataModel != dataModel) { |
| 40 this.list_.dataModel = dataModel; |
| 40 if (this.list_.dataModel) { | 41 if (this.list_.dataModel) { |
| 42 this.list_.dataModel.removeEventListener('splice', this.boundRedraw_); |
| 41 this.list_.dataModel.removeEventListener('sorted', | 43 this.list_.dataModel.removeEventListener('sorted', |
| 42 this.boundHandleSorted_); | 44 this.boundHandleSorted_); |
| 43 } | 45 } |
| 44 this.list_.dataModel = dataModel; | 46 this.list_.dataModel = dataModel; |
| 47 this.list_.dataModel.table = this; |
| 48 |
| 49 |
| 45 if (this.list_.dataModel) { | 50 if (this.list_.dataModel) { |
| 51 this.list_.dataModel.addEventListener('splice', this.boundRedraw_); |
| 46 this.list_.dataModel.addEventListener('sorted', | 52 this.list_.dataModel.addEventListener('sorted', |
| 47 this.boundHandleSorted_); | 53 this.boundHandleSorted_); |
| 48 } | 54 } |
| 49 this.header_.redraw(); | 55 this.header_.redraw(); |
| 50 } | 56 } |
| 51 }, | 57 }, |
| 52 | 58 |
| 53 /** | 59 /** |
| 54 * The table column model. | 60 * The table column model. |
| 55 * | 61 * |
| 56 * @type {cr.ui.table.TableColumnModel} | 62 * @type {cr.ui.table.TableColumnModel} |
| 57 */ | 63 */ |
| 58 get columnModel() { | 64 get columnModel() { |
| 59 return this.columnModel_; | 65 return this.columnModel_; |
| 60 }, | 66 }, |
| 61 set columnModel(columnModel) { | 67 set columnModel(columnModel) { |
| 62 if (this.columnModel_ != columnModel) { | 68 if (this.columnModel_ != columnModel) { |
| 63 if (this.columnModel_) | 69 if (this.columnModel_) { |
| 70 this.columnModel_.removeEventListener('change', this.boundRedraw_); |
| 64 this.columnModel_.removeEventListener('resize', this.boundResize_); | 71 this.columnModel_.removeEventListener('resize', this.boundResize_); |
| 72 } |
| 65 this.columnModel_ = columnModel; | 73 this.columnModel_ = columnModel; |
| 66 | 74 |
| 67 if (this.columnModel_) | 75 if (this.columnModel_) { |
| 76 this.columnModel_.addEventListener('change', this.boundRedraw_); |
| 68 this.columnModel_.addEventListener('resize', this.boundResize_); | 77 this.columnModel_.addEventListener('resize', this.boundResize_); |
| 69 this.list_.invalidate(); | 78 } |
| 70 this.redraw(); | 79 this.redraw(); |
| 71 } | 80 } |
| 72 }, | 81 }, |
| 73 | 82 |
| 74 /** | 83 /** |
| 75 * The table selection model. | 84 * The table selection model. |
| 76 * | 85 * |
| 77 * @type | 86 * @type |
| 78 * {cr.ui.ListSelectionModel|cr.ui.table.ListSingleSelectionModel} | 87 * {cr.ui.table.TableSelectionModel|cr.ui.table.TableSingleSelectionModel} |
| 79 */ | 88 */ |
| 80 get selectionModel() { | 89 get selectionModel() { |
| 81 return this.list_.selectionModel; | 90 return this.list_.selectionModel; |
| 82 }, | 91 }, |
| 83 set selectionModel(selectionModel) { | 92 set selectionModel(selectionModel) { |
| 84 if (this.list_.selectionModel != selectionModel) { | 93 if (this.list_.selectionModel != selectionModel) { |
| 85 if (this.dataModel) | 94 if (this.dataModel) |
| 86 selectionModel.adjustLength(this.dataModel.length); | 95 selectionModel.adjust(0, 0, this.dataModel.length); |
| 87 this.list_.selectionModel = selectionModel; | 96 this.list_.selectionModel = selectionModel; |
| 97 this.redraw(); |
| 88 } | 98 } |
| 89 }, | 99 }, |
| 90 | 100 |
| 91 /** | 101 /** |
| 92 * Sets width of the column at the given index. | 102 * Sets width of the column at the given index. |
| 93 * | 103 * |
| 94 * @param {number} index The index of the column. | 104 * @param {number} index The index of the column. |
| 95 * @param {number} Column width. | 105 * @param {number} Column width. |
| 96 */ | 106 */ |
| 97 setColumnWidth: function(index, width) { | 107 setColumnWidth: function(index, width) { |
| 98 this.columnWidths_[index] = width; | 108 this.columnWidths_[index] = width; |
| 99 }, | 109 }, |
| 100 | 110 |
| 101 /** | 111 /** |
| 102 * Initializes the element. | 112 * Initializes the element. |
| 103 */ | 113 */ |
| 104 decorate: function() { | 114 decorate: function() { |
| 105 this.list_ = this.ownerDocument.createElement('list'); | 115 this.list_ = this.ownerDocument.createElement('list'); |
| 106 TableList.decorate(this.list_); | 116 TableList.decorate(this.list_); |
| 107 this.list_.selectionModel = new ListSelectionModel(this); | 117 this.list_.selectionModel = new TableSelectionModel(this); |
| 108 this.list_.table = this; | 118 this.list_.table = this; |
| 109 | 119 |
| 110 this.header_ = this.ownerDocument.createElement('div'); | 120 this.header_ = this.ownerDocument.createElement('div'); |
| 111 TableHeader.decorate(this.header_); | 121 TableHeader.decorate(this.header_); |
| 112 this.header_.table = this; | 122 this.header_.table = this; |
| 113 | 123 |
| 114 this.classList.add('table'); | 124 this.classList.add('table'); |
| 115 this.appendChild(this.header_); | 125 this.appendChild(this.header_); |
| 116 this.appendChild(this.list_); | 126 this.appendChild(this.list_); |
| 117 this.ownerDocument.defaultView.addEventListener( | 127 this.ownerDocument.defaultView.addEventListener( |
| 118 'resize', this.header_.updateWidth.bind(this.header_)); | 128 'resize', this.header_.updateWidth.bind(this.header_)); |
| 119 | 129 |
| 130 this.boundRedraw_ = this.redraw.bind(this); |
| 120 this.boundResize_ = this.resize.bind(this); | 131 this.boundResize_ = this.resize.bind(this); |
| 121 this.boundHandleSorted_ = this.handleSorted_.bind(this); | 132 this.boundHandleSorted_ = this.handleSorted_.bind(this); |
| 122 | 133 |
| 123 // Make table focusable | 134 // Make table focusable |
| 124 if (!this.hasAttribute('tabindex')) | 135 if (!this.hasAttribute('tabindex')) |
| 125 this.tabIndex = 0; | 136 this.tabIndex = 0; |
| 126 this.addEventListener('focus', this.handleElementFocus_, true); | 137 this.addEventListener('focus', this.handleElementFocus_, true); |
| 127 this.addEventListener('blur', this.handleElementBlur_, true); | 138 this.addEventListener('blur', this.handleElementBlur_, true); |
| 128 }, | 139 }, |
| 129 | 140 |
| 130 /** | 141 /** |
| 131 * Redraws the table. | |
| 132 */ | |
| 133 redraw: function(index) { | |
| 134 this.list_.redraw(); | |
| 135 this.header_.redraw(); | |
| 136 }, | |
| 137 | |
| 138 /** | |
| 139 * Resize the table columns. | 142 * Resize the table columns. |
| 140 */ | 143 */ |
| 141 resize: function() { | 144 resize: function() { |
| 142 // We resize columns only instead of full redraw. | 145 // We resize columns only instead of full redraw. |
| 143 this.list_.resize(); | 146 this.list_.resize(); |
| 144 this.header_.resize(); | 147 this.header_.resize(); |
| 145 }, | 148 }, |
| 146 | 149 |
| 147 /** | 150 /** |
| 148 * Ensures that a given index is inside the viewport. | 151 * Ensures that a given index is inside the viewport. |
| 149 * @param {number} index The index of the item to scroll into view. | 152 * @param {number} index The index of the item to scroll into view. |
| 150 * @return {boolean} Whether any scrolling was needed. | 153 * @return {boolean} Whether any scrolling was needed. |
| 151 */ | 154 */ |
| 152 scrollIndexIntoView: function(i) { | 155 scrollIndexIntoView: function(i) { |
| 153 this.list_.scrollIndexIntoView(i); | 156 this.list_.scrollIndexIntoView(i); |
| 154 }, | 157 }, |
| 155 | 158 |
| 156 /** | 159 /** |
| 157 * Find the list item element at the given index. | 160 * Find the list item element at the given index. |
| 158 * @param {number} index The index of the list item to get. | 161 * @param {number} index The index of the list item to get. |
| 159 * @return {ListItem} The found list item or null if not found. | 162 * @return {ListItem} The found list item or null if not found. |
| 160 */ | 163 */ |
| 161 getListItemByIndex: function(index) { | 164 getListItemByIndex: function(index) { |
| 162 return this.list_.getListItemByIndex(index); | 165 return this.list_.getListItemByIndex(index); |
| 163 }, | 166 }, |
| 164 | 167 |
| 165 /** | 168 /** |
| 169 * Redraws the table. |
| 170 * This forces the list to remove all cached items. |
| 171 */ |
| 172 redraw: function() { |
| 173 this.list_.startBatchUpdates(); |
| 174 if (this.list_.dataModel) { |
| 175 for (var i = 0; i < this.list_.dataModel.length; i++) { |
| 176 this.list_.redrawItem(i); |
| 177 } |
| 178 } |
| 179 this.list_.endBatchUpdates(); |
| 180 this.list_.redraw(); |
| 181 this.header_.redraw(); |
| 182 }, |
| 183 |
| 184 /** |
| 166 * This handles data model 'sorted' event. | 185 * This handles data model 'sorted' event. |
| 167 * After sorting we need to redraw header | 186 * After sorting we need to |
| 187 * - adjust selection |
| 188 * - redraw all the items |
| 189 * - scroll the list to show selection. |
| 168 * @param {Event} e The 'sorted' event. | 190 * @param {Event} e The 'sorted' event. |
| 169 */ | 191 */ |
| 170 handleSorted_: function(e) { | 192 handleSorted_: function(e) { |
| 171 this.header_.redraw(); | 193 var sm = this.list_.selectionModel; |
| 194 sm.adjustToReordering(e.sortPermutation); |
| 195 |
| 196 this.redraw(); |
| 197 if (sm.leadIndex != -1) |
| 198 this.list_.scrollIndexIntoView(sm.leadIndex) |
| 172 }, | 199 }, |
| 173 | 200 |
| 174 /** | 201 /** |
| 175 * Sort data by the given column. | 202 * Sort data by the given column. |
| 176 * @param {number} index The index of the column to sort by. | 203 * @param {number} index The index of the column to sort by. |
| 177 */ | 204 */ |
| 178 sort: function(i) { | 205 sort: function(i) { |
| 179 var cm = this.columnModel_; | 206 var cm = this.columnModel_; |
| 180 var sortStatus = this.list_.dataModel.sortStatus; | 207 var sortStatus = this.list_.dataModel.sortStatus; |
| 181 if (sortStatus.field == cm.getId(i)) { | 208 if (sortStatus.field == cm.getId(i)) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 * because table contents can contain controls that can be focused, and for | 257 * because table contents can contain controls that can be focused, and for |
| 231 * some purposes (e.g., styling), the table can still be conceptually focused | 258 * some purposes (e.g., styling), the table can still be conceptually focused |
| 232 * at that point even though it doesn't actually have the page focus. | 259 * at that point even though it doesn't actually have the page focus. |
| 233 */ | 260 */ |
| 234 cr.defineProperty(Table, 'hasElementFocus', cr.PropertyKind.BOOL_ATTR); | 261 cr.defineProperty(Table, 'hasElementFocus', cr.PropertyKind.BOOL_ATTR); |
| 235 | 262 |
| 236 return { | 263 return { |
| 237 Table: Table | 264 Table: Table |
| 238 }; | 265 }; |
| 239 }); | 266 }); |
| OLD | NEW |