/
dataTables.scroller.js
1344 lines (1164 loc) · 38.4 KB
/
dataTables.scroller.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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*! Scroller 2.4.1
* © SpryMedia Ltd - datatables.net/license
*/
/**
* @summary Scroller
* @description Virtual rendering for DataTables
* @version 2.4.1
* @copyright SpryMedia Ltd.
*
* This source file is free software, available under the following license:
* MIT license - http://datatables.net/license/mit
*
* This source file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the license files for details.
*
* For details please refer to: http://www.datatables.net
*/
/**
* Scroller is a virtual rendering plug-in for DataTables which allows large
* datasets to be drawn on screen very quickly. What the virtual rendering means
* is that only the visible portion of the table (and a bit to either side to make
* the scrolling smooth) is drawn, while the scrolling container gives the
* visual impression that the whole table is visible. This is done by making use
* of the pagination abilities of DataTables and moving the table around in the
* scrolling container DataTables adds to the page. The scrolling container is
* forced to the height it would be for the full table display using an extra
* element.
*
* Note that rows in the table MUST all be the same height. Information in a cell
* which expands on to multiple lines will cause some odd behaviour in the scrolling.
*
* Scroller is initialised by simply including the letter 'S' in the sDom for the
* table you want to have this feature enabled on. Note that the 'S' must come
* AFTER the 't' parameter in `dom`.
*
* Key features include:
* <ul class="limit_length">
* <li>Speed! The aim of Scroller for DataTables is to make rendering large data sets fast</li>
* <li>Full compatibility with deferred rendering in DataTables for maximum speed</li>
* <li>Display millions of rows</li>
* <li>Integration with state saving in DataTables (scrolling position is saved)</li>
* <li>Easy to use</li>
* </ul>
*
* @class
* @constructor
* @global
* @param {object} dt DataTables settings object or API instance
* @param {object} [opts={}] Configuration object for Scroller. Options
* are defined by {@link Scroller.defaults}
*
* @requires jQuery 1.7+
* @requires DataTables 1.11.0+
*/
var Scroller = function (dt, opts) {
/* Sanity check - you just know it will happen */
if (!(this instanceof Scroller)) {
alert(
"Scroller warning: Scroller must be initialised with the 'new' keyword."
);
return;
}
if (opts === undefined) {
opts = {};
}
var dtApi = $.fn.dataTable.Api(dt);
/**
* Settings object which contains customisable information for the Scroller instance
* @namespace
* @private
* @extends Scroller.defaults
*/
this.s = {
/**
* DataTables settings object
* @type object
* @default Passed in as first parameter to constructor
*/
dt: dtApi.settings()[0],
/**
* DataTables API instance
* @type DataTable.Api
*/
dtApi: dtApi,
/**
* Pixel location of the top of the drawn table in the viewport
* @type int
* @default 0
*/
tableTop: 0,
/**
* Pixel location of the bottom of the drawn table in the viewport
* @type int
* @default 0
*/
tableBottom: 0,
/**
* Pixel location of the boundary for when the next data set should be loaded and drawn
* when scrolling up the way.
* @type int
* @default 0
* @private
*/
redrawTop: 0,
/**
* Pixel location of the boundary for when the next data set should be loaded and drawn
* when scrolling down the way. Note that this is actually calculated as the offset from
* the top.
* @type int
* @default 0
* @private
*/
redrawBottom: 0,
/**
* Auto row height or not indicator
* @type bool
* @default 0
*/
autoHeight: true,
/**
* Number of rows calculated as visible in the visible viewport
* @type int
* @default 0
*/
viewportRows: 0,
/**
* setTimeout reference for state saving, used when state saving is enabled in the DataTable
* and when the user scrolls the viewport in order to stop the cookie set taking too much
* CPU!
* @type int
* @default 0
*/
stateTO: null,
stateSaveThrottle: function () {},
/**
* setTimeout reference for the redraw, used when server-side processing is enabled in the
* DataTables in order to prevent DoSing the server
* @type int
* @default null
*/
drawTO: null,
heights: {
jump: null,
page: null,
virtual: null,
scroll: null,
/**
* Height of rows in the table
* @type int
* @default 0
*/
row: null,
/**
* Pixel height of the viewport
* @type int
* @default 0
*/
viewport: null,
labelHeight: 0,
xbar: 0
},
topRowFloat: 0,
scrollDrawDiff: null,
loaderVisible: false,
forceReposition: false,
baseRowTop: 0,
baseScrollTop: 0,
mousedown: false,
lastScrollTop: 0
};
// @todo The defaults should extend a `c` property and the internal settings
// only held in the `s` property. At the moment they are mixed
this.s = $.extend(this.s, Scroller.oDefaults, opts);
// Workaround for row height being read from height object (see above comment)
this.s.heights.row = this.s.rowHeight;
/**
* DOM elements used by the class instance
* @private
* @namespace
*
*/
this.dom = {
force: document.createElement('div'),
label: $('<div class="dts_label">0</div>'),
scroller: null,
table: null,
loader: null
};
// Attach the instance to the DataTables instance so it can be accessed in
// future. Don't initialise Scroller twice on the same table
if (this.s.dt.oScroller) {
return;
}
this.s.dt.oScroller = this;
/* Let's do it */
this.construct();
};
$.extend(Scroller.prototype, {
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Public methods - to be exposed via the DataTables API
*/
/**
* Calculate and store information about how many rows are to be displayed
* in the scrolling viewport, based on current dimensions in the browser's
* rendering. This can be particularly useful if the table is initially
* drawn in a hidden element - for example in a tab.
* @param {bool} [redraw=true] Redraw the table automatically after the recalculation, with
* the new dimensions forming the basis for the draw.
* @returns {void}
*/
measure: function (redraw) {
if (this.s.autoHeight) {
this._calcRowHeight();
}
var heights = this.s.heights;
if (heights.row) {
heights.viewport = this._parseHeight(
$(this.dom.scroller).css('max-height')
);
this.s.viewportRows =
parseInt(heights.viewport / heights.row, 10) + 1;
this.s.dt._iDisplayLength =
this.s.viewportRows * this.s.displayBuffer;
}
var label = this.dom.label.outerHeight();
heights.xbar =
this.dom.scroller.offsetHeight - this.dom.scroller.clientHeight;
heights.labelHeight = label;
if (redraw === undefined || redraw) {
this.s.dtApi.draw(false);
}
},
/**
* Get information about current displayed record range. This corresponds to
* the information usually displayed in the "Info" block of the table.
*
* @returns {object} info as an object:
* {
* start: {int}, // the 0-indexed record at the top of the viewport
* end: {int}, // the 0-indexed record at the bottom of the viewport
* }
*/
pageInfo: function () {
var dt = this.s.dt,
iScrollTop = this.dom.scroller.scrollTop,
iTotal = dt.fnRecordsDisplay(),
iPossibleEnd = Math.ceil(
this.pixelsToRow(
iScrollTop + this.s.heights.viewport,
false,
this.s.ani
)
);
return {
start: Math.floor(this.pixelsToRow(iScrollTop, false, this.s.ani)),
end: iTotal < iPossibleEnd ? iTotal - 1 : iPossibleEnd - 1
};
},
/**
* Calculate the row number that will be found at the given pixel position
* (y-scroll).
*
* Please note that when the height of the full table exceeds 1 million
* pixels, Scroller switches into a non-linear mode for the scrollbar to fit
* all of the records into a finite area, but this function returns a linear
* value (relative to the last non-linear positioning).
* @param {int} pixels Offset from top to calculate the row number of
* @param {int} [intParse=true] If an integer value should be returned
* @param {int} [virtual=false] Perform the calculations in the virtual domain
* @returns {int} Row index
*/
pixelsToRow: function (pixels, intParse, virtual) {
var diff = pixels - this.s.baseScrollTop;
var row = virtual
? (this._domain('physicalToVirtual', this.s.baseScrollTop) + diff) /
this.s.heights.row
: diff / this.s.heights.row + this.s.baseRowTop;
return intParse || intParse === undefined ? parseInt(row, 10) : row;
},
/**
* Calculate the pixel position from the top of the scrolling container for
* a given row
* @param {int} iRow Row number to calculate the position of
* @returns {int} Pixels
*/
rowToPixels: function (rowIdx, intParse, virtual) {
var pixels;
var diff = rowIdx - this.s.baseRowTop;
if (virtual) {
pixels = this._domain('virtualToPhysical', this.s.baseScrollTop);
pixels += diff * this.s.heights.row;
}
else {
pixels = this.s.baseScrollTop;
pixels += diff * this.s.heights.row;
}
return intParse || intParse === undefined
? parseInt(pixels, 10)
: pixels;
},
/**
* Calculate the row number that will be found at the given pixel position (y-scroll)
* @param {int} row Row index to scroll to
* @param {bool} [animate=true] Animate the transition or not
* @returns {void}
*/
scrollToRow: function (row, animate) {
var that = this;
var ani = false;
var px = this.rowToPixels(row);
// We need to know if the table will redraw or not before doing the
// scroll. If it will not redraw, then we need to use the currently
// displayed table, and scroll with the physical pixels. Otherwise, we
// need to calculate the table's new position from the virtual
// transform.
var preRows = ((this.s.displayBuffer - 1) / 2) * this.s.viewportRows;
var drawRow = row - preRows;
if (drawRow < 0) {
drawRow = 0;
}
if (
(px > this.s.redrawBottom || px < this.s.redrawTop) &&
this.s.dt._iDisplayStart !== drawRow
) {
ani = true;
px = this._domain('virtualToPhysical', row * this.s.heights.row);
// If we need records outside the current draw region, but the new
// scrolling position is inside that (due to the non-linear nature
// for larger numbers of records), we need to force position update.
if (this.s.redrawTop < px && px < this.s.redrawBottom) {
this.s.forceReposition = true;
animate = false;
}
}
if (animate === undefined || animate) {
this.s.ani = ani;
$(this.dom.scroller).animate(
{
scrollTop: px
},
function () {
// This needs to happen after the animation has completed and
// the final scroll event fired
setTimeout(function () {
that.s.ani = false;
}, 250);
}
);
}
else {
$(this.dom.scroller).scrollTop(px);
}
},
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Constructor
*/
/**
* Initialisation for Scroller
* @returns {void}
* @private
*/
construct: function () {
var that = this;
var dt = this.s.dtApi;
/* Sanity check */
if (!this.s.dt.oFeatures.bPaginate) {
throw new Error(
'Pagination must be enabled for Scroller to operate'
);
}
/* Insert a div element that we can use to force the DT scrolling container to
* the height that would be required if the whole table was being displayed
*/
this.dom.force.style.position = 'relative';
this.dom.force.style.top = '0px';
this.dom.force.style.left = '0px';
this.dom.force.style.width = '1px';
this.dom.scroller = dt.table().node().parentNode;
this.dom.scroller.appendChild(this.dom.force);
this.dom.scroller.style.position = 'relative';
this.dom.table = $('>table', this.dom.scroller)[0];
this.dom.table.style.position = 'absolute';
this.dom.table.style.top = '0px';
this.dom.table.style.left = '0px';
// Add class to 'announce' that we are a Scroller table
$(dt.table().container()).addClass('dts DTS');
this.dom.label.appendTo(this.dom.scroller);
/* Initial size calculations */
if (this.s.heights.row && this.s.heights.row != 'auto') {
this.s.autoHeight = false;
}
// Scrolling callback to see if a page change is needed
this.s.ingnoreScroll = true;
$(this.dom.scroller).on('scroll.dt-scroller', function (e) {
that._scroll.call(that);
});
// In iOS we catch the touchstart event in case the user tries to scroll
// while the display is already scrolling
$(this.dom.scroller).on('touchstart.dt-scroller', function () {
that._scroll.call(that);
});
$(this.dom.scroller)
.on('mousedown.dt-scroller', function () {
that.s.mousedown = true;
})
.on('mouseup.dt-scroller', function () {
that.s.labelVisible = false;
that.s.mousedown = false;
that.dom.label.css('display', 'none');
});
// On resize, update the information element, since the number of rows shown might change
$(window).on('resize.dt-scroller', function () {
that.measure(false);
that._info();
});
// Add a state saving parameter to the DT state saving so we can restore the exact
// position of the scrolling.
var initialStateSave = true;
var loadedState = dt.state.loaded();
dt.on('stateSaveParams.scroller', function (e, settings, data) {
if (initialStateSave && loadedState) {
data.scroller = loadedState.scroller;
initialStateSave = false;
if (data.scroller) {
that.s.lastScrollTop = data.scroller.scrollTop;
}
}
else {
// Need to used the saved position on init
data.scroller = {
topRow: that.s.topRowFloat,
baseRowTop: that.s.baseRowTop
};
}
});
dt.on('stateLoadParams.scroller', function (e, settings, data) {
if (data.scroller !== undefined) {
that.scrollToRow(data.scroller.topRow);
}
});
this.measure(false);
if (loadedState && loadedState.scroller) {
this.s.topRowFloat = loadedState.scroller.topRow;
this.s.baseRowTop = loadedState.scroller.baseRowTop;
// Reconstruct the scroll positions from the rows - it is possible the
// row height has changed e.g. if the styling framework has changed.
// The scroll top is used in `_draw` further down.
this.s.baseScrollTop = this.s.baseRowTop * this.s.heights.row;
loadedState.scroller.scrollTop = this._domain('physicalToVirtual', this.s.topRowFloat * this.s.heights.row);
}
that.s.stateSaveThrottle = DataTable.util.throttle(function () {
that.s.dtApi.state.save();
}, 500);
dt.on('init.scroller', function () {
that.measure(false);
// Setting to `jump` will instruct _draw to calculate the scroll top
// position
that.s.scrollType = 'jump';
that._draw();
// Update the scroller when the DataTable is redrawn
dt.on('draw.scroller', function () {
that._draw();
});
});
// Set height before the draw happens, allowing everything else to update
// on draw complete without worry for roder.
dt.on('preDraw.dt.scroller', function () {
that._scrollForce();
});
// Destructor
dt.on('destroy.scroller', function () {
$(window).off('resize.dt-scroller');
$(that.dom.scroller).off('.dt-scroller');
$(that.s.dt.nTable).off('.scroller');
$(that.s.dt.nTableWrapper).removeClass('DTS');
$('div.DTS_Loading', that.dom.scroller.parentNode).remove();
that.dom.table.style.position = '';
that.dom.table.style.top = '';
that.dom.table.style.left = '';
});
},
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Private methods
*/
/**
* Automatic calculation of table row height. This is just a little tricky here as using
* initialisation DataTables has tale the table out of the document, so we need to create
* a new table and insert it into the document, calculate the row height and then whip the
* table out.
* @returns {void}
* @private
*/
_calcRowHeight: function () {
var dt = this.s.dt;
var origTable = dt.nTable;
var nTable = origTable.cloneNode(false);
var tbody = $('<tbody/>').appendTo(nTable);
var dtClasses = dt.oClasses;
// Different locations for classes in DT2
var classes = DataTable.versionCheck('2')
? {
container: dtClasses.container,
scroller: dtClasses.scrolling.container,
body: dtClasses.scrolling.body
}
: {
container: dtClasses.sWrapper,
scroller: dtClasses.sScrollWrapper,
body: dtClasses.sScrollBody
};
var container = $(
'<div class="' +
classes.container +
' DTS"><div class="' +
classes.scroller +
'"><div class="' +
classes.body +
'"></div></div></div>'
);
// Want 3 rows in the sizing table so :first-child and :last-child
// CSS styles don't come into play - take the size of the middle row
$('tbody tr:lt(4)', origTable).clone().appendTo(tbody);
var rowsCount = $('tr', tbody).length;
if (rowsCount === 1) {
tbody.prepend('<tr><td> </td></tr>');
tbody.append('<tr><td> </td></tr>');
}
else {
for (; rowsCount < 3; rowsCount++) {
tbody.append('<tr><td> </td></tr>');
}
}
$('div.' + classes.body, container).append(nTable);
// If initialised using `dom`, use the holding element as the insert point
var insertEl = this.s.dt.nHolding || origTable.parentNode;
if (!$(insertEl).is(':visible')) {
insertEl = 'body';
}
// Remove form element links as they might select over others (particularly radio and checkboxes)
container.find('input').removeAttr('name');
container.appendTo(insertEl);
this.s.heights.row = $('tr', tbody).eq(1).outerHeight();
container.remove();
},
/**
* Draw callback function which is fired when the DataTable is redrawn. The main function of
* this method is to position the drawn table correctly the scrolling container for the rows
* that is displays as a result of the scrolling position.
* @returns {void}
* @private
*/
_draw: function () {
var that = this,
heights = this.s.heights,
iScrollTop = this.dom.scroller.scrollTop,
iTableHeight = $(this.s.dt.nTable).height(),
displayStart = this.s.dt._iDisplayStart,
displayLen = this.s.dt._iDisplayLength,
displayEnd = this.s.dt.fnRecordsDisplay(),
viewportEndY = iScrollTop + heights.viewport;
// Disable the scroll event listener while we are updating the DOM
this.s.skip = true;
// If paging is reset
if (
(this.s.dt.bSorted || this.s.dt.bFiltered) &&
displayStart === 0 &&
!this.s.dt._drawHold
) {
this.s.topRowFloat = 0;
}
iScrollTop =
this.s.scrollType === 'jump'
? this._domain(
'virtualToPhysical',
this.s.topRowFloat * heights.row
)
: iScrollTop;
// Store positional information so positional calculations can be based
// upon the current table draw position
this.s.baseScrollTop = iScrollTop;
this.s.baseRowTop = this.s.topRowFloat;
// Position the table in the virtual scroller
var tableTop =
iScrollTop - (this.s.topRowFloat - displayStart) * heights.row;
if (displayStart === 0) {
tableTop = 0;
}
else if (displayStart + displayLen >= displayEnd) {
tableTop = heights.scroll - iTableHeight;
}
else {
var iTableBottomY = tableTop + iTableHeight;
if (iTableBottomY < viewportEndY) {
// The last row of the data is above the end of the viewport.
// This means the background is visible, which is not what the user expects.
var newTableTop = viewportEndY - iTableHeight;
var diffPx = newTableTop - tableTop;
this.s.baseScrollTop += diffPx + 1; // Update start row number in footer.
tableTop = newTableTop; // Move table so last line of data is at the bottom of the viewport.
}
}
this.dom.table.style.top = tableTop + 'px';
/* Cache some information for the scroller */
this.s.tableTop = tableTop;
this.s.tableBottom = iTableHeight + this.s.tableTop;
// Calculate the boundaries for where a redraw will be triggered by the
// scroll event listener
var boundaryPx = (iScrollTop - this.s.tableTop) * this.s.boundaryScale;
this.s.redrawTop = iScrollTop - boundaryPx;
this.s.redrawBottom =
iScrollTop + boundaryPx >
heights.scroll - heights.viewport - heights.row
? heights.scroll - heights.viewport - heights.row
: iScrollTop + boundaryPx;
this.s.skip = false;
if (that.s.ingnoreScroll) {
// Restore the scrolling position that was saved by DataTable's state
// saving Note that this is done on the second draw when data is Ajax
// sourced, and the first draw when DOM soured
if (
this.s.dt.oFeatures.bStateSave &&
this.s.dt.oLoadedState !== null &&
typeof this.s.dt.oLoadedState.scroller != 'undefined'
) {
// A quirk of DataTables is that the draw callback will occur on an
// empty set if Ajax sourced, but not if server-side processing.
var ajaxSourced =
(this.s.dt.sAjaxSource || that.s.dt.ajax) &&
!this.s.dt.oFeatures.bServerSide
? true
: false;
if (
(ajaxSourced && this.s.dt.iDraw >= 2) ||
(!ajaxSourced && this.s.dt.iDraw >= 1)
) {
setTimeout(function () {
$(that.dom.scroller).scrollTop(
that.s.dt.oLoadedState.scroller.scrollTop
);
// In order to prevent layout thrashing we need another
// small delay
setTimeout(function () {
that.s.ingnoreScroll = false;
}, 0);
}, 0);
}
}
else {
that.s.ingnoreScroll = false;
}
}
// Because of the order of the DT callbacks, the info update will
// take precedence over the one we want here. So a 'thread' break is
// needed. Only add the thread break if bInfo is set
if (this.s.dt.oFeatures.bInfo) {
setTimeout(function () {
that._info.call(that);
}, 0);
}
$(this.s.dt.nTable).triggerHandler('position.dts.dt', tableTop);
},
/**
* Convert from one domain to another. The physical domain is the actual
* pixel count on the screen, while the virtual is if we had browsers which
* had scrolling containers of infinite height (i.e. the absolute value)
*
* @param {string} dir Domain transform direction, `virtualToPhysical` or
* `physicalToVirtual`
* @returns {number} Calculated transform
* @private
*/
_domain: function (dir, val) {
var heights = this.s.heights;
var diff;
var magic = 10000; // the point at which the non-linear calculations start to happen
// If the virtual and physical height match, then we use a linear
// transform between the two, allowing the scrollbar to be linear
if (heights.virtual === heights.scroll) {
return val;
}
// In the first 10k pixels and the last 10k pixels, we want the scrolling
// to be linear. After that it can be non-linear. It would be unusual for
// anyone to mouse wheel through that much.
if (val < magic) {
return val;
}
else if (
dir === 'virtualToPhysical' &&
val >= heights.virtual - magic
) {
diff = heights.virtual - val;
return heights.scroll - diff;
}
else if (dir === 'physicalToVirtual' && val >= heights.scroll - magic) {
diff = heights.scroll - val;
return heights.virtual - diff;
}
// Otherwise, we want a non-linear scrollbar to take account of the
// redrawing regions at the start and end of the table, otherwise these
// can stutter badly - on large tables 30px (for example) scroll might
// be hundreds of rows, so the table would be redrawing every few px at
// the start and end. Use a simple linear eq. to stop this, effectively
// causing a kink in the scrolling ratio. It does mean the scrollbar is
// non-linear, but with such massive data sets, the scrollbar is going
// to be a best guess anyway
var m =
(heights.virtual - magic - magic) /
(heights.scroll - magic - magic);
var c = magic - m * magic;
return dir === 'virtualToPhysical' ? (val - c) / m : m * val + c;
},
/**
* Update any information elements that are controlled by the DataTable based on the scrolling
* viewport and what rows are visible in it. This function basically acts in the same way as
* _fnUpdateInfo in DataTables, and effectively replaces that function.
* @returns {void}
* @private
*/
_info: function () {
if (!this.s.dt.oFeatures.bInfo) {
return;
}
var dt = this.s.dt,
dtApi = this.s.dtApi,
language = dt.oLanguage,
info = dtApi.page.info(),
total = info.recordsDisplay,
max = info.recordsTotal;
// If the scroll type is `cont` (continuous) we need to use `baseRowTop`, which
// also means we need to work out the difference between the current scroll position
// and the "base" for when it was required
var diffRows = (this.s.lastScrollTop - this.s.baseScrollTop) / this.s.heights.row;
var start = Math.floor(this.s.baseRowTop + diffRows) + 1;
// For a jump scroll type, we just use the straightforward calculation based on
// `topRowFloat`
if (this.s.scrollType === 'jump') {
start = Math.floor(this.s.topRowFloat) + 1;
}
var
possibleEnd = start + Math.floor(this.s.heights.viewport / this.s.heights.row),
end = possibleEnd > total ? total : possibleEnd,
result;
if (total === 0 && total == max) {
/* Empty record set */
result = language.sInfoEmpty + language.sInfoPostFix;
}
else if (total === 0) {
// Empty record set after filtering
result =
language.sInfoEmpty +
' ' +
language.sInfoFiltered +
language.sInfoPostFix;
}
else if (total == max) {
// Normal record set
result = language.sInfo + language.sInfoPostFix;
}
else {
// Record set after filtering
result = language.sInfo + ' ' + language.sInfoFiltered + language.sInfoPostFix;
}
result = this._macros(result, start, end, max, total);
var callback = language.fnInfoCallback;
if (callback) {
result = callback.call(
dt.oInstance,
dt,
start,
end,
max,
total,
result
);
}
// DT 1.x features
var n = dt.aanFeatures.i;
if (typeof n != 'undefined') {
for (var i = 0, iLen = n.length; i < iLen; i++) {
$(n[i]).html(result);
}
$(dt.nTable).triggerHandler('info.dt');
}
// DT 2.x features
$('div.dt-info', dtApi.table().container()).each(function () {
$(this).html(result);
dtApi.trigger('info', [dtApi.settings()[0], this, result]);
});
},
/**
* String replacement for info display. Basically the same as what DataTables does.
*
* @param {*} str
* @param {*} start
* @param {*} end
* @param {*} max
* @param {*} total
* @returns Formatted string
*/
_macros: function (str, start, end, max, total) {
var api = this.s.dtApi;
var settings = this.s.dt;
var formatter = settings.fnFormatNumber;
return str
.replace(/_START_/g, formatter.call(settings, start))
.replace(/_END_/g, formatter.call(settings, end))
.replace(/_MAX_/g, formatter.call(settings, max))
.replace(/_TOTAL_/g, formatter.call(settings, total))
.replace(/_ENTRIES_/g, api.i18n('entries', ''))
.replace(/_ENTRIES-MAX_/g, api.i18n('entries', '', max))
.replace(/_ENTRIES-TOTAL_/g, api.i18n('entries', '', total));
},
/**
* Parse CSS height property string as number
*
* An attempt is made to parse the string as a number. Currently supported units are 'px',
* 'vh', and 'rem'. 'em' is partially supported; it works as long as the parent element's
* font size matches the body element. Zero is returned for unrecognized strings.
* @param {string} cssHeight CSS height property string
* @returns {number} height
* @private
*/
_parseHeight: function (cssHeight) {
var height;
var matches = /^([+-]?(?:\d+(?:\.\d+)?|\.\d+))(px|em|rem|vh)$/.exec(
cssHeight
);
if (matches === null) {
return 0;
}
var value = parseFloat(matches[1]);
var unit = matches[2];
if (unit === 'px') {
height = value;
}
else if (unit === 'vh') {
height = (value / 100) * $(window).height();
}
else if (unit === 'rem') {
height = value * parseFloat($(':root').css('font-size'));
}
else if (unit === 'em') {
height = value * parseFloat($('body').css('font-size'));
}
return height ? height : 0;
},
/**
* Scrolling function - fired whenever the scrolling position is changed.
* This method needs to use the stored values to see if the table should be
* redrawn as we are moving towards the end of the information that is
* currently drawn or not. If needed, then it will redraw the table based on
* the new position.
* @returns {void}
* @private
*/
_scroll: function () {
var that = this,
heights = this.s.heights,
iScrollTop = this.dom.scroller.scrollTop,
iTopRow;
if (this.s.skip) {
return;
}
if (this.s.ingnoreScroll) {
return;
}
if (iScrollTop === this.s.lastScrollTop) {
return;
}
/* If the table has been sorted or filtered, then we use the redraw that
* DataTables as done, rather than performing our own