dataTables.scroller.js 40 KB

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