forked from maslianok/stickyRows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stickyRows.js
491 lines (395 loc) · 19.9 KB
/
stickyRows.js
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
(function($) {
var uuid = 0;
//http://chris-spittles.co.uk/jquery-calculate-scrollbar-width/
function scrollbarWidth() {
var $inner = $('<div style="width: 100%; height:200px;">test</div>'),
$outer = $('<div style="width:200px;height:150px; position: absolute; top: 0; left: 0; visibility: hidden; overflow:hidden;"></div>').append($inner),
inner = $inner[0],
outer = $outer[0];
$('body').append(outer);
var w1 = inner.offsetWidth;
$outer.css('overflow', 'scroll');
var w2 = outer.clientWidth;
$outer.remove();
return (w1 - w2);
}
$.fn.stickyRows = function(options) {
var val = [];
var args = Array.prototype.slice.call(arguments, 1);
if (typeof options === 'string') {
this.each(function () {
var instance = $.data(this, 'stickyRows');
if (typeof instance !== 'undefined' && $.isFunction(instance[options])) {
var methodVal = instance[options].apply(instance, args);
if (methodVal !== undefined && methodVal !== instance) val.push(methodVal);
}
else return $.error('No such method "' + options + '" for stickyRows');
});
} else {
this.each(function () {
if (!$(this).is('.sticky-table-for-shifting, .sticky-table')) {
if (!$.data(this, 'stickyRows')) {
$.data(this, 'stickyRows', StickyRows(this, options));
} else {
$.data(this, 'stickyRows').setRowSets().calculateDimensions().redraw('init');
}
}
});
}
if (val.length === 0) return this;
else if (val.length === 1) return val[0];
else return val;
};
// Initialization
function StickyRows(el, options) {
return new StickyRows.prototype.init(el, options);
}
$.StickyRows = StickyRows;
$.StickyRows.opts = {
container: 'body',
rows: 'thead',
scrollWidth: 'auto',
containersToSynchronize: false,
performanceDebugging: false
};
// Functionality
StickyRows.fn = $.StickyRows.prototype = {
//initialize
init: function(el, options) {
var self = this;
this.document = document;
this.window = window;
this.body = $('body');
this.uuid = uuid++;
this.table = {$element: $(el)};
// current settings
this.opts = $.extend(
{},
$.StickyRows.opts,
this.table.$element.data(),
options
);
if (this.opts.performanceDebugging) console.time("initialize stickyRows");
//calculate scrollBar width
this.opts.scrollWidth = this.opts.scrollWidth == 'auto' ? scrollbarWidth() : this.opts.scrollWidth;
this.container = {$element: this.table.$element.closest(this.opts.container)};
this.container.isBody = this.container.$element.is('body');
this.container.isBodyScrollable = false;
this.scroll = {'vertical': false, 'horizontal': false, mode: 'scrolling', shiftToRow: -1};
this.setRowSets();
this.setContainersToSynchronize();
this.calculateDimensions();
if (this.opts.performanceDebugging) console.timeEnd("initialize stickyRows");
//onScroll functionality
if (this.container.isBody) {
$(this.document).off('.stickyRowsRedraw'+uuid).on('scroll.stickyRowsRedraw'+uuid, $.proxy(this.redraw, this));
} else {
this.container.$element.off('.stickyRowsRedraw'+uuid).on('scroll.stickyRowsRedraw'+uuid, $.proxy(this.redraw, this));
}
//onResize window
$(this.window).off('.stickyRowsCalc'+uuid).on('resize.stickyRowsCalc'+uuid, $.proxy(this.calculateDimensions, this));
if (typeof ResizeSensor != 'undefined' && !this.container.$element.is('body')) {
//onResize container
new ResizeSensor(this.container.$element, $.proxy(this.calculateDimensions, this));
}
//onScroll elements to synchronize
if (this.containersToSynchronize) {
this.containersToSynchronize.each(function() {
if ($(this).is('body')) {
self.container.isBodyScrollable = true;
$(self.document).off('.stickyRowsCalc'+uuid).on('scroll.stickyRowsCalc'+uuid, $.proxy(self.calculateDimensions, self));
return false;
}
});
this.containersToSynchronize.not('body').off('.stickyRowsCalc'+uuid).on('scroll.stickyRowsCalc'+uuid, $.proxy(this.calculateDimensions, this));
}
this.redraw('init');
return this;
},
destroy: function() {
if (this.container.isBody) {
$(this.document).off('.stickyRowsRedraw'+uuid);
} else {
this.container.$element.off('.stickyRowsRedraw'+uuid);
}
$(this.window).off('.stickyRowsCalc'+uuid);
if (this.containersToSynchronize) {
this.containersToSynchronize.each(function() {
if ($(this).is('body')) {
$(self.document).off('.stickyRowsCalc'+uuid);
return false;
}
});
this.containersToSynchronize.off('.stickyRowsCalc'+uuid);
}
this.container.$element.children('div.resize-sensor').remove();
this.stickyHead.$element.remove();
this.body = null;
this.container = null;
this.containersToSynchronize = null;
this.document = null;
this.rowSets = null;
this.stickyHead = null;
this.stickyNow = null;
this.table = null;
this.window = null;
},
redraw: function(mode) {
var isForceRedraw = mode && mode == 'init';
if (this.blocked) return this;
if (this.opts.performanceDebugging) console.time("stickyRows redraw");
this.blocked = true;
//current container scroll
var containerScrollTop = this.container.$element.scrollTop();
var scrollContainerLeft = this.container.$element.scrollLeft();
this.scroll.vertical = false;
this.scroll.horizontal = false;
//is vertical scrolling
if (containerScrollTop != this.container.scroll.top || isForceRedraw) {
this.scroll.vertical = containerScrollTop > this.container.scroll.top ? 'down' : 'up';
this.container.scroll.top = containerScrollTop;
if (this.table.offset.top - this.container.offset.top > containerScrollTop || this.table.offset.top - this.container.offset.top + this.table.height < containerScrollTop) {
if (!this.stickyHead.$element.hasClass('hidden')) {
this.stickyNow = [];
this.stickyHead.$element.addClass('hidden');
this.stickyHead.$table.empty();
this.stickyHead.$tableForShifting.empty().addClass('hidden');
}
} else {
this.searchCurrentStickyRows();
this.renderStickyRows();
}
}
//is horizontal scrolling
if (scrollContainerLeft != this.container.scroll.left || isForceRedraw) {
this.scroll.horizontal = scrollContainerLeft > this.container.scroll.left ? 'right' : 'left';
this.container.scroll.left = scrollContainerLeft;
this.setHorizontalOffset();
}
this.blocked = false;
if (this.opts.performanceDebugging) console.timeEnd("stickyRows redraw");
return this;
},
//collect all sticky rows
setRowSets: function() {
var self = this;
var rows = self.opts.rows;
self.rowSets = [];
self.stickyNow = [];
if ($.isFunction(rows)) {
$.each(rows(), function(i, rowSet) {
self.rowSets.push($.map(rowSet.get().reverse(), function(row) {
return {$row: $(row)}
}));
});
} else if (rows instanceof jQuery) {
self.rowSets.push($.map(rows.get().reverse(), function(row) {
return {$row: $(row)}
}));
} else if (typeof rows === 'string') {
self.rowSets.push($.map(self.table.$element.children(rows).get().reverse(), function(row) {
return {$row: $(row)}
}));
} else if ($.isArray(rows)) {
$.each(rows, function(i, selector) {
self.rowSets.push($.map(self.table.$element.children(selector).get().reverse(), function(row) {
return {$row: $(row)}
}));
});
} else {
$.error('stickyRows.rows has incorrect format.');
}
return this;
},
//when this containers will scroll - sticky header will redraw
setContainersToSynchronize: function() {
var self = this;
if (self.opts.containersToSynchronize) {
var containersToSynchronize = self.opts.containersToSynchronize;
self.containersToSynchronize = [];
if ($.isFunction(containersToSynchronize)) {
self.containersToSynchronize = containersToSynchronize();
} else if (containersToSynchronize instanceof jQuery) {
self.containersToSynchronize = containersToSynchronize;
} else if (typeof containersToSynchronize === 'string') {
self.containersToSynchronize = $(containersToSynchronize);
} else if ($.isArray(containersToSynchronize)) {
self.containersToSynchronize = $(containersToSynchronize.join(','));
} else {
$.error('stickyRows.containersToSynchronize has incorrect format.');
}
}
return this;
},
//calculate width & offset of elements
calculateDimensions: function() {
var self = this;
var offset;
if (this.blocked) return this;
if (this.opts.performanceDebugging) console.time("stickyRows: calculate dimensions");
this.blocked = true;
//scrollable container
this.container.offset = this.container.$element.offset();
this.container.width = this.container.$element.outerWidth() - this.opts.scrollWidth;
this.container.scroll = {top: this.container.$element.scrollTop(), left: this.container.$element.scrollLeft()};
//table
this.table.width = this.table.$element.outerWidth();
this.table.height = this.table.$element.height();
this.table.offset = this.table.$element.offset();
//correction when calculate with container.scroll.top
if (!self.container.isBody) {
this.table.offset.top = this.table.offset.top + this.container.scroll.top;
}
//sticky rows
$.each(self.rowSets, function(i, rowSet) {
$.each(rowSet, function(j, rowObj) {
offset = rowObj.$row.offset();
rowObj.offset = {top: offset.top - self.container.offset.top + (self.container.isBody ? 0 : self.container.scroll.top), left: offset.left - self.container.offset.left};
rowObj.height = rowObj.$row.outerHeight();
});
});
//insert sticky wrapper into DOM
var bodyCorrection = {top: 0, left: 0};
if (this.container.isBodyScrollable) {
bodyCorrection = {top: this.body.scrollTop(), left: this.body.scrollLeft()};
}
if (!this.stickyHead) {
var $element = $('<div/>').addClass('sticky-header hidden').css({'top': this.container.offset.top - bodyCorrection.top, 'left': this.table.offset.left + this.container.scroll.left - bodyCorrection.left, 'width': this.container.width}).insertBefore(this.table.$element);
var $table = $('<table/>').addClass('sticky-table').addClass(this.table.$element.attr('class')).css({'width': this.table.width}).appendTo($element);
var $tableForShifting = $table.clone().addClass('sticky-table-for-shifting hidden').appendTo($element);
this.stickyHead = {$element: $element, $table: $table, $tableForShifting: $tableForShifting, height: 0, marginTop: 0, changed: false};
} else {
this.stickyHead.$element.css({'top': this.container.offset.top - bodyCorrection.top, 'left': this.table.offset.left + this.container.scroll.left - bodyCorrection.left, 'width': this.container.width});
this.stickyHead.$table.css({'width': this.table.width});
this.stickyHead.$tableForShifting.css({'width': this.table.width});
}
this.blocked = false;
if (this.opts.performanceDebugging) console.timeEnd("stickyRows: calculate dimensions");
return this;
},
//set
// this.stickyNow - rows, that currently sticky
// this.scroll.mode - scrolling/shifting
// this.stickyHead.changed - is stickyNow changed in compare with previous data
searchCurrentStickyRows: function() {
var self = this;
var upperBoundToScrolling = this.container.scroll.top;
var upperBoundToShifting = this.container.scroll.top + this.stickyHead.height;
var cnt;
var stickyNow = [];
if (self.scroll.vertical == 'up') {
self.scroll.mode = 'scrolling';
self.scroll.shiftToRow = -1;
}
$.each(this.rowSets, function(i, rowSet) {
//set upperBoundToScrolling with correction to priority
upperBoundToScrolling = self.container.scroll.top;
cnt = i;
while (cnt--) {
if (stickyNow[cnt]) {
upperBoundToScrolling += stickyNow[cnt].height;
}
}
$.each(rowSet, function(j, rowObj) {
if (!stickyNow.length || (stickyNow[i-1] && stickyNow[i-1].offset.top < rowObj.offset.top)) {
if (self.scroll.vertical == 'down' && rowObj.offset.top <= upperBoundToShifting && rowObj.offset.top >= upperBoundToScrolling) {
if (self.stickyNow[i] && self.stickyNow[i].$row != rowObj.$row && (self.scroll.mode == 'scrolling' || (self.scroll.shiftToRow == i && self.stickyNow[self.stickyNow.length - 1].$row != rowObj.$row))) {
if (self.scroll.mode == 'shifting') {
self.stickyNow.splice(i, 1);
}
self.stickyNow.push(rowObj);
self.scroll.mode = 'shifting';
self.scroll.shiftToRow = i;
}
return false;
} else if (rowObj.offset.top < upperBoundToScrolling) {
stickyNow.push(rowObj);
if (!self.stickyNow[i] || self.stickyNow[i].$row != rowObj.$row) {
self.stickyHead.changed = true;
self.scroll.mode = 'scrolling';
self.scroll.shiftToRow = -1;
}
return false;
}
}
});
});
if (this.stickyHead.changed || this.stickyNow.length != stickyNow.length) {
if (this.scroll.mode == 'scrolling') {
this.stickyNow = stickyNow;
}
this.stickyHead.changed = true;
}
},
//redraw sticky header
renderStickyRows: function() {
var self = this;
var $el;
var marginTop = 0;
var stickyHeadHeight = 0;
//if something changed
if (this.stickyHead.changed) {
this.stickyHead.$table.empty();
self.stickyHead.$tableForShifting.empty();
if (this.stickyNow.length) {
this.stickyHead.$element.removeClass('hidden');
$.each(this.stickyNow, function(i, el) {
stickyHeadHeight += el.height;
self.stickyHead.$table.append(self.getCloneOfRow(el));
});
} else {
this.stickyHead.$element.addClass('hidden');
}
if (self.scroll.mode == 'scrolling') {
//if 'scrolling' mode
self.stickyHead.$tableForShifting.addClass('hidden');
this.stickyHead.height = stickyHeadHeight;
} else {
//if 'shifting' mode
//fill and show 'tableForShifting'
if (self.scroll.shiftToRow) {
self.stickyHead.$tableForShifting.removeClass('hidden');
self.stickyHead.$table.children(':lt(' + self.scroll.shiftToRow + ')').clone().appendTo(self.stickyHead.$tableForShifting);
}
}
this.stickyHead.changed = false;
}
//set margin-top of table header in 'shifting' mode
if (this.scroll.mode == 'shifting') {
$el = this.stickyNow[this.stickyNow.length - 1];
marginTop = ($el.offset.top - this.container.scroll.top) - this.stickyHead.height;
}
//set margin-top of table header when table scrolled to the end
if (this.table.height + this.table.offset.top <= this.container.scroll.top + this.stickyHead.height && this.table.height + this.table.offset.top >= this.container.scroll.top) {
marginTop = (self.table.offset.top + self.table.height + 1 - this.container.scroll.top) - this.stickyHead.height;
}
//set margin-top once per method. this can happen in shifting mode or when table scrolled to the end
if (this.stickyHead.marginTop != marginTop) {
this.stickyHead.marginTop = marginTop;
this.stickyHead.$table.css({'margin-top': marginTop});
}
},
//called when scrolled horizontal
setHorizontalOffset: function() {
this.stickyHead.$table.css({'margin-left': -this.container.scroll.left});
this.stickyHead.$tableForShifting.css({'margin-left': -this.container.scroll.left});
},
getRowCells: function($row) {
//FIXME something wrong here
return $row.is('thead, tbody') ? $row.children().children() : $row.children();
},
//lazy implementation of $clone row
getCloneOfRow: function(rowObj) {
var $rowCloneTd;
if (!rowObj.$clone) {
rowObj.$clone = rowObj.$row.clone();
$rowCloneTd = this.getRowCells(rowObj.$clone);
$.each(this.getRowCells(rowObj.$row), function(i, cell) {
$rowCloneTd.eq(i).css('width', $(cell).width());
});
}
return rowObj.$clone;
}
};
StickyRows.prototype.init.prototype = StickyRows.prototype;
})(jQuery);