|
|
|
|
|
|
|
|
|
(function( factory ){ |
|
if ( typeof define === 'function' && define.amd ) { |
|
|
|
define( ['jquery', 'datatables.net'], function ( $ ) { |
|
return factory( $, window, document ); |
|
} ); |
|
} |
|
else if ( typeof exports === 'object' ) { |
|
|
|
module.exports = function (root, $) { |
|
if ( ! root ) { |
|
root = window; |
|
} |
|
|
|
if ( ! $ || ! $.fn.dataTable ) { |
|
$ = require('datatables.net')(root, $).$; |
|
} |
|
|
|
return factory( $, root, root.document ); |
|
}; |
|
} |
|
else { |
|
|
|
factory( jQuery, window, document ); |
|
} |
|
}(function( $, window, document, undefined ) { |
|
'use strict'; |
|
var DataTable = $.fn.dataTable; |
|
|
|
|
|
|
|
|
|
var _instCounter = 0; |
|
|
|
|
|
var _buttonCounter = 0; |
|
|
|
var _dtButtons = DataTable.ext.buttons; |
|
|
|
|
|
|
|
|
|
|
|
|
|
var Buttons = function( dt, config ) |
|
{ |
|
|
|
if ( config === true ) { |
|
config = {}; |
|
} |
|
|
|
|
|
if ( $.isArray( config ) ) { |
|
config = { buttons: config }; |
|
} |
|
|
|
this.c = $.extend( true, {}, Buttons.defaults, config ); |
|
|
|
|
|
if ( config.buttons ) { |
|
this.c.buttons = config.buttons; |
|
} |
|
|
|
this.s = { |
|
dt: new DataTable.Api( dt ), |
|
buttons: [], |
|
listenKeys: '', |
|
namespace: 'dtb'+(_instCounter++) |
|
}; |
|
|
|
this.dom = { |
|
container: $('<'+this.c.dom.container.tag+'/>') |
|
.addClass( this.c.dom.container.className ) |
|
}; |
|
|
|
this._constructor(); |
|
}; |
|
|
|
|
|
$.extend( Buttons.prototype, { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
action: function ( node, action ) |
|
{ |
|
var button = this._nodeToButton( node ); |
|
|
|
if ( action === undefined ) { |
|
return button.conf.action; |
|
} |
|
|
|
button.conf.action = action; |
|
|
|
return this; |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
active: function ( node, flag ) { |
|
var button = this._nodeToButton( node ); |
|
var klass = this.c.dom.button.active; |
|
var jqNode = $(button.node); |
|
|
|
if ( flag === undefined ) { |
|
return jqNode.hasClass( klass ); |
|
} |
|
|
|
jqNode.toggleClass( klass, flag === undefined ? true : flag ); |
|
|
|
return this; |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
add: function ( config, idx ) |
|
{ |
|
var buttons = this.s.buttons; |
|
|
|
if ( typeof idx === 'string' ) { |
|
var split = idx.split('-'); |
|
var base = this.s; |
|
|
|
for ( var i=0, ien=split.length-1 ; i<ien ; i++ ) { |
|
base = base.buttons[ split[i]*1 ]; |
|
} |
|
|
|
buttons = base.buttons; |
|
idx = split[ split.length-1 ]*1; |
|
} |
|
|
|
this._expandButton( buttons, config, false, idx ); |
|
this._draw(); |
|
|
|
return this; |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
container: function () |
|
{ |
|
return this.dom.container; |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
disable: function ( node ) { |
|
var button = this._nodeToButton( node ); |
|
|
|
$(button.node).addClass( this.c.dom.button.disabled ); |
|
|
|
return this; |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
destroy: function () |
|
{ |
|
|
|
$('body').off( 'keyup.'+this.s.namespace ); |
|
|
|
|
|
|
|
var buttons = this.s.buttons.slice(); |
|
var i, ien; |
|
|
|
for ( i=0, ien=buttons.length ; i<ien ; i++ ) { |
|
this.remove( buttons[i].node ); |
|
} |
|
|
|
|
|
this.dom.container.remove(); |
|
|
|
|
|
var buttonInsts = this.s.dt.settings()[0]; |
|
|
|
for ( i=0, ien=buttonInsts.length ; i<ien ; i++ ) { |
|
if ( buttonInsts.inst === this ) { |
|
buttonInsts.splice( i, 1 ); |
|
break; |
|
} |
|
} |
|
|
|
return this; |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
enable: function ( node, flag ) |
|
{ |
|
if ( flag === false ) { |
|
return this.disable( node ); |
|
} |
|
|
|
var button = this._nodeToButton( node ); |
|
$(button.node).removeClass( this.c.dom.button.disabled ); |
|
|
|
return this; |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
name: function () |
|
{ |
|
return this.c.name; |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
node: function ( node ) |
|
{ |
|
var button = this._nodeToButton( node ); |
|
return $(button.node); |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
remove: function ( node ) |
|
{ |
|
var button = this._nodeToButton( node ); |
|
var host = this._nodeToHost( node ); |
|
var dt = this.s.dt; |
|
|
|
|
|
if ( button.buttons.length ) { |
|
for ( var i=button.buttons.length-1 ; i>=0 ; i-- ) { |
|
this.remove( button.buttons[i].node ); |
|
} |
|
} |
|
|
|
|
|
if ( button.conf.destroy ) { |
|
button.conf.destroy.call( dt.button(node), dt, $(node), button.conf ); |
|
} |
|
|
|
this._removeKey( button.conf ); |
|
|
|
$(button.node).remove(); |
|
|
|
var idx = $.inArray( button, host ); |
|
host.splice( idx, 1 ); |
|
|
|
return this; |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
text: function ( node, label ) |
|
{ |
|
var button = this._nodeToButton( node ); |
|
var buttonLiner = this.c.dom.collection.buttonLiner; |
|
var linerTag = button.inCollection && buttonLiner && buttonLiner.tag ? |
|
buttonLiner.tag : |
|
this.c.dom.buttonLiner.tag; |
|
var dt = this.s.dt; |
|
var jqNode = $(button.node); |
|
var text = function ( opt ) { |
|
return typeof opt === 'function' ? |
|
opt( dt, jqNode, button.conf ) : |
|
opt; |
|
}; |
|
|
|
if ( label === undefined ) { |
|
return text( button.conf.text ); |
|
} |
|
|
|
button.conf.text = label; |
|
|
|
if ( linerTag ) { |
|
jqNode.children( linerTag ).html( text(label) ); |
|
} |
|
else { |
|
jqNode.html( text(label) ); |
|
} |
|
|
|
return this; |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_constructor: function () |
|
{ |
|
var that = this; |
|
var dt = this.s.dt; |
|
var dtSettings = dt.settings()[0]; |
|
var buttons = this.c.buttons; |
|
|
|
if ( ! dtSettings._buttons ) { |
|
dtSettings._buttons = []; |
|
} |
|
|
|
dtSettings._buttons.push( { |
|
inst: this, |
|
name: this.c.name |
|
} ); |
|
|
|
for ( var i=0, ien=buttons.length ; i<ien ; i++ ) { |
|
this.add( buttons[i] ); |
|
} |
|
|
|
dt.on( 'destroy', function () { |
|
that.destroy(); |
|
} ); |
|
|
|
|
|
$('body').on( 'keyup.'+this.s.namespace, function ( e ) { |
|
if ( ! document.activeElement || document.activeElement === document.body ) { |
|
|
|
|
|
var character = String.fromCharCode(e.keyCode).toLowerCase(); |
|
|
|
if ( that.s.listenKeys.toLowerCase().indexOf( character ) !== -1 ) { |
|
that._keypress( character, e ); |
|
} |
|
} |
|
} ); |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_addKey: function ( conf ) |
|
{ |
|
if ( conf.key ) { |
|
this.s.listenKeys += $.isPlainObject( conf.key ) ? |
|
conf.key.key : |
|
conf.key; |
|
} |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_draw: function ( container, buttons ) |
|
{ |
|
if ( ! container ) { |
|
container = this.dom.container; |
|
buttons = this.s.buttons; |
|
} |
|
|
|
container.children().detach(); |
|
|
|
for ( var i=0, ien=buttons.length ; i<ien ; i++ ) { |
|
container.append( buttons[i].inserter ); |
|
|
|
if ( buttons[i].buttons && buttons[i].buttons.length ) { |
|
this._draw( buttons[i].collection, buttons[i].buttons ); |
|
} |
|
} |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_expandButton: function ( attachTo, button, inCollection, attachPoint ) |
|
{ |
|
var dt = this.s.dt; |
|
var buttonCounter = 0; |
|
var buttons = ! $.isArray( button ) ? |
|
[ button ] : |
|
button; |
|
|
|
for ( var i=0, ien=buttons.length ; i<ien ; i++ ) { |
|
var conf = this._resolveExtends( buttons[i] ); |
|
|
|
if ( ! conf ) { |
|
continue; |
|
} |
|
|
|
|
|
|
|
if ( $.isArray( conf ) ) { |
|
this._expandButton( attachTo, conf, inCollection, attachPoint ); |
|
continue; |
|
} |
|
|
|
var built = this._buildButton( conf, inCollection ); |
|
if ( ! built ) { |
|
continue; |
|
} |
|
|
|
if ( attachPoint !== undefined ) { |
|
attachTo.splice( attachPoint, 0, built ); |
|
attachPoint++; |
|
} |
|
else { |
|
attachTo.push( built ); |
|
} |
|
|
|
if ( built.conf.buttons ) { |
|
var collectionDom = this.c.dom.collection; |
|
built.collection = $('<'+collectionDom.tag+'/>') |
|
.addClass( collectionDom.className ); |
|
built.conf._collection = built.collection; |
|
|
|
this._expandButton( built.buttons, built.conf.buttons, true, attachPoint ); |
|
} |
|
|
|
|
|
|
|
if ( conf.init ) { |
|
conf.init.call( dt.button( built.node ), dt, $(built.node), conf ); |
|
} |
|
|
|
buttonCounter++; |
|
} |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_buildButton: function ( config, inCollection ) |
|
{ |
|
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 ( inCollection && collectionDom.button ) { |
|
buttonDom = collectionDom.button; |
|
} |
|
|
|
if ( inCollection && collectionDom.buttonLiner ) { |
|
linerDom = collectionDom.buttonLiner; |
|
} |
|
|
|
|
|
|
|
if ( config.available && ! config.available( dt, config ) ) { |
|
return false; |
|
} |
|
|
|
var action = function ( e, dt, button, config ) { |
|
config.action.call( dt.button( button ), e, dt, button, config ); |
|
|
|
$(dt.table().node()).triggerHandler( 'buttons-action.dt', [ |
|
dt.button( button ), dt, button, config |
|
] ); |
|
}; |
|
|
|
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 ) { |
|
action( e, dt, button, config ); |
|
} |
|
|
|
button.blur(); |
|
} ) |
|
.on( 'keyup.dtb', function (e) { |
|
if ( e.keyCode === 13 ) { |
|
if ( ! button.hasClass( buttonDom.disabled ) && config.action ) { |
|
action( e, dt, button, config ); |
|
} |
|
} |
|
} ); |
|
|
|
|
|
if ( buttonDom.tag.toLowerCase() === 'a' ) { |
|
button.attr( 'href', '#' ); |
|
} |
|
|
|
if ( linerDom.tag ) { |
|
var liner = $('<'+linerDom.tag+'/>') |
|
.html( text( config.text ) ) |
|
.addClass( linerDom.className ); |
|
|
|
if ( linerDom.tag.toLowerCase() === 'a' ) { |
|
liner.attr( 'href', '#' ); |
|
} |
|
|
|
button.append( liner ); |
|
} |
|
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 && buttonContainer.tag ) { |
|
inserter = $('<'+buttonContainer.tag+'/>') |
|
.addClass( buttonContainer.className ) |
|
.append( button ); |
|
} |
|
else { |
|
inserter = button; |
|
} |
|
|
|
this._addKey( config ); |
|
|
|
return { |
|
conf: config, |
|
node: button.get(0), |
|
inserter: inserter, |
|
buttons: [], |
|
inCollection: inCollection, |
|
collection: null |
|
}; |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_nodeToButton: function ( node, buttons ) |
|
{ |
|
if ( ! buttons ) { |
|
buttons = this.s.buttons; |
|
} |
|
|
|
for ( var i=0, ien=buttons.length ; i<ien ; i++ ) { |
|
if ( buttons[i].node === node ) { |
|
return buttons[i]; |
|
} |
|
|
|
if ( buttons[i].buttons.length ) { |
|
var ret = this._nodeToButton( node, buttons[i].buttons ); |
|
|
|
if ( ret ) { |
|
return ret; |
|
} |
|
} |
|
} |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_nodeToHost: function ( node, buttons ) |
|
{ |
|
if ( ! buttons ) { |
|
buttons = this.s.buttons; |
|
} |
|
|
|
for ( var i=0, ien=buttons.length ; i<ien ; i++ ) { |
|
if ( buttons[i].node === node ) { |
|
return buttons; |
|
} |
|
|
|
if ( buttons[i].buttons.length ) { |
|
var ret = this._nodeToHost( node, buttons[i].buttons ); |
|
|
|
if ( ret ) { |
|
return ret; |
|
} |
|
} |
|
} |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_keypress: function ( character, e ) |
|
{ |
|
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; |
|
} |
|
|
|
|
|
$(node).click(); |
|
} |
|
}; |
|
|
|
var recurse = function ( a ) { |
|
for ( var i=0, ien=a.length ; i<ien ; i++ ) { |
|
run( a[i].conf, a[i].node ); |
|
|
|
if ( a[i].buttons.length ) { |
|
recurse( a[i].buttons ); |
|
} |
|
} |
|
}; |
|
|
|
recurse( this.s.buttons ); |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_removeKey: function ( conf ) |
|
{ |
|
if ( conf.key ) { |
|
var character = $.isPlainObject( conf.key ) ? |
|
conf.key.key : |
|
conf.key; |
|
|
|
|
|
|
|
var a = this.s.listenKeys.split(''); |
|
var idx = $.inArray( character, a ); |
|
a.splice( idx, 1 ); |
|
this.s.listenKeys = a.join(''); |
|
} |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_resolveExtends: function ( conf ) |
|
{ |
|
var dt = this.s.dt; |
|
var i, ien; |
|
var toConfObject = function ( base ) { |
|
var loop = 0; |
|
|
|
|
|
|
|
|
|
while ( ! $.isPlainObject(base) && ! $.isArray(base) ) { |
|
if ( base === undefined ) { |
|
return; |
|
} |
|
|
|
if ( typeof base === 'function' ) { |
|
base = base( dt, conf ); |
|
|
|
if ( ! base ) { |
|
return false; |
|
} |
|
} |
|
else if ( typeof base === 'string' ) { |
|
if ( ! _dtButtons[ base ] ) { |
|
throw 'Unknown button type: '+base; |
|
} |
|
|
|
base = _dtButtons[ base ]; |
|
} |
|
|
|
loop++; |
|
if ( loop > 30 ) { |
|
|
|
throw 'Buttons: Too many iterations'; |
|
} |
|
} |
|
|
|
return $.isArray( base ) ? |
|
base : |
|
$.extend( {}, base ); |
|
}; |
|
|
|
conf = toConfObject( conf ); |
|
|
|
while ( conf && conf.extend ) { |
|
|
|
|
|
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 ) { |
|
|
|
|
|
|
|
return false; |
|
} |
|
|
|
|
|
var originalClassName = objArray.className; |
|
|
|
conf = $.extend( {}, objArray, conf ); |
|
|
|
|
|
|
|
|
|
if ( originalClassName && conf.className !== originalClassName ) { |
|
conf.className = originalClassName+' '+conf.className; |
|
} |
|
|
|
|
|
|
|
var postfixButtons = conf.postfixButtons; |
|
if ( postfixButtons ) { |
|
if ( ! conf.buttons ) { |
|
conf.buttons = []; |
|
} |
|
|
|
for ( i=0, ien=postfixButtons.length ; i<ien ; i++ ) { |
|
conf.buttons.push( postfixButtons[i] ); |
|
} |
|
|
|
conf.postfixButtons = null; |
|
} |
|
|
|
var prefixButtons = conf.prefixButtons; |
|
if ( prefixButtons ) { |
|
if ( ! conf.buttons ) { |
|
conf.buttons = []; |
|
} |
|
|
|
for ( i=0, ien=prefixButtons.length ; i<ien ; i++ ) { |
|
conf.buttons.splice( i, 0, prefixButtons[i] ); |
|
} |
|
|
|
conf.prefixButtons = null; |
|
} |
|
|
|
|
|
|
|
|
|
conf.extend = objArray.extend; |
|
} |
|
|
|
return conf; |
|
} |
|
} ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Buttons.background = function ( show, className, fade ) { |
|
if ( fade === undefined ) { |
|
fade = 400; |
|
} |
|
|
|
if ( show ) { |
|
$('<div/>') |
|
.addClass( className ) |
|
.css( 'display', 'none' ) |
|
.appendTo( 'body' ) |
|
.fadeIn( fade ); |
|
} |
|
else { |
|
$('body > div.'+className) |
|
.fadeOut( fade, function () { |
|
$(this).remove(); |
|
} ); |
|
} |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Buttons.instanceSelector = function ( group, buttons ) |
|
{ |
|
if ( ! group ) { |
|
return $.map( buttons, function ( v ) { |
|
return v.inst; |
|
} ); |
|
} |
|
|
|
var ret = []; |
|
var names = $.map( buttons, function ( v ) { |
|
return v.name; |
|
} ); |
|
|
|
|
|
var process = function ( input ) { |
|
if ( $.isArray( input ) ) { |
|
for ( var i=0, ien=input.length ; i<ien ; i++ ) { |
|
process( input[i] ); |
|
} |
|
return; |
|
} |
|
|
|
if ( typeof input === 'string' ) { |
|
if ( input.indexOf( ',' ) !== -1 ) { |
|
|
|
process( input.split(',') ); |
|
} |
|
else { |
|
|
|
var idx = $.inArray( $.trim(input), names ); |
|
|
|
if ( idx !== -1 ) { |
|
ret.push( buttons[ idx ].inst ); |
|
} |
|
} |
|
} |
|
else if ( typeof input === 'number' ) { |
|
|
|
ret.push( buttons[ input ].inst ); |
|
} |
|
}; |
|
|
|
process( group ); |
|
|
|
return ret; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Buttons.buttonSelector = function ( insts, selector ) |
|
{ |
|
var ret = []; |
|
var nodeBuilder = function ( a, buttons, baseIdx ) { |
|
var button; |
|
var idx; |
|
|
|
for ( var i=0, ien=buttons.length ; i<ien ; i++ ) { |
|
button = buttons[i]; |
|
|
|
if ( button ) { |
|
idx = baseIdx !== undefined ? |
|
baseIdx+i : |
|
i+''; |
|
|
|
a.push( { |
|
node: button.node, |
|
name: button.conf.name, |
|
idx: idx |
|
} ); |
|
|
|
if ( button.buttons ) { |
|
nodeBuilder( a, button.buttons, idx+'-' ); |
|
} |
|
} |
|
} |
|
}; |
|
|
|
var run = function ( selector, inst ) { |
|
var i, ien; |
|
var buttons = []; |
|
nodeBuilder( buttons, inst.s.buttons ); |
|
|
|
var nodes = $.map( buttons, function (v) { |
|
return v.node; |
|
} ); |
|
|
|
if ( $.isArray( selector ) || selector instanceof $ ) { |
|
for ( i=0, ien=selector.length ; i<ien ; i++ ) { |
|
run( selector[i], inst ); |
|
} |
|
return; |
|
} |
|
|
|
if ( selector === null || selector === undefined || selector === '*' ) { |
|
|
|
for ( i=0, ien=buttons.length ; i<ien ; i++ ) { |
|
ret.push( { |
|
inst: inst, |
|
node: buttons[i].node |
|
} ); |
|
} |
|
} |
|
else if ( typeof selector === 'number' ) { |
|
|
|
ret.push( { |
|
inst: inst, |
|
node: inst.s.buttons[ selector ].node |
|
} ); |
|
} |
|
else if ( typeof selector === 'string' ) { |
|
if ( selector.indexOf( ',' ) !== -1 ) { |
|
|
|
var a = selector.split(','); |
|
|
|
for ( i=0, ien=a.length ; i<ien ; i++ ) { |
|
run( $.trim(a[i]), inst ); |
|
} |
|
} |
|
else if ( selector.match( /^\d+(\-\d+)*$/ ) ) { |
|
|
|
var indexes = $.map( buttons, function (v) { |
|
return v.idx; |
|
} ); |
|
|
|
ret.push( { |
|
inst: inst, |
|
node: buttons[ $.inArray( selector, indexes ) ].node |
|
} ); |
|
} |
|
else if ( selector.indexOf( ':name' ) !== -1 ) { |
|
|
|
var name = selector.replace( ':name', '' ); |
|
|
|
for ( i=0, ien=buttons.length ; i<ien ; i++ ) { |
|
if ( buttons[i].name === name ) { |
|
ret.push( { |
|
inst: inst, |
|
node: buttons[i].node |
|
} ); |
|
} |
|
} |
|
} |
|
else { |
|
|
|
$( nodes ).filter( selector ).each( function () { |
|
ret.push( { |
|
inst: inst, |
|
node: this |
|
} ); |
|
} ); |
|
} |
|
} |
|
else if ( typeof selector === 'object' && selector.nodeName ) { |
|
|
|
var idx = $.inArray( selector, nodes ); |
|
|
|
if ( idx !== -1 ) { |
|
ret.push( { |
|
inst: inst, |
|
node: nodes[ idx ] |
|
} ); |
|
} |
|
} |
|
}; |
|
|
|
|
|
for ( var i=0, ien=insts.length ; i<ien ; i++ ) { |
|
var inst = insts[i]; |
|
|
|
run( selector, inst ); |
|
} |
|
|
|
return ret; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Buttons.defaults = { |
|
buttons: [ 'copy', 'excel', 'csv', 'pdf', 'print' ], |
|
name: 'main', |
|
tabIndex: 0, |
|
dom: { |
|
container: { |
|
tag: 'div', |
|
className: 'dt-buttons' |
|
}, |
|
collection: { |
|
tag: 'div', |
|
className: 'dt-button-collection' |
|
}, |
|
button: { |
|
tag: 'a', |
|
className: 'dt-button', |
|
active: 'active', |
|
disabled: 'disabled' |
|
}, |
|
buttonLiner: { |
|
tag: 'span', |
|
className: '' |
|
} |
|
} |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
Buttons.version = '1.2.1'; |
|
|
|
|
|
$.extend( _dtButtons, { |
|
collection: { |
|
text: function ( dt ) { |
|
return dt.i18n( 'buttons.collection', 'Collection' ); |
|
}, |
|
className: 'buttons-collection', |
|
action: function ( e, dt, button, config ) { |
|
var host = button; |
|
var hostOffset = host.offset(); |
|
var tableContainer = $( dt.table().container() ); |
|
var multiLevel = false; |
|
|
|
|
|
if ( $('div.dt-button-background').length ) { |
|
multiLevel = $('div.dt-button-collection').offset(); |
|
$('body').trigger( 'click.dtb-collection' ); |
|
} |
|
|
|
config._collection |
|
.addClass( config.collectionLayout ) |
|
.css( 'display', 'none' ) |
|
.appendTo( 'body' ) |
|
.fadeIn( config.fade ); |
|
|
|
var position = config._collection.css( 'position' ); |
|
|
|
if ( multiLevel && position === 'absolute' ) { |
|
config._collection.css( { |
|
top: multiLevel.top + 5, |
|
left: multiLevel.left + 5 |
|
} ); |
|
} |
|
else if ( position === 'absolute' ) { |
|
config._collection.css( { |
|
top: hostOffset.top + host.outerHeight(), |
|
left: hostOffset.left |
|
} ); |
|
|
|
var listRight = hostOffset.left + config._collection.outerWidth(); |
|
var tableRight = tableContainer.offset().left + tableContainer.width(); |
|
if ( listRight > tableRight ) { |
|
config._collection.css( 'left', hostOffset.left - ( listRight - tableRight ) ); |
|
} |
|
} |
|
else { |
|
|
|
var top = config._collection.height() / 2; |
|
if ( top > $(window).height() / 2 ) { |
|
top = $(window).height() / 2; |
|
} |
|
|
|
config._collection.css( 'marginTop', top*-1 ); |
|
} |
|
|
|
if ( config.background ) { |
|
Buttons.background( true, config.backgroundClassName, config.fade ); |
|
} |
|
|
|
|
|
|
|
setTimeout( function () { |
|
|
|
|
|
|
|
|
|
$('div.dt-button-background').on( 'click.dtb-collection', function () {} ); |
|
|
|
$('body').on( 'click.dtb-collection', function (e) { |
|
if ( ! $(e.target).parents().andSelf().filter( config._collection ).length ) { |
|
config._collection |
|
.fadeOut( config.fade, function () { |
|
config._collection.detach(); |
|
} ); |
|
|
|
$('div.dt-button-background').off( 'click.dtb-collection' ); |
|
Buttons.background( false, config.backgroundClassName, config.fade ); |
|
|
|
$('body').off( 'click.dtb-collection' ); |
|
dt.off( 'buttons-action.b-internal' ); |
|
} |
|
} ); |
|
}, 10 ); |
|
|
|
if ( config.autoClose ) { |
|
dt.on( 'buttons-action.b-internal', function () { |
|
$('div.dt-button-background').click(); |
|
} ); |
|
} |
|
}, |
|
background: true, |
|
collectionLayout: '', |
|
backgroundClassName: 'dt-button-background', |
|
autoClose: false, |
|
fade: 400 |
|
}, |
|
copy: function ( dt, conf ) { |
|
if ( _dtButtons.copyHtml5 ) { |
|
return 'copyHtml5'; |
|
} |
|
if ( _dtButtons.copyFlash && _dtButtons.copyFlash.available( dt, conf ) ) { |
|
return 'copyFlash'; |
|
} |
|
}, |
|
csv: function ( dt, conf ) { |
|
|
|
if ( _dtButtons.csvHtml5 && _dtButtons.csvHtml5.available( dt, conf ) ) { |
|
return 'csvHtml5'; |
|
} |
|
if ( _dtButtons.csvFlash && _dtButtons.csvFlash.available( dt, conf ) ) { |
|
return 'csvFlash'; |
|
} |
|
}, |
|
excel: function ( dt, conf ) { |
|
|
|
if ( _dtButtons.excelHtml5 && _dtButtons.excelHtml5.available( dt, conf ) ) { |
|
return 'excelHtml5'; |
|
} |
|
if ( _dtButtons.excelFlash && _dtButtons.excelFlash.available( dt, conf ) ) { |
|
return 'excelFlash'; |
|
} |
|
}, |
|
pdf: function ( dt, conf ) { |
|
|
|
if ( _dtButtons.pdfHtml5 && _dtButtons.pdfHtml5.available( dt, conf ) ) { |
|
return 'pdfHtml5'; |
|
} |
|
if ( _dtButtons.pdfFlash && _dtButtons.pdfFlash.available( dt, conf ) ) { |
|
return 'pdfFlash'; |
|
} |
|
}, |
|
pageLength: function ( dt ) { |
|
var lengthMenu = dt.settings()[0].aLengthMenu; |
|
var vals = $.isArray( lengthMenu[0] ) ? lengthMenu[0] : lengthMenu; |
|
var lang = $.isArray( lengthMenu[0] ) ? lengthMenu[1] : lengthMenu; |
|
var text = function ( dt ) { |
|
return dt.i18n( 'buttons.pageLength', { |
|
"-1": 'Show all rows', |
|
_: 'Show %d rows' |
|
}, dt.page.len() ); |
|
}; |
|
|
|
return { |
|
extend: 'collection', |
|
text: text, |
|
className: 'buttons-page-length', |
|
autoClose: true, |
|
buttons: $.map( vals, function ( val, i ) { |
|
return { |
|
text: lang[i], |
|
action: function ( e, dt ) { |
|
dt.page.len( val ).draw(); |
|
}, |
|
init: function ( dt, node, conf ) { |
|
var that = this; |
|
var fn = function () { |
|
that.active( dt.page.len() === val ); |
|
}; |
|
|
|
dt.on( 'length.dt'+conf.namespace, fn ); |
|
fn(); |
|
}, |
|
destroy: function ( dt, node, conf ) { |
|
dt.off( 'length.dt'+conf.namespace ); |
|
} |
|
}; |
|
} ), |
|
init: function ( dt, node, conf ) { |
|
var that = this; |
|
dt.on( 'length.dt'+conf.namespace, function () { |
|
that.text( text( dt ) ); |
|
} ); |
|
}, |
|
destroy: function ( dt, node, conf ) { |
|
dt.off( 'length.dt'+conf.namespace ); |
|
} |
|
}; |
|
} |
|
} ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DataTable.Api.register( 'buttons()', function ( group, selector ) { |
|
|
|
if ( selector === undefined ) { |
|
selector = group; |
|
group = undefined; |
|
} |
|
|
|
return this.iterator( true, 'table', function ( ctx ) { |
|
if ( ctx._buttons ) { |
|
return Buttons.buttonSelector( |
|
Buttons.instanceSelector( group, ctx._buttons ), |
|
selector |
|
); |
|
} |
|
}, true ); |
|
} ); |
|
|
|
|
|
DataTable.Api.register( 'button()', function ( group, selector ) { |
|
|
|
var buttons = this.buttons( group, selector ); |
|
|
|
if ( buttons.length > 1 ) { |
|
buttons.splice( 1, buttons.length ); |
|
} |
|
|
|
return buttons; |
|
} ); |
|
|
|
|
|
DataTable.Api.registerPlural( 'buttons().active()', 'button().active()', function ( flag ) { |
|
if ( flag === undefined ) { |
|
return this.map( function ( set ) { |
|
return set.inst.active( set.node ); |
|
} ); |
|
} |
|
|
|
return this.each( function ( set ) { |
|
set.inst.active( set.node, flag ); |
|
} ); |
|
} ); |
|
|
|
|
|
DataTable.Api.registerPlural( 'buttons().action()', 'button().action()', function ( action ) { |
|
if ( action === undefined ) { |
|
return this.map( function ( set ) { |
|
return set.inst.action( set.node ); |
|
} ); |
|
} |
|
|
|
return this.each( function ( set ) { |
|
set.inst.action( set.node, action ); |
|
} ); |
|
} ); |
|
|
|
|
|
DataTable.Api.register( ['buttons().enable()', 'button().enable()'], function ( flag ) { |
|
return this.each( function ( set ) { |
|
set.inst.enable( set.node, flag ); |
|
} ); |
|
} ); |
|
|
|
|
|
DataTable.Api.register( ['buttons().disable()', 'button().disable()'], function () { |
|
return this.each( function ( set ) { |
|
set.inst.disable( set.node ); |
|
} ); |
|
} ); |
|
|
|
|
|
DataTable.Api.registerPlural( 'buttons().nodes()', 'button().node()', function () { |
|
var jq = $(); |
|
|
|
|
|
$( this.each( function ( set ) { |
|
jq = jq.add( set.inst.node( set.node ) ); |
|
} ) ); |
|
|
|
return jq; |
|
} ); |
|
|
|
|
|
DataTable.Api.registerPlural( 'buttons().text()', 'button().text()', function ( label ) { |
|
if ( label === undefined ) { |
|
return this.map( function ( set ) { |
|
return set.inst.text( set.node ); |
|
} ); |
|
} |
|
|
|
return this.each( function ( set ) { |
|
set.inst.text( set.node, label ); |
|
} ); |
|
} ); |
|
|
|
|
|
DataTable.Api.registerPlural( 'buttons().trigger()', 'button().trigger()', function () { |
|
return this.each( function ( set ) { |
|
set.inst.node( set.node ).trigger( 'click' ); |
|
} ); |
|
} ); |
|
|
|
|
|
DataTable.Api.registerPlural( 'buttons().containers()', 'buttons().container()', function () { |
|
var jq = $(); |
|
|
|
|
|
$( this.each( function ( set ) { |
|
jq = jq.add( set.inst.container() ); |
|
} ) ); |
|
|
|
return jq; |
|
} ); |
|
|
|
|
|
DataTable.Api.register( 'button().add()', function ( idx, conf ) { |
|
if ( this.length === 1 ) { |
|
this[0].inst.add( conf, idx ); |
|
} |
|
|
|
return this.button( idx ); |
|
} ); |
|
|
|
|
|
DataTable.Api.register( 'buttons().destroy()', function () { |
|
this.pluck( 'inst' ).unique().each( function ( inst ) { |
|
inst.destroy(); |
|
} ); |
|
|
|
return this; |
|
} ); |
|
|
|
|
|
DataTable.Api.registerPlural( 'buttons().remove()', 'buttons().remove()', function () { |
|
this.each( function ( set ) { |
|
set.inst.remove( set.node ); |
|
} ); |
|
|
|
return this; |
|
} ); |
|
|
|
|
|
var _infoTimer; |
|
DataTable.Api.register( 'buttons.info()', function ( title, message, time ) { |
|
var that = this; |
|
|
|
if ( title === false ) { |
|
$('#datatables_buttons_info').fadeOut( function () { |
|
$(this).remove(); |
|
} ); |
|
clearTimeout( _infoTimer ); |
|
_infoTimer = null; |
|
|
|
return this; |
|
} |
|
|
|
if ( _infoTimer ) { |
|
clearTimeout( _infoTimer ); |
|
} |
|
|
|
if ( $('#datatables_buttons_info').length ) { |
|
$('#datatables_buttons_info').remove(); |
|
} |
|
|
|
title = title ? '<h2>'+title+'</h2>' : ''; |
|
|
|
$('<div id="datatables_buttons_info" class="dt-button-info"/>') |
|
.html( title ) |
|
.append( $('<div/>')[ typeof message === 'string' ? 'html' : 'append' ]( message ) ) |
|
.css( 'display', 'none' ) |
|
.appendTo( 'body' ) |
|
.fadeIn(); |
|
|
|
if ( time !== undefined && time !== 0 ) { |
|
_infoTimer = setTimeout( function () { |
|
that.buttons.info( false ); |
|
}, time ); |
|
} |
|
|
|
return this; |
|
} ); |
|
|
|
|
|
|
|
DataTable.Api.register( 'buttons.exportData()', function ( options ) { |
|
if ( this.context.length ) { |
|
return _exportData( new DataTable.Api( this.context[0] ), options ); |
|
} |
|
} ); |
|
|
|
|
|
var _exportTextarea = $('<textarea/>')[0]; |
|
var _exportData = function ( dt, inOpts ) |
|
{ |
|
var config = $.extend( true, {}, { |
|
rows: null, |
|
columns: '', |
|
modifier: { |
|
search: 'applied', |
|
order: 'applied' |
|
}, |
|
orthogonal: 'display', |
|
stripHtml: true, |
|
stripNewlines: true, |
|
decodeEntities: true, |
|
trim: true, |
|
format: { |
|
header: function ( d ) { |
|
return strip( d ); |
|
}, |
|
footer: function ( d ) { |
|
return strip( d ); |
|
}, |
|
body: function ( d ) { |
|
return strip( d ); |
|
} |
|
} |
|
}, inOpts ); |
|
|
|
var strip = function ( str ) { |
|
if ( typeof str !== 'string' ) { |
|
return str; |
|
} |
|
|
|
if ( config.stripHtml ) { |
|
str = str.replace( /<[^>]*>/g, '' ); |
|
} |
|
|
|
if ( config.trim ) { |
|
str = str.replace( /^\s+|\s+$/g, '' ); |
|
} |
|
|
|
if ( config.stripNewlines ) { |
|
str = str.replace( /\n/g, ' ' ); |
|
} |
|
|
|
if ( config.decodeEntities ) { |
|
_exportTextarea.innerHTML = str; |
|
str = _exportTextarea.value; |
|
} |
|
|
|
return str; |
|
}; |
|
|
|
|
|
var header = dt.columns( config.columns ).indexes().map( function (idx) { |
|
return config.format.header( dt.column( idx ).header().innerHTML, idx ); |
|
} ).toArray(); |
|
|
|
var footer = dt.table().footer() ? |
|
dt.columns( config.columns ).indexes().map( function (idx) { |
|
var el = dt.column( idx ).footer(); |
|
return config.format.footer( el ? el.innerHTML : '', idx ); |
|
} ).toArray() : |
|
null; |
|
|
|
var rowIndexes = dt.rows( config.rows, config.modifier ).indexes().toArray(); |
|
var cells = dt |
|
.cells( rowIndexes, config.columns ) |
|
.render( config.orthogonal ) |
|
.toArray(); |
|
var columns = header.length; |
|
var rows = columns > 0 ? cells.length / columns : 0; |
|
var body = new Array( rows ); |
|
var cellCounter = 0; |
|
|
|
for ( var i=0, ien=rows ; i<ien ; i++ ) { |
|
var row = new Array( columns ); |
|
|
|
for ( var j=0 ; j<columns ; j++ ) { |
|
row[j] = config.format.body( cells[ cellCounter ], j, i ); |
|
cellCounter++; |
|
} |
|
|
|
body[i] = row; |
|
} |
|
|
|
return { |
|
header: header, |
|
footer: footer, |
|
body: body |
|
}; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$.fn.dataTable.Buttons = Buttons; |
|
$.fn.DataTable.Buttons = Buttons; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$(document).on( 'init.dt plugin-init.dt', function (e, settings) { |
|
if ( e.namespace !== 'dt' ) { |
|
return; |
|
} |
|
|
|
var opts = settings.oInit.buttons || DataTable.defaults.buttons; |
|
|
|
if ( opts && ! settings._buttons ) { |
|
new Buttons( settings, opts ).container(); |
|
} |
|
} ); |
|
|
|
|
|
DataTable.ext.feature.push( { |
|
fnInit: function( settings ) { |
|
var api = new DataTable.Api( settings ); |
|
var opts = api.init().buttons || DataTable.defaults.buttons; |
|
|
|
return new Buttons( api, opts ).container(); |
|
}, |
|
cFeature: "B" |
|
} ); |
|
|
|
|
|
return Buttons; |
|
})); |
|
|