-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathGrid.js
More file actions
374 lines (287 loc) · 11.8 KB
/
Grid.js
File metadata and controls
374 lines (287 loc) · 11.8 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
(function($) {
var Grid = Tiles.Grid = function(element) {
this.$el = $(element);
// animation lasts 500 ms by default
this.animationDuration = 500;
// min width and height of a cell in the grid
this.cellSizeMin = 150;
// the default set of factories used when creating templates
this.templateFactory = Tiles.UniformTemplates;
// defines the page size for prioritization of positions and tiles
this.priorityPageSize = Number.MAX_VALUE;
// spacing between tiles
this.cellPadding = 10;
// actual width and height of a cell in the grid
this.cellSize = 0;
// number of tile cell columns
this.numCols = 1;
// cache the current template
this.template = null;
// flag that tracks whether a redraw is necessary
this.isDirty = true;
this.tiles = [];
// keep track of added and removed tiles so we can update tiles
// and the render the grid independently.
this.tilesAdded = [];
this.tilesRemoved = [];
};
Grid.prototype.getContentWidth = function() {
// by default, the entire container width is used when drawing tiles
return this.$el.width();
};
// gets the number of columns during a resize
Grid.prototype.resizeColumns = function() {
var panelWidth = this.getContentWidth();
// ensure we have at least one column
return Math.max(1, Math.floor((panelWidth + this.cellPadding) /
(this.cellSizeMin + this.cellPadding)));
};
// gets the cell size during a grid resize
Grid.prototype.resizeCellSize = function() {
var panelWidth = this.getContentWidth();
return Math.ceil((panelWidth + this.cellPadding) / this.numCols) -
this.cellPadding;
};
Grid.prototype.resize = function() {
var newCols = this.resizeColumns();
if (this.numCols !== newCols && newCols > 0) {
this.numCols = newCols;
this.isDirty = true;
}
var newCellSize = this.resizeCellSize();
if (this.cellSize !== newCellSize && newCellSize > 0) {
this.cellSize = newCellSize;
this.isDirty = true;
}
};
// refresh all tiles based on the current content
Grid.prototype.updateTiles = function(newTileIds) {
// ensure we dont have duplicate ids
newTileIds = uniques(newTileIds);
var numTiles = newTileIds.length,
newTiles = [],
i, tile, tileId, index;
// retain existing tiles and queue remaining tiles for removal
for (i = this.tiles.length - 1; i >= 0; i--) {
tile = this.tiles[i];
index = $.inArray(tile.id, newTileIds);
if (index < 0) {
this.tilesRemoved.push(tile);
//console.log('Removing tile: ' + tile.id)
}
else {
newTiles[index] = tile;
}
}
// clear existing tiles
this.tiles = [];
// make sure we have tiles for new additions
for (i = 0; i < numTiles; i++) {
tile = newTiles[i];
if (!tile) {
tileId = newTileIds[i];
// see if grid has a custom tile factory
if (this.createTile) {
tile = this.createTile(tileId);
// skip the tile if it couldn't be created
if (!tile) {
//console.log('Tile element could not be created, id: ' + tileId);
continue;
}
} else {
tile = new Tiles.Tile(tileId);
}
// add tiles to queue (will be appended to DOM during redraw)
this.tilesAdded.push(tile);
//console.log('Adding tile: ' + tile.id);
}
this.tiles.push(tile);
}
};
// helper to return unique items
function uniques(items) {
var results = [],
numItems = items ? items.length : 0,
i, item;
for (i = 0; i < numItems; i++) {
item = items[i];
if ($.inArray(item, results) === -1) {
results.push(item);
}
}
return results;
}
// prepend new tiles
Grid.prototype.insertTiles = function(newTileIds) {
this.addTiles(newTileIds, true);
};
// append new tiles
Grid.prototype.addTiles = function(newTileIds, prepend) {
if (!newTileIds || newTileIds.length === 0) {
return;
}
var prevTileIds = [],
prevTileCount = this.tiles.length,
i;
// get the existing tile ids
for (i = 0; i < prevTileCount; i++) {
prevTileIds.push(this.tiles[i].id);
}
var tileIds = prepend ? newTileIds.concat(prevTileIds)
: prevTileIds.concat(newTileIds);
this.updateTiles(tileIds);
};
Grid.prototype.removeTiles = function(removeTileIds) {
if (!removeTileIds || removeTileIds.length === 0) {
return;
}
var updateTileIds = [],
i, len, id;
// get the set of ids which have not been removed
for (i = 0, len = this.tiles.length; i < len; i++) {
id = this.tiles[i].id;
if ($.inArray(id, removeTileIds) === -1) {
updateTileIds.push(id);
}
}
this.updateTiles(updateTileIds);
};
Grid.prototype.createTemplate = function(numCols, targetTiles) {
// ensure that we have at least one column
numCols = Math.max(1, numCols);
var template = this.templateFactory.get(numCols, targetTiles, this.tiles);
if (!template) {
// fallback in case the default factory can't generate a good template
template = Tiles.UniformTemplates.get(numCols, targetTiles);
}
return template;
};
// ensures we have a good template for the specified numbef of tiles
Grid.prototype.ensureTemplate = function(numTiles) {
// verfiy that the current template is still valid
if (!this.template || this.template.numCols !== this.numCols) {
this.template = this.createTemplate(this.numCols, numTiles);
this.isDirty = true;
} else {
// append another template if we don't have enough rects
var missingRects = numTiles - this.template.rects.length;
if (missingRects > 0) {
this.template.append(
this.createTemplate(this.numCols, missingRects));
this.isDirty = true;
}
}
};
// helper that returns true if a tile was in the viewport or will be given
// the new pixel rect coordinates and dimensions
function wasOrWillBeVisible(viewRect, tile, newRect) {
var viewMaxY = viewRect.y + viewRect.height,
viewMaxX = viewRect.x + viewRect.width;
// note: y axis is the more common exclusion, so check that first
// was the tile visible?
if (tile) {
if (!((tile.top > viewMaxY) || (tile.top + tile.height < viewRect.y) ||
(tile.left > viewMaxX) || (tile.left + tile.width < viewRect.x))) {
return true;
}
}
if (newRect) {
// will it be visible?
if (!((newRect.y > viewMaxY) || (newRect.y + newRect.height < viewRect.y) ||
(newRect.x > viewMaxX) || (newRect.x + newRect.width < viewRect.x))) {
return true;
}
}
return false;
}
Grid.prototype.shouldRedraw = function() {
// see if we need to calculate the cell size
if (this.cellSize <= 0) {
this.resize();
}
// verify that we have a template
this.ensureTemplate(this.tiles.length);
// only redraw when necessary
var shouldRedraw = (this.isDirty ||
this.tilesAdded.length > 0 ||
this.tilesRemoved.length > 0);
return shouldRedraw;
};
// redraws the grid after tile collection changes
Grid.prototype.redraw = function(animate, onComplete) {
// see if we should redraw
if (!this.shouldRedraw()) {
if (onComplete) {
onComplete(false); // tell callback that we did not redraw
}
return;
}
var numTiles = this.tiles.length,
pageSize = this.priorityPageSize,
duration = this.animationDuration,
cellPlusPadding = this.cellSize + this.cellPadding,
tileIndex = 0,
appendDelay = 0,
viewRect = new Tiles.Rectangle(
this.$el.scrollLeft(),
this.$el.scrollTop(),
this.$el.width(),
this.$el.height()),
tile, added, pageRects, pageTiles, i, len, cellRect, pixelRect,
animateTile, priorityRects, priorityTiles;
// chunk tile layout by pages which are internally prioritized
for (tileIndex = 0; tileIndex < numTiles; tileIndex += pageSize) {
// get the next page of rects and tiles
pageRects = this.template.rects.slice(tileIndex, tileIndex + pageSize);
pageTiles = this.tiles.slice(tileIndex, tileIndex + pageSize);
// create a copy that can be ordered
priorityRects = pageRects.slice(0);
priorityTiles = pageTiles.slice(0);
// prioritize the page of rects and tiles
if (this.prioritizePage) {
this.prioritizePage(priorityRects, priorityTiles);
}
// place all the tiles for the current page
for (i = 0, len = priorityTiles.length; i < len; i++) {
tile = priorityTiles[i];
added = $.inArray(tile, this.tilesAdded) >= 0;
cellRect = priorityRects[i];
pixelRect = new Tiles.Rectangle(
cellRect.x * cellPlusPadding,
cellRect.y * cellPlusPadding,
(cellRect.width * cellPlusPadding) - this.cellPadding,
(cellRect.height * cellPlusPadding) - this.cellPadding);
tile.resize(
cellRect,
pixelRect,
animate && !added && wasOrWillBeVisible(viewRect, tile, pixelRect),
duration);
if (added) {
// decide whether to animate (fadeIn) and get the duration
animateTile = animate && wasOrWillBeVisible(viewRect, null, pixelRect);
if (animateTile && this.getAppendDelay) {
appendDelay = this.getAppendDelay(
cellRect, pageRects, priorityRects,
tile, pageTiles, priorityTiles);
} else {
appendDelay = 0;
}
tile.appendTo(this.$el, animateTile, appendDelay, duration);
}
}
}
// fade out all removed tiles
for (i = 0, len = this.tilesRemoved.length; i < len; i++) {
tile = this.tilesRemoved[i];
animateTile = animate && wasOrWillBeVisible(viewRect, tile, null);
tile.remove(animateTile, duration);
}
// clear pending queues for add / remove
this.tilesRemoved = [];
this.tilesAdded = [];
this.isDirty = false;
if (onComplete) {
setTimeout(function() { onComplete(true); }, duration + 10);
}
};
})(jQuery);