You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dataTables.autoFill.js 39KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. /*! AutoFill 2.3.5
  2. * ©2008-2020 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 2.3.5
  8. * @file dataTables.autoFill.js
  9. * @author SpryMedia Ltd (www.sprymedia.co.uk)
  10. * @contact www.sprymedia.co.uk/contact
  11. * @copyright Copyright 2010-2020 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. } else if (typeof exports === 'object') {
  29. // CommonJS
  30. module.exports = function (root, $) {
  31. if (!root) {
  32. root = window;
  33. }
  34. if (!$ || !$.fn.dataTable) {
  35. $ = require('datatables.net')(root, $).$;
  36. }
  37. return factory($, root, root.document);
  38. };
  39. } else {
  40. // Browser
  41. factory(jQuery, window, document);
  42. }
  43. }(function ($, window, document, undefined) {
  44. 'use strict';
  45. var DataTable = $.fn.dataTable;
  46. var _instance = 0;
  47. /**
  48. * AutoFill provides Excel like auto-fill features for a DataTable
  49. *
  50. * @class AutoFill
  51. * @constructor
  52. * @param {object} oTD DataTables settings object
  53. * @param {object} oConfig Configuration object for AutoFill
  54. */
  55. var AutoFill = function (dt, opts) {
  56. if (!DataTable.versionCheck || !DataTable.versionCheck('1.10.8')) {
  57. throw("Warning: AutoFill requires DataTables 1.10.8 or greater");
  58. }
  59. // User and defaults configuration object
  60. this.c = $.extend(true, {},
  61. DataTable.defaults.autoFill,
  62. AutoFill.defaults,
  63. opts
  64. );
  65. /**
  66. * @namespace Settings object which contains customisable information for AutoFill instance
  67. */
  68. this.s = {
  69. /** @type {DataTable.Api} DataTables' API instance */
  70. dt: new DataTable.Api(dt),
  71. /** @type {String} Unique namespace for events attached to the document */
  72. namespace: '.autoFill' + (_instance++),
  73. /** @type {Object} Cached dimension information for use in the mouse move event handler */
  74. scroll: {},
  75. /** @type {integer} Interval object used for smooth scrolling */
  76. scrollInterval: null,
  77. handle: {
  78. height: 0,
  79. width: 0
  80. },
  81. /**
  82. * Enabled setting
  83. * @type {Boolean}
  84. */
  85. enabled: false
  86. };
  87. /**
  88. * @namespace Common and useful DOM elements for the class instance
  89. */
  90. this.dom = {
  91. /** @type {jQuery} AutoFill handle */
  92. handle: $('<div class="dt-autofill-handle"/>'),
  93. /**
  94. * @type {Object} Selected cells outline - Need to use 4 elements,
  95. * otherwise the mouse over if you back into the selected rectangle
  96. * will be over that element, rather than the cells!
  97. */
  98. select: {
  99. top: $('<div class="dt-autofill-select top"/>'),
  100. right: $('<div class="dt-autofill-select right"/>'),
  101. bottom: $('<div class="dt-autofill-select bottom"/>'),
  102. left: $('<div class="dt-autofill-select left"/>')
  103. },
  104. /** @type {jQuery} Fill type chooser background */
  105. background: $('<div class="dt-autofill-background"/>'),
  106. /** @type {jQuery} Fill type chooser */
  107. list: $('<div class="dt-autofill-list">' + this.s.dt.i18n('autoFill.info', '') + '<ul/></div>'),
  108. /** @type {jQuery} DataTables scrolling container */
  109. dtScroll: null,
  110. /** @type {jQuery} Offset parent element */
  111. offsetParent: null
  112. };
  113. /* Constructor logic */
  114. this._constructor();
  115. };
  116. $.extend(AutoFill.prototype, {
  117. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  118. * Public methods (exposed via the DataTables API below)
  119. */
  120. enabled: function () {
  121. return this.s.enabled;
  122. },
  123. enable: function (flag) {
  124. var that = this;
  125. if (flag === false) {
  126. return this.disable();
  127. }
  128. this.s.enabled = true;
  129. this._focusListener();
  130. this.dom.handle.on('mousedown', function (e) {
  131. that._mousedown(e);
  132. return false;
  133. });
  134. return this;
  135. },
  136. disable: function () {
  137. this.s.enabled = false;
  138. this._focusListenerRemove();
  139. return this;
  140. },
  141. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  142. * Constructor
  143. */
  144. /**
  145. * Initialise the RowReorder instance
  146. *
  147. * @private
  148. */
  149. _constructor: function () {
  150. var that = this;
  151. var dt = this.s.dt;
  152. var dtScroll = $('div.dataTables_scrollBody', this.s.dt.table().container());
  153. // Make the instance accessible to the API
  154. dt.settings()[0].autoFill = this;
  155. if (dtScroll.length) {
  156. this.dom.dtScroll = dtScroll;
  157. // Need to scroll container to be the offset parent
  158. if (dtScroll.css('position') === 'static') {
  159. dtScroll.css('position', 'relative');
  160. }
  161. }
  162. if (this.c.enable !== false) {
  163. this.enable();
  164. }
  165. dt.on('destroy.autoFill', function () {
  166. that._focusListenerRemove();
  167. });
  168. },
  169. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  170. * Private methods
  171. */
  172. /**
  173. * Display the AutoFill drag handle by appending it to a table cell. This
  174. * is the opposite of the _detach method.
  175. *
  176. * @param {node} node TD/TH cell to insert the handle into
  177. * @private
  178. */
  179. _attach: function (node) {
  180. var dt = this.s.dt;
  181. var idx = dt.cell(node).index();
  182. var handle = this.dom.handle;
  183. var handleDim = this.s.handle;
  184. if (!idx || dt.columns(this.c.columns).indexes().indexOf(idx.column) === -1) {
  185. this._detach();
  186. return;
  187. }
  188. if (!this.dom.offsetParent) {
  189. // We attach to the table's offset parent
  190. this.dom.offsetParent = $(dt.table().node()).offsetParent();
  191. }
  192. if (!handleDim.height || !handleDim.width) {
  193. // Append to document so we can get its size. Not expecting it to
  194. // change during the life time of the page
  195. handle.appendTo('body');
  196. handleDim.height = handle.outerHeight();
  197. handleDim.width = handle.outerWidth();
  198. }
  199. // Might need to go through multiple offset parents
  200. var offset = this._getPosition(node, this.dom.offsetParent);
  201. this.dom.attachedTo = node;
  202. handle
  203. .css({
  204. top: offset.top + node.offsetHeight - handleDim.height,
  205. left: offset.left + node.offsetWidth - handleDim.width
  206. })
  207. .appendTo(this.dom.offsetParent);
  208. },
  209. /**
  210. * Determine can the fill type should be. This can be automatic, or ask the
  211. * end user.
  212. *
  213. * @param {array} cells Information about the selected cells from the key
  214. * up function
  215. * @private
  216. */
  217. _actionSelector: function (cells) {
  218. var that = this;
  219. var dt = this.s.dt;
  220. var actions = AutoFill.actions;
  221. var available = [];
  222. // "Ask" each plug-in if it wants to handle this data
  223. $.each(actions, function (key, action) {
  224. if (action.available(dt, cells)) {
  225. available.push(key);
  226. }
  227. });
  228. if (available.length === 1 && this.c.alwaysAsk === false) {
  229. // Only one action available - enact it immediately
  230. var result = actions[available[0]].execute(dt, cells);
  231. this._update(result, cells);
  232. } else if (available.length > 1) {
  233. // Multiple actions available - ask the end user what they want to do
  234. var list = this.dom.list.children('ul').empty();
  235. // Add a cancel option
  236. available.push('cancel');
  237. $.each(available, function (i, name) {
  238. list.append($('<li/>')
  239. .append(
  240. '<div class="dt-autofill-question">' +
  241. actions[name].option(dt, cells) +
  242. '<div>'
  243. )
  244. .append($('<div class="dt-autofill-button">')
  245. .append($('<button class="' + AutoFill.classes.btn + '">' + dt.i18n('autoFill.button', '&gt;') + '</button>')
  246. .on('click', function () {
  247. var result = actions[name].execute(
  248. dt, cells, $(this).closest('li')
  249. );
  250. that._update(result, cells);
  251. that.dom.background.remove();
  252. that.dom.list.remove();
  253. })
  254. )
  255. )
  256. );
  257. });
  258. this.dom.background.appendTo('body');
  259. this.dom.list.appendTo('body');
  260. this.dom.list.css('margin-top', this.dom.list.outerHeight() / 2 * -1);
  261. }
  262. },
  263. /**
  264. * Remove the AutoFill handle from the document
  265. *
  266. * @private
  267. */
  268. _detach: function () {
  269. this.dom.attachedTo = null;
  270. this.dom.handle.detach();
  271. },
  272. /**
  273. * Draw the selection outline by calculating the range between the start
  274. * and end cells, then placing the highlighting elements to draw a rectangle
  275. *
  276. * @param {node} target End cell
  277. * @param {object} e Originating event
  278. * @private
  279. */
  280. _drawSelection: function (target, e) {
  281. // Calculate boundary for start cell to this one
  282. var dt = this.s.dt;
  283. var start = this.s.start;
  284. var startCell = $(this.dom.start);
  285. var end = {
  286. row: this.c.vertical ?
  287. dt.rows({page: 'current'}).nodes().indexOf(target.parentNode) :
  288. start.row,
  289. column: this.c.horizontal ?
  290. $(target).index() :
  291. start.column
  292. };
  293. var colIndx = dt.column.index('toData', end.column);
  294. var endRow = dt.row(':eq(' + end.row + ')', {page: 'current'}); // Workaround for M581
  295. var endCell = $(dt.cell(endRow.index(), colIndx).node());
  296. // Be sure that is a DataTables controlled cell
  297. if (!dt.cell(endCell).any()) {
  298. return;
  299. }
  300. // if target is not in the columns available - do nothing
  301. if (dt.columns(this.c.columns).indexes().indexOf(colIndx) === -1) {
  302. return;
  303. }
  304. this.s.end = end;
  305. var top, bottom, left, right, height, width;
  306. top = start.row < end.row ? startCell : endCell;
  307. bottom = start.row < end.row ? endCell : startCell;
  308. left = start.column < end.column ? startCell : endCell;
  309. right = start.column < end.column ? endCell : startCell;
  310. top = this._getPosition(top.get(0)).top;
  311. left = this._getPosition(left.get(0)).left;
  312. height = this._getPosition(bottom.get(0)).top + bottom.outerHeight() - top;
  313. width = this._getPosition(right.get(0)).left + right.outerWidth() - left;
  314. var select = this.dom.select;
  315. select.top.css({
  316. top: top,
  317. left: left,
  318. width: width
  319. });
  320. select.left.css({
  321. top: top,
  322. left: left,
  323. height: height
  324. });
  325. select.bottom.css({
  326. top: top + height,
  327. left: left,
  328. width: width
  329. });
  330. select.right.css({
  331. top: top,
  332. left: left + width,
  333. height: height
  334. });
  335. },
  336. /**
  337. * Use the Editor API to perform an update based on the new data for the
  338. * cells
  339. *
  340. * @param {array} cells Information about the selected cells from the key
  341. * up function
  342. * @private
  343. */
  344. _editor: function (cells) {
  345. var dt = this.s.dt;
  346. var editor = this.c.editor;
  347. if (!editor) {
  348. return;
  349. }
  350. // Build the object structure for Editor's multi-row editing
  351. var idValues = {};
  352. var nodes = [];
  353. var fields = editor.fields();
  354. for (var i = 0, ien = cells.length; i < ien; i++) {
  355. for (var j = 0, jen = cells[i].length; j < jen; j++) {
  356. var cell = cells[i][j];
  357. // Determine the field name for the cell being edited
  358. var col = dt.settings()[0].aoColumns[cell.index.column];
  359. var fieldName = col.editField;
  360. if (fieldName === undefined) {
  361. var dataSrc = col.mData;
  362. // dataSrc is the `field.data` property, but we need to set
  363. // using the field name, so we need to translate from the
  364. // data to the name
  365. for (var k = 0, ken = fields.length; k < ken; k++) {
  366. var field = editor.field(fields[k]);
  367. if (field.dataSrc() === dataSrc) {
  368. fieldName = field.name();
  369. break;
  370. }
  371. }
  372. }
  373. if (!fieldName) {
  374. throw 'Could not automatically determine field data. ' +
  375. 'Please see https://datatables.net/tn/11';
  376. }
  377. if (!idValues[fieldName]) {
  378. idValues[fieldName] = {};
  379. }
  380. var id = dt.row(cell.index.row).id();
  381. idValues[fieldName][id] = cell.set;
  382. // Keep a list of cells so we can activate the bubble editing
  383. // with them
  384. nodes.push(cell.index);
  385. }
  386. }
  387. // Perform the edit using bubble editing as it allows us to specify
  388. // the cells to be edited, rather than using full rows
  389. editor
  390. .bubble(nodes, false)
  391. .multiSet(idValues)
  392. .submit();
  393. },
  394. /**
  395. * Emit an event on the DataTable for listeners
  396. *
  397. * @param {string} name Event name
  398. * @param {array} args Event arguments
  399. * @private
  400. */
  401. _emitEvent: function (name, args) {
  402. this.s.dt.iterator('table', function (ctx, i) {
  403. $(ctx.nTable).triggerHandler(name + '.dt', args);
  404. });
  405. },
  406. /**
  407. * Attach suitable listeners (based on the configuration) that will attach
  408. * and detach the AutoFill handle in the document.
  409. *
  410. * @private
  411. */
  412. _focusListener: function () {
  413. var that = this;
  414. var dt = this.s.dt;
  415. var namespace = this.s.namespace;
  416. var focus = this.c.focus !== null ?
  417. this.c.focus :
  418. dt.init().keys || dt.settings()[0].keytable ?
  419. 'focus' :
  420. 'hover';
  421. // All event listeners attached here are removed in the `destroy`
  422. // callback in the constructor
  423. if (focus === 'focus') {
  424. dt
  425. .on('key-focus.autoFill', function (e, dt, cell) {
  426. that._attach(cell.node());
  427. })
  428. .on('key-blur.autoFill', function (e, dt, cell) {
  429. that._detach();
  430. });
  431. } else if (focus === 'click') {
  432. $(dt.table().body()).on('click' + namespace, 'td, th', function (e) {
  433. that._attach(this);
  434. });
  435. $(document.body).on('click' + namespace, function (e) {
  436. if (!$(e.target).parents().filter(dt.table().body()).length) {
  437. that._detach();
  438. }
  439. });
  440. } else {
  441. $(dt.table().body())
  442. .on('mouseenter' + namespace, 'td, th', function (e) {
  443. that._attach(this);
  444. })
  445. .on('mouseleave' + namespace, function (e) {
  446. if ($(e.relatedTarget).hasClass('dt-autofill-handle')) {
  447. return;
  448. }
  449. that._detach();
  450. });
  451. }
  452. },
  453. _focusListenerRemove: function () {
  454. var dt = this.s.dt;
  455. dt.off('.autoFill');
  456. $(dt.table().body()).off(this.s.namespace);
  457. $(document.body).off(this.s.namespace);
  458. },
  459. /**
  460. * Get the position of a node, relative to another, including any scrolling
  461. * offsets.
  462. * @param {Node} node Node to get the position of
  463. * @param {jQuery} targetParent Node to use as the parent
  464. * @return {object} Offset calculation
  465. * @private
  466. */
  467. _getPosition: function (node, targetParent) {
  468. var
  469. currNode = node,
  470. currOffsetParent,
  471. top = 0,
  472. left = 0;
  473. if (!targetParent) {
  474. targetParent = $($(this.s.dt.table().node())[0].offsetParent);
  475. }
  476. do {
  477. // Don't use jQuery().position() the behaviour changes between 1.x and 3.x for
  478. // tables
  479. var positionTop = currNode.offsetTop;
  480. var positionLeft = currNode.offsetLeft;
  481. // jQuery doesn't give a `table` as the offset parent oddly, so use DOM directly
  482. currOffsetParent = $(currNode.offsetParent);
  483. top += positionTop + parseInt(currOffsetParent.css('border-top-width')) * 1;
  484. left += positionLeft + parseInt(currOffsetParent.css('border-left-width')) * 1;
  485. // Emergency fall back. Shouldn't happen, but just in case!
  486. if (currNode.nodeName.toLowerCase() === 'body') {
  487. break;
  488. }
  489. currNode = currOffsetParent.get(0); // for next loop
  490. }
  491. while (currOffsetParent.get(0) !== targetParent.get(0))
  492. return {
  493. top: top,
  494. left: left
  495. };
  496. },
  497. /**
  498. * Start mouse drag - selects the start cell
  499. *
  500. * @param {object} e Mouse down event
  501. * @private
  502. */
  503. _mousedown: function (e) {
  504. var that = this;
  505. var dt = this.s.dt;
  506. this.dom.start = this.dom.attachedTo;
  507. this.s.start = {
  508. row: dt.rows({page: 'current'}).nodes().indexOf($(this.dom.start).parent()[0]),
  509. column: $(this.dom.start).index()
  510. };
  511. $(document.body)
  512. .on('mousemove.autoFill', function (e) {
  513. that._mousemove(e);
  514. })
  515. .on('mouseup.autoFill', function (e) {
  516. that._mouseup(e);
  517. });
  518. var select = this.dom.select;
  519. var offsetParent = $(dt.table().node()).offsetParent();
  520. select.top.appendTo(offsetParent);
  521. select.left.appendTo(offsetParent);
  522. select.right.appendTo(offsetParent);
  523. select.bottom.appendTo(offsetParent);
  524. this._drawSelection(this.dom.start, e);
  525. this.dom.handle.css('display', 'none');
  526. // Cache scrolling information so mouse move doesn't need to read.
  527. // This assumes that the window and DT scroller will not change size
  528. // during an AutoFill drag, which I think is a fair assumption
  529. var scrollWrapper = this.dom.dtScroll;
  530. this.s.scroll = {
  531. windowHeight: $(window).height(),
  532. windowWidth: $(window).width(),
  533. dtTop: scrollWrapper ? scrollWrapper.offset().top : null,
  534. dtLeft: scrollWrapper ? scrollWrapper.offset().left : null,
  535. dtHeight: scrollWrapper ? scrollWrapper.outerHeight() : null,
  536. dtWidth: scrollWrapper ? scrollWrapper.outerWidth() : null
  537. };
  538. },
  539. /**
  540. * Mouse drag - selects the end cell and update the selection display for
  541. * the end user
  542. *
  543. * @param {object} e Mouse move event
  544. * @private
  545. */
  546. _mousemove: function (e) {
  547. var that = this;
  548. var dt = this.s.dt;
  549. var name = e.target.nodeName.toLowerCase();
  550. if (name !== 'td' && name !== 'th') {
  551. return;
  552. }
  553. this._drawSelection(e.target, e);
  554. this._shiftScroll(e);
  555. },
  556. /**
  557. * End mouse drag - perform the update actions
  558. *
  559. * @param {object} e Mouse up event
  560. * @private
  561. */
  562. _mouseup: function (e) {
  563. $(document.body).off('.autoFill');
  564. var that = this;
  565. var dt = this.s.dt;
  566. var select = this.dom.select;
  567. select.top.remove();
  568. select.left.remove();
  569. select.right.remove();
  570. select.bottom.remove();
  571. this.dom.handle.css('display', 'block');
  572. // Display complete - now do something useful with the selection!
  573. var start = this.s.start;
  574. var end = this.s.end;
  575. // Haven't selected multiple cells, so nothing to do
  576. if (start.row === end.row && start.column === end.column) {
  577. return;
  578. }
  579. var startDt = dt.cell(':eq(' + start.row + ')', start.column + ':visible', {page: 'current'});
  580. // If Editor is active inside this cell (inline editing) we need to wait for Editor to
  581. // submit and then we can loop back and trigger the fill.
  582. if ($('div.DTE', startDt.node()).length) {
  583. var editor = dt.editor();
  584. editor
  585. .on('submitSuccess.dtaf close.dtaf', function () {
  586. editor.off('.dtaf');
  587. setTimeout(function () {
  588. that._mouseup(e);
  589. }, 100);
  590. })
  591. .on('submitComplete.dtaf preSubmitCancelled.dtaf close.dtaf', function () {
  592. editor.off('.dtaf');
  593. });
  594. // Make the current input submit
  595. editor.submit();
  596. return;
  597. }
  598. // Build a matrix representation of the selected rows
  599. var rows = this._range(start.row, end.row);
  600. var columns = this._range(start.column, end.column);
  601. var selected = [];
  602. var dtSettings = dt.settings()[0];
  603. var dtColumns = dtSettings.aoColumns;
  604. var enabledColumns = dt.columns(this.c.columns).indexes();
  605. // Can't use Array.prototype.map as IE8 doesn't support it
  606. // Can't use $.map as jQuery flattens 2D arrays
  607. // Need to use a good old fashioned for loop
  608. for (var rowIdx = 0; rowIdx < rows.length; rowIdx++) {
  609. selected.push(
  610. $.map(columns, function (column) {
  611. var row = dt.row(':eq(' + rows[rowIdx] + ')', {page: 'current'}); // Workaround for M581
  612. var cell = dt.cell(row.index(), column + ':visible');
  613. var data = cell.data();
  614. var cellIndex = cell.index();
  615. var editField = dtColumns[cellIndex.column].editField;
  616. if (editField !== undefined) {
  617. data = dtSettings.oApi._fnGetObjectDataFn(editField)(dt.row(cellIndex.row).data());
  618. }
  619. if (enabledColumns.indexOf(cellIndex.column) === -1) {
  620. return;
  621. }
  622. return {
  623. cell: cell,
  624. data: data,
  625. label: cell.data(),
  626. index: cellIndex
  627. };
  628. })
  629. );
  630. }
  631. this._actionSelector(selected);
  632. // Stop shiftScroll
  633. clearInterval(this.s.scrollInterval);
  634. this.s.scrollInterval = null;
  635. },
  636. /**
  637. * Create an array with a range of numbers defined by the start and end
  638. * parameters passed in (inclusive!).
  639. *
  640. * @param {integer} start Start
  641. * @param {integer} end End
  642. * @private
  643. */
  644. _range: function (start, end) {
  645. var out = [];
  646. var i;
  647. if (start <= end) {
  648. for (i = start; i <= end; i++) {
  649. out.push(i);
  650. }
  651. } else {
  652. for (i = start; i >= end; i--) {
  653. out.push(i);
  654. }
  655. }
  656. return out;
  657. },
  658. /**
  659. * Move the window and DataTables scrolling during a drag to scroll new
  660. * content into view. This is done by proximity to the edge of the scrolling
  661. * container of the mouse - for example near the top edge of the window
  662. * should scroll up. This is a little complicated as there are two elements
  663. * that can be scrolled - the window and the DataTables scrolling view port
  664. * (if scrollX and / or scrollY is enabled).
  665. *
  666. * @param {object} e Mouse move event object
  667. * @private
  668. */
  669. _shiftScroll: function (e) {
  670. var that = this;
  671. var dt = this.s.dt;
  672. var scroll = this.s.scroll;
  673. var runInterval = false;
  674. var scrollSpeed = 5;
  675. var buffer = 65;
  676. var
  677. windowY = e.pageY - document.body.scrollTop,
  678. windowX = e.pageX - document.body.scrollLeft,
  679. windowVert, windowHoriz,
  680. dtVert, dtHoriz;
  681. // Window calculations - based on the mouse position in the window,
  682. // regardless of scrolling
  683. if (windowY < buffer) {
  684. windowVert = scrollSpeed * -1;
  685. } else if (windowY > scroll.windowHeight - buffer) {
  686. windowVert = scrollSpeed;
  687. }
  688. if (windowX < buffer) {
  689. windowHoriz = scrollSpeed * -1;
  690. } else if (windowX > scroll.windowWidth - buffer) {
  691. windowHoriz = scrollSpeed;
  692. }
  693. // DataTables scrolling calculations - based on the table's position in
  694. // the document and the mouse position on the page
  695. if (scroll.dtTop !== null && e.pageY < scroll.dtTop + buffer) {
  696. dtVert = scrollSpeed * -1;
  697. } else if (scroll.dtTop !== null && e.pageY > scroll.dtTop + scroll.dtHeight - buffer) {
  698. dtVert = scrollSpeed;
  699. }
  700. if (scroll.dtLeft !== null && e.pageX < scroll.dtLeft + buffer) {
  701. dtHoriz = scrollSpeed * -1;
  702. } else if (scroll.dtLeft !== null && e.pageX > scroll.dtLeft + scroll.dtWidth - buffer) {
  703. dtHoriz = scrollSpeed;
  704. }
  705. // This is where it gets interesting. We want to continue scrolling
  706. // without requiring a mouse move, so we need an interval to be
  707. // triggered. The interval should continue until it is no longer needed,
  708. // but it must also use the latest scroll commands (for example consider
  709. // that the mouse might move from scrolling up to scrolling left, all
  710. // with the same interval running. We use the `scroll` object to "pass"
  711. // this information to the interval. Can't use local variables as they
  712. // wouldn't be the ones that are used by an already existing interval!
  713. if (windowVert || windowHoriz || dtVert || dtHoriz) {
  714. scroll.windowVert = windowVert;
  715. scroll.windowHoriz = windowHoriz;
  716. scroll.dtVert = dtVert;
  717. scroll.dtHoriz = dtHoriz;
  718. runInterval = true;
  719. } else if (this.s.scrollInterval) {
  720. // Don't need to scroll - remove any existing timer
  721. clearInterval(this.s.scrollInterval);
  722. this.s.scrollInterval = null;
  723. }
  724. // If we need to run the interval to scroll and there is no existing
  725. // interval (if there is an existing one, it will continue to run)
  726. if (!this.s.scrollInterval && runInterval) {
  727. this.s.scrollInterval = setInterval(function () {
  728. // Don't need to worry about setting scroll <0 or beyond the
  729. // scroll bound as the browser will just reject that.
  730. if (scroll.windowVert) {
  731. document.body.scrollTop += scroll.windowVert;
  732. }
  733. if (scroll.windowHoriz) {
  734. document.body.scrollLeft += scroll.windowHoriz;
  735. }
  736. // DataTables scrolling
  737. if (scroll.dtVert || scroll.dtHoriz) {
  738. var scroller = that.dom.dtScroll[0];
  739. if (scroll.dtVert) {
  740. scroller.scrollTop += scroll.dtVert;
  741. }
  742. if (scroll.dtHoriz) {
  743. scroller.scrollLeft += scroll.dtHoriz;
  744. }
  745. }
  746. }, 20);
  747. }
  748. },
  749. /**
  750. * Update the DataTable after the user has selected what they want to do
  751. *
  752. * @param {false|undefined} result Return from the `execute` method - can
  753. * be false internally to do nothing. This is not documented for plug-ins
  754. * and is used only by the cancel option.
  755. * @param {array} cells Information about the selected cells from the key
  756. * up function, argumented with the set values
  757. * @private
  758. */
  759. _update: function (result, cells) {
  760. // Do nothing on `false` return from an execute function
  761. if (result === false) {
  762. return;
  763. }
  764. var dt = this.s.dt;
  765. var cell;
  766. var columns = dt.columns(this.c.columns).indexes();
  767. // Potentially allow modifications to the cells matrix
  768. this._emitEvent('preAutoFill', [dt, cells]);
  769. this._editor(cells);
  770. // Automatic updates are not performed if `update` is null and the
  771. // `editor` parameter is passed in - the reason being that Editor will
  772. // update the data once submitted
  773. var update = this.c.update !== null ?
  774. this.c.update :
  775. this.c.editor ?
  776. false :
  777. true;
  778. if (update) {
  779. for (var i = 0, ien = cells.length; i < ien; i++) {
  780. for (var j = 0, jen = cells[i].length; j < jen; j++) {
  781. cell = cells[i][j];
  782. if (columns.indexOf(cell.index.column) !== -1) {
  783. cell.cell.data(cell.set);
  784. }
  785. }
  786. }
  787. dt.draw(false);
  788. }
  789. this._emitEvent('autoFill', [dt, cells]);
  790. }
  791. });
  792. /**
  793. * AutoFill actions. The options here determine how AutoFill will fill the data
  794. * in the table when the user has selected a range of cells. Please see the
  795. * documentation on the DataTables site for full details on how to create plug-
  796. * ins.
  797. *
  798. * @type {Object}
  799. */
  800. AutoFill.actions = {
  801. increment: {
  802. available: function (dt, cells) {
  803. var d = cells[0][0].label;
  804. // is numeric test based on jQuery's old `isNumeric` function
  805. return !isNaN(d - parseFloat(d));
  806. },
  807. option: function (dt, cells) {
  808. return dt.i18n(
  809. 'autoFill.increment',
  810. 'Increment / decrement each cell by: <input type="number" value="1">'
  811. );
  812. },
  813. execute: function (dt, cells, node) {
  814. var value = cells[0][0].data * 1;
  815. var increment = $('input', node).val() * 1;
  816. for (var i = 0, ien = cells.length; i < ien; i++) {
  817. for (var j = 0, jen = cells[i].length; j < jen; j++) {
  818. cells[i][j].set = value;
  819. value += increment;
  820. }
  821. }
  822. }
  823. },
  824. fill: {
  825. available: function (dt, cells) {
  826. return true;
  827. },
  828. option: function (dt, cells) {
  829. return dt.i18n('autoFill.fill', 'Fill all cells with <i>' + cells[0][0].label + '</i>');
  830. },
  831. execute: function (dt, cells, node) {
  832. var value = cells[0][0].data;
  833. for (var i = 0, ien = cells.length; i < ien; i++) {
  834. for (var j = 0, jen = cells[i].length; j < jen; j++) {
  835. cells[i][j].set = value;
  836. }
  837. }
  838. }
  839. },
  840. fillHorizontal: {
  841. available: function (dt, cells) {
  842. return cells.length > 1 && cells[0].length > 1;
  843. },
  844. option: function (dt, cells) {
  845. return dt.i18n('autoFill.fillHorizontal', 'Fill cells horizontally');
  846. },
  847. execute: function (dt, cells, node) {
  848. for (var i = 0, ien = cells.length; i < ien; i++) {
  849. for (var j = 0, jen = cells[i].length; j < jen; j++) {
  850. cells[i][j].set = cells[i][0].data;
  851. }
  852. }
  853. }
  854. },
  855. fillVertical: {
  856. available: function (dt, cells) {
  857. return cells.length > 1;
  858. },
  859. option: function (dt, cells) {
  860. return dt.i18n('autoFill.fillVertical', 'Fill cells vertically');
  861. },
  862. execute: function (dt, cells, node) {
  863. for (var i = 0, ien = cells.length; i < ien; i++) {
  864. for (var j = 0, jen = cells[i].length; j < jen; j++) {
  865. cells[i][j].set = cells[0][j].data;
  866. }
  867. }
  868. }
  869. },
  870. // Special type that does not make itself available, but is added
  871. // automatically by AutoFill if a multi-choice list is shown. This allows
  872. // sensible code reuse
  873. cancel: {
  874. available: function () {
  875. return false;
  876. },
  877. option: function (dt) {
  878. return dt.i18n('autoFill.cancel', 'Cancel');
  879. },
  880. execute: function () {
  881. return false;
  882. }
  883. }
  884. };
  885. /**
  886. * AutoFill version
  887. *
  888. * @static
  889. * @type String
  890. */
  891. AutoFill.version = '2.3.5';
  892. /**
  893. * AutoFill defaults
  894. *
  895. * @namespace
  896. */
  897. AutoFill.defaults = {
  898. /** @type {Boolean} Ask user what they want to do, even for a single option */
  899. alwaysAsk: false,
  900. /** @type {string|null} What will trigger a focus */
  901. focus: null, // focus, click, hover
  902. /** @type {column-selector} Columns to provide auto fill for */
  903. columns: '', // all
  904. /** @type {Boolean} Enable AutoFill on load */
  905. enable: true,
  906. /** @type {boolean|null} Update the cells after a drag */
  907. update: null, // false is editor given, true otherwise
  908. /** @type {DataTable.Editor} Editor instance for automatic submission */
  909. editor: null,
  910. /** @type {boolean} Enable vertical fill */
  911. vertical: true,
  912. /** @type {boolean} Enable horizontal fill */
  913. horizontal: true
  914. };
  915. /**
  916. * Classes used by AutoFill that are configurable
  917. *
  918. * @namespace
  919. */
  920. AutoFill.classes = {
  921. /** @type {String} Class used by the selection button */
  922. btn: 'btn'
  923. };
  924. /*
  925. * API
  926. */
  927. var Api = $.fn.dataTable.Api;
  928. // Doesn't do anything - Not documented
  929. Api.register('autoFill()', function () {
  930. return this;
  931. });
  932. Api.register('autoFill().enabled()', function () {
  933. var ctx = this.context[0];
  934. return ctx.autoFill ?
  935. ctx.autoFill.enabled() :
  936. false;
  937. });
  938. Api.register('autoFill().enable()', function (flag) {
  939. return this.iterator('table', function (ctx) {
  940. if (ctx.autoFill) {
  941. ctx.autoFill.enable(flag);
  942. }
  943. });
  944. });
  945. Api.register('autoFill().disable()', function () {
  946. return this.iterator('table', function (ctx) {
  947. if (ctx.autoFill) {
  948. ctx.autoFill.disable();
  949. }
  950. });
  951. });
  952. // Attach a listener to the document which listens for DataTables initialisation
  953. // events so we can automatically initialise
  954. $(document).on('preInit.dt.autofill', function (e, settings, json) {
  955. if (e.namespace !== 'dt') {
  956. return;
  957. }
  958. var init = settings.oInit.autoFill;
  959. var defaults = DataTable.defaults.autoFill;
  960. if (init || defaults) {
  961. var opts = $.extend({}, init, defaults);
  962. if (init !== false) {
  963. new AutoFill(settings, opts);
  964. }
  965. }
  966. });
  967. // Alias for access
  968. DataTable.AutoFill = AutoFill;
  969. DataTable.AutoFill = AutoFill;
  970. return AutoFill;
  971. }));