dataTables.buttons.js 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833
  1. /*! Buttons for DataTables 1.4.2
  2. * ©2016-2017 SpryMedia Ltd - datatables.net/license
  3. */
  4. (function( factory ){
  5. if ( typeof define === 'function' && define.amd ) {
  6. // AMD
  7. define( ['jquery', 'datatables.net'], function ( $ ) {
  8. return factory( $, window, document );
  9. } );
  10. }
  11. else if ( typeof exports === 'object' ) {
  12. // CommonJS
  13. module.exports = function (root, $) {
  14. if ( ! root ) {
  15. root = window;
  16. }
  17. if ( ! $ || ! $.fn.dataTable ) {
  18. $ = require('datatables.net')(root, $).$;
  19. }
  20. return factory( $, root, root.document );
  21. };
  22. }
  23. else {
  24. // Browser
  25. factory( jQuery, window, document );
  26. }
  27. }(function( $, window, document, undefined ) {
  28. 'use strict';
  29. var DataTable = $.fn.dataTable;
  30. // Used for namespacing events added to the document by each instance, so they
  31. // can be removed on destroy
  32. var _instCounter = 0;
  33. // Button namespacing counter for namespacing events on individual buttons
  34. var _buttonCounter = 0;
  35. var _dtButtons = DataTable.ext.buttons;
  36. /**
  37. * [Buttons description]
  38. * @param {[type]}
  39. * @param {[type]}
  40. */
  41. var Buttons = function( dt, config )
  42. {
  43. // If there is no config set it to an empty object
  44. if ( typeof( config ) === 'undefined' ) {
  45. config = {};
  46. }
  47. // Allow a boolean true for defaults
  48. if ( config === true ) {
  49. config = {};
  50. }
  51. // For easy configuration of buttons an array can be given
  52. if ( $.isArray( config ) ) {
  53. config = { buttons: config };
  54. }
  55. this.c = $.extend( true, {}, Buttons.defaults, config );
  56. // Don't want a deep copy for the buttons
  57. if ( config.buttons ) {
  58. this.c.buttons = config.buttons;
  59. }
  60. this.s = {
  61. dt: new DataTable.Api( dt ),
  62. buttons: [],
  63. listenKeys: '',
  64. namespace: 'dtb'+(_instCounter++)
  65. };
  66. this.dom = {
  67. container: $('<'+this.c.dom.container.tag+'/>')
  68. .addClass( this.c.dom.container.className )
  69. };
  70. this._constructor();
  71. };
  72. $.extend( Buttons.prototype, {
  73. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  74. * Public methods
  75. */
  76. /**
  77. * Get the action of a button
  78. * @param {int|string} Button index
  79. * @return {function}
  80. *//**
  81. * Set the action of a button
  82. * @param {node} node Button element
  83. * @param {function} action Function to set
  84. * @return {Buttons} Self for chaining
  85. */
  86. action: function ( node, action )
  87. {
  88. var button = this._nodeToButton( node );
  89. if ( action === undefined ) {
  90. return button.conf.action;
  91. }
  92. button.conf.action = action;
  93. return this;
  94. },
  95. /**
  96. * Add an active class to the button to make to look active or get current
  97. * active state.
  98. * @param {node} node Button element
  99. * @param {boolean} [flag] Enable / disable flag
  100. * @return {Buttons} Self for chaining or boolean for getter
  101. */
  102. active: function ( node, flag ) {
  103. var button = this._nodeToButton( node );
  104. var klass = this.c.dom.button.active;
  105. var jqNode = $(button.node);
  106. if ( flag === undefined ) {
  107. return jqNode.hasClass( klass );
  108. }
  109. jqNode.toggleClass( klass, flag === undefined ? true : flag );
  110. return this;
  111. },
  112. /**
  113. * Add a new button
  114. * @param {object} config Button configuration object, base string name or function
  115. * @param {int|string} [idx] Button index for where to insert the button
  116. * @return {Buttons} Self for chaining
  117. */
  118. add: function ( config, idx )
  119. {
  120. var buttons = this.s.buttons;
  121. if ( typeof idx === 'string' ) {
  122. var split = idx.split('-');
  123. var base = this.s;
  124. for ( var i=0, ien=split.length-1 ; i<ien ; i++ ) {
  125. base = base.buttons[ split[i]*1 ];
  126. }
  127. buttons = base.buttons;
  128. idx = split[ split.length-1 ]*1;
  129. }
  130. this._expandButton( buttons, config, false, idx );
  131. this._draw();
  132. return this;
  133. },
  134. /**
  135. * Get the container node for the buttons
  136. * @return {jQuery} Buttons node
  137. */
  138. container: function ()
  139. {
  140. return this.dom.container;
  141. },
  142. /**
  143. * Disable a button
  144. * @param {node} node Button node
  145. * @return {Buttons} Self for chaining
  146. */
  147. disable: function ( node ) {
  148. var button = this._nodeToButton( node );
  149. $(button.node).addClass( this.c.dom.button.disabled );
  150. return this;
  151. },
  152. /**
  153. * Destroy the instance, cleaning up event handlers and removing DOM
  154. * elements
  155. * @return {Buttons} Self for chaining
  156. */
  157. destroy: function ()
  158. {
  159. // Key event listener
  160. $('body').off( 'keyup.'+this.s.namespace );
  161. // Individual button destroy (so they can remove their own events if
  162. // needed). Take a copy as the array is modified by `remove`
  163. var buttons = this.s.buttons.slice();
  164. var i, ien;
  165. for ( i=0, ien=buttons.length ; i<ien ; i++ ) {
  166. this.remove( buttons[i].node );
  167. }
  168. // Container
  169. this.dom.container.remove();
  170. // Remove from the settings object collection
  171. var buttonInsts = this.s.dt.settings()[0];
  172. for ( i=0, ien=buttonInsts.length ; i<ien ; i++ ) {
  173. if ( buttonInsts.inst === this ) {
  174. buttonInsts.splice( i, 1 );
  175. break;
  176. }
  177. }
  178. return this;
  179. },
  180. /**
  181. * Enable / disable a button
  182. * @param {node} node Button node
  183. * @param {boolean} [flag=true] Enable / disable flag
  184. * @return {Buttons} Self for chaining
  185. */
  186. enable: function ( node, flag )
  187. {
  188. if ( flag === false ) {
  189. return this.disable( node );
  190. }
  191. var button = this._nodeToButton( node );
  192. $(button.node).removeClass( this.c.dom.button.disabled );
  193. return this;
  194. },
  195. /**
  196. * Get the instance name for the button set selector
  197. * @return {string} Instance name
  198. */
  199. name: function ()
  200. {
  201. return this.c.name;
  202. },
  203. /**
  204. * Get a button's node
  205. * @param {node} node Button node
  206. * @return {jQuery} Button element
  207. */
  208. node: function ( node )
  209. {
  210. var button = this._nodeToButton( node );
  211. return $(button.node);
  212. },
  213. /**
  214. * Set / get a processing class on the selected button
  215. * @param {boolean} flag true to add, false to remove, undefined to get
  216. * @return {boolean|Buttons} Getter value or this if a setter.
  217. */
  218. processing: function ( node, flag )
  219. {
  220. var button = this._nodeToButton( node );
  221. if ( flag === undefined ) {
  222. return $(button.node).hasClass( 'processing' );
  223. }
  224. $(button.node).toggleClass( 'processing', flag );
  225. return this;
  226. },
  227. /**
  228. * Remove a button.
  229. * @param {node} node Button node
  230. * @return {Buttons} Self for chaining
  231. */
  232. remove: function ( node )
  233. {
  234. var button = this._nodeToButton( node );
  235. var host = this._nodeToHost( node );
  236. var dt = this.s.dt;
  237. // Remove any child buttons first
  238. if ( button.buttons.length ) {
  239. for ( var i=button.buttons.length-1 ; i>=0 ; i-- ) {
  240. this.remove( button.buttons[i].node );
  241. }
  242. }
  243. // Allow the button to remove event handlers, etc
  244. if ( button.conf.destroy ) {
  245. button.conf.destroy.call( dt.button(node), dt, $(node), button.conf );
  246. }
  247. this._removeKey( button.conf );
  248. $(button.node).remove();
  249. var idx = $.inArray( button, host );
  250. host.splice( idx, 1 );
  251. return this;
  252. },
  253. /**
  254. * Get the text for a button
  255. * @param {int|string} node Button index
  256. * @return {string} Button text
  257. *//**
  258. * Set the text for a button
  259. * @param {int|string|function} node Button index
  260. * @param {string} label Text
  261. * @return {Buttons} Self for chaining
  262. */
  263. text: function ( node, label )
  264. {
  265. var button = this._nodeToButton( node );
  266. var buttonLiner = this.c.dom.collection.buttonLiner;
  267. var linerTag = button.inCollection && buttonLiner && buttonLiner.tag ?
  268. buttonLiner.tag :
  269. this.c.dom.buttonLiner.tag;
  270. var dt = this.s.dt;
  271. var jqNode = $(button.node);
  272. var text = function ( opt ) {
  273. return typeof opt === 'function' ?
  274. opt( dt, jqNode, button.conf ) :
  275. opt;
  276. };
  277. if ( label === undefined ) {
  278. return text( button.conf.text );
  279. }
  280. button.conf.text = label;
  281. if ( linerTag ) {
  282. jqNode.children( linerTag ).html( text(label) );
  283. }
  284. else {
  285. jqNode.html( text(label) );
  286. }
  287. return this;
  288. },
  289. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  290. * Constructor
  291. */
  292. /**
  293. * Buttons constructor
  294. * @private
  295. */
  296. _constructor: function ()
  297. {
  298. var that = this;
  299. var dt = this.s.dt;
  300. var dtSettings = dt.settings()[0];
  301. var buttons = this.c.buttons;
  302. if ( ! dtSettings._buttons ) {
  303. dtSettings._buttons = [];
  304. }
  305. dtSettings._buttons.push( {
  306. inst: this,
  307. name: this.c.name
  308. } );
  309. for ( var i=0, ien=buttons.length ; i<ien ; i++ ) {
  310. this.add( buttons[i] );
  311. }
  312. dt.on( 'destroy', function () {
  313. that.destroy();
  314. } );
  315. // Global key event binding to listen for button keys
  316. $('body').on( 'keyup.'+this.s.namespace, function ( e ) {
  317. if ( ! document.activeElement || document.activeElement === document.body ) {
  318. // SUse a string of characters for fast lookup of if we need to
  319. // handle this
  320. var character = String.fromCharCode(e.keyCode).toLowerCase();
  321. if ( that.s.listenKeys.toLowerCase().indexOf( character ) !== -1 ) {
  322. that._keypress( character, e );
  323. }
  324. }
  325. } );
  326. },
  327. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  328. * Private methods
  329. */
  330. /**
  331. * Add a new button to the key press listener
  332. * @param {object} conf Resolved button configuration object
  333. * @private
  334. */
  335. _addKey: function ( conf )
  336. {
  337. if ( conf.key ) {
  338. this.s.listenKeys += $.isPlainObject( conf.key ) ?
  339. conf.key.key :
  340. conf.key;
  341. }
  342. },
  343. /**
  344. * Insert the buttons into the container. Call without parameters!
  345. * @param {node} [container] Recursive only - Insert point
  346. * @param {array} [buttons] Recursive only - Buttons array
  347. * @private
  348. */
  349. _draw: function ( container, buttons )
  350. {
  351. if ( ! container ) {
  352. container = this.dom.container;
  353. buttons = this.s.buttons;
  354. }
  355. container.children().detach();
  356. for ( var i=0, ien=buttons.length ; i<ien ; i++ ) {
  357. container.append( buttons[i].inserter );
  358. container.append( ' ' );
  359. if ( buttons[i].buttons && buttons[i].buttons.length ) {
  360. this._draw( buttons[i].collection, buttons[i].buttons );
  361. }
  362. }
  363. },
  364. /**
  365. * Create buttons from an array of buttons
  366. * @param {array} attachTo Buttons array to attach to
  367. * @param {object} button Button definition
  368. * @param {boolean} inCollection true if the button is in a collection
  369. * @private
  370. */
  371. _expandButton: function ( attachTo, button, inCollection, attachPoint )
  372. {
  373. var dt = this.s.dt;
  374. var buttonCounter = 0;
  375. var buttons = ! $.isArray( button ) ?
  376. [ button ] :
  377. button;
  378. for ( var i=0, ien=buttons.length ; i<ien ; i++ ) {
  379. var conf = this._resolveExtends( buttons[i] );
  380. if ( ! conf ) {
  381. continue;
  382. }
  383. // If the configuration is an array, then expand the buttons at this
  384. // point
  385. if ( $.isArray( conf ) ) {
  386. this._expandButton( attachTo, conf, inCollection, attachPoint );
  387. continue;
  388. }
  389. var built = this._buildButton( conf, inCollection );
  390. if ( ! built ) {
  391. continue;
  392. }
  393. if ( attachPoint !== undefined ) {
  394. attachTo.splice( attachPoint, 0, built );
  395. attachPoint++;
  396. }
  397. else {
  398. attachTo.push( built );
  399. }
  400. if ( built.conf.buttons ) {
  401. var collectionDom = this.c.dom.collection;
  402. built.collection = $('<'+collectionDom.tag+'/>')
  403. .addClass( collectionDom.className )
  404. .attr( 'role', 'menu') ;
  405. built.conf._collection = built.collection;
  406. this._expandButton( built.buttons, built.conf.buttons, true, attachPoint );
  407. }
  408. // init call is made here, rather than buildButton as it needs to
  409. // be selectable, and for that it needs to be in the buttons array
  410. if ( conf.init ) {
  411. conf.init.call( dt.button( built.node ), dt, $(built.node), conf );
  412. }
  413. buttonCounter++;
  414. }
  415. },
  416. /**
  417. * Create an individual button
  418. * @param {object} config Resolved button configuration
  419. * @param {boolean} inCollection `true` if a collection button
  420. * @return {jQuery} Created button node (jQuery)
  421. * @private
  422. */
  423. _buildButton: function ( config, inCollection )
  424. {
  425. var buttonDom = this.c.dom.button;
  426. var linerDom = this.c.dom.buttonLiner;
  427. var collectionDom = this.c.dom.collection;
  428. var dt = this.s.dt;
  429. var text = function ( opt ) {
  430. return typeof opt === 'function' ?
  431. opt( dt, button, config ) :
  432. opt;
  433. };
  434. if ( inCollection && collectionDom.button ) {
  435. buttonDom = collectionDom.button;
  436. }
  437. if ( inCollection && collectionDom.buttonLiner ) {
  438. linerDom = collectionDom.buttonLiner;
  439. }
  440. // Make sure that the button is available based on whatever requirements
  441. // it has. For example, Flash buttons require Flash
  442. if ( config.available && ! config.available( dt, config ) ) {
  443. return false;
  444. }
  445. var action = function ( e, dt, button, config ) {
  446. config.action.call( dt.button( button ), e, dt, button, config );
  447. $(dt.table().node()).triggerHandler( 'buttons-action.dt', [
  448. dt.button( button ), dt, button, config
  449. ] );
  450. };
  451. var button = $('<'+buttonDom.tag+'/>')
  452. .addClass( buttonDom.className )
  453. .attr( 'tabindex', this.s.dt.settings()[0].iTabIndex )
  454. .attr( 'aria-controls', this.s.dt.table().node().id )
  455. .on( 'click.dtb', function (e) {
  456. e.preventDefault();
  457. if ( ! button.hasClass( buttonDom.disabled ) && config.action ) {
  458. action( e, dt, button, config );
  459. }
  460. button.blur();
  461. } )
  462. .on( 'keyup.dtb', function (e) {
  463. if ( e.keyCode === 13 ) {
  464. if ( ! button.hasClass( buttonDom.disabled ) && config.action ) {
  465. action( e, dt, button, config );
  466. }
  467. }
  468. } );
  469. // Make `a` tags act like a link
  470. if ( buttonDom.tag.toLowerCase() === 'a' ) {
  471. button.attr( 'href', '#' );
  472. }
  473. if ( linerDom.tag ) {
  474. var liner = $('<'+linerDom.tag+'/>')
  475. .html( text( config.text ) )
  476. .addClass( linerDom.className );
  477. if ( linerDom.tag.toLowerCase() === 'a' ) {
  478. liner.attr( 'href', '#' );
  479. }
  480. button.append( liner );
  481. }
  482. else {
  483. button.html( text( config.text ) );
  484. }
  485. if ( config.enabled === false ) {
  486. button.addClass( buttonDom.disabled );
  487. }
  488. if ( config.className ) {
  489. button.addClass( config.className );
  490. }
  491. if ( config.titleAttr ) {
  492. button.attr( 'title', text( config.titleAttr ) );
  493. }
  494. if ( ! config.namespace ) {
  495. config.namespace = '.dt-button-'+(_buttonCounter++);
  496. }
  497. var buttonContainer = this.c.dom.buttonContainer;
  498. var inserter;
  499. if ( buttonContainer && buttonContainer.tag ) {
  500. inserter = $('<'+buttonContainer.tag+'/>')
  501. .addClass( buttonContainer.className )
  502. .append( button );
  503. }
  504. else {
  505. inserter = button;
  506. }
  507. this._addKey( config );
  508. return {
  509. conf: config,
  510. node: button.get(0),
  511. inserter: inserter,
  512. buttons: [],
  513. inCollection: inCollection,
  514. collection: null
  515. };
  516. },
  517. /**
  518. * Get the button object from a node (recursive)
  519. * @param {node} node Button node
  520. * @param {array} [buttons] Button array, uses base if not defined
  521. * @return {object} Button object
  522. * @private
  523. */
  524. _nodeToButton: function ( node, buttons )
  525. {
  526. if ( ! buttons ) {
  527. buttons = this.s.buttons;
  528. }
  529. for ( var i=0, ien=buttons.length ; i<ien ; i++ ) {
  530. if ( buttons[i].node === node ) {
  531. return buttons[i];
  532. }
  533. if ( buttons[i].buttons.length ) {
  534. var ret = this._nodeToButton( node, buttons[i].buttons );
  535. if ( ret ) {
  536. return ret;
  537. }
  538. }
  539. }
  540. },
  541. /**
  542. * Get container array for a button from a button node (recursive)
  543. * @param {node} node Button node
  544. * @param {array} [buttons] Button array, uses base if not defined
  545. * @return {array} Button's host array
  546. * @private
  547. */
  548. _nodeToHost: function ( node, buttons )
  549. {
  550. if ( ! buttons ) {
  551. buttons = this.s.buttons;
  552. }
  553. for ( var i=0, ien=buttons.length ; i<ien ; i++ ) {
  554. if ( buttons[i].node === node ) {
  555. return buttons;
  556. }
  557. if ( buttons[i].buttons.length ) {
  558. var ret = this._nodeToHost( node, buttons[i].buttons );
  559. if ( ret ) {
  560. return ret;
  561. }
  562. }
  563. }
  564. },
  565. /**
  566. * Handle a key press - determine if any button's key configured matches
  567. * what was typed and trigger the action if so.
  568. * @param {string} character The character pressed
  569. * @param {object} e Key event that triggered this call
  570. * @private
  571. */
  572. _keypress: function ( character, e )
  573. {
  574. var run = function ( conf, node ) {
  575. if ( ! conf.key ) {
  576. return;
  577. }
  578. if ( conf.key === character ) {
  579. $(node).click();
  580. }
  581. else if ( $.isPlainObject( conf.key ) ) {
  582. if ( conf.key.key !== character ) {
  583. return;
  584. }
  585. if ( conf.key.shiftKey && ! e.shiftKey ) {
  586. return;
  587. }
  588. if ( conf.key.altKey && ! e.altKey ) {
  589. return;
  590. }
  591. if ( conf.key.ctrlKey && ! e.ctrlKey ) {
  592. return;
  593. }
  594. if ( conf.key.metaKey && ! e.metaKey ) {
  595. return;
  596. }
  597. // Made it this far - it is good
  598. $(node).click();
  599. }
  600. };
  601. var recurse = function ( a ) {
  602. for ( var i=0, ien=a.length ; i<ien ; i++ ) {
  603. run( a[i].conf, a[i].node );
  604. if ( a[i].buttons.length ) {
  605. recurse( a[i].buttons );
  606. }
  607. }
  608. };
  609. recurse( this.s.buttons );
  610. },
  611. /**
  612. * Remove a key from the key listener for this instance (to be used when a
  613. * button is removed)
  614. * @param {object} conf Button configuration
  615. * @private
  616. */
  617. _removeKey: function ( conf )
  618. {
  619. if ( conf.key ) {
  620. var character = $.isPlainObject( conf.key ) ?
  621. conf.key.key :
  622. conf.key;
  623. // Remove only one character, as multiple buttons could have the
  624. // same listening key
  625. var a = this.s.listenKeys.split('');
  626. var idx = $.inArray( character, a );
  627. a.splice( idx, 1 );
  628. this.s.listenKeys = a.join('');
  629. }
  630. },
  631. /**
  632. * Resolve a button configuration
  633. * @param {string|function|object} conf Button config to resolve
  634. * @return {object} Button configuration
  635. * @private
  636. */
  637. _resolveExtends: function ( conf )
  638. {
  639. var dt = this.s.dt;
  640. var i, ien;
  641. var toConfObject = function ( base ) {
  642. var loop = 0;
  643. // Loop until we have resolved to a button configuration, or an
  644. // array of button configurations (which will be iterated
  645. // separately)
  646. while ( ! $.isPlainObject(base) && ! $.isArray(base) ) {
  647. if ( base === undefined ) {
  648. return;
  649. }
  650. if ( typeof base === 'function' ) {
  651. base = base( dt, conf );
  652. if ( ! base ) {
  653. return false;
  654. }
  655. }
  656. else if ( typeof base === 'string' ) {
  657. if ( ! _dtButtons[ base ] ) {
  658. throw 'Unknown button type: '+base;
  659. }
  660. base = _dtButtons[ base ];
  661. }
  662. loop++;
  663. if ( loop > 30 ) {
  664. // Protect against misconfiguration killing the browser
  665. throw 'Buttons: Too many iterations';
  666. }
  667. }
  668. return $.isArray( base ) ?
  669. base :
  670. $.extend( {}, base );
  671. };
  672. conf = toConfObject( conf );
  673. while ( conf && conf.extend ) {
  674. // Use `toConfObject` in case the button definition being extended
  675. // is itself a string or a function
  676. if ( ! _dtButtons[ conf.extend ] ) {
  677. throw 'Cannot extend unknown button type: '+conf.extend;
  678. }
  679. var objArray = toConfObject( _dtButtons[ conf.extend ] );
  680. if ( $.isArray( objArray ) ) {
  681. return objArray;
  682. }
  683. else if ( ! objArray ) {
  684. // This is a little brutal as it might be possible to have a
  685. // valid button without the extend, but if there is no extend
  686. // then the host button would be acting in an undefined state
  687. return false;
  688. }
  689. // Stash the current class name
  690. var originalClassName = objArray.className;
  691. conf = $.extend( {}, objArray, conf );
  692. // The extend will have overwritten the original class name if the
  693. // `conf` object also assigned a class, but we want to concatenate
  694. // them so they are list that is combined from all extended buttons
  695. if ( originalClassName && conf.className !== originalClassName ) {
  696. conf.className = originalClassName+' '+conf.className;
  697. }
  698. // Buttons to be added to a collection -gives the ability to define
  699. // if buttons should be added to the start or end of a collection
  700. var postfixButtons = conf.postfixButtons;
  701. if ( postfixButtons ) {
  702. if ( ! conf.buttons ) {
  703. conf.buttons = [];
  704. }
  705. for ( i=0, ien=postfixButtons.length ; i<ien ; i++ ) {
  706. conf.buttons.push( postfixButtons[i] );
  707. }
  708. conf.postfixButtons = null;
  709. }
  710. var prefixButtons = conf.prefixButtons;
  711. if ( prefixButtons ) {
  712. if ( ! conf.buttons ) {
  713. conf.buttons = [];
  714. }
  715. for ( i=0, ien=prefixButtons.length ; i<ien ; i++ ) {
  716. conf.buttons.splice( i, 0, prefixButtons[i] );
  717. }
  718. conf.prefixButtons = null;
  719. }
  720. // Although we want the `conf` object to overwrite almost all of
  721. // the properties of the object being extended, the `extend`
  722. // property should come from the object being extended
  723. conf.extend = objArray.extend;
  724. }
  725. return conf;
  726. }
  727. } );
  728. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  729. * Statics
  730. */
  731. /**
  732. * Show / hide a background layer behind a collection
  733. * @param {boolean} Flag to indicate if the background should be shown or
  734. * hidden
  735. * @param {string} Class to assign to the background
  736. * @static
  737. */
  738. Buttons.background = function ( show, className, fade ) {
  739. if ( fade === undefined ) {
  740. fade = 400;
  741. }
  742. if ( show ) {
  743. $('<div/>')
  744. .addClass( className )
  745. .css( 'display', 'none' )
  746. .appendTo( 'body' )
  747. .fadeIn( fade );
  748. }
  749. else {
  750. $('body > div.'+className)
  751. .fadeOut( fade, function () {
  752. $(this)
  753. .removeClass( className )
  754. .remove();
  755. } );
  756. }
  757. };
  758. /**
  759. * Instance selector - select Buttons instances based on an instance selector
  760. * value from the buttons assigned to a DataTable. This is only useful if
  761. * multiple instances are attached to a DataTable.
  762. * @param {string|int|array} Instance selector - see `instance-selector`
  763. * documentation on the DataTables site
  764. * @param {array} Button instance array that was attached to the DataTables
  765. * settings object
  766. * @return {array} Buttons instances
  767. * @static
  768. */
  769. Buttons.instanceSelector = function ( group, buttons )
  770. {
  771. if ( ! group ) {
  772. return $.map( buttons, function ( v ) {
  773. return v.inst;
  774. } );
  775. }
  776. var ret = [];
  777. var names = $.map( buttons, function ( v ) {
  778. return v.name;
  779. } );
  780. // Flatten the group selector into an array of single options
  781. var process = function ( input ) {
  782. if ( $.isArray( input ) ) {
  783. for ( var i=0, ien=input.length ; i<ien ; i++ ) {
  784. process( input[i] );
  785. }
  786. return;
  787. }
  788. if ( typeof input === 'string' ) {
  789. if ( input.indexOf( ',' ) !== -1 ) {
  790. // String selector, list of names
  791. process( input.split(',') );
  792. }
  793. else {
  794. // String selector individual name
  795. var idx = $.inArray( $.trim(input), names );
  796. if ( idx !== -1 ) {
  797. ret.push( buttons[ idx ].inst );
  798. }
  799. }
  800. }
  801. else if ( typeof input === 'number' ) {
  802. // Index selector
  803. ret.push( buttons[ input ].inst );
  804. }
  805. };
  806. process( group );
  807. return ret;
  808. };
  809. /**
  810. * Button selector - select one or more buttons from a selector input so some
  811. * operation can be performed on them.
  812. * @param {array} Button instances array that the selector should operate on
  813. * @param {string|int|node|jQuery|array} Button selector - see
  814. * `button-selector` documentation on the DataTables site
  815. * @return {array} Array of objects containing `inst` and `idx` properties of
  816. * the selected buttons so you know which instance each button belongs to.
  817. * @static
  818. */
  819. Buttons.buttonSelector = function ( insts, selector )
  820. {
  821. var ret = [];
  822. var nodeBuilder = function ( a, buttons, baseIdx ) {
  823. var button;
  824. var idx;
  825. for ( var i=0, ien=buttons.length ; i<ien ; i++ ) {
  826. button = buttons[i];
  827. if ( button ) {
  828. idx = baseIdx !== undefined ?
  829. baseIdx+i :
  830. i+'';
  831. a.push( {
  832. node: button.node,
  833. name: button.conf.name,
  834. idx: idx
  835. } );
  836. if ( button.buttons ) {
  837. nodeBuilder( a, button.buttons, idx+'-' );
  838. }
  839. }
  840. }
  841. };
  842. var run = function ( selector, inst ) {
  843. var i, ien;
  844. var buttons = [];
  845. nodeBuilder( buttons, inst.s.buttons );
  846. var nodes = $.map( buttons, function (v) {
  847. return v.node;
  848. } );
  849. if ( $.isArray( selector ) || selector instanceof $ ) {
  850. for ( i=0, ien=selector.length ; i<ien ; i++ ) {
  851. run( selector[i], inst );
  852. }
  853. return;
  854. }
  855. if ( selector === null || selector === undefined || selector === '*' ) {
  856. // Select all
  857. for ( i=0, ien=buttons.length ; i<ien ; i++ ) {
  858. ret.push( {
  859. inst: inst,
  860. node: buttons[i].node
  861. } );
  862. }
  863. }
  864. else if ( typeof selector === 'number' ) {
  865. // Main button index selector
  866. ret.push( {
  867. inst: inst,
  868. node: inst.s.buttons[ selector ].node
  869. } );
  870. }
  871. else if ( typeof selector === 'string' ) {
  872. if ( selector.indexOf( ',' ) !== -1 ) {
  873. // Split
  874. var a = selector.split(',');
  875. for ( i=0, ien=a.length ; i<ien ; i++ ) {
  876. run( $.trim(a[i]), inst );
  877. }
  878. }
  879. else if ( selector.match( /^\d+(\-\d+)*$/ ) ) {
  880. // Sub-button index selector
  881. var indexes = $.map( buttons, function (v) {
  882. return v.idx;
  883. } );
  884. ret.push( {
  885. inst: inst,
  886. node: buttons[ $.inArray( selector, indexes ) ].node
  887. } );
  888. }
  889. else if ( selector.indexOf( ':name' ) !== -1 ) {
  890. // Button name selector
  891. var name = selector.replace( ':name', '' );
  892. for ( i=0, ien=buttons.length ; i<ien ; i++ ) {
  893. if ( buttons[i].name === name ) {
  894. ret.push( {
  895. inst: inst,
  896. node: buttons[i].node
  897. } );
  898. }
  899. }
  900. }
  901. else {
  902. // jQuery selector on the nodes
  903. $( nodes ).filter( selector ).each( function () {
  904. ret.push( {
  905. inst: inst,
  906. node: this
  907. } );
  908. } );
  909. }
  910. }
  911. else if ( typeof selector === 'object' && selector.nodeName ) {
  912. // Node selector
  913. var idx = $.inArray( selector, nodes );
  914. if ( idx !== -1 ) {
  915. ret.push( {
  916. inst: inst,
  917. node: nodes[ idx ]
  918. } );
  919. }
  920. }
  921. };
  922. for ( var i=0, ien=insts.length ; i<ien ; i++ ) {
  923. var inst = insts[i];
  924. run( selector, inst );
  925. }
  926. return ret;
  927. };
  928. /**
  929. * Buttons defaults. For full documentation, please refer to the docs/option
  930. * directory or the DataTables site.
  931. * @type {Object}
  932. * @static
  933. */
  934. Buttons.defaults = {
  935. buttons: [ 'copy', 'excel', 'csv', 'pdf', 'print' ],
  936. name: 'main',
  937. tabIndex: 0,
  938. dom: {
  939. container: {
  940. tag: 'div',
  941. className: 'dt-buttons'
  942. },
  943. collection: {
  944. tag: 'div',
  945. className: 'dt-button-collection'
  946. },
  947. button: {
  948. tag: 'a',
  949. className: 'dt-button',
  950. active: 'active',
  951. disabled: 'disabled'
  952. },
  953. buttonLiner: {
  954. tag: 'span',
  955. className: ''
  956. }
  957. }
  958. };
  959. /**
  960. * Version information
  961. * @type {string}
  962. * @static
  963. */
  964. Buttons.version = '1.4.2';
  965. $.extend( _dtButtons, {
  966. collection: {
  967. text: function ( dt ) {
  968. return dt.i18n( 'buttons.collection', 'Collection' );
  969. },
  970. className: 'buttons-collection',
  971. action: function ( e, dt, button, config ) {
  972. var host = button;
  973. var hostOffset = host.offset();
  974. var tableContainer = $( dt.table().container() );
  975. var multiLevel = false;
  976. // Remove any old collection
  977. if ( $('div.dt-button-background').length ) {
  978. multiLevel = $('.dt-button-collection').offset();
  979. $('body').trigger( 'click.dtb-collection' );
  980. }
  981. config._collection
  982. .addClass( config.collectionLayout )
  983. .css( 'display', 'none' )
  984. .appendTo( 'body' )
  985. .fadeIn( config.fade );
  986. var position = config._collection.css( 'position' );
  987. if ( multiLevel && position === 'absolute' ) {
  988. config._collection.css( {
  989. top: multiLevel.top,
  990. left: multiLevel.left
  991. } );
  992. }
  993. else if ( position === 'absolute' ) {
  994. config._collection.css( {
  995. top: hostOffset.top + host.outerHeight(),
  996. left: hostOffset.left
  997. } );
  998. // calculate overflow when positioned beneath
  999. var tableBottom = tableContainer.offset().top + tableContainer.height();
  1000. var listBottom = hostOffset.top + host.outerHeight() + config._collection.outerHeight();
  1001. var bottomOverflow = listBottom - tableBottom;
  1002. // calculate overflow when positioned above
  1003. var listTop = hostOffset.top - config._collection.outerHeight();
  1004. var tableTop = tableContainer.offset().top;
  1005. var topOverflow = tableTop - listTop;
  1006. // if bottom overflow is larger, move to the top because it fits better
  1007. if (bottomOverflow > topOverflow) {
  1008. config._collection.css( 'top', hostOffset.top - config._collection.outerHeight() - 5);
  1009. }
  1010. var listRight = hostOffset.left + config._collection.outerWidth();
  1011. var tableRight = tableContainer.offset().left + tableContainer.width();
  1012. if ( listRight > tableRight ) {
  1013. config._collection.css( 'left', hostOffset.left - ( listRight - tableRight ) );
  1014. }
  1015. }
  1016. else {
  1017. // Fix position - centre on screen
  1018. var top = config._collection.height() / 2;
  1019. if ( top > $(window).height() / 2 ) {
  1020. top = $(window).height() / 2;
  1021. }
  1022. config._collection.css( 'marginTop', top*-1 );
  1023. }
  1024. if ( config.background ) {
  1025. Buttons.background( true, config.backgroundClassName, config.fade );
  1026. }
  1027. // Need to break the 'thread' for the collection button being
  1028. // activated by a click - it would also trigger this event
  1029. setTimeout( function () {
  1030. // This is bonkers, but if we don't have a click listener on the
  1031. // background element, iOS Safari will ignore the body click
  1032. // listener below. An empty function here is all that is
  1033. // required to make it work...
  1034. $('div.dt-button-background').on( 'click.dtb-collection', function () {} );
  1035. $('body').on( 'click.dtb-collection', function (e) {
  1036. // andSelf is deprecated in jQ1.8, but we want 1.7 compat
  1037. var back = $.fn.addBack ? 'addBack' : 'andSelf';
  1038. if ( ! $(e.target).parents()[back]().filter( config._collection ).length ) {
  1039. config._collection
  1040. .fadeOut( config.fade, function () {
  1041. config._collection.detach();
  1042. } );
  1043. $('div.dt-button-background').off( 'click.dtb-collection' );
  1044. Buttons.background( false, config.backgroundClassName, config.fade );
  1045. $('body').off( 'click.dtb-collection' );
  1046. dt.off( 'buttons-action.b-internal' );
  1047. }
  1048. } );
  1049. }, 10 );
  1050. if ( config.autoClose ) {
  1051. dt.on( 'buttons-action.b-internal', function () {
  1052. $('div.dt-button-background').click();
  1053. } );
  1054. }
  1055. },
  1056. background: true,
  1057. collectionLayout: '',
  1058. backgroundClassName: 'dt-button-background',
  1059. autoClose: false,
  1060. fade: 400
  1061. },
  1062. copy: function ( dt, conf ) {
  1063. if ( _dtButtons.copyHtml5 ) {
  1064. return 'copyHtml5';
  1065. }
  1066. if ( _dtButtons.copyFlash && _dtButtons.copyFlash.available( dt, conf ) ) {
  1067. return 'copyFlash';
  1068. }
  1069. },
  1070. csv: function ( dt, conf ) {
  1071. // Common option that will use the HTML5 or Flash export buttons
  1072. if ( _dtButtons.csvHtml5 && _dtButtons.csvHtml5.available( dt, conf ) ) {
  1073. return 'csvHtml5';
  1074. }
  1075. if ( _dtButtons.csvFlash && _dtButtons.csvFlash.available( dt, conf ) ) {
  1076. return 'csvFlash';
  1077. }
  1078. },
  1079. excel: function ( dt, conf ) {
  1080. // Common option that will use the HTML5 or Flash export buttons
  1081. if ( _dtButtons.excelHtml5 && _dtButtons.excelHtml5.available( dt, conf ) ) {
  1082. return 'excelHtml5';
  1083. }
  1084. if ( _dtButtons.excelFlash && _dtButtons.excelFlash.available( dt, conf ) ) {
  1085. return 'excelFlash';
  1086. }
  1087. },
  1088. pdf: function ( dt, conf ) {
  1089. // Common option that will use the HTML5 or Flash export buttons
  1090. if ( _dtButtons.pdfHtml5 && _dtButtons.pdfHtml5.available( dt, conf ) ) {
  1091. return 'pdfHtml5';
  1092. }
  1093. if ( _dtButtons.pdfFlash && _dtButtons.pdfFlash.available( dt, conf ) ) {
  1094. return 'pdfFlash';
  1095. }
  1096. },
  1097. pageLength: function ( dt ) {
  1098. var lengthMenu = dt.settings()[0].aLengthMenu;
  1099. var vals = $.isArray( lengthMenu[0] ) ? lengthMenu[0] : lengthMenu;
  1100. var lang = $.isArray( lengthMenu[0] ) ? lengthMenu[1] : lengthMenu;
  1101. var text = function ( dt ) {
  1102. return dt.i18n( 'buttons.pageLength', {
  1103. "-1": 'Show all rows',
  1104. _: 'Show %d rows'
  1105. }, dt.page.len() );
  1106. };
  1107. return {
  1108. extend: 'collection',
  1109. text: text,
  1110. className: 'buttons-page-length',
  1111. autoClose: true,
  1112. buttons: $.map( vals, function ( val, i ) {
  1113. return {
  1114. text: lang[i],
  1115. className: 'button-page-length',
  1116. action: function ( e, dt ) {
  1117. dt.page.len( val ).draw();
  1118. },
  1119. init: function ( dt, node, conf ) {
  1120. var that = this;
  1121. var fn = function () {
  1122. that.active( dt.page.len() === val );
  1123. };
  1124. dt.on( 'length.dt'+conf.namespace, fn );
  1125. fn();
  1126. },
  1127. destroy: function ( dt, node, conf ) {
  1128. dt.off( 'length.dt'+conf.namespace );
  1129. }
  1130. };
  1131. } ),
  1132. init: function ( dt, node, conf ) {
  1133. var that = this;
  1134. dt.on( 'length.dt'+conf.namespace, function () {
  1135. that.text( text( dt ) );
  1136. } );
  1137. },
  1138. destroy: function ( dt, node, conf ) {
  1139. dt.off( 'length.dt'+conf.namespace );
  1140. }
  1141. };
  1142. }
  1143. } );
  1144. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  1145. * DataTables API
  1146. *
  1147. * For complete documentation, please refer to the docs/api directory or the
  1148. * DataTables site
  1149. */
  1150. // Buttons group and individual button selector
  1151. DataTable.Api.register( 'buttons()', function ( group, selector ) {
  1152. // Argument shifting
  1153. if ( selector === undefined ) {
  1154. selector = group;
  1155. group = undefined;
  1156. }
  1157. this.selector.buttonGroup = group;
  1158. var res = this.iterator( true, 'table', function ( ctx ) {
  1159. if ( ctx._buttons ) {
  1160. return Buttons.buttonSelector(
  1161. Buttons.instanceSelector( group, ctx._buttons ),
  1162. selector
  1163. );
  1164. }
  1165. }, true );
  1166. res._groupSelector = group;
  1167. return res;
  1168. } );
  1169. // Individual button selector
  1170. DataTable.Api.register( 'button()', function ( group, selector ) {
  1171. // just run buttons() and truncate
  1172. var buttons = this.buttons( group, selector );
  1173. if ( buttons.length > 1 ) {
  1174. buttons.splice( 1, buttons.length );
  1175. }
  1176. return buttons;
  1177. } );
  1178. // Active buttons
  1179. DataTable.Api.registerPlural( 'buttons().active()', 'button().active()', function ( flag ) {
  1180. if ( flag === undefined ) {
  1181. return this.map( function ( set ) {
  1182. return set.inst.active( set.node );
  1183. } );
  1184. }
  1185. return this.each( function ( set ) {
  1186. set.inst.active( set.node, flag );
  1187. } );
  1188. } );
  1189. // Get / set button action
  1190. DataTable.Api.registerPlural( 'buttons().action()', 'button().action()', function ( action ) {
  1191. if ( action === undefined ) {
  1192. return this.map( function ( set ) {
  1193. return set.inst.action( set.node );
  1194. } );
  1195. }
  1196. return this.each( function ( set ) {
  1197. set.inst.action( set.node, action );
  1198. } );
  1199. } );
  1200. // Enable / disable buttons
  1201. DataTable.Api.register( ['buttons().enable()', 'button().enable()'], function ( flag ) {
  1202. return this.each( function ( set ) {
  1203. set.inst.enable( set.node, flag );
  1204. } );
  1205. } );
  1206. // Disable buttons
  1207. DataTable.Api.register( ['buttons().disable()', 'button().disable()'], function () {
  1208. return this.each( function ( set ) {
  1209. set.inst.disable( set.node );
  1210. } );
  1211. } );
  1212. // Get button nodes
  1213. DataTable.Api.registerPlural( 'buttons().nodes()', 'button().node()', function () {
  1214. var jq = $();
  1215. // jQuery will automatically reduce duplicates to a single entry
  1216. $( this.each( function ( set ) {
  1217. jq = jq.add( set.inst.node( set.node ) );
  1218. } ) );
  1219. return jq;
  1220. } );
  1221. // Get / set button processing state
  1222. DataTable.Api.registerPlural( 'buttons().processing()', 'button().processing()', function ( flag ) {
  1223. if ( flag === undefined ) {
  1224. return this.map( function ( set ) {
  1225. return set.inst.processing( set.node );
  1226. } );
  1227. }
  1228. return this.each( function ( set ) {
  1229. set.inst.processing( set.node, flag );
  1230. } );
  1231. } );
  1232. // Get / set button text (i.e. the button labels)
  1233. DataTable.Api.registerPlural( 'buttons().text()', 'button().text()', function ( label ) {
  1234. if ( label === undefined ) {
  1235. return this.map( function ( set ) {
  1236. return set.inst.text( set.node );
  1237. } );
  1238. }
  1239. return this.each( function ( set ) {
  1240. set.inst.text( set.node, label );
  1241. } );
  1242. } );
  1243. // Trigger a button's action
  1244. DataTable.Api.registerPlural( 'buttons().trigger()', 'button().trigger()', function () {
  1245. return this.each( function ( set ) {
  1246. set.inst.node( set.node ).trigger( 'click' );
  1247. } );
  1248. } );
  1249. // Get the container elements
  1250. DataTable.Api.registerPlural( 'buttons().containers()', 'buttons().container()', function () {
  1251. var jq = $();
  1252. var groupSelector = this._groupSelector;
  1253. // We need to use the group selector directly, since if there are no buttons
  1254. // the result set will be empty
  1255. this.iterator( true, 'table', function ( ctx ) {
  1256. if ( ctx._buttons ) {
  1257. var insts = Buttons.instanceSelector( groupSelector, ctx._buttons );
  1258. for ( var i=0, ien=insts.length ; i<ien ; i++ ) {
  1259. jq = jq.add( insts[i].container() );
  1260. }
  1261. }
  1262. } );
  1263. return jq;
  1264. } );
  1265. // Add a new button
  1266. DataTable.Api.register( 'button().add()', function ( idx, conf ) {
  1267. var ctx = this.context;
  1268. // Don't use `this` as it could be empty - select the instances directly
  1269. if ( ctx.length ) {
  1270. var inst = Buttons.instanceSelector( this._groupSelector, ctx[0]._buttons );
  1271. if ( inst.length ) {
  1272. inst[0].add( conf, idx );
  1273. }
  1274. }
  1275. return this.button( this._groupSelector, idx );
  1276. } );
  1277. // Destroy the button sets selected
  1278. DataTable.Api.register( 'buttons().destroy()', function () {
  1279. this.pluck( 'inst' ).unique().each( function ( inst ) {
  1280. inst.destroy();
  1281. } );
  1282. return this;
  1283. } );
  1284. // Remove a button
  1285. DataTable.Api.registerPlural( 'buttons().remove()', 'buttons().remove()', function () {
  1286. this.each( function ( set ) {
  1287. set.inst.remove( set.node );
  1288. } );
  1289. return this;
  1290. } );
  1291. // Information box that can be used by buttons
  1292. var _infoTimer;
  1293. DataTable.Api.register( 'buttons.info()', function ( title, message, time ) {
  1294. var that = this;
  1295. if ( title === false ) {
  1296. $('#datatables_buttons_info').fadeOut( function () {
  1297. $(this).remove();
  1298. } );
  1299. clearTimeout( _infoTimer );
  1300. _infoTimer = null;
  1301. return this;
  1302. }
  1303. if ( _infoTimer ) {
  1304. clearTimeout( _infoTimer );
  1305. }
  1306. if ( $('#datatables_buttons_info').length ) {
  1307. $('#datatables_buttons_info').remove();
  1308. }
  1309. title = title ? '<h2>'+title+'</h2>' : '';
  1310. $('<div id="datatables_buttons_info" class="dt-button-info"/>')
  1311. .html( title )
  1312. .append( $('<div/>')[ typeof message === 'string' ? 'html' : 'append' ]( message ) )
  1313. .css( 'display', 'none' )
  1314. .appendTo( 'body' )
  1315. .fadeIn();
  1316. if ( time !== undefined && time !== 0 ) {
  1317. _infoTimer = setTimeout( function () {
  1318. that.buttons.info( false );
  1319. }, time );
  1320. }
  1321. return this;
  1322. } );
  1323. // Get data from the table for export - this is common to a number of plug-in
  1324. // buttons so it is included in the Buttons core library
  1325. DataTable.Api.register( 'buttons.exportData()', function ( options ) {
  1326. if ( this.context.length ) {
  1327. return _exportData( new DataTable.Api( this.context[0] ), options );
  1328. }
  1329. } );
  1330. // Get information about the export that is common to many of the export data
  1331. // types (DRY)
  1332. DataTable.Api.register( 'buttons.exportInfo()', function ( conf ) {
  1333. if ( ! conf ) {
  1334. conf = {};
  1335. }
  1336. return {
  1337. filename: _filename( conf ),
  1338. title: _title( conf ),
  1339. messageTop: _message(this, conf.messageTop || conf.message, 'top'),
  1340. messageBottom: _message(this, conf.messageBottom, 'bottom')
  1341. };
  1342. } );
  1343. /**
  1344. * Get the file name for an exported file.
  1345. *
  1346. * @param {object} config Button configuration
  1347. * @param {boolean} incExtension Include the file name extension
  1348. */
  1349. var _filename = function ( config )
  1350. {
  1351. // Backwards compatibility
  1352. var filename = config.filename === '*' && config.title !== '*' && config.title !== undefined ?
  1353. config.title :
  1354. config.filename;
  1355. if ( typeof filename === 'function' ) {
  1356. filename = filename();
  1357. }
  1358. if ( filename === undefined || filename === null ) {
  1359. return null;
  1360. }
  1361. if ( filename.indexOf( '*' ) !== -1 ) {
  1362. filename = $.trim( filename.replace( '*', $('title').text() ) );
  1363. }
  1364. // Strip characters which the OS will object to
  1365. filename = filename.replace(/[^a-zA-Z0-9_\u00A1-\uFFFF\.,\-_ !\(\)]/g, "");
  1366. var extension = _stringOrFunction( config.extension );
  1367. if ( ! extension ) {
  1368. extension = '';
  1369. }
  1370. return filename + extension;
  1371. };
  1372. /**
  1373. * Simply utility method to allow parameters to be given as a function
  1374. *
  1375. * @param {undefined|string|function} option Option
  1376. * @return {null|string} Resolved value
  1377. */
  1378. var _stringOrFunction = function ( option )
  1379. {
  1380. if ( option === null || option === undefined ) {
  1381. return null;
  1382. }
  1383. else if ( typeof option === 'function' ) {
  1384. return option();
  1385. }
  1386. return option;
  1387. };
  1388. /**
  1389. * Get the title for an exported file.
  1390. *
  1391. * @param {object} config Button configuration
  1392. */
  1393. var _title = function ( config )
  1394. {
  1395. var title = _stringOrFunction( config.title );
  1396. return title === null ?
  1397. null : title.indexOf( '*' ) !== -1 ?
  1398. title.replace( '*', $('title').text() || 'Exported data' ) :
  1399. title;
  1400. };
  1401. var _message = function ( dt, option, position )
  1402. {
  1403. var message = _stringOrFunction( option );
  1404. if ( message === null ) {
  1405. return null;
  1406. }
  1407. var caption = $('caption', dt.table().container()).eq(0);
  1408. if ( message === '*' ) {
  1409. var side = caption.css( 'caption-side' );
  1410. if ( side !== position ) {
  1411. return null;
  1412. }
  1413. return caption.length ?
  1414. caption.text() :
  1415. '';
  1416. }
  1417. return message;
  1418. };
  1419. var _exportTextarea = $('<textarea/>')[0];
  1420. var _exportData = function ( dt, inOpts )
  1421. {
  1422. var config = $.extend( true, {}, {
  1423. rows: null,
  1424. columns: '',
  1425. modifier: {
  1426. search: 'applied',
  1427. order: 'applied'
  1428. },
  1429. orthogonal: 'display',
  1430. stripHtml: true,
  1431. stripNewlines: true,
  1432. decodeEntities: true,
  1433. trim: true,
  1434. format: {
  1435. header: function ( d ) {
  1436. return strip( d );
  1437. },
  1438. footer: function ( d ) {
  1439. return strip( d );
  1440. },
  1441. body: function ( d ) {
  1442. return strip( d );
  1443. }
  1444. }
  1445. }, inOpts );
  1446. var strip = function ( str ) {
  1447. if ( typeof str !== 'string' ) {
  1448. return str;
  1449. }
  1450. // Always remove script tags
  1451. str = str.replace( /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '' );
  1452. if ( config.stripHtml ) {
  1453. str = str.replace( /<[^>]*>/g, '' );
  1454. }
  1455. if ( config.trim ) {
  1456. str = str.replace( /^\s+|\s+$/g, '' );
  1457. }
  1458. if ( config.stripNewlines ) {
  1459. str = str.replace( /\n/g, ' ' );
  1460. }
  1461. if ( config.decodeEntities ) {
  1462. _exportTextarea.innerHTML = str;
  1463. str = _exportTextarea.value;
  1464. }
  1465. return str;
  1466. };
  1467. var header = dt.columns( config.columns ).indexes().map( function (idx) {
  1468. var el = dt.column( idx ).header();
  1469. return config.format.header( el.innerHTML, idx, el );
  1470. } ).toArray();
  1471. var footer = dt.table().footer() ?
  1472. dt.columns( config.columns ).indexes().map( function (idx) {
  1473. var el = dt.column( idx ).footer();
  1474. return config.format.footer( el ? el.innerHTML : '', idx, el );
  1475. } ).toArray() :
  1476. null;
  1477. var rowIndexes = dt.rows( config.rows, config.modifier ).indexes().toArray();
  1478. var selectedCells = dt.cells( rowIndexes, config.columns );
  1479. var cells = selectedCells
  1480. .render( config.orthogonal )
  1481. .toArray();
  1482. var cellNodes = selectedCells
  1483. .nodes()
  1484. .toArray();
  1485. var columns = header.length;
  1486. var rows = columns > 0 ? cells.length / columns : 0;
  1487. var body = new Array( rows );
  1488. var cellCounter = 0;
  1489. for ( var i=0, ien=rows ; i<ien ; i++ ) {
  1490. var row = new Array( columns );
  1491. for ( var j=0 ; j<columns ; j++ ) {
  1492. row[j] = config.format.body( cells[ cellCounter ], i, j, cellNodes[ cellCounter ] );
  1493. cellCounter++;
  1494. }
  1495. body[i] = row;
  1496. }
  1497. return {
  1498. header: header,
  1499. footer: footer,
  1500. body: body
  1501. };
  1502. };
  1503. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  1504. * DataTables interface
  1505. */
  1506. // Attach to DataTables objects for global access
  1507. $.fn.dataTable.Buttons = Buttons;
  1508. $.fn.DataTable.Buttons = Buttons;
  1509. // DataTables creation - check if the buttons have been defined for this table,
  1510. // they will have been if the `B` option was used in `dom`, otherwise we should
  1511. // create the buttons instance here so they can be inserted into the document
  1512. // using the API. Listen for `init` for compatibility with pre 1.10.10, but to
  1513. // be removed in future.
  1514. $(document).on( 'init.dt plugin-init.dt', function (e, settings) {
  1515. if ( e.namespace !== 'dt' ) {
  1516. return;
  1517. }
  1518. var opts = settings.oInit.buttons || DataTable.defaults.buttons;
  1519. if ( opts && ! settings._buttons ) {
  1520. new Buttons( settings, opts ).container();
  1521. }
  1522. } );
  1523. // DataTables `dom` feature option
  1524. DataTable.ext.feature.push( {
  1525. fnInit: function( settings ) {
  1526. var api = new DataTable.Api( settings );
  1527. var opts = api.init().buttons || DataTable.defaults.buttons;
  1528. return new Buttons( api, opts ).container();
  1529. },
  1530. cFeature: "B"
  1531. } );
  1532. return Buttons;
  1533. }));