dataTables.autoFill.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. /*! AutoFill 1.2.1
  2. * ©2008-2014 SpryMedia Ltd - datatables.net/license
  3. */
  4. /**
  5. * @summary AutoFill
  6. * @description Add Excel like click and drag auto-fill options to DataTables
  7. * @version 1.2.1
  8. * @file dataTables.autoFill.js
  9. * @author SpryMedia Ltd (www.sprymedia.co.uk)
  10. * @contact www.sprymedia.co.uk/contact
  11. * @copyright Copyright 2010-2014 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( window, document, undefined ) {
  23. var factory = function( $, DataTable ) {
  24. "use strict";
  25. /**
  26. * AutoFill provides Excel like auto-fill features for a DataTable
  27. *
  28. * @class AutoFill
  29. * @constructor
  30. * @param {object} oTD DataTables settings object
  31. * @param {object} oConfig Configuration object for AutoFill
  32. */
  33. var AutoFill = function( oDT, oConfig )
  34. {
  35. /* Sanity check that we are a new instance */
  36. if ( ! (this instanceof AutoFill) ) {
  37. throw( "Warning: AutoFill must be initialised with the keyword 'new'" );
  38. }
  39. if ( ! $.fn.dataTableExt.fnVersionCheck('1.7.0') ) {
  40. throw( "Warning: AutoFill requires DataTables 1.7 or greater");
  41. }
  42. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  43. * Public class variables
  44. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  45. this.c = {};
  46. /**
  47. * @namespace Settings object which contains customisable information for AutoFill instance
  48. */
  49. this.s = {
  50. /**
  51. * @namespace Cached information about the little dragging icon (the filler)
  52. */
  53. "filler": {
  54. "height": 0,
  55. "width": 0
  56. },
  57. /**
  58. * @namespace Cached information about the border display
  59. */
  60. "border": {
  61. "width": 2
  62. },
  63. /**
  64. * @namespace Store for live information for the current drag
  65. */
  66. "drag": {
  67. "startX": -1,
  68. "startY": -1,
  69. "startTd": null,
  70. "endTd": null,
  71. "dragging": false
  72. },
  73. /**
  74. * @namespace Data cache for information that we need for scrolling the screen when we near
  75. * the edges
  76. */
  77. "screen": {
  78. "interval": null,
  79. "y": 0,
  80. "height": 0,
  81. "scrollTop": 0
  82. },
  83. /**
  84. * @namespace Data cache for the position of the DataTables scrolling element (when scrolling
  85. * is enabled)
  86. */
  87. "scroller": {
  88. "top": 0,
  89. "bottom": 0
  90. },
  91. /**
  92. * @namespace Information stored for each column. An array of objects
  93. */
  94. "columns": []
  95. };
  96. /**
  97. * @namespace Common and useful DOM elements for the class instance
  98. */
  99. this.dom = {
  100. "table": null,
  101. "filler": null,
  102. "borderTop": null,
  103. "borderRight": null,
  104. "borderBottom": null,
  105. "borderLeft": null,
  106. "currentTarget": null
  107. };
  108. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  109. * Public class methods
  110. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  111. /**
  112. * Retreieve the settings object from an instance
  113. * @method fnSettings
  114. * @returns {object} AutoFill settings object
  115. */
  116. this.fnSettings = function () {
  117. return this.s;
  118. };
  119. /* Constructor logic */
  120. this._fnInit( oDT, oConfig );
  121. return this;
  122. };
  123. AutoFill.prototype = {
  124. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  125. * Private methods (they are of course public in JS, but recommended as private)
  126. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  127. /**
  128. * Initialisation
  129. * @method _fnInit
  130. * @param {object} dt DataTables settings object
  131. * @param {object} config Configuration object for AutoFill
  132. * @returns void
  133. */
  134. "_fnInit": function ( dt, config )
  135. {
  136. var
  137. that = this,
  138. i, iLen;
  139. // Use DataTables API to get the settings allowing selectors, instances
  140. // etc to be used, or for backwards compatibility get from the old
  141. // fnSettings method
  142. this.s.dt = DataTable.Api ?
  143. new DataTable.Api( dt ).settings()[0] :
  144. dt.fnSettings();
  145. this.s.init = config || {};
  146. this.dom.table = this.s.dt.nTable;
  147. $.extend( true, this.c, AutoFill.defaults, config );
  148. /* Add and configure the columns */
  149. this._initColumns();
  150. /* Auto Fill click and drag icon */
  151. var filler = $('<div/>', {
  152. 'class': 'AutoFill_filler'
  153. } )
  154. .appendTo( 'body' );
  155. this.dom.filler = filler[0];
  156. // Get the height / width of the click element
  157. this.s.filler.height = filler.height();
  158. this.s.filler.width = filler.width();
  159. filler[0].style.display = "none";
  160. /* Border display - one div for each side. We can't just use a single
  161. * one with a border, as we want the events to effectively pass through
  162. * the transparent bit of the box
  163. */
  164. var border;
  165. var appender = document.body;
  166. if ( that.s.dt.oScroll.sY !== "" ) {
  167. that.s.dt.nTable.parentNode.style.position = "relative";
  168. appender = that.s.dt.nTable.parentNode;
  169. }
  170. border = $('<div/>', {
  171. "class": "AutoFill_border"
  172. } );
  173. this.dom.borderTop = border.clone().appendTo( appender )[0];
  174. this.dom.borderRight = border.clone().appendTo( appender )[0];
  175. this.dom.borderBottom = border.clone().appendTo( appender )[0];
  176. this.dom.borderLeft = border.clone().appendTo( appender )[0];
  177. /* Events */
  178. filler.on( 'mousedown.DTAF', function (e) {
  179. this.onselectstart = function() { return false; };
  180. that._fnFillerDragStart.call( that, e );
  181. return false;
  182. } );
  183. $('tbody', this.dom.table).on(
  184. 'mouseover.DTAF mouseout.DTAF',
  185. '>tr>td, >tr>th',
  186. function (e) {
  187. that._fnFillerDisplay.call( that, e );
  188. }
  189. );
  190. $(this.dom.table).on( 'destroy.dt.DTAF', function () {
  191. filler.off( 'mousedown.DTAF' ).remove();
  192. $('tbody', this.dom.table).off( 'mouseover.DTAF mouseout.DTAF' );
  193. } );
  194. },
  195. _initColumns: function ( )
  196. {
  197. var that = this;
  198. var i, ien;
  199. var dt = this.s.dt;
  200. var config = this.s.init;
  201. for ( i=0, ien=dt.aoColumns.length ; i<ien ; i++ ) {
  202. this.s.columns[i] = $.extend( true, {}, AutoFill.defaults.column );
  203. }
  204. dt.oApi._fnApplyColumnDefs(
  205. dt,
  206. config.aoColumnDefs || config.columnDefs,
  207. config.aoColumns || config.columns,
  208. function (colIdx, def) {
  209. that._fnColumnOptions( colIdx, def );
  210. }
  211. );
  212. // For columns which don't have read, write, step functions defined,
  213. // use the default ones
  214. for ( i=0, ien=dt.aoColumns.length ; i<ien ; i++ ) {
  215. var column = this.s.columns[i];
  216. if ( ! column.read ) {
  217. column.read = this._fnReadCell;
  218. }
  219. if ( ! column.write ) {
  220. column.read = this._fnWriteCell;
  221. }
  222. if ( ! column.step ) {
  223. column.read = this._fnStep;
  224. }
  225. }
  226. },
  227. "_fnColumnOptions": function ( i, opts )
  228. {
  229. var column = this.s.columns[ i ];
  230. var set = function ( outProp, inProp ) {
  231. if ( opts[ inProp[0] ] !== undefined ) {
  232. column[ outProp ] = opts[ inProp[0] ];
  233. }
  234. if ( opts[ inProp[1] ] !== undefined ) {
  235. column[ outProp ] = opts[ inProp[1] ];
  236. }
  237. };
  238. // Compatibility with the old Hungarian style of notation
  239. set( 'enable', ['bEnable', 'enable'] );
  240. set( 'read', ['fnRead', 'read'] );
  241. set( 'write', ['fnWrite', 'write'] );
  242. set( 'step', ['fnStep', 'step'] );
  243. set( 'increment', ['bIncrement', 'increment'] );
  244. },
  245. /**
  246. * Find out the coordinates of a given TD cell in a table
  247. * @method _fnTargetCoords
  248. * @param {Node} nTd
  249. * @returns {Object} x and y properties, for the position of the cell in the tables DOM
  250. */
  251. "_fnTargetCoords": function ( nTd )
  252. {
  253. var nTr = $(nTd).parents('tr')[0];
  254. var position = this.s.dt.oInstance.fnGetPosition( nTd );
  255. return {
  256. "x": $('td', nTr).index(nTd),
  257. "y": $('tr', nTr.parentNode).index(nTr),
  258. "row": position[0],
  259. "column": position[2]
  260. };
  261. },
  262. /**
  263. * Display the border around one or more cells (from start to end)
  264. * @method _fnUpdateBorder
  265. * @param {Node} nStart Starting cell
  266. * @param {Node} nEnd Ending cell
  267. * @returns void
  268. */
  269. "_fnUpdateBorder": function ( nStart, nEnd )
  270. {
  271. var
  272. border = this.s.border.width,
  273. offsetStart = $(nStart).offset(),
  274. offsetEnd = $(nEnd).offset(),
  275. x1 = offsetStart.left - border,
  276. x2 = offsetEnd.left + $(nEnd).outerWidth(),
  277. y1 = offsetStart.top - border,
  278. y2 = offsetEnd.top + $(nEnd).outerHeight(),
  279. width = offsetEnd.left + $(nEnd).outerWidth() - offsetStart.left + (2*border),
  280. height = offsetEnd.top + $(nEnd).outerHeight() - offsetStart.top + (2*border),
  281. oStyle;
  282. // Recalculate start and end (when dragging "backwards")
  283. if( offsetStart.left > offsetEnd.left) {
  284. x1 = offsetEnd.left - border;
  285. x2 = offsetStart.left + $(nStart).outerWidth();
  286. width = offsetStart.left + $(nStart).outerWidth() - offsetEnd.left + (2*border);
  287. }
  288. if ( this.s.dt.oScroll.sY !== "" )
  289. {
  290. /* The border elements are inside the DT scroller - so position relative to that */
  291. var
  292. offsetScroll = $(this.s.dt.nTable.parentNode).offset(),
  293. scrollTop = $(this.s.dt.nTable.parentNode).scrollTop(),
  294. scrollLeft = $(this.s.dt.nTable.parentNode).scrollLeft();
  295. x1 -= offsetScroll.left - scrollLeft;
  296. x2 -= offsetScroll.left - scrollLeft;
  297. y1 -= offsetScroll.top - scrollTop;
  298. y2 -= offsetScroll.top - scrollTop;
  299. }
  300. /* Top */
  301. oStyle = this.dom.borderTop.style;
  302. oStyle.top = y1+"px";
  303. oStyle.left = x1+"px";
  304. oStyle.height = this.s.border.width+"px";
  305. oStyle.width = width+"px";
  306. /* Bottom */
  307. oStyle = this.dom.borderBottom.style;
  308. oStyle.top = y2+"px";
  309. oStyle.left = x1+"px";
  310. oStyle.height = this.s.border.width+"px";
  311. oStyle.width = width+"px";
  312. /* Left */
  313. oStyle = this.dom.borderLeft.style;
  314. oStyle.top = y1+"px";
  315. oStyle.left = x1+"px";
  316. oStyle.height = height+"px";
  317. oStyle.width = this.s.border.width+"px";
  318. /* Right */
  319. oStyle = this.dom.borderRight.style;
  320. oStyle.top = y1+"px";
  321. oStyle.left = x2+"px";
  322. oStyle.height = height+"px";
  323. oStyle.width = this.s.border.width+"px";
  324. },
  325. /**
  326. * Mouse down event handler for starting a drag
  327. * @method _fnFillerDragStart
  328. * @param {Object} e Event object
  329. * @returns void
  330. */
  331. "_fnFillerDragStart": function (e)
  332. {
  333. var that = this;
  334. var startingTd = this.dom.currentTarget;
  335. this.s.drag.dragging = true;
  336. that.dom.borderTop.style.display = "block";
  337. that.dom.borderRight.style.display = "block";
  338. that.dom.borderBottom.style.display = "block";
  339. that.dom.borderLeft.style.display = "block";
  340. var coords = this._fnTargetCoords( startingTd );
  341. this.s.drag.startX = coords.x;
  342. this.s.drag.startY = coords.y;
  343. this.s.drag.startTd = startingTd;
  344. this.s.drag.endTd = startingTd;
  345. this._fnUpdateBorder( startingTd, startingTd );
  346. $(document).bind('mousemove.AutoFill', function (e) {
  347. that._fnFillerDragMove.call( that, e );
  348. } );
  349. $(document).bind('mouseup.AutoFill', function (e) {
  350. that._fnFillerFinish.call( that, e );
  351. } );
  352. /* Scrolling information cache */
  353. this.s.screen.y = e.pageY;
  354. this.s.screen.height = $(window).height();
  355. this.s.screen.scrollTop = $(document).scrollTop();
  356. if ( this.s.dt.oScroll.sY !== "" )
  357. {
  358. this.s.scroller.top = $(this.s.dt.nTable.parentNode).offset().top;
  359. this.s.scroller.bottom = this.s.scroller.top + $(this.s.dt.nTable.parentNode).height();
  360. }
  361. /* Scrolling handler - we set an interval (which is cancelled on mouse up) which will fire
  362. * regularly and see if we need to do any scrolling
  363. */
  364. this.s.screen.interval = setInterval( function () {
  365. var iScrollTop = $(document).scrollTop();
  366. var iScrollDelta = iScrollTop - that.s.screen.scrollTop;
  367. that.s.screen.y += iScrollDelta;
  368. if ( that.s.screen.height - that.s.screen.y + iScrollTop < 50 )
  369. {
  370. $('html, body').animate( {
  371. "scrollTop": iScrollTop + 50
  372. }, 240, 'linear' );
  373. }
  374. else if ( that.s.screen.y - iScrollTop < 50 )
  375. {
  376. $('html, body').animate( {
  377. "scrollTop": iScrollTop - 50
  378. }, 240, 'linear' );
  379. }
  380. if ( that.s.dt.oScroll.sY !== "" )
  381. {
  382. if ( that.s.screen.y > that.s.scroller.bottom - 50 )
  383. {
  384. $(that.s.dt.nTable.parentNode).animate( {
  385. "scrollTop": $(that.s.dt.nTable.parentNode).scrollTop() + 50
  386. }, 240, 'linear' );
  387. }
  388. else if ( that.s.screen.y < that.s.scroller.top + 50 )
  389. {
  390. $(that.s.dt.nTable.parentNode).animate( {
  391. "scrollTop": $(that.s.dt.nTable.parentNode).scrollTop() - 50
  392. }, 240, 'linear' );
  393. }
  394. }
  395. }, 250 );
  396. },
  397. /**
  398. * Mouse move event handler for during a move. See if we want to update the display based on the
  399. * new cursor position
  400. * @method _fnFillerDragMove
  401. * @param {Object} e Event object
  402. * @returns void
  403. */
  404. "_fnFillerDragMove": function (e)
  405. {
  406. if ( e.target && e.target.nodeName.toUpperCase() == "TD" &&
  407. e.target != this.s.drag.endTd )
  408. {
  409. var coords = this._fnTargetCoords( e.target );
  410. if ( this.c.mode == "y" && coords.x != this.s.drag.startX )
  411. {
  412. e.target = $('tbody>tr:eq('+coords.y+')>td:eq('+this.s.drag.startX+')', this.dom.table)[0];
  413. }
  414. if ( this.c.mode == "x" && coords.y != this.s.drag.startY )
  415. {
  416. e.target = $('tbody>tr:eq('+this.s.drag.startY+')>td:eq('+coords.x+')', this.dom.table)[0];
  417. }
  418. if ( this.c.mode == "either")
  419. {
  420. if(coords.x != this.s.drag.startX )
  421. {
  422. e.target = $('tbody>tr:eq('+this.s.drag.startY+')>td:eq('+coords.x+')', this.dom.table)[0];
  423. }
  424. else if ( coords.y != this.s.drag.startY ) {
  425. e.target = $('tbody>tr:eq('+coords.y+')>td:eq('+this.s.drag.startX+')', this.dom.table)[0];
  426. }
  427. }
  428. // update coords
  429. if ( this.c.mode !== "both" ) {
  430. coords = this._fnTargetCoords( e.target );
  431. }
  432. var drag = this.s.drag;
  433. drag.endTd = e.target;
  434. if ( coords.y >= this.s.drag.startY ) {
  435. this._fnUpdateBorder( drag.startTd, drag.endTd );
  436. }
  437. else {
  438. this._fnUpdateBorder( drag.endTd, drag.startTd );
  439. }
  440. this._fnFillerPosition( e.target );
  441. }
  442. /* Update the screen information so we can perform scrolling */
  443. this.s.screen.y = e.pageY;
  444. this.s.screen.scrollTop = $(document).scrollTop();
  445. if ( this.s.dt.oScroll.sY !== "" )
  446. {
  447. this.s.scroller.scrollTop = $(this.s.dt.nTable.parentNode).scrollTop();
  448. this.s.scroller.top = $(this.s.dt.nTable.parentNode).offset().top;
  449. this.s.scroller.bottom = this.s.scroller.top + $(this.s.dt.nTable.parentNode).height();
  450. }
  451. },
  452. /**
  453. * Mouse release handler - end the drag and take action to update the cells with the needed values
  454. * @method _fnFillerFinish
  455. * @param {Object} e Event object
  456. * @returns void
  457. */
  458. "_fnFillerFinish": function (e)
  459. {
  460. var that = this, i, iLen, j;
  461. $(document).unbind('mousemove.AutoFill mouseup.AutoFill');
  462. this.dom.borderTop.style.display = "none";
  463. this.dom.borderRight.style.display = "none";
  464. this.dom.borderBottom.style.display = "none";
  465. this.dom.borderLeft.style.display = "none";
  466. this.s.drag.dragging = false;
  467. clearInterval( this.s.screen.interval );
  468. var cells = [];
  469. var table = this.dom.table;
  470. var coordsStart = this._fnTargetCoords( this.s.drag.startTd );
  471. var coordsEnd = this._fnTargetCoords( this.s.drag.endTd );
  472. var columnIndex = function ( visIdx ) {
  473. return that.s.dt.oApi._fnVisibleToColumnIndex( that.s.dt, visIdx );
  474. };
  475. // xxx - urgh - there must be a way of reducing this...
  476. if ( coordsStart.y <= coordsEnd.y ) {
  477. for ( i=coordsStart.y ; i<=coordsEnd.y ; i++ ) {
  478. if ( coordsStart.x <= coordsEnd.x ) {
  479. for ( j=coordsStart.x ; j<=coordsEnd.x ; j++ ) {
  480. cells.push( {
  481. node: $('tbody>tr:eq('+i+')>td:eq('+j+')', table)[0],
  482. x: j - coordsStart.x,
  483. y: i - coordsStart.y,
  484. colIdx: columnIndex( j )
  485. } );
  486. }
  487. }
  488. else {
  489. for ( j=coordsStart.x ; j>=coordsEnd.x ; j-- ) {
  490. cells.push( {
  491. node: $('tbody>tr:eq('+i+')>td:eq('+j+')', table)[0],
  492. x: j - coordsStart.x,
  493. y: i - coordsStart.y,
  494. colIdx: columnIndex( j )
  495. } );
  496. }
  497. }
  498. }
  499. }
  500. else {
  501. for ( i=coordsStart.y ; i>=coordsEnd.y ; i-- ) {
  502. if ( coordsStart.x <= coordsEnd.x ) {
  503. for ( j=coordsStart.x ; j<=coordsEnd.x ; j++ ) {
  504. cells.push( {
  505. node: $('tbody>tr:eq('+i+')>td:eq('+j+')', table)[0],
  506. x: j - coordsStart.x,
  507. y: i - coordsStart.y,
  508. colIdx: columnIndex( j )
  509. } );
  510. }
  511. }
  512. else {
  513. for ( j=coordsStart.x ; j>=coordsEnd.x ; j-- ) {
  514. cells.push( {
  515. node: $('tbody>tr:eq('+i+')>td:eq('+j+')', table)[0],
  516. x: coordsStart.x - j,
  517. y: coordsStart.y - i,
  518. colIdx: columnIndex( j )
  519. } );
  520. }
  521. }
  522. }
  523. }
  524. // An auto-fill requires 2 or more cells
  525. if ( cells.length <= 1 ) {
  526. return;
  527. }
  528. var edited = [];
  529. var previous;
  530. for ( i=0, iLen=cells.length ; i<iLen ; i++ ) {
  531. var cell = cells[i];
  532. var column = this.s.columns[ cell.colIdx ];
  533. var read = column.read.call( column, cell.node );
  534. var stepValue = column.step.call( column, cell.node, read, previous, i, cell.x, cell.y );
  535. column.write.call( column, cell.node, stepValue );
  536. previous = stepValue;
  537. edited.push( {
  538. cell: cell,
  539. colIdx: cell.colIdx,
  540. newValue: stepValue,
  541. oldValue: read
  542. } );
  543. }
  544. if ( this.c.complete !== null ) {
  545. this.c.complete.call( this, edited );
  546. }
  547. // In 1.10 we can do a static draw
  548. if ( DataTable.Api ) {
  549. new DataTable.Api( this.s.dt ).draw( false );
  550. }
  551. else {
  552. this.s.dt.oInstance.fnDraw();
  553. }
  554. },
  555. /**
  556. * Display the drag handle on mouse over cell
  557. * @method _fnFillerDisplay
  558. * @param {Object} e Event object
  559. * @returns void
  560. */
  561. "_fnFillerDisplay": function (e)
  562. {
  563. var filler = this.dom.filler;
  564. /* Don't display automatically when dragging */
  565. if ( this.s.drag.dragging)
  566. {
  567. return;
  568. }
  569. /* Check that we are allowed to AutoFill this column or not */
  570. var nTd = (e.target.nodeName.toLowerCase() == 'td') ? e.target : $(e.target).parents('td')[0];
  571. var iX = this._fnTargetCoords(nTd).column;
  572. if ( !this.s.columns[iX].enable )
  573. {
  574. filler.style.display = "none";
  575. return;
  576. }
  577. if (e.type == 'mouseover')
  578. {
  579. this.dom.currentTarget = nTd;
  580. this._fnFillerPosition( nTd );
  581. filler.style.display = "block";
  582. }
  583. else if ( !e.relatedTarget || !e.relatedTarget.className.match(/AutoFill/) )
  584. {
  585. filler.style.display = "none";
  586. }
  587. },
  588. /**
  589. * Position the filler icon over a cell
  590. * @method _fnFillerPosition
  591. * @param {Node} nTd Cell to position filler icon over
  592. * @returns void
  593. */
  594. "_fnFillerPosition": function ( nTd )
  595. {
  596. var offset = $(nTd).offset();
  597. var filler = this.dom.filler;
  598. filler.style.top = (offset.top - (this.s.filler.height / 2)-1 + $(nTd).outerHeight())+"px";
  599. filler.style.left = (offset.left - (this.s.filler.width / 2)-1 + $(nTd).outerWidth())+"px";
  600. }
  601. };
  602. // Alias for access
  603. DataTable.AutoFill = AutoFill;
  604. DataTable.AutoFill = AutoFill;
  605. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  606. * Constants
  607. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  608. /**
  609. * AutoFill version
  610. * @constant version
  611. * @type String
  612. * @default See code
  613. */
  614. AutoFill.version = "1.2.1";
  615. /**
  616. * AutoFill defaults
  617. * @namespace
  618. */
  619. AutoFill.defaults = {
  620. /**
  621. * Mode for dragging (restrict to y-axis only, x-axis only, either one or none):
  622. *
  623. * * `y` - y-axis only (default)
  624. * * `x` - x-axis only
  625. * * `either` - either one, but not both axis at the same time
  626. * * `both` - multiple cells allowed
  627. *
  628. * @type {string}
  629. * @default `y`
  630. */
  631. mode: 'y',
  632. complete: null,
  633. /**
  634. * Column definition defaults
  635. * @namespace
  636. */
  637. column: {
  638. /**
  639. * If AutoFill should be enabled on this column
  640. *
  641. * @type {boolean}
  642. * @default true
  643. */
  644. enable: true,
  645. /**
  646. * Allow automatic increment / decrement on this column if a number
  647. * is found.
  648. *
  649. * @type {boolean}
  650. * @default true
  651. */
  652. increment: true,
  653. /**
  654. * Cell read function
  655. *
  656. * Default function will simply read the value from the HTML of the
  657. * cell.
  658. *
  659. * @type {function}
  660. * @param {node} cell `th` / `td` element to read the value from
  661. * @return {string} Data that has been read
  662. */
  663. read: function ( cell ) {
  664. return $(cell).html();
  665. },
  666. /**
  667. * Cell write function
  668. *
  669. * Default function will simply write to the HTML and tell the DataTable
  670. * to update.
  671. *
  672. * @type {function}
  673. * @param {node} cell `th` / `td` element to write the value to
  674. * @return {string} Data two write
  675. */
  676. write: function ( cell, val ) {
  677. var table = $(cell).parents('table');
  678. if ( DataTable.Api ) {
  679. // 1.10
  680. table.DataTable().cell( cell ).data( val );
  681. }
  682. else {
  683. // 1.9
  684. var dt = table.dataTable();
  685. var pos = dt.fnGetPosition( cell );
  686. dt.fnUpdate( val, pos[0], pos[2], false );
  687. }
  688. },
  689. /**
  690. * Step function. This provides the ability to customise how the values
  691. * are incremented.
  692. *
  693. * @param {node} cell `th` / `td` element that is being operated upon
  694. * @param {string} read Cell value from `read` function
  695. * @param {string} last Value of the previous cell
  696. * @param {integer} i Loop counter
  697. * @param {integer} x Cell x-position in the current auto-fill. The
  698. * starting cell is coordinate 0 regardless of its physical position
  699. * in the DataTable.
  700. * @param {integer} y Cell y-position in the current auto-fill. The
  701. * starting cell is coordinate 0 regardless of its physical position
  702. * in the DataTable.
  703. * @return {string} Value to write
  704. */
  705. step: function ( cell, read, last, i, x, y ) {
  706. // Increment a number if it is found
  707. var re = /(\-?\d+)/;
  708. var match = this.increment && last ? last.match(re) : null;
  709. if ( match ) {
  710. return last.replace( re, parseInt(match[1],10) + (x<0 || y<0 ? -1 : 1) );
  711. }
  712. return last === undefined ?
  713. read :
  714. last;
  715. }
  716. }
  717. };
  718. return AutoFill;
  719. };
  720. // Define as an AMD module if possible
  721. if ( typeof define === 'function' && define.amd ) {
  722. define( ['jquery', 'datatables'], factory );
  723. }
  724. else if ( typeof exports === 'object' ) {
  725. // Node/CommonJS
  726. factory( require('jquery'), require('datatables') );
  727. }
  728. else if ( jQuery && !jQuery.fn.dataTable.AutoFill ) {
  729. // Otherwise simply initialise as normal, stopping multiple evaluation
  730. factory( jQuery, jQuery.fn.dataTable );
  731. }
  732. }(window, document));