dataTables.rowReorder.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. /*! RowReorder 1.2.6
  2. * 2015-2019 SpryMedia Ltd - datatables.net/license
  3. */
  4. /**
  5. * @summary RowReorder
  6. * @description Row reordering extension for DataTables
  7. * @version 1.2.6
  8. * @file dataTables.rowReorder.js
  9. * @author SpryMedia Ltd (www.sprymedia.co.uk)
  10. * @contact www.sprymedia.co.uk/contact
  11. * @copyright Copyright 2015-2019 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. * RowReorder provides the ability in DataTables to click and drag rows to
  50. * reorder them. When a row is dropped the data for the rows effected will be
  51. * updated to reflect the change. Normally this data point should also be the
  52. * column being sorted upon in the DataTable but this does not need to be the
  53. * case. RowReorder implements a "data swap" method - so the rows being
  54. * reordered take the value of the data point from the row that used to occupy
  55. * the row's new position.
  56. *
  57. * Initialisation is done by either:
  58. *
  59. * * `rowReorder` parameter in the DataTable initialisation object
  60. * * `new $.fn.dataTable.RowReorder( table, opts )` after DataTables
  61. * initialisation.
  62. *
  63. * @class
  64. * @param {object} settings DataTables settings object for the host table
  65. * @param {object} [opts] Configuration options
  66. * @requires jQuery 1.7+
  67. * @requires DataTables 1.10.7+
  68. */
  69. var RowReorder = function ( dt, opts ) {
  70. // Sanity check that we are using DataTables 1.10 or newer
  71. if ( ! DataTable.versionCheck || ! DataTable.versionCheck( '1.10.8' ) ) {
  72. throw 'DataTables RowReorder requires DataTables 1.10.8 or newer';
  73. }
  74. // User and defaults configuration object
  75. this.c = $.extend( true, {},
  76. DataTable.defaults.rowReorder,
  77. RowReorder.defaults,
  78. opts
  79. );
  80. // Internal settings
  81. this.s = {
  82. /** @type {integer} Scroll body top cache */
  83. bodyTop: null,
  84. /** @type {DataTable.Api} DataTables' API instance */
  85. dt: new DataTable.Api( dt ),
  86. /** @type {function} Data fetch function */
  87. getDataFn: DataTable.ext.oApi._fnGetObjectDataFn( this.c.dataSrc ),
  88. /** @type {array} Pixel positions for row insertion calculation */
  89. middles: null,
  90. /** @type {Object} Cached dimension information for use in the mouse move event handler */
  91. scroll: {},
  92. /** @type {integer} Interval object used for smooth scrolling */
  93. scrollInterval: null,
  94. /** @type {function} Data set function */
  95. setDataFn: DataTable.ext.oApi._fnSetObjectDataFn( this.c.dataSrc ),
  96. /** @type {Object} Mouse down information */
  97. start: {
  98. top: 0,
  99. left: 0,
  100. offsetTop: 0,
  101. offsetLeft: 0,
  102. nodes: []
  103. },
  104. /** @type {integer} Window height cached value */
  105. windowHeight: 0,
  106. /** @type {integer} Document outer height cached value */
  107. documentOuterHeight: 0,
  108. /** @type {integer} DOM clone outer height cached value */
  109. domCloneOuterHeight: 0
  110. };
  111. // DOM items
  112. this.dom = {
  113. /** @type {jQuery} Cloned row being moved around */
  114. clone: null,
  115. /** @type {jQuery} DataTables scrolling container */
  116. dtScroll: $('div.dataTables_scrollBody', this.s.dt.table().container())
  117. };
  118. // Check if row reorder has already been initialised on this table
  119. var settings = this.s.dt.settings()[0];
  120. var exisiting = settings.rowreorder;
  121. if ( exisiting ) {
  122. return exisiting;
  123. }
  124. settings.rowreorder = this;
  125. this._constructor();
  126. };
  127. $.extend( RowReorder.prototype, {
  128. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  129. * Constructor
  130. */
  131. /**
  132. * Initialise the RowReorder instance
  133. *
  134. * @private
  135. */
  136. _constructor: function ()
  137. {
  138. var that = this;
  139. var dt = this.s.dt;
  140. var table = $( dt.table().node() );
  141. // Need to be able to calculate the row positions relative to the table
  142. if ( table.css('position') === 'static' ) {
  143. table.css( 'position', 'relative' );
  144. }
  145. // listen for mouse down on the target column - we have to implement
  146. // this rather than using HTML5 drag and drop as drag and drop doesn't
  147. // appear to work on table rows at this time. Also mobile browsers are
  148. // not supported.
  149. // Use `table().container()` rather than just the table node for IE8 -
  150. // otherwise it only works once...
  151. $(dt.table().container()).on( 'mousedown.rowReorder touchstart.rowReorder', this.c.selector, function (e) {
  152. if ( ! that.c.enable ) {
  153. return;
  154. }
  155. // Ignore excluded children of the selector
  156. if ( $(e.target).is(that.c.excludedChildren) ) {
  157. return true;
  158. }
  159. var tr = $(this).closest('tr');
  160. var row = dt.row( tr );
  161. // Double check that it is a DataTable row
  162. if ( row.any() ) {
  163. that._emitEvent( 'pre-row-reorder', {
  164. node: row.node(),
  165. index: row.index()
  166. } );
  167. that._mouseDown( e, tr );
  168. return false;
  169. }
  170. } );
  171. dt.on( 'destroy.rowReorder', function () {
  172. $(dt.table().container()).off( '.rowReorder' );
  173. dt.off( '.rowReorder' );
  174. } );
  175. },
  176. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  177. * Private methods
  178. */
  179. /**
  180. * Cache the measurements that RowReorder needs in the mouse move handler
  181. * to attempt to speed things up, rather than reading from the DOM.
  182. *
  183. * @private
  184. */
  185. _cachePositions: function ()
  186. {
  187. var dt = this.s.dt;
  188. // Frustratingly, if we add `position:relative` to the tbody, the
  189. // position is still relatively to the parent. So we need to adjust
  190. // for that
  191. var headerHeight = $( dt.table().node() ).find('thead').outerHeight();
  192. // Need to pass the nodes through jQuery to get them in document order,
  193. // not what DataTables thinks it is, since we have been altering the
  194. // order
  195. var nodes = $.unique( dt.rows( { page: 'current' } ).nodes().toArray() );
  196. var tops = $.map( nodes, function ( node, i ) {
  197. return $(node).position().top - headerHeight;
  198. } );
  199. var middles = $.map( tops, function ( top, i ) {
  200. return tops.length < i-1 ?
  201. (top + tops[i+1]) / 2 :
  202. (top + top + $( dt.row( ':last-child' ).node() ).outerHeight() ) / 2;
  203. } );
  204. this.s.middles = middles;
  205. this.s.bodyTop = $( dt.table().body() ).offset().top;
  206. this.s.windowHeight = $(window).height();
  207. this.s.documentOuterHeight = $(document).outerHeight();
  208. },
  209. /**
  210. * Clone a row so it can be floated around the screen
  211. *
  212. * @param {jQuery} target Node to be cloned
  213. * @private
  214. */
  215. _clone: function ( target )
  216. {
  217. var dt = this.s.dt;
  218. var clone = $( dt.table().node().cloneNode(false) )
  219. .addClass( 'dt-rowReorder-float' )
  220. .append('<tbody/>')
  221. .append( target.clone( false ) );
  222. // Match the table and column widths - read all sizes before setting
  223. // to reduce reflows
  224. var tableWidth = target.outerWidth();
  225. var tableHeight = target.outerHeight();
  226. var sizes = target.children().map( function () {
  227. return $(this).width();
  228. } );
  229. clone
  230. .width( tableWidth )
  231. .height( tableHeight )
  232. .find('tr').children().each( function (i) {
  233. this.style.width = sizes[i]+'px';
  234. } );
  235. // Insert into the document to have it floating around
  236. clone.appendTo( 'body' );
  237. this.dom.clone = clone;
  238. this.s.domCloneOuterHeight = clone.outerHeight();
  239. },
  240. /**
  241. * Update the cloned item's position in the document
  242. *
  243. * @param {object} e Event giving the mouse's position
  244. * @private
  245. */
  246. _clonePosition: function ( e )
  247. {
  248. var start = this.s.start;
  249. var topDiff = this._eventToPage( e, 'Y' ) - start.top;
  250. var leftDiff = this._eventToPage( e, 'X' ) - start.left;
  251. var snap = this.c.snapX;
  252. var left;
  253. var top = topDiff + start.offsetTop;
  254. if ( snap === true ) {
  255. left = start.offsetLeft;
  256. }
  257. else if ( typeof snap === 'number' ) {
  258. left = start.offsetLeft + snap;
  259. }
  260. else {
  261. left = leftDiff + start.offsetLeft;
  262. }
  263. if(top < 0) {
  264. top = 0
  265. }
  266. else if(top + this.s.domCloneOuterHeight > this.s.documentOuterHeight) {
  267. top = this.s.documentOuterHeight - this.s.domCloneOuterHeight;
  268. }
  269. this.dom.clone.css( {
  270. top: top,
  271. left: left
  272. } );
  273. },
  274. /**
  275. * Emit an event on the DataTable for listeners
  276. *
  277. * @param {string} name Event name
  278. * @param {array} args Event arguments
  279. * @private
  280. */
  281. _emitEvent: function ( name, args )
  282. {
  283. this.s.dt.iterator( 'table', function ( ctx, i ) {
  284. $(ctx.nTable).triggerHandler( name+'.dt', args );
  285. } );
  286. },
  287. /**
  288. * Get pageX/Y position from an event, regardless of if it is a mouse or
  289. * touch event.
  290. *
  291. * @param {object} e Event
  292. * @param {string} pos X or Y (must be a capital)
  293. * @private
  294. */
  295. _eventToPage: function ( e, pos )
  296. {
  297. if ( e.type.indexOf( 'touch' ) !== -1 ) {
  298. return e.originalEvent.touches[0][ 'page'+pos ];
  299. }
  300. return e[ 'page'+pos ];
  301. },
  302. /**
  303. * Mouse down event handler. Read initial positions and add event handlers
  304. * for the move.
  305. *
  306. * @param {object} e Mouse event
  307. * @param {jQuery} target TR element that is to be moved
  308. * @private
  309. */
  310. _mouseDown: function ( e, target )
  311. {
  312. var that = this;
  313. var dt = this.s.dt;
  314. var start = this.s.start;
  315. var offset = target.offset();
  316. start.top = this._eventToPage( e, 'Y' );
  317. start.left = this._eventToPage( e, 'X' );
  318. start.offsetTop = offset.top;
  319. start.offsetLeft = offset.left;
  320. start.nodes = $.unique( dt.rows( { page: 'current' } ).nodes().toArray() );
  321. this._cachePositions();
  322. this._clone( target );
  323. this._clonePosition( e );
  324. this.dom.target = target;
  325. target.addClass( 'dt-rowReorder-moving' );
  326. $( document )
  327. .on( 'mouseup.rowReorder touchend.rowReorder', function (e) {
  328. that._mouseUp(e);
  329. } )
  330. .on( 'mousemove.rowReorder touchmove.rowReorder', function (e) {
  331. that._mouseMove(e);
  332. } );
  333. // Check if window is x-scrolling - if not, disable it for the duration
  334. // of the drag
  335. if ( $(window).width() === $(document).width() ) {
  336. $(document.body).addClass( 'dt-rowReorder-noOverflow' );
  337. }
  338. // Cache scrolling information so mouse move doesn't need to read.
  339. // This assumes that the window and DT scroller will not change size
  340. // during an row drag, which I think is a fair assumption
  341. var scrollWrapper = this.dom.dtScroll;
  342. this.s.scroll = {
  343. windowHeight: $(window).height(),
  344. windowWidth: $(window).width(),
  345. dtTop: scrollWrapper.length ? scrollWrapper.offset().top : null,
  346. dtLeft: scrollWrapper.length ? scrollWrapper.offset().left : null,
  347. dtHeight: scrollWrapper.length ? scrollWrapper.outerHeight() : null,
  348. dtWidth: scrollWrapper.length ? scrollWrapper.outerWidth() : null
  349. };
  350. },
  351. /**
  352. * Mouse move event handler - move the cloned row and shuffle the table's
  353. * rows if required.
  354. *
  355. * @param {object} e Mouse event
  356. * @private
  357. */
  358. _mouseMove: function ( e )
  359. {
  360. this._clonePosition( e );
  361. // Transform the mouse position into a position in the table's body
  362. var bodyY = this._eventToPage( e, 'Y' ) - this.s.bodyTop;
  363. var middles = this.s.middles;
  364. var insertPoint = null;
  365. var dt = this.s.dt;
  366. var body = dt.table().body();
  367. // Determine where the row should be inserted based on the mouse
  368. // position
  369. for ( var i=0, ien=middles.length ; i<ien ; i++ ) {
  370. if ( bodyY < middles[i] ) {
  371. insertPoint = i;
  372. break;
  373. }
  374. }
  375. if ( insertPoint === null ) {
  376. insertPoint = middles.length;
  377. }
  378. // Perform the DOM shuffle if it has changed from last time
  379. if ( this.s.lastInsert === null || this.s.lastInsert !== insertPoint ) {
  380. if ( insertPoint === 0 ) {
  381. this.dom.target.prependTo( body );
  382. }
  383. else {
  384. var nodes = $.unique( dt.rows( { page: 'current' } ).nodes().toArray() );
  385. if ( insertPoint > this.s.lastInsert ) {
  386. this.dom.target.insertAfter( nodes[ insertPoint-1 ] );
  387. }
  388. else {
  389. this.dom.target.insertBefore( nodes[ insertPoint ] );
  390. }
  391. }
  392. this._cachePositions();
  393. this.s.lastInsert = insertPoint;
  394. }
  395. this._shiftScroll( e );
  396. },
  397. /**
  398. * Mouse up event handler - release the event handlers and perform the
  399. * table updates
  400. *
  401. * @param {object} e Mouse event
  402. * @private
  403. */
  404. _mouseUp: function ( e )
  405. {
  406. var that = this;
  407. var dt = this.s.dt;
  408. var i, ien;
  409. var dataSrc = this.c.dataSrc;
  410. this.dom.clone.remove();
  411. this.dom.clone = null;
  412. this.dom.target.removeClass( 'dt-rowReorder-moving' );
  413. //this.dom.target = null;
  414. $(document).off( '.rowReorder' );
  415. $(document.body).removeClass( 'dt-rowReorder-noOverflow' );
  416. clearInterval( this.s.scrollInterval );
  417. this.s.scrollInterval = null;
  418. // Calculate the difference
  419. var startNodes = this.s.start.nodes;
  420. var endNodes = $.unique( dt.rows( { page: 'current' } ).nodes().toArray() );
  421. var idDiff = {};
  422. var fullDiff = [];
  423. var diffNodes = [];
  424. var getDataFn = this.s.getDataFn;
  425. var setDataFn = this.s.setDataFn;
  426. for ( i=0, ien=startNodes.length ; i<ien ; i++ ) {
  427. if ( startNodes[i] !== endNodes[i] ) {
  428. var id = dt.row( endNodes[i] ).id();
  429. var endRowData = dt.row( endNodes[i] ).data();
  430. var startRowData = dt.row( startNodes[i] ).data();
  431. if ( id ) {
  432. idDiff[ id ] = getDataFn( startRowData );
  433. }
  434. fullDiff.push( {
  435. node: endNodes[i],
  436. oldData: getDataFn( endRowData ),
  437. newData: getDataFn( startRowData ),
  438. newPosition: i,
  439. oldPosition: $.inArray( endNodes[i], startNodes )
  440. } );
  441. diffNodes.push( endNodes[i] );
  442. }
  443. }
  444. // Create event args
  445. var eventArgs = [ fullDiff, {
  446. dataSrc: dataSrc,
  447. nodes: diffNodes,
  448. values: idDiff,
  449. triggerRow: dt.row( this.dom.target ),
  450. originalEvent: e
  451. } ];
  452. // Emit event
  453. this._emitEvent( 'row-reorder', eventArgs );
  454. var update = function () {
  455. if ( that.c.update ) {
  456. for ( i=0, ien=fullDiff.length ; i<ien ; i++ ) {
  457. var row = dt.row( fullDiff[i].node );
  458. var rowData = row.data();
  459. setDataFn( rowData, fullDiff[i].newData );
  460. // Invalidate the cell that has the same data source as the dataSrc
  461. dt.columns().every( function () {
  462. if ( this.dataSrc() === dataSrc ) {
  463. dt.cell( fullDiff[i].node, this.index() ).invalidate( 'data' );
  464. }
  465. } );
  466. }
  467. // Trigger row reordered event
  468. that._emitEvent( 'row-reordered', eventArgs );
  469. dt.draw( false );
  470. }
  471. };
  472. // Editor interface
  473. if ( this.c.editor ) {
  474. // Disable user interaction while Editor is submitting
  475. this.c.enable = false;
  476. this.c.editor
  477. .edit(
  478. diffNodes,
  479. false,
  480. $.extend( {submit: 'changed'}, this.c.formOptions )
  481. )
  482. .multiSet( dataSrc, idDiff )
  483. .one( 'preSubmitCancelled.rowReorder', function () {
  484. that.c.enable = true;
  485. that.c.editor.off( '.rowReorder' );
  486. dt.draw( false );
  487. } )
  488. .one( 'submitUnsuccessful.rowReorder', function () {
  489. dt.draw( false );
  490. } )
  491. .one( 'submitSuccess.rowReorder', function () {
  492. update();
  493. } )
  494. .one( 'submitComplete', function () {
  495. that.c.enable = true;
  496. that.c.editor.off( '.rowReorder' );
  497. } )
  498. .submit();
  499. }
  500. else {
  501. update();
  502. }
  503. },
  504. /**
  505. * Move the window and DataTables scrolling during a drag to scroll new
  506. * content into view.
  507. *
  508. * This matches the `_shiftScroll` method used in AutoFill, but only
  509. * horizontal scrolling is considered here.
  510. *
  511. * @param {object} e Mouse move event object
  512. * @private
  513. */
  514. _shiftScroll: function ( e )
  515. {
  516. var that = this;
  517. var dt = this.s.dt;
  518. var scroll = this.s.scroll;
  519. var runInterval = false;
  520. var scrollSpeed = 5;
  521. var buffer = 65;
  522. var
  523. windowY = e.pageY - document.body.scrollTop,
  524. windowVert,
  525. dtVert;
  526. // Window calculations - based on the mouse position in the window,
  527. // regardless of scrolling
  528. if ( windowY < buffer ) {
  529. windowVert = scrollSpeed * -1;
  530. }
  531. else if ( windowY > scroll.windowHeight - buffer ) {
  532. windowVert = scrollSpeed;
  533. }
  534. // DataTables scrolling calculations - based on the table's position in
  535. // the document and the mouse position on the page
  536. if ( scroll.dtTop !== null && e.pageY < scroll.dtTop + buffer ) {
  537. dtVert = scrollSpeed * -1;
  538. }
  539. else if ( scroll.dtTop !== null && e.pageY > scroll.dtTop + scroll.dtHeight - buffer ) {
  540. dtVert = scrollSpeed;
  541. }
  542. // This is where it gets interesting. We want to continue scrolling
  543. // without requiring a mouse move, so we need an interval to be
  544. // triggered. The interval should continue until it is no longer needed,
  545. // but it must also use the latest scroll commands (for example consider
  546. // that the mouse might move from scrolling up to scrolling left, all
  547. // with the same interval running. We use the `scroll` object to "pass"
  548. // this information to the interval. Can't use local variables as they
  549. // wouldn't be the ones that are used by an already existing interval!
  550. if ( windowVert || dtVert ) {
  551. scroll.windowVert = windowVert;
  552. scroll.dtVert = dtVert;
  553. runInterval = true;
  554. }
  555. else if ( this.s.scrollInterval ) {
  556. // Don't need to scroll - remove any existing timer
  557. clearInterval( this.s.scrollInterval );
  558. this.s.scrollInterval = null;
  559. }
  560. // If we need to run the interval to scroll and there is no existing
  561. // interval (if there is an existing one, it will continue to run)
  562. if ( ! this.s.scrollInterval && runInterval ) {
  563. this.s.scrollInterval = setInterval( function () {
  564. // Don't need to worry about setting scroll <0 or beyond the
  565. // scroll bound as the browser will just reject that.
  566. if ( scroll.windowVert ) {
  567. document.body.scrollTop += scroll.windowVert;
  568. }
  569. // DataTables scrolling
  570. if ( scroll.dtVert ) {
  571. var scroller = that.dom.dtScroll[0];
  572. if ( scroll.dtVert ) {
  573. scroller.scrollTop += scroll.dtVert;
  574. }
  575. }
  576. }, 20 );
  577. }
  578. }
  579. } );
  580. /**
  581. * RowReorder default settings for initialisation
  582. *
  583. * @namespace
  584. * @name RowReorder.defaults
  585. * @static
  586. */
  587. RowReorder.defaults = {
  588. /**
  589. * Data point in the host row's data source object for where to get and set
  590. * the data to reorder. This will normally also be the sorting column.
  591. *
  592. * @type {Number}
  593. */
  594. dataSrc: 0,
  595. /**
  596. * Editor instance that will be used to perform the update
  597. *
  598. * @type {DataTable.Editor}
  599. */
  600. editor: null,
  601. /**
  602. * Enable / disable RowReorder's user interaction
  603. * @type {Boolean}
  604. */
  605. enable: true,
  606. /**
  607. * Form options to pass to Editor when submitting a change in the row order.
  608. * See the Editor `from-options` object for details of the options
  609. * available.
  610. * @type {Object}
  611. */
  612. formOptions: {},
  613. /**
  614. * Drag handle selector. This defines the element that when dragged will
  615. * reorder a row.
  616. *
  617. * @type {String}
  618. */
  619. selector: 'td:first-child',
  620. /**
  621. * Optionally lock the dragged row's x-position. This can be `true` to
  622. * fix the position match the host table's, `false` to allow free movement
  623. * of the row, or a number to define an offset from the host table.
  624. *
  625. * @type {Boolean|number}
  626. */
  627. snapX: false,
  628. /**
  629. * Update the table's data on drop
  630. *
  631. * @type {Boolean}
  632. */
  633. update: true,
  634. /**
  635. * Selector for children of the drag handle selector that mouseDown events
  636. * will be passed through to and drag will not activate
  637. *
  638. * @type {String}
  639. */
  640. excludedChildren: 'a'
  641. };
  642. /*
  643. * API
  644. */
  645. var Api = $.fn.dataTable.Api;
  646. // Doesn't do anything - work around for a bug in DT... Not documented
  647. Api.register( 'rowReorder()', function () {
  648. return this;
  649. } );
  650. Api.register( 'rowReorder.enable()', function ( toggle ) {
  651. if ( toggle === undefined ) {
  652. toggle = true;
  653. }
  654. return this.iterator( 'table', function ( ctx ) {
  655. if ( ctx.rowreorder ) {
  656. ctx.rowreorder.c.enable = toggle;
  657. }
  658. } );
  659. } );
  660. Api.register( 'rowReorder.disable()', function () {
  661. return this.iterator( 'table', function ( ctx ) {
  662. if ( ctx.rowreorder ) {
  663. ctx.rowreorder.c.enable = false;
  664. }
  665. } );
  666. } );
  667. /**
  668. * Version information
  669. *
  670. * @name RowReorder.version
  671. * @static
  672. */
  673. RowReorder.version = '1.2.6';
  674. $.fn.dataTable.RowReorder = RowReorder;
  675. $.fn.DataTable.RowReorder = RowReorder;
  676. // Attach a listener to the document which listens for DataTables initialisation
  677. // events so we can automatically initialise
  678. $(document).on( 'init.dt.dtr', function (e, settings, json) {
  679. if ( e.namespace !== 'dt' ) {
  680. return;
  681. }
  682. var init = settings.oInit.rowReorder;
  683. var defaults = DataTable.defaults.rowReorder;
  684. if ( init || defaults ) {
  685. var opts = $.extend( {}, init, defaults );
  686. if ( init !== false ) {
  687. new RowReorder( settings, opts );
  688. }
  689. }
  690. } );
  691. return RowReorder;
  692. }));