-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtiler.js
More file actions
441 lines (380 loc) · 9.13 KB
/
tiler.js
File metadata and controls
441 lines (380 loc) · 9.13 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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/**
* Tiler 0.2.4
*
* Endless grid of tiles.
* https://github.com/borbit/tiler/
*
* (c) 2011-2015 Serge Borbit <serge.borbit@gmail.com>
*
* Licensed under the MIT license
*/
(function(root) {
// lookup dependencies "global" scope
var $ = root.jQuery
var Grid = root.Grid
// or require them if we are in CommonJS envieronment
if (typeof require !== 'undefined' &&
typeof module !== 'undefined' && module.exports) {
$ = require('jquery')
Grid = require('grid')
}
/**
* Constructor
*
* @param {jQuery|DOM} element
* @param {Object} options
*/
function Tiler(element, options) {
// Initializing options by Extending the default by custom
this.options = $.extend({}, Tiler.defaults, options)
// Tiles elements will be saved here
this.tiles = new Grid()
// Saving the viewport element as a property
this.element = element.jquery ? element : $(element)
// Current and initial (starting) coordinates of the top left visible tile
this.x = this.initX = this.options.x
this.y = this.initY = this.options.y
// Offset of the grid element (in tiles) from the latest refresh
this.gridOffsetX = 0
this.gridOffsetY = 0
// Creating the grid element that will contain tiles. Appending it to the viewport element.
this.grid = $('<div/>').css({'position': 'absolute'}).appendTo(this.element)
this.calcRowsColsCount()
this.calcCornersCoords()
this.setGridPosition()
}
/**
* Default options
*/
Tiler.defaults = {
fetch: null
, tileSize: null
, margin: 2
, x: 0, y: 0
}
var Proto = Tiler.prototype
/**
* Sets the initial grid position
*
* @api private
*/
Proto.setGridPosition = function() {
this.grid.css({left: 0, top: 0})
}
/**
* Calculates grid offset in tiles regarding the initial and
* new (absolute) position of the grid element
*
* @return {Object} offset {x: {Number}, y: {Number}}
* @api private
*/
Proto.calcGridOffset = function() {
var pos = this.grid.position()
var ts = this.options.tileSize
pos.left > 0 && (pos.left += ts)
pos.top > 0 && (pos.top += ts)
return {
x: ~~(pos.left / ts) - this.gridOffsetX
, y: ~~(pos.top / ts) - this.gridOffsetY
}
}
/**
* Removes tiles that don't fall within the current grid area and fetches absent
* tiles. Call this method if the grid was dragged/moved or viewport size is changed,
* also in case unless all tiles are present after the fetch and you have to fetch
* absent tiles only.
*
* @api public
*/
Proto.refresh = function() {
var offset = this.calcGridOffset()
this.x -= offset.x
this.y -= offset.y
this.gridOffsetX += offset.x
this.gridOffsetY += offset.y
this.calcRowsColsCount()
this.calcCornersCoords()
var removed = this.getHiddenTilesCoords()
var tofetch = this.getTilesCoordsToFetch()
if (removed.length) {
this.remove(removed)
}
if (tofetch.length || removed.length) {
this.fetchTiles(tofetch, removed)
}
}
/**
* Refetches all tiles that fall within the grid area
*
* @api public
*/
Proto.reload = function() {
this.fetchTiles(this.getAllTilesCoords(), [])
}
/**
* If arguments are passed - changes current grid coordinates (top left visible tile)
* and fetches/removes tiles as in the same way as `refresh` method does. If method
* is called without arguments - returns current grid coordinates.
*
* @param {Number} x
* @param {Number} y
* @return {Object}
* @api public
*/
Proto.coords = function(x, y) {
if (!arguments.length) {
return {x: this.x, y: this.y}
}
this.x = this.initX = x
this.y = this.initY = y
this.gridOffsetX = 0
this.gridOffsetY = 0
this.calcRowsColsCount()
this.calcCornersCoords()
this.setGridPosition()
var removed = this.getHiddenTilesCoords()
var tofetch = this.getTilesCoordsToFetch()
this.remove(removed)
this.arrangeAll()
this.fetchTiles(tofetch, removed)
}
/**
* Shows tiles
*
* show(x, y, tile)
* @param {Number} x
* @param {Number} y
* @param {jQuery|DOM|Array} elems
*
* show(tiles)
* @param {Array} tiles [
* [x1, y1, el1|[el1,e2]]
* , [x2, y2, el2|[el3,e4]]
* , ...]
*
* @api public
*/
Proto.show = function(x, y, elems) {
var fragment = document.createDocumentFragment()
var tiles = $.isArray(x) ? x : [[x, y, elems]]
var elems
for(var i = 0, l = tiles.length; i < l; i++) {
x = tiles[i][0]
y = tiles[i][1]
if (!this.inGrid(x, y)) {
continue
}
elems = tiles[i][2]
elems = $.isArray(elems) ? elems : [elems]
elems = $.map(elems, function(elem) {
return elem.jquery ? elem : $(elem)
})
$.each(elems, function(i, elem) {
fragment.appendChild(elem.get(0))
})
this.remove(x, y)
this.tiles.set(x, y, elems)
this.arrange(x, y)
}
this.grid.append(fragment)
}
/**
* Arranges tile position
*
* @param {Number} x
* @param {Number} y
*
* @api private
*/
Proto.arrange = function(x, y) {
var elems = this.tiles.get(x, y)
, ts = this.options.tileSize
, ix = this.initX
, iy = this.initY
$.each(elems, function(i, elem) {
elem.css({
position: 'absolute'
, left: (x - ix) * ts
, top: (y - iy) * ts
})
})
}
/**
* Arranges position of all tiles
*
* @api private
*/
Proto.arrangeAll = function() {
var coords = this.tiles.coords()
for (var i = coords.length; i--;) {
this.arrange(coords[i][0], coords[i][1])
}
}
/**
* Removes tiles in passed coordinates
*
* remove(x, y)
* @param {Number} x
* @param {Number} y
*
* remove(coords)
* @param {Array} coords [[x1, y1], [x2, y2], ...]
*
* @api private
*/
Proto.remove = function(x, y) {
var coords = $.isArray(x) ? x : [[x, y]]
for (var i = 0, l = coords.length; i < l; i++) {
x = coords[i][0]
y = coords[i][1]
var present = this.tiles.get(x, y)
if (present) {
this.tiles.remove(x, y)
$.each(present, function(i, elem) {
elem.remove()
})
}
}
}
/**
* Returns array of tiles coordinates that should be present on a grid
*
* @return {Array} [[x1, y1], [x2, y2], ...]
* @api private
*/
Proto.getTilesCoordsToFetch = function() {
var tofetch = []
, all = this.getAllTilesCoords()
, x, y
for(var i = 0, l = all.length; i < l; i++) {
x = all[i][0]
y = all[i][1]
if (!this.tiles.get(x, y)) {
tofetch.push([x, y])
}
}
return tofetch
}
/**
* Returns array of tiles coordinates which coordinates don't fall
* within the grid area. Method is used to determine which tiles were
* hidden after the grid was dragged or grid position was changed
*
* @return {Array} [[x1, y1], [x2, y2], ...]
* @api private
*/
Proto.getHiddenTilesCoords = function() {
var coords = []
, self = this
this.tiles.each(function(tile, x, y) {
if (!self.inGrid(x, y)) {
coords.push([x, y])
}
})
return coords
}
/**
* Returns array of tiles coordinates which do fall within the grid area.
*
* @return {Array} [[x1, y1], [x2, y2], ...]
* @api private
*/
Proto.getAllTilesCoords = function() {
var coords = []
for(var y = this.corners.y1; y <= this.corners.y2; y++) {
for(var x = this.corners.x1; x <= this.corners.x2; x++) {
coords.push([x, y])
}}
return coords
}
/**
* Fetches tiles
*
* @param {Array} tofetch Array of tiles coordinates that should be fetched
* @param {Array} removed Array of tiles coordinates that were removed/hidden from the grid
* @api private
*/
Proto.fetchTiles = function(tofetch, removed) {
if ($.isFunction(this.options.fetch)) {
this.options.fetch(tofetch, removed)
}
}
/**
* Calculates grid's columns count
*
* @return {Number}
* @api private
*/
Proto.calcColsCount = function() {
var width = this.element.width()
, op = this.options
if (width && op.tileSize) {
return Math.ceil(width / op.tileSize) + op.margin * 2
}
return 0
}
/**
* Calculates grid's rows count
*
* @return {Number}
* @api private
*/
Proto.calcRowsCount = function() {
var height = this.element.height()
, op = this.options
if (height && op.tileSize) {
return Math.ceil(height / op.tileSize) + op.margin * 2
}
return 0
}
/**
* Calculates and sets current rows and cols count
*
* @api private
*/
Proto.calcRowsColsCount = function() {
this.rowsCount = this.calcRowsCount()
this.colsCount = this.calcColsCount()
}
/**
* Calculates and sets the current corner tiles coordinates
*
* @api private
*/
Proto.calcCornersCoords = function() {
var x1 = this.x - this.options.margin
, y1 = this.y - this.options.margin
this.corners = {
x1: x1, y1: y1
, x2: x1 + this.colsCount - 1
, y2: y1 + this.rowsCount - 1
}
}
/**
*
*/
Proto.inGrid = function(x, y) {
if (y < this.corners.y1 || y > this.corners.y2 ||
x < this.corners.x1 || x > this.corners.x2) {
return false;
}
return true;
}
// Export Tiler for CommonJS. If being loaded as an
// AMD module, define it as such. Otherwise, just add
// `Tiler` to the global object
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = Tiler
}
exports.Tiler = Tiler
} else if (typeof define === 'function' && define.amd) {
// Return the Tiler as an AMD module:
define([], function() {
return Tiler
})
} else {
// Declare `Tiler` on the root (global/window) object:
root['Tiler'] = Tiler
}
})(this)