dataTables.scroller.js 41 KB

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