| 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 // require: list_selection_model.js | 5 // require: list_selection_model.js |
| 6 // require: list_selection_controller.js | 6 // require: list_selection_controller.js |
| 7 // require: list.js | 7 // require: list.js |
| 8 | 8 |
| 9 /** | 9 /** |
| 10 * @fileoverview This implements a grid control. Grid contains a bunch of | 10 * @fileoverview This implements a grid control. Grid contains a bunch of |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 */ | 68 */ |
| 69 itemConstructor_: GridItem, | 69 itemConstructor_: GridItem, |
| 70 | 70 |
| 71 /** | 71 /** |
| 72 * In the case of multiple columns lead item must have the same height | 72 * In the case of multiple columns lead item must have the same height |
| 73 * as a regular item. | 73 * as a regular item. |
| 74 * @type {number} | 74 * @type {number} |
| 75 * @override | 75 * @override |
| 76 */ | 76 */ |
| 77 get leadItemHeight() { | 77 get leadItemHeight() { |
| 78 return this.getItemHeight_(); | 78 return this.getDefaultItemHeight_(); |
| 79 }, | 79 }, |
| 80 set leadItemHeight(height) { | 80 set leadItemHeight(height) { |
| 81 // Lead item height cannot be set. | 81 // Lead item height cannot be set. |
| 82 }, | 82 }, |
| 83 | 83 |
| 84 /** | 84 /** |
| 85 * Whether or not the rows on list have various heights. |
| 86 * Shows a warning at the setter because cr.ui.Grid does not support this. |
| 87 * @type {boolean} |
| 88 */ |
| 89 get fixedHeight() { |
| 90 return true; |
| 91 }, |
| 92 set fixedHeight(fixedHeight) { |
| 93 if (!fixedHeight) |
| 94 console.warn('cr.ui.Grid does not support fixedHeight = false'); |
| 95 }, |
| 96 |
| 97 /** |
| 85 * @return {number} The number of columns determined by width of the grid | 98 * @return {number} The number of columns determined by width of the grid |
| 86 * and width of the items. | 99 * and width of the items. |
| 87 * @private | 100 * @private |
| 88 */ | 101 */ |
| 89 getColumnCount_: function() { | 102 getColumnCount_: function() { |
| 90 var size = this.getItemSize_(); | 103 var size = this.getDefaultItemSize_(); |
| 91 var width = size.width + size.marginHorizontal; // Uncollapse margin. | 104 var width = size.width + size.marginHorizontal; // Uncollapse margin. |
| 92 return width ? Math.floor(this.clientWidth / width) : 0; | 105 return width ? Math.floor(this.clientWidth / width) : 0; |
| 93 }, | 106 }, |
| 94 | 107 |
| 95 /** | 108 /** |
| 96 * The number of columns in the grid. If not set, determined automatically | 109 * The number of columns in the grid. If not set, determined automatically |
| 97 * as the maximum number of items fitting in the grid width. | 110 * as the maximum number of items fitting in the grid width. |
| 98 * @type {number} | 111 * @type {number} |
| 99 */ | 112 */ |
| 100 get columns() { | 113 get columns() { |
| 101 if (!this.columns_) { | 114 if (!this.columns_) { |
| 102 this.columns_ = this.getColumnCount_(); | 115 this.columns_ = this.getColumnCount_(); |
| 103 } | 116 } |
| 104 return this.columns_ || 1; | 117 return this.columns_ || 1; |
| 105 }, | 118 }, |
| 106 set columns(value) { | 119 set columns(value) { |
| 107 if (value >= 0 && value != this.columns_) { | 120 if (value >= 0 && value != this.columns_) { |
| 108 this.columns_ = value; | 121 this.columns_ = value; |
| 109 this.redraw(); | 122 this.redraw(); |
| 110 } | 123 } |
| 111 }, | 124 }, |
| 112 | 125 |
| 113 /** | 126 /** |
| 114 * @param {number} index The index of the item. | 127 * @param {number} index The index of the item. |
| 115 * @return {number} The top position of the item inside the list, not taking | 128 * @return {number} The top position of the item inside the list, not taking |
| 116 * into account lead item. May vary in the case of multiple columns. | 129 * into account lead item. May vary in the case of multiple columns. |
| 117 * @override | 130 * @override |
| 118 */ | 131 */ |
| 119 getItemTop: function(index) { | 132 getItemTop: function(index) { |
| 120 return Math.floor(index / this.columns) * this.getItemHeight_(); | 133 return Math.floor(index / this.columns) * this.getDefaultItemHeight_(); |
| 121 }, | 134 }, |
| 122 | 135 |
| 123 /** | 136 /** |
| 124 * @param {number} index The index of the item. | 137 * @param {number} index The index of the item. |
| 125 * @return {number} The row of the item. May vary in the case | 138 * @return {number} The row of the item. May vary in the case |
| 126 * of multiple columns. | 139 * of multiple columns. |
| 127 * @override | 140 * @override |
| 128 */ | 141 */ |
| 129 getItemRow: function(index) { | 142 getItemRow: function(index) { |
| 130 return Math.floor(index / this.columns); | 143 return Math.floor(index / this.columns); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 144 * @param {cr.ui.ListSelectionModel} sm The underlying selection model. | 157 * @param {cr.ui.ListSelectionModel} sm The underlying selection model. |
| 145 * @return {!cr.ui.ListSelectionController} The newly created selection | 158 * @return {!cr.ui.ListSelectionController} The newly created selection |
| 146 * controller. | 159 * controller. |
| 147 * @override | 160 * @override |
| 148 */ | 161 */ |
| 149 createSelectionController: function(sm) { | 162 createSelectionController: function(sm) { |
| 150 return new GridSelectionController(sm, this); | 163 return new GridSelectionController(sm, this); |
| 151 }, | 164 }, |
| 152 | 165 |
| 153 /** | 166 /** |
| 154 * Calculates the number of items fitting in viewport given the index of | 167 * Calculates the number of items fitting in the given viewport. |
| 155 * first item and heights. | |
| 156 * @param {number} itemHeight The height of the item. | |
| 157 * @param {number} firstIndex Index of the first item in viewport. | |
| 158 * @param {number} scrollTop The scroll top position. | 168 * @param {number} scrollTop The scroll top position. |
| 159 * @return {number} The number of items in view port. | 169 * @param {number} clientHeight The height of viewport. |
| 170 * @return {{first: number, length: number, last: number}} The index of |
| 171 * first item in view port, The number of items, The item past the last. |
| 160 * @override | 172 * @override |
| 161 */ | 173 */ |
| 162 getItemsInViewPort: function(itemHeight, firstIndex, scrollTop) { | 174 getItemsInViewPort: function(scrollTop, clientHeight) { |
| 175 var itemHeight = this.getDefaultItemHeight_(); |
| 176 var firstIndex = |
| 177 this.autoExpands ? 0 : this.getIndexForListOffset_(scrollTop); |
| 163 var columns = this.columns; | 178 var columns = this.columns; |
| 164 var clientHeight = this.clientHeight; | |
| 165 var count = this.autoExpands_ ? this.dataModel.length : Math.max( | 179 var count = this.autoExpands_ ? this.dataModel.length : Math.max( |
| 166 columns * (Math.ceil(clientHeight / itemHeight) + 1), | 180 columns * (Math.ceil(clientHeight / itemHeight) + 1), |
| 167 this.countItemsInRange_(firstIndex, scrollTop + clientHeight)); | 181 this.countItemsInRange_(firstIndex, scrollTop + clientHeight)); |
| 168 count = columns * Math.ceil(count / columns); | 182 count = columns * Math.ceil(count / columns); |
| 169 count = Math.min(count, this.dataModel.length - firstIndex); | 183 count = Math.min(count, this.dataModel.length - firstIndex); |
| 170 return count; | 184 return count; |
| 171 }, | 185 }, |
| 172 | 186 |
| 173 /** | 187 /** |
| 174 * Adds items to the list and {@code newCachedItems}. | 188 * Adds items to the list and {@code newCachedItems}. |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 return index + 1; | 305 return index + 1; |
| 292 } | 306 } |
| 293 }; | 307 }; |
| 294 | 308 |
| 295 return { | 309 return { |
| 296 Grid: Grid, | 310 Grid: Grid, |
| 297 GridItem: GridItem, | 311 GridItem: GridItem, |
| 298 GridSelectionController: GridSelectionController | 312 GridSelectionController: GridSelectionController |
| 299 } | 313 } |
| 300 }); | 314 }); |
| OLD | NEW |