1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// require: list_selection_model.js
// require: list_selection_controller.js
// require: list.js
/**
* @fileoverview This implements a grid control. Grid contains a bunch of
* similar elements placed in multiple columns. It's pretty similar to the list,
* except the multiple columns layout.
*/
cr.define('cr.ui', function() {
const ListSelectionController = cr.ui.ListSelectionController;
const List = cr.ui.List;
const ListItem = cr.ui.ListItem;
/**
* Creates a new grid item element.
* @param {*} dataItem The data item.
* @constructor
* @extends {cr.ui.ListItem}
*/
function GridItem(dataItem) {
var el = cr.doc.createElement('span');
el.dataItem = dataItem;
el.__proto__ = GridItem.prototype;
return el;
}
GridItem.prototype = {
__proto__: ListItem.prototype,
/**
* Called when an element is decorated as a grid item.
*/
decorate: function() {
ListItem.prototype.decorate.call(this, arguments);
this.textContent = this.dataItem;
}
};
/**
* Creates a new grid element.
* @param {Object=} opt_propertyBag Optional properties.
* @constructor
* @extends {cr.ui.List}
*/
var Grid = cr.ui.define('grid');
Grid.prototype = {
__proto__: List.prototype,
/**
* The number of columns in the grid. Either set by the user, or lazy
* calculated as the maximum number of items fitting in the grid width.
* @type {number}
* @private
*/
columns_: 0,
/**
* Function used to create grid items.
* @type {function(): !GridItem}
* @override
*/
itemConstructor_: GridItem,
/**
* In the case of multiple columns lead item must have the same height
* as a regular item.
* @type {number}
* @override
*/
get leadItemHeight() {
return this.getItemHeight_();
},
set leadItemHeight(height) {
// Lead item height cannot be set.
},
/**
* @return {number} The number of columns determined by width of the grid
* and width of the items.
* @private
*/
getColumnCount_: function() {
var size = this.getItemSize_();
var width = size.width + size.marginHorizontal; // Uncollapse margin.
return width ? Math.floor(this.clientWidth / width) : 0;
},
/**
* The number of columns in the grid. If not set, determined automatically
* as the maximum number of items fitting in the grid width.
* @type {number}
*/
get columns() {
if (!this.columns_) {
this.columns_ = this.getColumnCount_();
}
return this.columns_ || 1;
},
set columns(value) {
if (value >= 0 && value != this.columns_) {
this.columns_ = value;
this.redraw();
}
},
/**
* @param {number} index The index of the item.
* @return {number} The top position of the item inside the list, not taking
* into account lead item. May vary in the case of multiple columns.
* @override
*/
getItemTop: function(index) {
return Math.floor(index / this.columns) * this.getItemHeight_();
},
/**
* @param {number} index The index of the item.
* @return {number} The row of the item. May vary in the case
* of multiple columns.
* @override
*/
getItemRow: function(index) {
return Math.floor(index / this.columns);
},
/**
* @param {number} row The row.
* @return {number} The index of the first item in the row.
* @override
*/
getFirstItemInRow: function(row) {
return row * this.columns;
},
/**
* Creates the selection controller to use internally.
* @param {cr.ui.ListSelectionModel} sm The underlying selection model.
* @return {!cr.ui.ListSelectionController} The newly created selection
* controller.
* @override
*/
createSelectionController: function(sm) {
return new GridSelectionController(sm, this);
},
/**
* Calculates the number of items fitting in viewport given the index of
* first item and heights.
* @param {number} itemHeight The height of the item.
* @param {number} firstIndex Index of the first item in viewport.
* @param {number} scrollTop The scroll top position.
* @return {number} The number of items in view port.
* @override
*/
getItemsInViewPort: function(itemHeight, firstIndex, scrollTop) {
var columns = this.columns;
var clientHeight = this.clientHeight;
var count = this.autoExpands_ ? this.dataModel.length : Math.max(
columns * (Math.ceil(clientHeight / itemHeight) + 1),
this.countItemsInRange_(firstIndex, scrollTop + clientHeight));
count = columns * Math.ceil(count / columns);
count = Math.min(count, this.dataModel.length - firstIndex);
return count;
},
/**
* Adds items to the list and {@code newCachedItems}.
* @param {number} firstIndex The index of first item, inclusively.
* @param {number} lastIndex The index of last item, exclusively.
* @param {Object.<string, ListItem>} cachedItems Old items cache.
* @param {Object.<string, ListItem>} newCachedItems New items cache.
* @override
*/
addItems: function(firstIndex, lastIndex, cachedItems, newCachedItems) {
var listItem;
var dataModel = this.dataModel;
var spacers = this.spacers_ || {};
var spacerIndex = 0;
var columns = this.columns;
for (var y = firstIndex; y < lastIndex; y++) {
if (y % columns == 0 && y > 0) {
var spacer = spacers[spacerIndex];
if (!spacer) {
spacer = this.ownerDocument.createElement('div');
spacer.className = 'spacer';
spacers[spacerIndex] = spacer;
}
this.appendChild(spacer);
spacerIndex++;
}
var dataItem = dataModel.item(y);
listItem = cachedItems[y] || this.createItem(dataItem);
listItem.listIndex = y;
this.appendChild(listItem);
newCachedItems[y] = listItem;
}
this.spacers_ = spacers;
},
/**
* Returns the height of after filler in the list.
* @param {number} lastIndex The index of item past the last in viewport.
* @param {number} itemHeight The height of the item.
* @return {number} The height of after filler.
* @override
*/
getAfterFillerHeight: function(lastIndex, itemHeight) {
var columns = this.columns;
// We calculate the row of last item, and the row of last shown item.
// The difference is the number of rows not shown.
var afterRows = Math.floor((this.dataModel.length - 1) / columns) -
Math.floor((lastIndex - 1) / columns);
return afterRows * itemHeight;
}
};
/**
* Creates a selection controller that is to be used with grids.
* @param {cr.ui.ListSelectionModel} selectionModel The selection model to
* interact with.
* @param {cr.ui.Grid} grid The grid to interact with.
* @constructor
* @extends {!cr.ui.ListSelectionController}
*/
function GridSelectionController(selectionModel, grid) {
this.selectionModel_ = selectionModel;
this.grid_ = grid;
}
GridSelectionController.prototype = {
__proto__: ListSelectionController.prototype,
/**
* Returns the index below (y axis) the given element.
* @param {number} index The index to get the index below.
* @return {number} The index below or -1 if not found.
* @override
*/
getIndexBelow: function(index) {
var last = this.getLastIndex();
if (index == last) {
return -1;
}
index += this.grid_.columns;
return Math.min(index, last);
},
/**
* Returns the index above (y axis) the given element.
* @param {number} index The index to get the index above.
* @return {number} The index below or -1 if not found.
* @override
*/
getIndexAbove: function(index) {
if (index == 0) {
return -1;
}
index -= this.grid_.columns;
return Math.max(index, 0);
},
/**
* Returns the index before (x axis) the given element.
* @param {number} index The index to get the index before.
* @return {number} The index before or -1 if not found.
* @override
*/
getIndexBefore: function(index) {
return index - 1;
},
/**
* Returns the index after (x axis) the given element.
* @param {number} index The index to get the index after.
* @return {number} The index after or -1 if not found.
* @override
*/
getIndexAfter: function(index) {
if (index == this.getLastIndex()) {
return -1;
}
return index + 1;
}
};
return {
Grid: Grid,
GridItem: GridItem,
GridSelectionController: GridSelectionController
}
});
|