source: public/vendors/dataTable/Scroller-2.0.1/js/dataTables.scroller.js@ 4952f8e

develop
Last change on this file since 4952f8e was 7304c7f, checked in by beratkjufliju <kufliju@…>, 3 years ago

added user authentication, create & forgot password methods and blades

  • Property mode set to 100644
File size: 37.9 KB
RevLine 
[7304c7f]1/*! Scroller 2.0.1
2 * ©2011-2019 SpryMedia Ltd - datatables.net/license
3 */
4
5/**
6 * @summary Scroller
7 * @description Virtual rendering for DataTables
8 * @version 2.0.1
9 * @file dataTables.scroller.js
10 * @author SpryMedia Ltd (www.sprymedia.co.uk)
11 * @contact www.sprymedia.co.uk/contact
12 * @copyright Copyright 2011-2019 SpryMedia Ltd.
13 *
14 * This source file is free software, available under the following license:
15 * MIT license - http://datatables.net/license/mit
16 *
17 * This source file is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the license files for details.
20 *
21 * For details please refer to: http://www.datatables.net
22 */
23
24(function( factory ){
25 if ( typeof define === 'function' && define.amd ) {
26 // AMD
27 define( ['jquery', 'datatables.net'], function ( $ ) {
28 return factory( $, window, document );
29 } );
30 }
31 else if ( typeof exports === 'object' ) {
32 // CommonJS
33 module.exports = function (root, $) {
34 if ( ! root ) {
35 root = window;
36 }
37
38 if ( ! $ || ! $.fn.dataTable ) {
39 $ = require('datatables.net')(root, $).$;
40 }
41
42 return factory( $, root, root.document );
43 };
44 }
45 else {
46 // Browser
47 factory( jQuery, window, document );
48 }
49}(function( $, window, document, undefined ) {
50'use strict';
51var DataTable = $.fn.dataTable;
52
53
54/**
55 * Scroller is a virtual rendering plug-in for DataTables which allows large
56 * datasets to be drawn on screen every quickly. What the virtual rendering means
57 * is that only the visible portion of the table (and a bit to either side to make
58 * the scrolling smooth) is drawn, while the scrolling container gives the
59 * visual impression that the whole table is visible. This is done by making use
60 * of the pagination abilities of DataTables and moving the table around in the
61 * scrolling container DataTables adds to the page. The scrolling container is
62 * forced to the height it would be for the full table display using an extra
63 * element.
64 *
65 * Note that rows in the table MUST all be the same height. Information in a cell
66 * which expands on to multiple lines will cause some odd behaviour in the scrolling.
67 *
68 * Scroller is initialised by simply including the letter 'S' in the sDom for the
69 * table you want to have this feature enabled on. Note that the 'S' must come
70 * AFTER the 't' parameter in `dom`.
71 *
72 * Key features include:
73 * <ul class="limit_length">
74 * <li>Speed! The aim of Scroller for DataTables is to make rendering large data sets fast</li>
75 * <li>Full compatibility with deferred rendering in DataTables for maximum speed</li>
76 * <li>Display millions of rows</li>
77 * <li>Integration with state saving in DataTables (scrolling position is saved)</li>
78 * <li>Easy to use</li>
79 * </ul>
80 *
81 * @class
82 * @constructor
83 * @global
84 * @param {object} dt DataTables settings object or API instance
85 * @param {object} [opts={}] Configuration object for FixedColumns. Options
86 * are defined by {@link Scroller.defaults}
87 *
88 * @requires jQuery 1.7+
89 * @requires DataTables 1.10.0+
90 *
91 * @example
92 * $(document).ready(function() {
93 * $('#example').DataTable( {
94 * "scrollY": "200px",
95 * "ajax": "media/dataset/large.txt",
96 * "scroller": true,
97 * "deferRender": true
98 * } );
99 * } );
100 */
101var Scroller = function ( dt, opts ) {
102 /* Sanity check - you just know it will happen */
103 if ( ! (this instanceof Scroller) ) {
104 alert( "Scroller warning: Scroller must be initialised with the 'new' keyword." );
105 return;
106 }
107
108 if ( opts === undefined ) {
109 opts = {};
110 }
111
112 var dtApi = $.fn.dataTable.Api( dt );
113
114 /**
115 * Settings object which contains customisable information for the Scroller instance
116 * @namespace
117 * @private
118 * @extends Scroller.defaults
119 */
120 this.s = {
121 /**
122 * DataTables settings object
123 * @type object
124 * @default Passed in as first parameter to constructor
125 */
126 dt: dtApi.settings()[0],
127
128 /**
129 * DataTables API instance
130 * @type DataTable.Api
131 */
132 dtApi: dtApi,
133
134 /**
135 * Pixel location of the top of the drawn table in the viewport
136 * @type int
137 * @default 0
138 */
139 tableTop: 0,
140
141 /**
142 * Pixel location of the bottom of the drawn table in the viewport
143 * @type int
144 * @default 0
145 */
146 tableBottom: 0,
147
148 /**
149 * Pixel location of the boundary for when the next data set should be loaded and drawn
150 * when scrolling up the way.
151 * @type int
152 * @default 0
153 * @private
154 */
155 redrawTop: 0,
156
157 /**
158 * Pixel location of the boundary for when the next data set should be loaded and drawn
159 * when scrolling down the way. Note that this is actually calculated as the offset from
160 * the top.
161 * @type int
162 * @default 0
163 * @private
164 */
165 redrawBottom: 0,
166
167 /**
168 * Auto row height or not indicator
169 * @type bool
170 * @default 0
171 */
172 autoHeight: true,
173
174 /**
175 * Number of rows calculated as visible in the visible viewport
176 * @type int
177 * @default 0
178 */
179 viewportRows: 0,
180
181 /**
182 * setTimeout reference for state saving, used when state saving is enabled in the DataTable
183 * and when the user scrolls the viewport in order to stop the cookie set taking too much
184 * CPU!
185 * @type int
186 * @default 0
187 */
188 stateTO: null,
189
190 /**
191 * setTimeout reference for the redraw, used when server-side processing is enabled in the
192 * DataTables in order to prevent DoSing the server
193 * @type int
194 * @default null
195 */
196 drawTO: null,
197
198 heights: {
199 jump: null,
200 page: null,
201 virtual: null,
202 scroll: null,
203
204 /**
205 * Height of rows in the table
206 * @type int
207 * @default 0
208 */
209 row: null,
210
211 /**
212 * Pixel height of the viewport
213 * @type int
214 * @default 0
215 */
216 viewport: null,
217 labelFactor: 1
218 },
219
220 topRowFloat: 0,
221 scrollDrawDiff: null,
222 loaderVisible: false,
223 forceReposition: false,
224 baseRowTop: 0,
225 baseScrollTop: 0,
226 mousedown: false,
227 lastScrollTop: 0
228 };
229
230 // @todo The defaults should extend a `c` property and the internal settings
231 // only held in the `s` property. At the moment they are mixed
232 this.s = $.extend( this.s, Scroller.oDefaults, opts );
233
234 // Workaround for row height being read from height object (see above comment)
235 this.s.heights.row = this.s.rowHeight;
236
237 /**
238 * DOM elements used by the class instance
239 * @private
240 * @namespace
241 *
242 */
243 this.dom = {
244 "force": document.createElement('div'),
245 "label": $('<div class="dts_label">0</div>'),
246 "scroller": null,
247 "table": null,
248 "loader": null
249 };
250
251 // Attach the instance to the DataTables instance so it can be accessed in
252 // future. Don't initialise Scroller twice on the same table
253 if ( this.s.dt.oScroller ) {
254 return;
255 }
256
257 this.s.dt.oScroller = this;
258
259 /* Let's do it */
260 this.construct();
261};
262
263
264
265$.extend( Scroller.prototype, {
266 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
267 * Public methods - to be exposed via the DataTables API
268 */
269
270 /**
271 * Calculate and store information about how many rows are to be displayed
272 * in the scrolling viewport, based on current dimensions in the browser's
273 * rendering. This can be particularly useful if the table is initially
274 * drawn in a hidden element - for example in a tab.
275 * @param {bool} [redraw=true] Redraw the table automatically after the recalculation, with
276 * the new dimensions forming the basis for the draw.
277 * @returns {void}
278 */
279 measure: function ( redraw )
280 {
281 if ( this.s.autoHeight )
282 {
283 this._calcRowHeight();
284 }
285
286 var heights = this.s.heights;
287
288 if ( heights.row ) {
289 heights.viewport = $.contains(document, this.dom.scroller) ?
290 this.dom.scroller.clientHeight :
291 this._parseHeight($(this.dom.scroller).css('height'));
292
293 // If collapsed (no height) use the max-height parameter
294 if ( ! heights.viewport ) {
295 heights.viewport = this._parseHeight($(this.dom.scroller).css('max-height'));
296 }
297
298 this.s.viewportRows = parseInt( heights.viewport / heights.row, 10 )+1;
299 this.s.dt._iDisplayLength = this.s.viewportRows * this.s.displayBuffer;
300 }
301
302 var label = this.dom.label.outerHeight();
303 heights.labelFactor = (heights.viewport-label) / heights.scroll;
304
305 if ( redraw === undefined || redraw )
306 {
307 this.s.dt.oInstance.fnDraw( false );
308 }
309 },
310
311 /**
312 * Get information about current displayed record range. This corresponds to
313 * the information usually displayed in the "Info" block of the table.
314 *
315 * @returns {object} info as an object:
316 * {
317 * start: {int}, // the 0-indexed record at the top of the viewport
318 * end: {int}, // the 0-indexed record at the bottom of the viewport
319 * }
320 */
321 pageInfo: function()
322 {
323 var
324 dt = this.s.dt,
325 iScrollTop = this.dom.scroller.scrollTop,
326 iTotal = dt.fnRecordsDisplay(),
327 iPossibleEnd = Math.ceil(this.pixelsToRow(iScrollTop + this.s.heights.viewport, false, this.s.ani));
328
329 return {
330 start: Math.floor(this.pixelsToRow(iScrollTop, false, this.s.ani)),
331 end: iTotal < iPossibleEnd ? iTotal-1 : iPossibleEnd-1
332 };
333 },
334
335 /**
336 * Calculate the row number that will be found at the given pixel position
337 * (y-scroll).
338 *
339 * Please note that when the height of the full table exceeds 1 million
340 * pixels, Scroller switches into a non-linear mode for the scrollbar to fit
341 * all of the records into a finite area, but this function returns a linear
342 * value (relative to the last non-linear positioning).
343 * @param {int} pixels Offset from top to calculate the row number of
344 * @param {int} [intParse=true] If an integer value should be returned
345 * @param {int} [virtual=false] Perform the calculations in the virtual domain
346 * @returns {int} Row index
347 */
348 pixelsToRow: function ( pixels, intParse, virtual )
349 {
350 var diff = pixels - this.s.baseScrollTop;
351 var row = virtual ?
352 (this._domain( 'physicalToVirtual', this.s.baseScrollTop ) + diff) / this.s.heights.row :
353 ( diff / this.s.heights.row ) + this.s.baseRowTop;
354
355 return intParse || intParse === undefined ?
356 parseInt( row, 10 ) :
357 row;
358 },
359
360 /**
361 * Calculate the pixel position from the top of the scrolling container for
362 * a given row
363 * @param {int} iRow Row number to calculate the position of
364 * @returns {int} Pixels
365 */
366 rowToPixels: function ( rowIdx, intParse, virtual )
367 {
368 var pixels;
369 var diff = rowIdx - this.s.baseRowTop;
370
371 if ( virtual ) {
372 pixels = this._domain( 'virtualToPhysical', this.s.baseScrollTop );
373 pixels += diff * this.s.heights.row;
374 }
375 else {
376 pixels = this.s.baseScrollTop;
377 pixels += diff * this.s.heights.row;
378 }
379
380 return intParse || intParse === undefined ?
381 parseInt( pixels, 10 ) :
382 pixels;
383 },
384
385
386 /**
387 * Calculate the row number that will be found at the given pixel position (y-scroll)
388 * @param {int} row Row index to scroll to
389 * @param {bool} [animate=true] Animate the transition or not
390 * @returns {void}
391 */
392 scrollToRow: function ( row, animate )
393 {
394 var that = this;
395 var ani = false;
396 var px = this.rowToPixels( row );
397
398 // We need to know if the table will redraw or not before doing the
399 // scroll. If it will not redraw, then we need to use the currently
400 // displayed table, and scroll with the physical pixels. Otherwise, we
401 // need to calculate the table's new position from the virtual
402 // transform.
403 var preRows = ((this.s.displayBuffer-1)/2) * this.s.viewportRows;
404 var drawRow = row - preRows;
405 if ( drawRow < 0 ) {
406 drawRow = 0;
407 }
408
409 if ( (px > this.s.redrawBottom || px < this.s.redrawTop) && this.s.dt._iDisplayStart !== drawRow ) {
410 ani = true;
411 px = this._domain( 'virtualToPhysical', row * this.s.heights.row );
412
413 // If we need records outside the current draw region, but the new
414 // scrolling position is inside that (due to the non-linear nature
415 // for larger numbers of records), we need to force position update.
416 if ( this.s.redrawTop < px && px < this.s.redrawBottom ) {
417 this.s.forceReposition = true;
418 animate = false;
419 }
420 }
421
422 if ( animate === undefined || animate )
423 {
424 this.s.ani = ani;
425 $(this.dom.scroller).animate( {
426 "scrollTop": px
427 }, function () {
428 // This needs to happen after the animation has completed and
429 // the final scroll event fired
430 setTimeout( function () {
431 that.s.ani = false;
432 }, 250 );
433 } );
434 }
435 else
436 {
437 $(this.dom.scroller).scrollTop( px );
438 }
439 },
440
441
442 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
443 * Constructor
444 */
445
446 /**
447 * Initialisation for Scroller
448 * @returns {void}
449 * @private
450 */
451 construct: function ()
452 {
453 var that = this;
454 var dt = this.s.dtApi;
455
456 /* Sanity check */
457 if ( !this.s.dt.oFeatures.bPaginate ) {
458 this.s.dt.oApi._fnLog( this.s.dt, 0, 'Pagination must be enabled for Scroller' );
459 return;
460 }
461
462 /* Insert a div element that we can use to force the DT scrolling container to
463 * the height that would be required if the whole table was being displayed
464 */
465 this.dom.force.style.position = "relative";
466 this.dom.force.style.top = "0px";
467 this.dom.force.style.left = "0px";
468 this.dom.force.style.width = "1px";
469
470 this.dom.scroller = $('div.'+this.s.dt.oClasses.sScrollBody, this.s.dt.nTableWrapper)[0];
471 this.dom.scroller.appendChild( this.dom.force );
472 this.dom.scroller.style.position = "relative";
473
474 this.dom.table = $('>table', this.dom.scroller)[0];
475 this.dom.table.style.position = "absolute";
476 this.dom.table.style.top = "0px";
477 this.dom.table.style.left = "0px";
478
479 // Add class to 'announce' that we are a Scroller table
480 $(dt.table().container()).addClass('dts DTS');
481
482 // Add a 'loading' indicator
483 if ( this.s.loadingIndicator )
484 {
485 this.dom.loader = $('<div class="dataTables_processing dts_loading">'+this.s.dt.oLanguage.sLoadingRecords+'</div>')
486 .css('display', 'none');
487
488 $(this.dom.scroller.parentNode)
489 .css('position', 'relative')
490 .append( this.dom.loader );
491 }
492
493 this.dom.label.appendTo(this.dom.scroller);
494
495 /* Initial size calculations */
496 if ( this.s.heights.row && this.s.heights.row != 'auto' )
497 {
498 this.s.autoHeight = false;
499 }
500 this.measure( false );
501
502 // Scrolling callback to see if a page change is needed - use a throttled
503 // function for the save save callback so we aren't hitting it on every
504 // scroll
505 this.s.ingnoreScroll = true;
506 this.s.stateSaveThrottle = this.s.dt.oApi._fnThrottle( function () {
507 that.s.dtApi.state.save();
508 }, 500 );
509 $(this.dom.scroller).on( 'scroll.dt-scroller', function (e) {
510 that._scroll.call( that );
511 } );
512
513 // In iOS we catch the touchstart event in case the user tries to scroll
514 // while the display is already scrolling
515 $(this.dom.scroller).on('touchstart.dt-scroller', function () {
516 that._scroll.call( that );
517 } );
518
519 $(this.dom.scroller)
520 .on('mousedown.dt-scroller', function () {
521 that.s.mousedown = true;
522 })
523 .on('mouseup.dt-scroller', function () {
524 that.s.mouseup = false;
525 that.dom.label.css('display', 'none');
526 });
527
528 // On resize, update the information element, since the number of rows shown might change
529 $(window).on( 'resize.dt-scroller', function () {
530 that.measure( false );
531 that._info();
532 } );
533
534 // Add a state saving parameter to the DT state saving so we can restore the exact
535 // position of the scrolling. Slightly surprisingly the scroll position isn't actually
536 // stored, but rather tha base units which are needed to calculate it. This allows for
537 // virtual scrolling as well.
538 var initialStateSave = true;
539 var loadedState = dt.state.loaded();
540
541 dt.on( 'stateSaveParams.scroller', function ( e, settings, data ) {
542 // Need to used the saved position on init
543 data.scroller = {
544 topRow: initialStateSave && loadedState && loadedState.scroller ?
545 loadedState.scroller.topRow :
546 that.s.topRowFloat,
547 baseScrollTop: that.s.baseScrollTop,
548 baseRowTop: that.s.baseRowTop
549 };
550
551 initialStateSave = false;
552 } );
553
554 if ( loadedState && loadedState.scroller ) {
555 this.s.topRowFloat = loadedState.scroller.topRow;
556 this.s.baseScrollTop = loadedState.scroller.baseScrollTop;
557 this.s.baseRowTop = loadedState.scroller.baseRowTop;
558 }
559
560 dt.on( 'init.scroller', function () {
561 that.measure( false );
562
563 // Setting to `jump` will instruct _draw to calculate the scroll top
564 // position
565 that.s.scrollType = 'jump';
566 that._draw();
567
568 // Update the scroller when the DataTable is redrawn
569 dt.on( 'draw.scroller', function () {
570 that._draw();
571 });
572 } );
573
574 // Set height before the draw happens, allowing everything else to update
575 // on draw complete without worry for roder.
576 dt.on( 'preDraw.dt.scroller', function () {
577 that._scrollForce();
578 } );
579
580 // Destructor
581 dt.on( 'destroy.scroller', function () {
582 $(window).off( 'resize.dt-scroller' );
583 $(that.dom.scroller).off('.dt-scroller');
584 $(that.s.dt.nTable).off( '.scroller' );
585
586 $(that.s.dt.nTableWrapper).removeClass('DTS');
587 $('div.DTS_Loading', that.dom.scroller.parentNode).remove();
588
589 that.dom.table.style.position = "";
590 that.dom.table.style.top = "";
591 that.dom.table.style.left = "";
592 } );
593 },
594
595
596 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
597 * Private methods
598 */
599
600 /**
601 * Automatic calculation of table row height. This is just a little tricky here as using
602 * initialisation DataTables has tale the table out of the document, so we need to create
603 * a new table and insert it into the document, calculate the row height and then whip the
604 * table out.
605 * @returns {void}
606 * @private
607 */
608 _calcRowHeight: function ()
609 {
610 var dt = this.s.dt;
611 var origTable = dt.nTable;
612 var nTable = origTable.cloneNode( false );
613 var tbody = $('<tbody/>').appendTo( nTable );
614 var container = $(
615 '<div class="'+dt.oClasses.sWrapper+' DTS">'+
616 '<div class="'+dt.oClasses.sScrollWrapper+'">'+
617 '<div class="'+dt.oClasses.sScrollBody+'"></div>'+
618 '</div>'+
619 '</div>'
620 );
621
622 // Want 3 rows in the sizing table so :first-child and :last-child
623 // CSS styles don't come into play - take the size of the middle row
624 $('tbody tr:lt(4)', origTable).clone().appendTo( tbody );
625 var rowsCount = $('tr', tbody).length;
626
627 if ( rowsCount === 1 ) {
628 tbody.prepend('<tr><td>&#160;</td></tr>');
629 tbody.append('<tr><td>&#160;</td></tr>');
630 }
631 else {
632 for (; rowsCount < 3; rowsCount++) {
633 tbody.append('<tr><td>&#160;</td></tr>');
634 }
635 }
636
637 $('div.'+dt.oClasses.sScrollBody, container).append( nTable );
638
639 // If initialised using `dom`, use the holding element as the insert point
640 var insertEl = this.s.dt.nHolding || origTable.parentNode;
641
642 if ( ! $(insertEl).is(':visible') ) {
643 insertEl = 'body';
644 }
645
646 container.appendTo( insertEl );
647 this.s.heights.row = $('tr', tbody).eq(1).outerHeight();
648
649 container.remove();
650 },
651
652 /**
653 * Draw callback function which is fired when the DataTable is redrawn. The main function of
654 * this method is to position the drawn table correctly the scrolling container for the rows
655 * that is displays as a result of the scrolling position.
656 * @returns {void}
657 * @private
658 */
659 _draw: function ()
660 {
661 var
662 that = this,
663 heights = this.s.heights,
664 iScrollTop = this.dom.scroller.scrollTop,
665 iTableHeight = $(this.s.dt.nTable).height(),
666 displayStart = this.s.dt._iDisplayStart,
667 displayLen = this.s.dt._iDisplayLength,
668 displayEnd = this.s.dt.fnRecordsDisplay();
669
670 // Disable the scroll event listener while we are updating the DOM
671 this.s.skip = true;
672
673 // If paging is reset
674 if ( (this.s.dt.bSorted || this.s.dt.bFiltered) && displayStart === 0 && !this.s.dt._drawHold ) {
675 this.s.topRowFloat = 0;
676 }
677
678 iScrollTop = this.s.scrollType === 'jump' ?
679 this._domain( 'virtualToPhysical', this.s.topRowFloat * heights.row ) :
680 iScrollTop;
681
682 // Store positional information so positional calculations can be based
683 // upon the current table draw position
684 this.s.baseScrollTop = iScrollTop;
685 this.s.baseRowTop = this.s.topRowFloat;
686
687 // Position the table in the virtual scroller
688 var tableTop = iScrollTop - ((this.s.topRowFloat - displayStart) * heights.row);
689 if ( displayStart === 0 ) {
690 tableTop = 0;
691 }
692 else if ( displayStart + displayLen >= displayEnd ) {
693 tableTop = heights.scroll - iTableHeight;
694 }
695
696 this.dom.table.style.top = tableTop+'px';
697
698 /* Cache some information for the scroller */
699 this.s.tableTop = tableTop;
700 this.s.tableBottom = iTableHeight + this.s.tableTop;
701
702 // Calculate the boundaries for where a redraw will be triggered by the
703 // scroll event listener
704 var boundaryPx = (iScrollTop - this.s.tableTop) * this.s.boundaryScale;
705 this.s.redrawTop = iScrollTop - boundaryPx;
706 this.s.redrawBottom = iScrollTop + boundaryPx > heights.scroll - heights.viewport - heights.row ?
707 heights.scroll - heights.viewport - heights.row :
708 iScrollTop + boundaryPx;
709
710 this.s.skip = false;
711
712 // Restore the scrolling position that was saved by DataTable's state
713 // saving Note that this is done on the second draw when data is Ajax
714 // sourced, and the first draw when DOM soured
715 if ( this.s.dt.oFeatures.bStateSave && this.s.dt.oLoadedState !== null &&
716 typeof this.s.dt.oLoadedState.iScroller != 'undefined' )
717 {
718 // A quirk of DataTables is that the draw callback will occur on an
719 // empty set if Ajax sourced, but not if server-side processing.
720 var ajaxSourced = (this.s.dt.sAjaxSource || that.s.dt.ajax) && ! this.s.dt.oFeatures.bServerSide ?
721 true :
722 false;
723
724 if ( ( ajaxSourced && this.s.dt.iDraw == 2) ||
725 (!ajaxSourced && this.s.dt.iDraw == 1) )
726 {
727 setTimeout( function () {
728 $(that.dom.scroller).scrollTop( that.s.dt.oLoadedState.iScroller );
729 that.s.redrawTop = that.s.dt.oLoadedState.iScroller - (heights.viewport/2);
730
731 // In order to prevent layout thrashing we need another
732 // small delay
733 setTimeout( function () {
734 that.s.ingnoreScroll = false;
735 }, 0 );
736 }, 0 );
737 }
738 }
739 else {
740 that.s.ingnoreScroll = false;
741 }
742
743 // Because of the order of the DT callbacks, the info update will
744 // take precedence over the one we want here. So a 'thread' break is
745 // needed. Only add the thread break if bInfo is set
746 if ( this.s.dt.oFeatures.bInfo ) {
747 setTimeout( function () {
748 that._info.call( that );
749 }, 0 );
750 }
751
752 // Hide the loading indicator
753 if ( this.dom.loader && this.s.loaderVisible ) {
754 this.dom.loader.css( 'display', 'none' );
755 this.s.loaderVisible = false;
756 }
757 },
758
759 /**
760 * Convert from one domain to another. The physical domain is the actual
761 * pixel count on the screen, while the virtual is if we had browsers which
762 * had scrolling containers of infinite height (i.e. the absolute value)
763 *
764 * @param {string} dir Domain transform direction, `virtualToPhysical` or
765 * `physicalToVirtual`
766 * @returns {number} Calculated transform
767 * @private
768 */
769 _domain: function ( dir, val )
770 {
771 var heights = this.s.heights;
772 var diff;
773 var magic = 10000; // the point at which the non-linear calculations start to happen
774
775 // If the virtual and physical height match, then we use a linear
776 // transform between the two, allowing the scrollbar to be linear
777 if ( heights.virtual === heights.scroll ) {
778 return val;
779 }
780
781 // In the first 10k pixels and the last 10k pixels, we want the scrolling
782 // to be linear. After that it can be non-linear. It would be unusual for
783 // anyone to mouse wheel through that much.
784 if ( val < magic ) {
785 return val;
786 }
787 else if ( dir === 'virtualToPhysical' && val >= heights.virtual - magic ) {
788 diff = heights.virtual - val;
789 return heights.scroll - diff;
790 }
791 else if ( dir === 'physicalToVirtual' && val >= heights.scroll - magic ) {
792 diff = heights.scroll - val;
793 return heights.virtual - diff;
794 }
795
796 // Otherwise, we want a non-linear scrollbar to take account of the
797 // redrawing regions at the start and end of the table, otherwise these
798 // can stutter badly - on large tables 30px (for example) scroll might
799 // be hundreds of rows, so the table would be redrawing every few px at
800 // the start and end. Use a simple linear eq. to stop this, effectively
801 // causing a kink in the scrolling ratio. It does mean the scrollbar is
802 // non-linear, but with such massive data sets, the scrollbar is going
803 // to be a best guess anyway
804 var m = (heights.virtual - magic - magic) / (heights.scroll - magic - magic);
805 var c = magic - (m*magic);
806
807 return dir === 'virtualToPhysical' ?
808 (val-c) / m :
809 (m*val) + c;
810 },
811
812 /**
813 * Update any information elements that are controlled by the DataTable based on the scrolling
814 * viewport and what rows are visible in it. This function basically acts in the same way as
815 * _fnUpdateInfo in DataTables, and effectively replaces that function.
816 * @returns {void}
817 * @private
818 */
819 _info: function ()
820 {
821 if ( !this.s.dt.oFeatures.bInfo )
822 {
823 return;
824 }
825
826 var
827 dt = this.s.dt,
828 language = dt.oLanguage,
829 iScrollTop = this.dom.scroller.scrollTop,
830 iStart = Math.floor( this.pixelsToRow(iScrollTop, false, this.s.ani)+1 ),
831 iMax = dt.fnRecordsTotal(),
832 iTotal = dt.fnRecordsDisplay(),
833 iPossibleEnd = Math.ceil( this.pixelsToRow(iScrollTop+this.s.heights.viewport, false, this.s.ani) ),
834 iEnd = iTotal < iPossibleEnd ? iTotal : iPossibleEnd,
835 sStart = dt.fnFormatNumber( iStart ),
836 sEnd = dt.fnFormatNumber( iEnd ),
837 sMax = dt.fnFormatNumber( iMax ),
838 sTotal = dt.fnFormatNumber( iTotal ),
839 sOut;
840
841 if ( dt.fnRecordsDisplay() === 0 &&
842 dt.fnRecordsDisplay() == dt.fnRecordsTotal() )
843 {
844 /* Empty record set */
845 sOut = language.sInfoEmpty+ language.sInfoPostFix;
846 }
847 else if ( dt.fnRecordsDisplay() === 0 )
848 {
849 /* Empty record set after filtering */
850 sOut = language.sInfoEmpty +' '+
851 language.sInfoFiltered.replace('_MAX_', sMax)+
852 language.sInfoPostFix;
853 }
854 else if ( dt.fnRecordsDisplay() == dt.fnRecordsTotal() )
855 {
856 /* Normal record set */
857 sOut = language.sInfo.
858 replace('_START_', sStart).
859 replace('_END_', sEnd).
860 replace('_MAX_', sMax).
861 replace('_TOTAL_', sTotal)+
862 language.sInfoPostFix;
863 }
864 else
865 {
866 /* Record set after filtering */
867 sOut = language.sInfo.
868 replace('_START_', sStart).
869 replace('_END_', sEnd).
870 replace('_MAX_', sMax).
871 replace('_TOTAL_', sTotal) +' '+
872 language.sInfoFiltered.replace(
873 '_MAX_',
874 dt.fnFormatNumber(dt.fnRecordsTotal())
875 )+
876 language.sInfoPostFix;
877 }
878
879 var callback = language.fnInfoCallback;
880 if ( callback ) {
881 sOut = callback.call( dt.oInstance,
882 dt, iStart, iEnd, iMax, iTotal, sOut
883 );
884 }
885
886 var n = dt.aanFeatures.i;
887 if ( typeof n != 'undefined' )
888 {
889 for ( var i=0, iLen=n.length ; i<iLen ; i++ )
890 {
891 $(n[i]).html( sOut );
892 }
893 }
894
895 // DT doesn't actually (yet) trigger this event, but it will in future
896 $(dt.nTable).triggerHandler( 'info.dt' );
897 },
898
899 /**
900 * Parse CSS height property string as number
901 *
902 * An attempt is made to parse the string as a number. Currently supported units are 'px',
903 * 'vh', and 'rem'. 'em' is partially supported; it works as long as the parent element's
904 * font size matches the body element. Zero is returned for unrecognized strings.
905 * @param {string} cssHeight CSS height property string
906 * @returns {number} height
907 * @private
908 */
909 _parseHeight: function(cssHeight) {
910 var height;
911 var matches = /^([+-]?(?:\d+(?:\.\d+)?|\.\d+))(px|em|rem|vh)$/.exec(cssHeight);
912
913 if (matches === null) {
914 return 0;
915 }
916
917 var value = parseFloat(matches[1]);
918 var unit = matches[2];
919
920 if ( unit === 'px' ) {
921 height = value;
922 }
923 else if ( unit === 'vh' ) {
924 height = ( value / 100 ) * $(window).height();
925 }
926 else if ( unit === 'rem' ) {
927 height = value * parseFloat($(':root').css('font-size'));
928 }
929 else if ( unit === 'em' ) {
930 height = value * parseFloat($('body').css('font-size'));
931 }
932
933 return height ?
934 height :
935 0;
936 },
937
938 /**
939 * Scrolling function - fired whenever the scrolling position is changed.
940 * This method needs to use the stored values to see if the table should be
941 * redrawn as we are moving towards the end of the information that is
942 * currently drawn or not. If needed, then it will redraw the table based on
943 * the new position.
944 * @returns {void}
945 * @private
946 */
947 _scroll: function ()
948 {
949 var
950 that = this,
951 heights = this.s.heights,
952 iScrollTop = this.dom.scroller.scrollTop,
953 iTopRow;
954
955 if ( this.s.skip ) {
956 return;
957 }
958
959 if ( this.s.ingnoreScroll ) {
960 return;
961 }
962
963 if ( iScrollTop === this.s.lastScrollTop ) {
964 return;
965 }
966
967 /* If the table has been sorted or filtered, then we use the redraw that
968 * DataTables as done, rather than performing our own
969 */
970 if ( this.s.dt.bFiltered || this.s.dt.bSorted ) {
971 this.s.lastScrollTop = 0;
972 return;
973 }
974
975 /* Update the table's information display for what is now in the viewport */
976 this._info();
977
978 /* We don't want to state save on every scroll event - that's heavy
979 * handed, so use a timeout to update the state saving only when the
980 * scrolling has finished
981 */
982 clearTimeout( this.s.stateTO );
983 this.s.stateTO = setTimeout( function () {
984 that.s.dtApi.state.save();
985 }, 250 );
986
987 this.s.scrollType = Math.abs(iScrollTop - this.s.lastScrollTop) > heights.viewport ?
988 'jump' :
989 'cont';
990
991 this.s.topRowFloat = this.s.scrollType === 'cont' ?
992 this.pixelsToRow( iScrollTop, false, false ) :
993 this._domain( 'physicalToVirtual', iScrollTop ) / heights.row;
994
995 if ( this.s.topRowFloat < 0 ) {
996 this.s.topRowFloat = 0;
997 }
998
999 /* Check if the scroll point is outside the trigger boundary which would required
1000 * a DataTables redraw
1001 */
1002 if ( this.s.forceReposition || iScrollTop < this.s.redrawTop || iScrollTop > this.s.redrawBottom ) {
1003 var preRows = Math.ceil( ((this.s.displayBuffer-1)/2) * this.s.viewportRows );
1004
1005 iTopRow = parseInt(this.s.topRowFloat, 10) - preRows;
1006 this.s.forceReposition = false;
1007
1008 if ( iTopRow <= 0 ) {
1009 /* At the start of the table */
1010 iTopRow = 0;
1011 }
1012 else if ( iTopRow + this.s.dt._iDisplayLength > this.s.dt.fnRecordsDisplay() ) {
1013 /* At the end of the table */
1014 iTopRow = this.s.dt.fnRecordsDisplay() - this.s.dt._iDisplayLength;
1015 if ( iTopRow < 0 ) {
1016 iTopRow = 0;
1017 }
1018 }
1019 else if ( iTopRow % 2 !== 0 ) {
1020 // For the row-striping classes (odd/even) we want only to start
1021 // on evens otherwise the stripes will change between draws and
1022 // look rubbish
1023 iTopRow++;
1024 }
1025
1026
1027 if ( iTopRow != this.s.dt._iDisplayStart ) {
1028 /* Cache the new table position for quick lookups */
1029 this.s.tableTop = $(this.s.dt.nTable).offset().top;
1030 this.s.tableBottom = $(this.s.dt.nTable).height() + this.s.tableTop;
1031
1032 var draw = function () {
1033 if ( that.s.scrollDrawReq === null ) {
1034 that.s.scrollDrawReq = iScrollTop;
1035 }
1036
1037 that.s.dt._iDisplayStart = iTopRow;
1038 that.s.dt.oApi._fnDraw( that.s.dt );
1039 };
1040
1041 /* Do the DataTables redraw based on the calculated start point - note that when
1042 * using server-side processing we introduce a small delay to not DoS the server...
1043 */
1044 if ( this.s.dt.oFeatures.bServerSide ) {
1045 clearTimeout( this.s.drawTO );
1046 this.s.drawTO = setTimeout( draw, this.s.serverWait );
1047 }
1048 else {
1049 draw();
1050 }
1051
1052 if ( this.dom.loader && ! this.s.loaderVisible ) {
1053 this.dom.loader.css( 'display', 'block' );
1054 this.s.loaderVisible = true;
1055 }
1056 }
1057 }
1058 else {
1059 this.s.topRowFloat = this.pixelsToRow( iScrollTop, false, true );
1060 }
1061
1062 this.s.lastScrollTop = iScrollTop;
1063 this.s.stateSaveThrottle();
1064
1065 if ( this.s.scrollType === 'jump' && this.s.mousedown ) {
1066 this.dom.label
1067 .html( this.s.dt.fnFormatNumber( parseInt( this.s.topRowFloat, 10 )+1 ) )
1068 .css( 'top', iScrollTop + (iScrollTop * heights.labelFactor ) )
1069 .css( 'display', 'block' );
1070 }
1071 },
1072
1073 /**
1074 * Force the scrolling container to have height beyond that of just the
1075 * table that has been drawn so the user can scroll the whole data set.
1076 *
1077 * Note that if the calculated required scrolling height exceeds a maximum
1078 * value (1 million pixels - hard-coded) the forcing element will be set
1079 * only to that maximum value and virtual / physical domain transforms will
1080 * be used to allow Scroller to display tables of any number of records.
1081 * @returns {void}
1082 * @private
1083 */
1084 _scrollForce: function ()
1085 {
1086 var heights = this.s.heights;
1087 var max = 1000000;
1088
1089 heights.virtual = heights.row * this.s.dt.fnRecordsDisplay();
1090 heights.scroll = heights.virtual;
1091
1092 if ( heights.scroll > max ) {
1093 heights.scroll = max;
1094 }
1095
1096 // Minimum height so there is always a row visible (the 'no rows found'
1097 // if reduced to zero filtering)
1098 this.dom.force.style.height = heights.scroll > this.s.heights.row ?
1099 heights.scroll+'px' :
1100 this.s.heights.row+'px';
1101 }
1102} );
1103
1104
1105
1106/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1107 * Statics
1108 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1109
1110
1111/**
1112 * Scroller default settings for initialisation
1113 * @namespace
1114 * @name Scroller.defaults
1115 * @static
1116 */
1117Scroller.defaults = {
1118 /**
1119 * Scroller uses the boundary scaling factor to decide when to redraw the table - which it
1120 * typically does before you reach the end of the currently loaded data set (in order to
1121 * allow the data to look continuous to a user scrolling through the data). If given as 0
1122 * then the table will be redrawn whenever the viewport is scrolled, while 1 would not
1123 * redraw the table until the currently loaded data has all been shown. You will want
1124 * something in the middle - the default factor of 0.5 is usually suitable.
1125 * @type float
1126 * @default 0.5
1127 * @static
1128 */
1129 boundaryScale: 0.5,
1130
1131 /**
1132 * The display buffer is what Scroller uses to calculate how many rows it should pre-fetch
1133 * for scrolling. Scroller automatically adjusts DataTables' display length to pre-fetch
1134 * rows that will be shown in "near scrolling" (i.e. just beyond the current display area).
1135 * The value is based upon the number of rows that can be displayed in the viewport (i.e.
1136 * a value of 1), and will apply the display range to records before before and after the
1137 * current viewport - i.e. a factor of 3 will allow Scroller to pre-fetch 1 viewport's worth
1138 * of rows before the current viewport, the current viewport's rows and 1 viewport's worth
1139 * of rows after the current viewport. Adjusting this value can be useful for ensuring
1140 * smooth scrolling based on your data set.
1141 * @type int
1142 * @default 7
1143 * @static
1144 */
1145 displayBuffer: 9,
1146
1147 /**
1148 * Show (or not) the loading element in the background of the table. Note that you should
1149 * include the dataTables.scroller.css file for this to be displayed correctly.
1150 * @type boolean
1151 * @default false
1152 * @static
1153 */
1154 loadingIndicator: false,
1155
1156 /**
1157 * Scroller will attempt to automatically calculate the height of rows for it's internal
1158 * calculations. However the height that is used can be overridden using this parameter.
1159 * @type int|string
1160 * @default auto
1161 * @static
1162 */
1163 rowHeight: "auto",
1164
1165 /**
1166 * When using server-side processing, Scroller will wait a small amount of time to allow
1167 * the scrolling to finish before requesting more data from the server. This prevents
1168 * you from DoSing your own server! The wait time can be configured by this parameter.
1169 * @type int
1170 * @default 200
1171 * @static
1172 */
1173 serverWait: 200
1174};
1175
1176Scroller.oDefaults = Scroller.defaults;
1177
1178
1179
1180/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1181 * Constants
1182 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1183
1184/**
1185 * Scroller version
1186 * @type String
1187 * @default See code
1188 * @name Scroller.version
1189 * @static
1190 */
1191Scroller.version = "2.0.1";
1192
1193
1194
1195/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1196 * Initialisation
1197 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1198
1199// Attach a listener to the document which listens for DataTables initialisation
1200// events so we can automatically initialise
1201$(document).on( 'preInit.dt.dtscroller', function (e, settings) {
1202 if ( e.namespace !== 'dt' ) {
1203 return;
1204 }
1205
1206 var init = settings.oInit.scroller;
1207 var defaults = DataTable.defaults.scroller;
1208
1209 if ( init || defaults ) {
1210 var opts = $.extend( {}, init, defaults );
1211
1212 if ( init !== false ) {
1213 new Scroller( settings, opts );
1214 }
1215 }
1216} );
1217
1218
1219// Attach Scroller to DataTables so it can be accessed as an 'extra'
1220$.fn.dataTable.Scroller = Scroller;
1221$.fn.DataTable.Scroller = Scroller;
1222
1223
1224// DataTables 1.10 API method aliases
1225var Api = $.fn.dataTable.Api;
1226
1227Api.register( 'scroller()', function () {
1228 return this;
1229} );
1230
1231// Undocumented and deprecated - is it actually useful at all?
1232Api.register( 'scroller().rowToPixels()', function ( rowIdx, intParse, virtual ) {
1233 var ctx = this.context;
1234
1235 if ( ctx.length && ctx[0].oScroller ) {
1236 return ctx[0].oScroller.rowToPixels( rowIdx, intParse, virtual );
1237 }
1238 // undefined
1239} );
1240
1241// Undocumented and deprecated - is it actually useful at all?
1242Api.register( 'scroller().pixelsToRow()', function ( pixels, intParse, virtual ) {
1243 var ctx = this.context;
1244
1245 if ( ctx.length && ctx[0].oScroller ) {
1246 return ctx[0].oScroller.pixelsToRow( pixels, intParse, virtual );
1247 }
1248 // undefined
1249} );
1250
1251// `scroller().scrollToRow()` is undocumented and deprecated. Use `scroller.toPosition()
1252Api.register( ['scroller().scrollToRow()', 'scroller.toPosition()'], function ( idx, ani ) {
1253 this.iterator( 'table', function ( ctx ) {
1254 if ( ctx.oScroller ) {
1255 ctx.oScroller.scrollToRow( idx, ani );
1256 }
1257 } );
1258
1259 return this;
1260} );
1261
1262Api.register( 'row().scrollTo()', function ( ani ) {
1263 var that = this;
1264
1265 this.iterator( 'row', function ( ctx, rowIdx ) {
1266 if ( ctx.oScroller ) {
1267 var displayIdx = that
1268 .rows( { order: 'applied', search: 'applied' } )
1269 .indexes()
1270 .indexOf( rowIdx );
1271
1272 ctx.oScroller.scrollToRow( displayIdx, ani );
1273 }
1274 } );
1275
1276 return this;
1277} );
1278
1279Api.register( 'scroller.measure()', function ( redraw ) {
1280 this.iterator( 'table', function ( ctx ) {
1281 if ( ctx.oScroller ) {
1282 ctx.oScroller.measure( redraw );
1283 }
1284 } );
1285
1286 return this;
1287} );
1288
1289Api.register( 'scroller.page()', function() {
1290 var ctx = this.context;
1291
1292 if ( ctx.length && ctx[0].oScroller ) {
1293 return ctx[0].oScroller.pageInfo();
1294 }
1295 // undefined
1296} );
1297
1298return Scroller;
1299}));
Note: See TracBrowser for help on using the repository browser.