/*! scroller 2.0.7
* ©2011-2022 sprymedia ltd - datatables.net/license
*/
/**
* @summary scroller
* @description virtual rendering for datatables
* @version 2.0.7
* @author sprymedia ltd (www.sprymedia.co.uk)
* @contact www.sprymedia.co.uk/contact
* @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
*/
(function( factory ){
if ( typeof define === 'function' && define.amd ) {
// amd
define( ['jquery', 'datatables.net'], function ( $ ) {
return factory( $, window, document );
} );
}
else if ( typeof exports === 'object' ) {
// commonjs
module.exports = function (root, $) {
if ( ! root ) {
root = window;
}
if ( ! $ || ! $.fn.datatable ) {
$ = require('datatables.net')(root, $).$;
}
return factory( $, root, root.document );
};
}
else {
// browser
factory( jquery, window, document );
}
}(function( $, window, document, undefined ) {
'use strict';
var datatable = $.fn.datatable;
/**
* scroller is a virtual rendering plug-in for datatables which allows large
* datasets to be drawn on screen every 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:
*
* - speed! the aim of scroller for datatables is to make rendering large data sets fast
* - full compatibility with deferred rendering in datatables for maximum speed
* - display millions of rows
* - integration with state saving in datatables (scrolling position is saved)
* - easy to use
*
*
* @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.10.0+
*
* @example
* $(document).ready(function() {
* $('#example').datatable( {
* "scrolly": "200px",
* "ajax": "media/dataset/large.txt",
* "scroller": true,
* "deferrender": true
* } );
* } );
*/
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": $('0
'),
"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.dt.oinstance.fndraw( 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 ) {
this.s.dt.oapi._fnlog( this.s.dt, 0, 'pagination must be enabled for scroller' );
return;
}
/* 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 = $('div.'+this.s.dt.oclasses.sscrollbody, this.s.dt.ntablewrapper)[0];
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');
// add a 'loading' indicator
if ( this.s.loadingindicator )
{
this.dom.loader = $(''+this.s.dt.olanguage.sloadingrecords+'
')
.css('display', 'none');
$(this.dom.scroller.parentnode)
.css('position', 'relative')
.append( this.dom.loader );
}
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,
basescrolltop: that.s.basescrolltop,
baserowtop: that.s.baserowtop,
scrolltop: that.s.lastscrolltop
};
}
} );
dt.on( 'stateloadparams.scroller', function( e, settings, data ) {
if (data.scroller !== undefined) {
that.scrolltorow(data.scroller.toprow);
}
});
if ( loadedstate && loadedstate.scroller ) {
this.s.toprowfloat = loadedstate.scroller.toprow;
this.s.basescrolltop = loadedstate.scroller.basescrolltop;
this.s.baserowtop = loadedstate.scroller.baserowtop;
}
this.measure( false );
that.s.statesavethrottle = that.s.dt.oapi._fnthrottle( 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 = $('').appendto( ntable );
var container = $(
''
);
// 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(' |
');
tbody.append(' |
');
}
else {
for (; rowscount < 3; rowscount++) {
tbody.append(' |
');
}
}
$('div.'+dt.oclasses.sscrollbody, 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();
// 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;
}
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);
// hide the loading indicator
if ( this.dom.loader && this.s.loadervisible ) {
this.dom.loader.css( 'display', 'none' );
this.s.loadervisible = false;
}
},
/**
* 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,
language = dt.olanguage,
iscrolltop = this.dom.scroller.scrolltop,
istart = math.floor( this.pixelstorow(iscrolltop, false, this.s.ani)+1 ),
imax = dt.fnrecordstotal(),
itotal = dt.fnrecordsdisplay(),
ipossibleend = math.ceil( this.pixelstorow(iscrolltop+this.s.heights.viewport, false, this.s.ani) ),
iend = itotal < ipossibleend ? itotal : ipossibleend,
sstart = dt.fnformatnumber( istart ),
send = dt.fnformatnumber( iend ),
smax = dt.fnformatnumber( imax ),
stotal = dt.fnformatnumber( itotal ),
sout;
if ( dt.fnrecordsdisplay() === 0 &&
dt.fnrecordsdisplay() == dt.fnrecordstotal() )
{
/* empty record set */
sout = language.sinfoempty+ language.sinfopostfix;
}
else if ( dt.fnrecordsdisplay() === 0 )
{
/* empty record set after filtering */
sout = language.sinfoempty +' '+
language.sinfofiltered.replace('_max_', smax)+
language.sinfopostfix;
}
else if ( dt.fnrecordsdisplay() == dt.fnrecordstotal() )
{
/* normal record set */
sout = language.sinfo.
replace('_start_', sstart).
replace('_end_', send).
replace('_max_', smax).
replace('_total_', stotal)+
language.sinfopostfix;
}
else
{
/* record set after filtering */
sout = language.sinfo.
replace('_start_', sstart).
replace('_end_', send).
replace('_max_', smax).
replace('_total_', stotal) +' '+
language.sinfofiltered.replace(
'_max_',
dt.fnformatnumber(dt.fnrecordstotal())
)+
language.sinfopostfix;
}
var callback = language.fninfocallback;
if ( callback ) {
sout = callback.call( dt.oinstance,
dt, istart, iend, imax, itotal, sout
);
}
var n = dt.aanfeatures.i;
if ( typeof n != 'undefined' )
{
for ( var i=0, ilen=n.length ; i heights.viewport ?
'jump' :
'cont';
this.s.toprowfloat = this.s.scrolltype === 'cont' ?
this.pixelstorow( iscrolltop, false, false ) :
this._domain( 'physicaltovirtual', iscrolltop ) / heights.row;
if ( this.s.toprowfloat < 0 ) {
this.s.toprowfloat = 0;
}
/* check if the scroll point is outside the trigger boundary which would required
* a datatables redraw
*/
if ( this.s.forcereposition || iscrolltop < this.s.redrawtop || iscrolltop > this.s.redrawbottom ) {
var prerows = math.ceil( ((this.s.displaybuffer-1)/2) * this.s.viewportrows );
itoprow = parseint(this.s.toprowfloat, 10) - prerows;
this.s.forcereposition = false;
if ( itoprow <= 0 ) {
/* at the start of the table */
itoprow = 0;
}
else if ( itoprow + this.s.dt._idisplaylength > this.s.dt.fnrecordsdisplay() ) {
/* at the end of the table */
itoprow = this.s.dt.fnrecordsdisplay() - this.s.dt._idisplaylength;
if ( itoprow < 0 ) {
itoprow = 0;
}
}
else if ( itoprow % 2 !== 0 ) {
// for the row-striping classes (odd/even) we want only to start
// on evens otherwise the stripes will change between draws and
// look rubbish
itoprow++;
}
// store calcuated value, in case the following condition is not met, but so
// that the draw function will still use it.
this.s.targettop = itoprow;
if ( itoprow != this.s.dt._idisplaystart ) {
/* cache the new table position for quick lookups */
this.s.tabletop = $(this.s.dt.ntable).offset().top;
this.s.tablebottom = $(this.s.dt.ntable).height() + this.s.tabletop;
var draw = function () {
that.s.dt._idisplaystart = that.s.targettop;
that.s.dt.oapi._fndraw( that.s.dt );
};
/* do the datatables redraw based on the calculated start point - note that when
* using server-side processing we introduce a small delay to not dos the server...
*/
if ( this.s.dt.ofeatures.bserverside ) {
this.s.forcereposition = true;
cleartimeout( this.s.drawto );
this.s.drawto = settimeout( draw, this.s.serverwait );
}
else {
draw();
}
if ( this.dom.loader && ! this.s.loadervisible ) {
this.dom.loader.css( 'display', 'block' );
this.s.loadervisible = true;
}
}
}
else {
this.s.toprowfloat = this.pixelstorow( iscrolltop, false, true );
}
this.s.lastscrolltop = iscrolltop;
this.s.statesavethrottle();
if ( this.s.scrolltype === 'jump' && this.s.mousedown ) {
this.s.labelvisible = true;
}
if (this.s.labelvisible) {
var labelfactor = (heights.viewport-heights.labelheight - heights.xbar) / heights.scroll;
this.dom.label
.html( this.s.dt.fnformatnumber( parseint( this.s.toprowfloat, 10 )+1 ) )
.css( 'top', iscrolltop + (iscrolltop * labelfactor) )
.css( 'right', 10 - this.dom.scroller.scrollleft)
.css( 'display', 'block' );
}
},
/**
* force the scrolling container to have height beyond that of just the
* table that has been drawn so the user can scroll the whole data set.
*
* note that if the calculated required scrolling height exceeds a maximum
* value (1 million pixels - hard-coded) the forcing element will be set
* only to that maximum value and virtual / physical domain transforms will
* be used to allow scroller to display tables of any number of records.
* @returns {void}
* @private
*/
_scrollforce: function ()
{
var heights = this.s.heights;
var max = 1000000;
heights.virtual = heights.row * this.s.dt.fnrecordsdisplay();
heights.scroll = heights.virtual;
if ( heights.scroll > max ) {
heights.scroll = max;
}
// minimum height so there is always a row visible (the 'no rows found'
// if reduced to zero filtering)
this.dom.force.style.height = heights.scroll > this.s.heights.row ?
heights.scroll+'px' :
this.s.heights.row+'px';
}
} );
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* statics
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
* scroller default settings for initialisation
* @namespace
* @name scroller.defaults
* @static
*/
scroller.defaults = {
/**
* scroller uses the boundary scaling factor to decide when to redraw the table - which it
* typically does before you reach the end of the currently loaded data set (in order to
* allow the data to look continuous to a user scrolling through the data). if given as 0
* then the table will be redrawn whenever the viewport is scrolled, while 1 would not
* redraw the table until the currently loaded data has all been shown. you will want
* something in the middle - the default factor of 0.5 is usually suitable.
* @type float
* @default 0.5
* @static
*/
boundaryscale: 0.5,
/**
* the display buffer is what scroller uses to calculate how many rows it should pre-fetch
* for scrolling. scroller automatically adjusts datatables' display length to pre-fetch
* rows that will be shown in "near scrolling" (i.e. just beyond the current display area).
* the value is based upon the number of rows that can be displayed in the viewport (i.e.
* a value of 1), and will apply the display range to records before before and after the
* current viewport - i.e. a factor of 3 will allow scroller to pre-fetch 1 viewport's worth
* of rows before the current viewport, the current viewport's rows and 1 viewport's worth
* of rows after the current viewport. adjusting this value can be useful for ensuring
* smooth scrolling based on your data set.
* @type int
* @default 7
* @static
*/
displaybuffer: 9,
/**
* show (or not) the loading element in the background of the table. note that you should
* include the datatables.scroller.css file for this to be displayed correctly.
* @type boolean
* @default false
* @static
*/
loadingindicator: false,
/**
* scroller will attempt to automatically calculate the height of rows for it's internal
* calculations. however the height that is used can be overridden using this parameter.
* @type int|string
* @default auto
* @static
*/
rowheight: "auto",
/**
* when using server-side processing, scroller will wait a small amount of time to allow
* the scrolling to finish before requesting more data from the server. this prevents
* you from dosing your own server! the wait time can be configured by this parameter.
* @type int
* @default 200
* @static
*/
serverwait: 200
};
scroller.odefaults = scroller.defaults;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* constants
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
* scroller version
* @type string
* @default see code
* @name scroller.version
* @static
*/
scroller.version = "2.0.7";
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* initialisation
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// attach a listener to the document which listens for datatables initialisation
// events so we can automatically initialise
$(document).on( 'preinit.dt.dtscroller', function (e, settings) {
if ( e.namespace !== 'dt' ) {
return;
}
var init = settings.oinit.scroller;
var defaults = datatable.defaults.scroller;
if ( init || defaults ) {
var opts = $.extend( {}, init, defaults );
if ( init !== false ) {
new scroller( settings, opts );
}
}
} );
// attach scroller to datatables so it can be accessed as an 'extra'
$.fn.datatable.scroller = scroller;
$.fn.datatable.scroller = scroller;
// datatables 1.10 api method aliases
var api = $.fn.datatable.api;
api.register( 'scroller()', function () {
return this;
} );
// undocumented and deprecated - is it actually useful at all?
api.register( 'scroller().rowtopixels()', function ( rowidx, intparse, virtual ) {
var ctx = this.context;
if ( ctx.length && ctx[0].oscroller ) {
return ctx[0].oscroller.rowtopixels( rowidx, intparse, virtual );
}
// undefined
} );
// undocumented and deprecated - is it actually useful at all?
api.register( 'scroller().pixelstorow()', function ( pixels, intparse, virtual ) {
var ctx = this.context;
if ( ctx.length && ctx[0].oscroller ) {
return ctx[0].oscroller.pixelstorow( pixels, intparse, virtual );
}
// undefined
} );
// `scroller().scrolltorow()` is undocumented and deprecated. use `scroller.toposition()
api.register( ['scroller().scrolltorow()', 'scroller.toposition()'], function ( idx, ani ) {
this.iterator( 'table', function ( ctx ) {
if ( ctx.oscroller ) {
ctx.oscroller.scrolltorow( idx, ani );
}
} );
return this;
} );
api.register( 'row().scrollto()', function ( ani ) {
var that = this;
this.iterator( 'row', function ( ctx, rowidx ) {
if ( ctx.oscroller ) {
var displayidx = that
.rows( { order: 'applied', search: 'applied' } )
.indexes()
.indexof( rowidx );
ctx.oscroller.scrolltorow( displayidx, ani );
}
} );
return this;
} );
api.register( 'scroller.measure()', function ( redraw ) {
this.iterator( 'table', function ( ctx ) {
if ( ctx.oscroller ) {
ctx.oscroller.measure( redraw );
}
} );
return this;
} );
api.register( 'scroller.page()', function() {
var ctx = this.context;
if ( ctx.length && ctx[0].oscroller ) {
return ctx[0].oscroller.pageinfo();
}
// undefined
} );
return scroller;
}));