dataTables.buttons.js 39 KB

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