/*! Buttons for DataTables 1.1.0 * ©2015 SpryMedia Ltd - datatables.net/license */ (function( factory ){ if ( typeof define === 'function' && define.amd ) { // AMD define( ['jquery', 'datatables.net'], function ( $ ) { return factory( $, window, document ); } ); } else if ( typeof exports === 'object' ) { // CommonJS module.exports = function (root, $) { if ( ! root ) { root = window; } if ( ! $ || ! $.fn.dataTable ) { $ = require('datatables.net')(root, $).$; } return factory( $, root, root.document ); }; } else { // Browser factory( jQuery, window, document ); } }(function( $, window, document, undefined ) { 'use strict'; var DataTable = $.fn.dataTable; // Used for namespacing events added to the document by each instance, so they // can be removed on destroy var _instCounter = 0; // Button namespacing counter for namespacing events on individual buttons var _buttonCounter = 0; var _dtButtons = DataTable.ext.buttons; /** * [Buttons description] * @param {[type]} * @param {[type]} */ var Buttons = function( dt, config ) { // Allow a boolean true for defaults if ( config === true ) { config = {}; } // For easy configuration of buttons an array can be given if ( $.isArray( config ) ) { config = { buttons: config }; } this.c = $.extend( true, {}, Buttons.defaults, config ); // Don't want a deep copy for the buttons if ( config.buttons ) { this.c.buttons = config.buttons; } this.s = { dt: new DataTable.Api( dt ), buttons: [], subButtons: [], listenKeys: '', namespace: 'dtb'+(_instCounter++) }; this.dom = { container: $('<'+this.c.dom.container.tag+'/>') .addClass( this.c.dom.container.className ) }; this._constructor(); }; $.extend( Buttons.prototype, { /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Public methods */ /** * Get the action of a button * @param {int|string} Button index * @return {function} *//** * Set the action of a button * @param {int|string} Button index * @param {function} Function to set * @return {Buttons} Self for chaining */ action: function ( idx, action ) { var button = this._indexToButton( idx ).conf; if ( action === undefined ) { return button.action; } button.action = action; return this; }, /** * Add an active class to the button to make to look active * @param {int|string} Button index * @param {boolean} [flag=true] Enable / disable flag * @return {Buttons} Self for chaining */ active: function ( idx, flag ) { var button = this._indexToButton( idx ); button.node.toggleClass( this.c.dom.button.active, flag === undefined ? true : flag ); return this; }, /** * Add a new button * @param {int|string} Button index for where to insert the button * @param {object} Button configuration object, base string name or function * @return {Buttons} Self for chaining */ add: function ( idx, config ) { if ( typeof idx === 'string' && idx.indexOf('-') !== -1 ) { var idxs = idx.split('-'); this.c.buttons[idxs[0]*1].buttons.splice( idxs[1]*1, 0, config ); } else { this.c.buttons.splice( idx*1, 0, config ); } this.dom.container.empty(); this._buildButtons( this.c.buttons ); return this; }, /** * Get the container node for the buttons * @return {jQuery} Buttons node */ container: function () { return this.dom.container; }, /** * Disable a button * @param {int|string} Button index * @return {Buttons} Self for chaining */ disable: function ( idx ) { var button = this._indexToButton( idx ); button.node.addClass( this.c.dom.button.disabled ); return this; }, /** * Destroy the instance, cleaning up event handlers and removing DOM * elements * @return {Buttons} Self for chaining */ destroy: function () { // Key event listener $('body').off( 'keyup.'+this.s.namespace ); // Individual button destroy (so they can remove their own events if // needed var buttons = this.s.buttons; var subButtons = this.s.subButtons; var i, ien, j, jen; for ( i=0, ien=buttons.length ; i=0 ; i-- ) { if ( buttons[i] === null ) { buttons.splice( i, 1 ); subButtons.splice( i, 1 ); this.c.buttons.splice( i, 1 ); } } for ( i=0, ien=subButtons.length ; i=0 ; j-- ) { if ( subButtons[i][j] === null ) { subButtons[i].splice( j, 1 ); this.c.buttons[i].buttons.splice( j, 1 ); } } } return this; }, /** * Scheduled a button for removal. This is required so multiple buttons can * be removed without upsetting the button indexes while removing them. * @return {Buttons} Self for chaining */ removePrep: function ( idx ) { var button; var dt = this.s.dt; if ( typeof idx === 'number' || idx.indexOf('-') === -1 ) { // Top level button button = this.s.buttons[ idx*1 ]; if ( button.conf.destroy ) { button.conf.destroy.call( dt.button(idx), dt, button, button.conf ); } button.node.remove(); this._removeKey( button.conf ); this.s.buttons[ idx*1 ] = null; } else { // Collection button var idxs = idx.split('-'); button = this.s.subButtons[ idxs[0]*1 ][ idxs[1]*1 ]; if ( button.conf.destroy ) { button.conf.destroy.call( dt.button(idx), dt, button, button.conf ); } button.node.remove(); this._removeKey( button.conf ); this.s.subButtons[ idxs[0]*1 ][ idxs[1]*1 ] = null; } return this; }, /** * Get the text for a button * @param {int|string} Button index * @return {string} Button text *//** * Set the text for a button * @param {int|string|function} Button index * @param {string} Text * @return {Buttons} Self for chaining */ text: function ( idx, label ) { var button = this._indexToButton( idx ); var buttonLiner = this.c.dom.collection.buttonLiner; var linerTag = typeof idx === 'string' && idx.indexOf( '-' ) !== -1 && buttonLiner && buttonLiner.tag ? buttonLiner.tag : this.c.dom.buttonLiner.tag; var dt = this.s.dt; var text = function ( opt ) { return typeof opt === 'function' ? opt( dt, button.node, button.conf ) : opt; }; if ( label === undefined ) { return text( button.conf.text ); } button.conf.text = label; if ( linerTag ) { button.node.children( linerTag ).html( text(label) ); } else { button.node.html( text(label) ); } return this; }, /** * Calculate button index from a node * @param {node} Button node (_not_ a jQuery object) * @return {string} Index. Undefined if not found */ toIndex: function ( node ) { var i, ien, j, jen; var buttons = this.s.buttons; var subButtons = this.s.subButtons; // Loop the main buttons first for ( i=0, ien=buttons.length ; i') .addClass( collectionDom.className ); this._buildButtons( conf.buttons, conf._collection, i ); } // init call is made here, rather than buildButton as it needs to // have been added to the buttons / subButtons array first if ( conf.init ) { conf.init.call( dt.button( buttonNode ), dt, buttonNode, conf ); } } }, /** * Create an individual button * @param {object} config Resolved button configuration * @param {boolean} collectionButton `true` if a collection button * @return {jQuery} Created button node (jQuery) * @private */ _buildButton: function ( config, collectionButton ) { var that = this; var buttonDom = this.c.dom.button; var linerDom = this.c.dom.buttonLiner; var collectionDom = this.c.dom.collection; var dt = this.s.dt; var text = function ( opt ) { return typeof opt === 'function' ? opt( dt, button, config ) : opt; }; if ( collectionButton && collectionDom.button ) { buttonDom = collectionDom.button; } if ( collectionButton && collectionDom.buttonLiner ) { linerDom = collectionDom.buttonLiner; } // Make sure that the button is available based on whatever requirements // it has. For example, Flash buttons require Flash if ( config.available && ! config.available( dt, config ) ) { return false; } var button = $('<'+buttonDom.tag+'/>') .addClass( buttonDom.className ) .attr( 'tabindex', this.s.dt.settings()[0].iTabIndex ) .attr( 'aria-controls', this.s.dt.table().node().id ) .on( 'click.dtb', function (e) { e.preventDefault(); if ( ! button.hasClass( buttonDom.disabled ) && config.action ) { config.action.call( dt.button( button ), e, dt, button, config ); } button.blur(); } ) .on( 'keyup.dtb', function (e) { if ( e.keyCode === 13 ) { if ( ! button.hasClass( buttonDom.disabled ) && config.action ) { config.action.call( dt.button( button ), e, dt, button, config ); } } } ); if ( linerDom.tag ) { button.append( $('<'+linerDom.tag+'/>') .html( text( config.text ) ) .addClass( linerDom.className ) ); } else { button.html( text( config.text ) ); } if ( config.enabled === false ) { button.addClass( buttonDom.disabled ); } if ( config.className ) { button.addClass( config.className ); } if ( config.titleAttr ) { button.attr( 'title', config.titleAttr ); } if ( ! config.namespace ) { config.namespace = '.dt-button-'+(_buttonCounter++); } var buttonContainer = this.c.dom.buttonContainer; var inserter; if ( buttonContainer ) { inserter = $('<'+buttonContainer.tag+'/>') .addClass( buttonContainer.className ) .append( button ); } else { inserter = button; } this._addKey( config ); return { node: button, inserter: inserter }; }, /** * Get a button's host information from a button index * @param {int|string} Button index * @return {object} Button information - object contains `node` and `conf` * properties * @private */ _indexToButton: function ( idx ) { if ( typeof idx === 'number' || idx.indexOf('-') === -1 ) { return this.s.buttons[ idx*1 ]; } var idxs = idx.split('-'); return this.s.subButtons[ idxs[0]*1 ][ idxs[1]*1 ]; }, /** * Handle a key press - determine if any button's key configured matches * what was typed and trigger the action if so. * @param {string} The character pressed * @param {object} Key event that triggered this call * @private */ _keypress: function ( character, e ) { var i, ien, j, jen; var buttons = this.s.buttons; var subButtons = this.s.subButtons; var run = function ( conf, node ) { if ( ! conf.key ) { return; } if ( conf.key === character ) { node.click(); } else if ( $.isPlainObject( conf.key ) ) { if ( conf.key.key !== character ) { return; } if ( conf.key.shiftKey && ! e.shiftKey ) { return; } if ( conf.key.altKey && ! e.altKey ) { return; } if ( conf.key.ctrlKey && ! e.ctrlKey ) { return; } if ( conf.key.metaKey && ! e.metaKey ) { return; } // Made it this far - it is good node.click(); } }; // Loop the main buttons first for ( i=0, ien=buttons.length ; i 30 ) { // Protect against misconfiguration killing the browser throw 'Buttons: Too many iterations'; } } return $.isArray( base ) ? base : $.extend( {}, base ); }; conf = toConfObject( conf ); while ( conf && conf.extend ) { // Use `toConfObject` in case the button definition being extended // is itself a string or a function if ( ! _dtButtons[ conf.extend ] ) { throw 'Cannot extend unknown button type: '+conf.extend; } var objArray = toConfObject( _dtButtons[ conf.extend ] ); if ( $.isArray( objArray ) ) { return objArray; } else if ( ! objArray ) { // This is a little brutal as it might be possible to have a // valid button without the extend, but if there is no extend // then the host button would be acting in an undefined state return false; } // Stash the current class name var originalClassName = objArray.className; conf = $.extend( {}, objArray, conf ); // The extend will have overwritten the original class name if the // `conf` object also assigned a class, but we want to concatenate // them so they are list that is combined from all extended buttons if ( originalClassName && conf.className !== originalClassName ) { conf.className = originalClassName+' '+conf.className; } // Buttons to be added to a collection -gives the ability to define // if buttons should be added to the start or end of a collection var postfixButtons = conf.postfixButtons; if ( postfixButtons ) { if ( ! conf.buttons ) { conf.buttons = []; } for ( i=0, ien=postfixButtons.length ; i