dataTables.select.js 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  1. /*! Select for DataTables 1.2.6
  2. * 2015-2018 SpryMedia Ltd - datatables.net/license/mit
  3. */
  4. /**
  5. * @summary Select for DataTables
  6. * @description A collection of API methods, events and buttons for DataTables
  7. * that provides selection options of the items in a DataTable
  8. * @version 1.2.6
  9. * @file dataTables.select.js
  10. * @author SpryMedia Ltd (www.sprymedia.co.uk)
  11. * @contact datatables.net/forums
  12. * @copyright Copyright 2015-2018 SpryMedia Ltd.
  13. *
  14. * This source file is free software, available under the following license:
  15. * MIT license - http://datatables.net/license/mit
  16. *
  17. * This source file is distributed in the hope that it will be useful, but
  18. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  19. * or FITNESS FOR A PARTICULAR PURPOSE. See the license files for details.
  20. *
  21. * For details please refer to: http://www.datatables.net/extensions/select
  22. */
  23. (function( factory ){
  24. if ( typeof define === 'function' && define.amd ) {
  25. // AMD
  26. define( ['jquery', 'datatables.net'], function ( $ ) {
  27. return factory( $, window, document );
  28. } );
  29. }
  30. else if ( typeof exports === 'object' ) {
  31. // CommonJS
  32. module.exports = function (root, $) {
  33. if ( ! root ) {
  34. root = window;
  35. }
  36. if ( ! $ || ! $.fn.dataTable ) {
  37. $ = require('datatables.net')(root, $).$;
  38. }
  39. return factory( $, root, root.document );
  40. };
  41. }
  42. else {
  43. // Browser
  44. factory( jQuery, window, document );
  45. }
  46. }(function( $, window, document, undefined ) {
  47. 'use strict';
  48. var DataTable = $.fn.dataTable;
  49. // Version information for debugger
  50. DataTable.select = {};
  51. DataTable.select.version = '1.2.6';
  52. DataTable.select.init = function ( dt ) {
  53. var ctx = dt.settings()[0];
  54. var init = ctx.oInit.select;
  55. var defaults = DataTable.defaults.select;
  56. var opts = init === undefined ?
  57. defaults :
  58. init;
  59. // Set defaults
  60. var items = 'row';
  61. var style = 'api';
  62. var blurable = false;
  63. var info = true;
  64. var selector = 'td, th';
  65. var className = 'selected';
  66. var setStyle = false;
  67. ctx._select = {};
  68. // Initialisation customisations
  69. if ( opts === true ) {
  70. style = 'os';
  71. setStyle = true;
  72. }
  73. else if ( typeof opts === 'string' ) {
  74. style = opts;
  75. setStyle = true;
  76. }
  77. else if ( $.isPlainObject( opts ) ) {
  78. if ( opts.blurable !== undefined ) {
  79. blurable = opts.blurable;
  80. }
  81. if ( opts.info !== undefined ) {
  82. info = opts.info;
  83. }
  84. if ( opts.items !== undefined ) {
  85. items = opts.items;
  86. }
  87. if ( opts.style !== undefined ) {
  88. style = opts.style;
  89. setStyle = true;
  90. }
  91. if ( opts.selector !== undefined ) {
  92. selector = opts.selector;
  93. }
  94. if ( opts.className !== undefined ) {
  95. className = opts.className;
  96. }
  97. }
  98. dt.select.selector( selector );
  99. dt.select.items( items );
  100. dt.select.style( style );
  101. dt.select.blurable( blurable );
  102. dt.select.info( info );
  103. ctx._select.className = className;
  104. // Sort table based on selected rows. Requires Select Datatables extension
  105. $.fn.dataTable.ext.order['select-checkbox'] = function ( settings, col ) {
  106. return this.api().column( col, {order: 'index'} ).nodes().map( function ( td ) {
  107. if ( settings._select.items === 'row' ) {
  108. return $( td ).parent().hasClass( settings._select.className );
  109. } else if ( settings._select.items === 'cell' ) {
  110. return $( td ).hasClass( settings._select.className );
  111. }
  112. return false;
  113. });
  114. };
  115. // If the init options haven't enabled select, but there is a selectable
  116. // class name, then enable
  117. if ( ! setStyle && $( dt.table().node() ).hasClass( 'selectable' ) ) {
  118. dt.select.style( 'os' );
  119. }
  120. };
  121. /*
  122. Select is a collection of API methods, event handlers, event emitters and
  123. buttons (for the `Buttons` extension) for DataTables. It provides the following
  124. features, with an overview of how they are implemented:
  125. ## Selection of rows, columns and cells. Whether an item is selected or not is
  126. stored in:
  127. * rows: a `_select_selected` property which contains a boolean value of the
  128. DataTables' `aoData` object for each row
  129. * columns: a `_select_selected` property which contains a boolean value of the
  130. DataTables' `aoColumns` object for each column
  131. * cells: a `_selected_cells` property which contains an array of boolean values
  132. of the `aoData` object for each row. The array is the same length as the
  133. columns array, with each element of it representing a cell.
  134. This method of using boolean flags allows Select to operate when nodes have not
  135. been created for rows / cells (DataTables' defer rendering feature).
  136. ## API methods
  137. A range of API methods are available for triggering selection and de-selection
  138. of rows. Methods are also available to configure the selection events that can
  139. be triggered by an end user (such as which items are to be selected). To a large
  140. extent, these of API methods *is* Select. It is basically a collection of helper
  141. functions that can be used to select items in a DataTable.
  142. Configuration of select is held in the object `_select` which is attached to the
  143. DataTables settings object on initialisation. Select being available on a table
  144. is not optional when Select is loaded, but its default is for selection only to
  145. be available via the API - so the end user wouldn't be able to select rows
  146. without additional configuration.
  147. The `_select` object contains the following properties:
  148. ```
  149. {
  150. items:string - Can be `rows`, `columns` or `cells`. Defines what item
  151. will be selected if the user is allowed to activate row
  152. selection using the mouse.
  153. style:string - Can be `none`, `single`, `multi` or `os`. Defines the
  154. interaction style when selecting items
  155. blurable:boolean - If row selection can be cleared by clicking outside of
  156. the table
  157. info:boolean - If the selection summary should be shown in the table
  158. information elements
  159. }
  160. ```
  161. In addition to the API methods, Select also extends the DataTables selector
  162. options for rows, columns and cells adding a `selected` option to the selector
  163. options object, allowing the developer to select only selected items or
  164. unselected items.
  165. ## Mouse selection of items
  166. Clicking on items can be used to select items. This is done by a simple event
  167. handler that will select the items using the API methods.
  168. */
  169. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  170. * Local functions
  171. */
  172. /**
  173. * Add one or more cells to the selection when shift clicking in OS selection
  174. * style cell selection.
  175. *
  176. * Cell range is more complicated than row and column as we want to select
  177. * in the visible grid rather than by index in sequence. For example, if you
  178. * click first in cell 1-1 and then shift click in 2-2 - cells 1-2 and 2-1
  179. * should also be selected (and not 1-3, 1-4. etc)
  180. *
  181. * @param {DataTable.Api} dt DataTable
  182. * @param {object} idx Cell index to select to
  183. * @param {object} last Cell index to select from
  184. * @private
  185. */
  186. function cellRange( dt, idx, last )
  187. {
  188. var indexes;
  189. var columnIndexes;
  190. var rowIndexes;
  191. var selectColumns = function ( start, end ) {
  192. if ( start > end ) {
  193. var tmp = end;
  194. end = start;
  195. start = tmp;
  196. }
  197. var record = false;
  198. return dt.columns( ':visible' ).indexes().filter( function (i) {
  199. if ( i === start ) {
  200. record = true;
  201. }
  202. if ( i === end ) { // not else if, as start might === end
  203. record = false;
  204. return true;
  205. }
  206. return record;
  207. } );
  208. };
  209. var selectRows = function ( start, end ) {
  210. var indexes = dt.rows( { search: 'applied' } ).indexes();
  211. // Which comes first - might need to swap
  212. if ( indexes.indexOf( start ) > indexes.indexOf( end ) ) {
  213. var tmp = end;
  214. end = start;
  215. start = tmp;
  216. }
  217. var record = false;
  218. return indexes.filter( function (i) {
  219. if ( i === start ) {
  220. record = true;
  221. }
  222. if ( i === end ) {
  223. record = false;
  224. return true;
  225. }
  226. return record;
  227. } );
  228. };
  229. if ( ! dt.cells( { selected: true } ).any() && ! last ) {
  230. // select from the top left cell to this one
  231. columnIndexes = selectColumns( 0, idx.column );
  232. rowIndexes = selectRows( 0 , idx.row );
  233. }
  234. else {
  235. // Get column indexes between old and new
  236. columnIndexes = selectColumns( last.column, idx.column );
  237. rowIndexes = selectRows( last.row , idx.row );
  238. }
  239. indexes = dt.cells( rowIndexes, columnIndexes ).flatten();
  240. if ( ! dt.cells( idx, { selected: true } ).any() ) {
  241. // Select range
  242. dt.cells( indexes ).select();
  243. }
  244. else {
  245. // Deselect range
  246. dt.cells( indexes ).deselect();
  247. }
  248. }
  249. /**
  250. * Disable mouse selection by removing the selectors
  251. *
  252. * @param {DataTable.Api} dt DataTable to remove events from
  253. * @private
  254. */
  255. function disableMouseSelection( dt )
  256. {
  257. var ctx = dt.settings()[0];
  258. var selector = ctx._select.selector;
  259. $( dt.table().container() )
  260. .off( 'mousedown.dtSelect', selector )
  261. .off( 'mouseup.dtSelect', selector )
  262. .off( 'click.dtSelect', selector );
  263. $('body').off( 'click.dtSelect' + dt.table().node().id );
  264. }
  265. /**
  266. * Attach mouse listeners to the table to allow mouse selection of items
  267. *
  268. * @param {DataTable.Api} dt DataTable to remove events from
  269. * @private
  270. */
  271. function enableMouseSelection ( dt )
  272. {
  273. var container = $( dt.table().container() );
  274. var ctx = dt.settings()[0];
  275. var selector = ctx._select.selector;
  276. var matchSelection;
  277. container
  278. .on( 'mousedown.dtSelect', selector, function(e) {
  279. // Disallow text selection for shift clicking on the table so multi
  280. // element selection doesn't look terrible!
  281. if ( e.shiftKey || e.metaKey || e.ctrlKey ) {
  282. container
  283. .css( '-moz-user-select', 'none' )
  284. .one('selectstart.dtSelect', selector, function () {
  285. return false;
  286. } );
  287. }
  288. if ( window.getSelection ) {
  289. matchSelection = window.getSelection();
  290. }
  291. } )
  292. .on( 'mouseup.dtSelect', selector, function() {
  293. // Allow text selection to occur again, Mozilla style (tested in FF
  294. // 35.0.1 - still required)
  295. container.css( '-moz-user-select', '' );
  296. } )
  297. .on( 'click.dtSelect', selector, function ( e ) {
  298. var items = dt.select.items();
  299. var idx;
  300. // If text was selected (click and drag), then we shouldn't change
  301. // the row's selected state
  302. if ( window.getSelection ) {
  303. var selection = window.getSelection();
  304. // If the element that contains the selection is not in the table, we can ignore it
  305. // This can happen if the developer selects text from the click event
  306. if ( ! selection.anchorNode || $(selection.anchorNode).closest('table')[0] === dt.table().node() ) {
  307. if ( selection !== matchSelection ) {
  308. return;
  309. }
  310. }
  311. }
  312. var ctx = dt.settings()[0];
  313. var wrapperClass = dt.settings()[0].oClasses.sWrapper.replace(/ /g, '.');
  314. // Ignore clicks inside a sub-table
  315. if ( $(e.target).closest('div.'+wrapperClass)[0] != dt.table().container() ) {
  316. return;
  317. }
  318. var cell = dt.cell( $(e.target).closest('td, th') );
  319. // Check the cell actually belongs to the host DataTable (so child
  320. // rows, etc, are ignored)
  321. if ( ! cell.any() ) {
  322. return;
  323. }
  324. var event = $.Event('user-select.dt');
  325. eventTrigger( dt, event, [ items, cell, e ] );
  326. if ( event.isDefaultPrevented() ) {
  327. return;
  328. }
  329. var cellIndex = cell.index();
  330. if ( items === 'row' ) {
  331. idx = cellIndex.row;
  332. typeSelect( e, dt, ctx, 'row', idx );
  333. }
  334. else if ( items === 'column' ) {
  335. idx = cell.index().column;
  336. typeSelect( e, dt, ctx, 'column', idx );
  337. }
  338. else if ( items === 'cell' ) {
  339. idx = cell.index();
  340. typeSelect( e, dt, ctx, 'cell', idx );
  341. }
  342. ctx._select_lastCell = cellIndex;
  343. } );
  344. // Blurable
  345. $('body').on( 'click.dtSelect' + dt.table().node().id, function ( e ) {
  346. if ( ctx._select.blurable ) {
  347. // If the click was inside the DataTables container, don't blur
  348. if ( $(e.target).parents().filter( dt.table().container() ).length ) {
  349. return;
  350. }
  351. // Ignore elements which have been removed from the DOM (i.e. paging
  352. // buttons)
  353. if ( $(e.target).parents('html').length === 0 ) {
  354. return;
  355. }
  356. // Don't blur in Editor form
  357. if ( $(e.target).parents('div.DTE').length ) {
  358. return;
  359. }
  360. clear( ctx, true );
  361. }
  362. } );
  363. }
  364. /**
  365. * Trigger an event on a DataTable
  366. *
  367. * @param {DataTable.Api} api DataTable to trigger events on
  368. * @param {boolean} selected true if selected, false if deselected
  369. * @param {string} type Item type acting on
  370. * @param {boolean} any Require that there are values before
  371. * triggering
  372. * @private
  373. */
  374. function eventTrigger ( api, type, args, any )
  375. {
  376. if ( any && ! api.flatten().length ) {
  377. return;
  378. }
  379. if ( typeof type === 'string' ) {
  380. type = type +'.dt';
  381. }
  382. args.unshift( api );
  383. $(api.table().node()).trigger( type, args );
  384. }
  385. /**
  386. * Update the information element of the DataTable showing information about the
  387. * items selected. This is done by adding tags to the existing text
  388. *
  389. * @param {DataTable.Api} api DataTable to update
  390. * @private
  391. */
  392. function info ( api )
  393. {
  394. var ctx = api.settings()[0];
  395. if ( ! ctx._select.info || ! ctx.aanFeatures.i ) {
  396. return;
  397. }
  398. if ( api.select.style() === 'api' ) {
  399. return;
  400. }
  401. var rows = api.rows( { selected: true } ).flatten().length;
  402. var columns = api.columns( { selected: true } ).flatten().length;
  403. var cells = api.cells( { selected: true } ).flatten().length;
  404. var add = function ( el, name, num ) {
  405. el.append( $('<span class="select-item"/>').append( api.i18n(
  406. 'select.'+name+'s',
  407. { _: '%d '+name+'s selected', 0: '', 1: '1 '+name+' selected' },
  408. num
  409. ) ) );
  410. };
  411. // Internal knowledge of DataTables to loop over all information elements
  412. $.each( ctx.aanFeatures.i, function ( i, el ) {
  413. el = $(el);
  414. var output = $('<span class="select-info"/>');
  415. add( output, 'row', rows );
  416. add( output, 'column', columns );
  417. add( output, 'cell', cells );
  418. var exisiting = el.children('span.select-info');
  419. if ( exisiting.length ) {
  420. exisiting.remove();
  421. }
  422. if ( output.text() !== '' ) {
  423. el.append( output );
  424. }
  425. } );
  426. }
  427. /**
  428. * Initialisation of a new table. Attach event handlers and callbacks to allow
  429. * Select to operate correctly.
  430. *
  431. * This will occur _after_ the initial DataTables initialisation, although
  432. * before Ajax data is rendered, if there is ajax data
  433. *
  434. * @param {DataTable.settings} ctx Settings object to operate on
  435. * @private
  436. */
  437. function init ( ctx ) {
  438. var api = new DataTable.Api( ctx );
  439. // Row callback so that classes can be added to rows and cells if the item
  440. // was selected before the element was created. This will happen with the
  441. // `deferRender` option enabled.
  442. //
  443. // This method of attaching to `aoRowCreatedCallback` is a hack until
  444. // DataTables has proper events for row manipulation If you are reviewing
  445. // this code to create your own plug-ins, please do not do this!
  446. ctx.aoRowCreatedCallback.push( {
  447. fn: function ( row, data, index ) {
  448. var i, ien;
  449. var d = ctx.aoData[ index ];
  450. // Row
  451. if ( d._select_selected ) {
  452. $( row ).addClass( ctx._select.className );
  453. }
  454. // Cells and columns - if separated out, we would need to do two
  455. // loops, so it makes sense to combine them into a single one
  456. for ( i=0, ien=ctx.aoColumns.length ; i<ien ; i++ ) {
  457. if ( ctx.aoColumns[i]._select_selected || (d._selected_cells && d._selected_cells[i]) ) {
  458. $(d.anCells[i]).addClass( ctx._select.className );
  459. }
  460. }
  461. },
  462. sName: 'select-deferRender'
  463. } );
  464. // On Ajax reload we want to reselect all rows which are currently selected,
  465. // if there is an rowId (i.e. a unique value to identify each row with)
  466. api.on( 'preXhr.dt.dtSelect', function () {
  467. // note that column selection doesn't need to be cached and then
  468. // reselected, as they are already selected
  469. var rows = api.rows( { selected: true } ).ids( true ).filter( function ( d ) {
  470. return d !== undefined;
  471. } );
  472. var cells = api.cells( { selected: true } ).eq(0).map( function ( cellIdx ) {
  473. var id = api.row( cellIdx.row ).id( true );
  474. return id ?
  475. { row: id, column: cellIdx.column } :
  476. undefined;
  477. } ).filter( function ( d ) {
  478. return d !== undefined;
  479. } );
  480. // On the next draw, reselect the currently selected items
  481. api.one( 'draw.dt.dtSelect', function () {
  482. api.rows( rows ).select();
  483. // `cells` is not a cell index selector, so it needs a loop
  484. if ( cells.any() ) {
  485. cells.each( function ( id ) {
  486. api.cells( id.row, id.column ).select();
  487. } );
  488. }
  489. } );
  490. } );
  491. // Update the table information element with selected item summary
  492. api.on( 'draw.dtSelect.dt select.dtSelect.dt deselect.dtSelect.dt info.dt', function () {
  493. info( api );
  494. } );
  495. // Clean up and release
  496. api.on( 'destroy.dtSelect', function () {
  497. disableMouseSelection( api );
  498. api.off( '.dtSelect' );
  499. } );
  500. }
  501. /**
  502. * Add one or more items (rows or columns) to the selection when shift clicking
  503. * in OS selection style
  504. *
  505. * @param {DataTable.Api} dt DataTable
  506. * @param {string} type Row or column range selector
  507. * @param {object} idx Item index to select to
  508. * @param {object} last Item index to select from
  509. * @private
  510. */
  511. function rowColumnRange( dt, type, idx, last )
  512. {
  513. // Add a range of rows from the last selected row to this one
  514. var indexes = dt[type+'s']( { search: 'applied' } ).indexes();
  515. var idx1 = $.inArray( last, indexes );
  516. var idx2 = $.inArray( idx, indexes );
  517. if ( ! dt[type+'s']( { selected: true } ).any() && idx1 === -1 ) {
  518. // select from top to here - slightly odd, but both Windows and Mac OS
  519. // do this
  520. indexes.splice( $.inArray( idx, indexes )+1, indexes.length );
  521. }
  522. else {
  523. // reverse so we can shift click 'up' as well as down
  524. if ( idx1 > idx2 ) {
  525. var tmp = idx2;
  526. idx2 = idx1;
  527. idx1 = tmp;
  528. }
  529. indexes.splice( idx2+1, indexes.length );
  530. indexes.splice( 0, idx1 );
  531. }
  532. if ( ! dt[type]( idx, { selected: true } ).any() ) {
  533. // Select range
  534. dt[type+'s']( indexes ).select();
  535. }
  536. else {
  537. // Deselect range - need to keep the clicked on row selected
  538. indexes.splice( $.inArray( idx, indexes ), 1 );
  539. dt[type+'s']( indexes ).deselect();
  540. }
  541. }
  542. /**
  543. * Clear all selected items
  544. *
  545. * @param {DataTable.settings} ctx Settings object of the host DataTable
  546. * @param {boolean} [force=false] Force the de-selection to happen, regardless
  547. * of selection style
  548. * @private
  549. */
  550. function clear( ctx, force )
  551. {
  552. if ( force || ctx._select.style === 'single' ) {
  553. var api = new DataTable.Api( ctx );
  554. api.rows( { selected: true } ).deselect();
  555. api.columns( { selected: true } ).deselect();
  556. api.cells( { selected: true } ).deselect();
  557. }
  558. }
  559. /**
  560. * Select items based on the current configuration for style and items.
  561. *
  562. * @param {object} e Mouse event object
  563. * @param {DataTables.Api} dt DataTable
  564. * @param {DataTable.settings} ctx Settings object of the host DataTable
  565. * @param {string} type Items to select
  566. * @param {int|object} idx Index of the item to select
  567. * @private
  568. */
  569. function typeSelect ( e, dt, ctx, type, idx )
  570. {
  571. var style = dt.select.style();
  572. var isSelected = dt[type]( idx, { selected: true } ).any();
  573. if ( style === 'os' ) {
  574. if ( e.ctrlKey || e.metaKey ) {
  575. // Add or remove from the selection
  576. dt[type]( idx ).select( ! isSelected );
  577. }
  578. else if ( e.shiftKey ) {
  579. if ( type === 'cell' ) {
  580. cellRange( dt, idx, ctx._select_lastCell || null );
  581. }
  582. else {
  583. rowColumnRange( dt, type, idx, ctx._select_lastCell ?
  584. ctx._select_lastCell[type] :
  585. null
  586. );
  587. }
  588. }
  589. else {
  590. // No cmd or shift click - deselect if selected, or select
  591. // this row only
  592. var selected = dt[type+'s']( { selected: true } );
  593. if ( isSelected && selected.flatten().length === 1 ) {
  594. dt[type]( idx ).deselect();
  595. }
  596. else {
  597. selected.deselect();
  598. dt[type]( idx ).select();
  599. }
  600. }
  601. } else if ( style == 'multi+shift' ) {
  602. if ( e.shiftKey ) {
  603. if ( type === 'cell' ) {
  604. cellRange( dt, idx, ctx._select_lastCell || null );
  605. }
  606. else {
  607. rowColumnRange( dt, type, idx, ctx._select_lastCell ?
  608. ctx._select_lastCell[type] :
  609. null
  610. );
  611. }
  612. }
  613. else {
  614. dt[ type ]( idx ).select( ! isSelected );
  615. }
  616. }
  617. else {
  618. dt[ type ]( idx ).select( ! isSelected );
  619. }
  620. }
  621. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  622. * DataTables selectors
  623. */
  624. // row and column are basically identical just assigned to different properties
  625. // and checking a different array, so we can dynamically create the functions to
  626. // reduce the code size
  627. $.each( [
  628. { type: 'row', prop: 'aoData' },
  629. { type: 'column', prop: 'aoColumns' }
  630. ], function ( i, o ) {
  631. DataTable.ext.selector[ o.type ].push( function ( settings, opts, indexes ) {
  632. var selected = opts.selected;
  633. var data;
  634. var out = [];
  635. if ( selected !== true && selected !== false ) {
  636. return indexes;
  637. }
  638. for ( var i=0, ien=indexes.length ; i<ien ; i++ ) {
  639. data = settings[ o.prop ][ indexes[i] ];
  640. if ( (selected === true && data._select_selected === true) ||
  641. (selected === false && ! data._select_selected )
  642. ) {
  643. out.push( indexes[i] );
  644. }
  645. }
  646. return out;
  647. } );
  648. } );
  649. DataTable.ext.selector.cell.push( function ( settings, opts, cells ) {
  650. var selected = opts.selected;
  651. var rowData;
  652. var out = [];
  653. if ( selected === undefined ) {
  654. return cells;
  655. }
  656. for ( var i=0, ien=cells.length ; i<ien ; i++ ) {
  657. rowData = settings.aoData[ cells[i].row ];
  658. if ( (selected === true && rowData._selected_cells && rowData._selected_cells[ cells[i].column ] === true) ||
  659. (selected === false && ( ! rowData._selected_cells || ! rowData._selected_cells[ cells[i].column ] ) )
  660. ) {
  661. out.push( cells[i] );
  662. }
  663. }
  664. return out;
  665. } );
  666. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  667. * DataTables API
  668. *
  669. * For complete documentation, please refer to the docs/api directory or the
  670. * DataTables site
  671. */
  672. // Local variables to improve compression
  673. var apiRegister = DataTable.Api.register;
  674. var apiRegisterPlural = DataTable.Api.registerPlural;
  675. apiRegister( 'select()', function () {
  676. return this.iterator( 'table', function ( ctx ) {
  677. DataTable.select.init( new DataTable.Api( ctx ) );
  678. } );
  679. } );
  680. apiRegister( 'select.blurable()', function ( flag ) {
  681. if ( flag === undefined ) {
  682. return this.context[0]._select.blurable;
  683. }
  684. return this.iterator( 'table', function ( ctx ) {
  685. ctx._select.blurable = flag;
  686. } );
  687. } );
  688. apiRegister( 'select.info()', function ( flag ) {
  689. if ( info === undefined ) {
  690. return this.context[0]._select.info;
  691. }
  692. return this.iterator( 'table', function ( ctx ) {
  693. ctx._select.info = flag;
  694. } );
  695. } );
  696. apiRegister( 'select.items()', function ( items ) {
  697. if ( items === undefined ) {
  698. return this.context[0]._select.items;
  699. }
  700. return this.iterator( 'table', function ( ctx ) {
  701. ctx._select.items = items;
  702. eventTrigger( new DataTable.Api( ctx ), 'selectItems', [ items ] );
  703. } );
  704. } );
  705. // Takes effect from the _next_ selection. None disables future selection, but
  706. // does not clear the current selection. Use the `deselect` methods for that
  707. apiRegister( 'select.style()', function ( style ) {
  708. if ( style === undefined ) {
  709. return this.context[0]._select.style;
  710. }
  711. return this.iterator( 'table', function ( ctx ) {
  712. ctx._select.style = style;
  713. if ( ! ctx._select_init ) {
  714. init( ctx );
  715. }
  716. // Add / remove mouse event handlers. They aren't required when only
  717. // API selection is available
  718. var dt = new DataTable.Api( ctx );
  719. disableMouseSelection( dt );
  720. if ( style !== 'api' ) {
  721. enableMouseSelection( dt );
  722. }
  723. eventTrigger( new DataTable.Api( ctx ), 'selectStyle', [ style ] );
  724. } );
  725. } );
  726. apiRegister( 'select.selector()', function ( selector ) {
  727. if ( selector === undefined ) {
  728. return this.context[0]._select.selector;
  729. }
  730. return this.iterator( 'table', function ( ctx ) {
  731. disableMouseSelection( new DataTable.Api( ctx ) );
  732. ctx._select.selector = selector;
  733. if ( ctx._select.style !== 'api' ) {
  734. enableMouseSelection( new DataTable.Api( ctx ) );
  735. }
  736. } );
  737. } );
  738. apiRegisterPlural( 'rows().select()', 'row().select()', function ( select ) {
  739. var api = this;
  740. if ( select === false ) {
  741. return this.deselect();
  742. }
  743. this.iterator( 'row', function ( ctx, idx ) {
  744. clear( ctx );
  745. ctx.aoData[ idx ]._select_selected = true;
  746. $( ctx.aoData[ idx ].nTr ).addClass( ctx._select.className );
  747. } );
  748. this.iterator( 'table', function ( ctx, i ) {
  749. eventTrigger( api, 'select', [ 'row', api[i] ], true );
  750. } );
  751. return this;
  752. } );
  753. apiRegisterPlural( 'columns().select()', 'column().select()', function ( select ) {
  754. var api = this;
  755. if ( select === false ) {
  756. return this.deselect();
  757. }
  758. this.iterator( 'column', function ( ctx, idx ) {
  759. clear( ctx );
  760. ctx.aoColumns[ idx ]._select_selected = true;
  761. var column = new DataTable.Api( ctx ).column( idx );
  762. $( column.header() ).addClass( ctx._select.className );
  763. $( column.footer() ).addClass( ctx._select.className );
  764. column.nodes().to$().addClass( ctx._select.className );
  765. } );
  766. this.iterator( 'table', function ( ctx, i ) {
  767. eventTrigger( api, 'select', [ 'column', api[i] ], true );
  768. } );
  769. return this;
  770. } );
  771. apiRegisterPlural( 'cells().select()', 'cell().select()', function ( select ) {
  772. var api = this;
  773. if ( select === false ) {
  774. return this.deselect();
  775. }
  776. this.iterator( 'cell', function ( ctx, rowIdx, colIdx ) {
  777. clear( ctx );
  778. var data = ctx.aoData[ rowIdx ];
  779. if ( data._selected_cells === undefined ) {
  780. data._selected_cells = [];
  781. }
  782. data._selected_cells[ colIdx ] = true;
  783. if ( data.anCells ) {
  784. $( data.anCells[ colIdx ] ).addClass( ctx._select.className );
  785. }
  786. } );
  787. this.iterator( 'table', function ( ctx, i ) {
  788. eventTrigger( api, 'select', [ 'cell', api[i] ], true );
  789. } );
  790. return this;
  791. } );
  792. apiRegisterPlural( 'rows().deselect()', 'row().deselect()', function () {
  793. var api = this;
  794. this.iterator( 'row', function ( ctx, idx ) {
  795. ctx.aoData[ idx ]._select_selected = false;
  796. $( ctx.aoData[ idx ].nTr ).removeClass( ctx._select.className );
  797. } );
  798. this.iterator( 'table', function ( ctx, i ) {
  799. eventTrigger( api, 'deselect', [ 'row', api[i] ], true );
  800. } );
  801. return this;
  802. } );
  803. apiRegisterPlural( 'columns().deselect()', 'column().deselect()', function () {
  804. var api = this;
  805. this.iterator( 'column', function ( ctx, idx ) {
  806. ctx.aoColumns[ idx ]._select_selected = false;
  807. var api = new DataTable.Api( ctx );
  808. var column = api.column( idx );
  809. $( column.header() ).removeClass( ctx._select.className );
  810. $( column.footer() ).removeClass( ctx._select.className );
  811. // Need to loop over each cell, rather than just using
  812. // `column().nodes()` as cells which are individually selected should
  813. // not have the `selected` class removed from them
  814. api.cells( null, idx ).indexes().each( function (cellIdx) {
  815. var data = ctx.aoData[ cellIdx.row ];
  816. var cellSelected = data._selected_cells;
  817. if ( data.anCells && (! cellSelected || ! cellSelected[ cellIdx.column ]) ) {
  818. $( data.anCells[ cellIdx.column ] ).removeClass( ctx._select.className );
  819. }
  820. } );
  821. } );
  822. this.iterator( 'table', function ( ctx, i ) {
  823. eventTrigger( api, 'deselect', [ 'column', api[i] ], true );
  824. } );
  825. return this;
  826. } );
  827. apiRegisterPlural( 'cells().deselect()', 'cell().deselect()', function () {
  828. var api = this;
  829. this.iterator( 'cell', function ( ctx, rowIdx, colIdx ) {
  830. var data = ctx.aoData[ rowIdx ];
  831. data._selected_cells[ colIdx ] = false;
  832. // Remove class only if the cells exist, and the cell is not column
  833. // selected, in which case the class should remain (since it is selected
  834. // in the column)
  835. if ( data.anCells && ! ctx.aoColumns[ colIdx ]._select_selected ) {
  836. $( data.anCells[ colIdx ] ).removeClass( ctx._select.className );
  837. }
  838. } );
  839. this.iterator( 'table', function ( ctx, i ) {
  840. eventTrigger( api, 'deselect', [ 'cell', api[i] ], true );
  841. } );
  842. return this;
  843. } );
  844. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  845. * Buttons
  846. */
  847. function i18n( label, def ) {
  848. return function (dt) {
  849. return dt.i18n( 'buttons.'+label, def );
  850. };
  851. }
  852. // Common events with suitable namespaces
  853. function namespacedEvents ( config ) {
  854. var unique = config._eventNamespace;
  855. return 'draw.dt.DT'+unique+' select.dt.DT'+unique+' deselect.dt.DT'+unique;
  856. }
  857. function enabled ( dt, config ) {
  858. if ( $.inArray( 'rows', config.limitTo ) !== -1 && dt.rows( { selected: true } ).any() ) {
  859. return true;
  860. }
  861. if ( $.inArray( 'columns', config.limitTo ) !== -1 && dt.columns( { selected: true } ).any() ) {
  862. return true;
  863. }
  864. if ( $.inArray( 'cells', config.limitTo ) !== -1 && dt.cells( { selected: true } ).any() ) {
  865. return true;
  866. }
  867. return false;
  868. }
  869. var _buttonNamespace = 0;
  870. $.extend( DataTable.ext.buttons, {
  871. selected: {
  872. text: i18n( 'selected', 'Selected' ),
  873. className: 'buttons-selected',
  874. limitTo: [ 'rows', 'columns', 'cells' ],
  875. init: function ( dt, node, config ) {
  876. var that = this;
  877. config._eventNamespace = '.select'+(_buttonNamespace++);
  878. // .DT namespace listeners are removed by DataTables automatically
  879. // on table destroy
  880. dt.on( namespacedEvents(config), function () {
  881. that.enable( enabled(dt, config) );
  882. } );
  883. this.disable();
  884. },
  885. destroy: function ( dt, node, config ) {
  886. dt.off( config._eventNamespace );
  887. }
  888. },
  889. selectedSingle: {
  890. text: i18n( 'selectedSingle', 'Selected single' ),
  891. className: 'buttons-selected-single',
  892. init: function ( dt, node, config ) {
  893. var that = this;
  894. config._eventNamespace = '.select'+(_buttonNamespace++);
  895. dt.on( namespacedEvents(config), function () {
  896. var count = dt.rows( { selected: true } ).flatten().length +
  897. dt.columns( { selected: true } ).flatten().length +
  898. dt.cells( { selected: true } ).flatten().length;
  899. that.enable( count === 1 );
  900. } );
  901. this.disable();
  902. },
  903. destroy: function ( dt, node, config ) {
  904. dt.off( config._eventNamespace );
  905. }
  906. },
  907. selectAll: {
  908. text: i18n( 'selectAll', 'Select all' ),
  909. className: 'buttons-select-all',
  910. action: function () {
  911. var items = this.select.items();
  912. this[ items+'s' ]().select();
  913. }
  914. },
  915. selectNone: {
  916. text: i18n( 'selectNone', 'Deselect all' ),
  917. className: 'buttons-select-none',
  918. action: function () {
  919. clear( this.settings()[0], true );
  920. },
  921. init: function ( dt, node, config ) {
  922. var that = this;
  923. config._eventNamespace = '.select'+(_buttonNamespace++);
  924. dt.on( namespacedEvents(config), function () {
  925. var count = dt.rows( { selected: true } ).flatten().length +
  926. dt.columns( { selected: true } ).flatten().length +
  927. dt.cells( { selected: true } ).flatten().length;
  928. that.enable( count > 0 );
  929. } );
  930. this.disable();
  931. },
  932. destroy: function ( dt, node, config ) {
  933. dt.off( config._eventNamespace );
  934. }
  935. }
  936. } );
  937. $.each( [ 'Row', 'Column', 'Cell' ], function ( i, item ) {
  938. var lc = item.toLowerCase();
  939. DataTable.ext.buttons[ 'select'+item+'s' ] = {
  940. text: i18n( 'select'+item+'s', 'Select '+lc+'s' ),
  941. className: 'buttons-select-'+lc+'s',
  942. action: function () {
  943. this.select.items( lc );
  944. },
  945. init: function ( dt ) {
  946. var that = this;
  947. dt.on( 'selectItems.dt.DT', function ( e, ctx, items ) {
  948. that.active( items === lc );
  949. } );
  950. }
  951. };
  952. } );
  953. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  954. * Initialisation
  955. */
  956. // DataTables creation - check if select has been defined in the options. Note
  957. // this required that the table be in the document! If it isn't then something
  958. // needs to trigger this method unfortunately. The next major release of
  959. // DataTables will rework the events and address this.
  960. $(document).on( 'preInit.dt.dtSelect', function (e, ctx) {
  961. if ( e.namespace !== 'dt' ) {
  962. return;
  963. }
  964. DataTable.select.init( new DataTable.Api( ctx ) );
  965. } );
  966. return DataTable.select;
  967. }));