4 * Author: Allan Jardine (www.sprymedia.co.uk)
6 * Copyright 2010-2011 Allan Jardine, all rights reserved.
8 * This source file is free software, under either the GPL v2 license or a
9 * BSD (3 point) style license, as supplied with this software.
11 * This source file is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the license files for details.
15 (function(d
){ColVis=function(a
,b
){if(!this.CLASS
||this.CLASS
!="ColVis")alert("Warning: ColVis must be initialised with the keyword 'new'");if(typeof b
=="undefined")b
={};this.s
={dt
:null,oInit
:b
,fnStateChange
:null,activate
:"click",sAlign
:"left",buttonText
:"Show / hide columns",hidden
:true,aiExclude
:[],abOriginal
:[],bRestore
:false,sRestore
:"Restore original",iOverlayFade
:500,fnLabel
:null,sSize
:"auto"};this.dom
={wrapper
:null,button
:null,collection
:null,background
:null,catcher
:null,buttons
:[],restore
:null};
16 ColVis
.aInstances
.push(this);this.s
.dt
=a
;this._fnConstruct();return this};ColVis
.prototype={fnRebuild:function(){for(var a
=this.dom
.buttons
.length
-1;a
>=0;a
--)this.dom
.buttons
[a
]!==null&&this.dom
.collection
.removeChild(this.dom
.buttons
[a
]);this.dom
.buttons
.splice(0,this.dom
.buttons
.length
);this.dom
.restore
&&this.dom
.restore
.parentNode(this.dom
.restore
);this._fnAddButtons();this._fnDrawCallback()},_fnConstruct:function(){this._fnApplyCustomisation();var a
=this;this.dom
.wrapper
=document
.createElement("div");
17 this.dom
.wrapper
.className
="ColVis TableTools";this.dom
.button
=this._fnDomBaseButton(this.s
.buttonText
);this.dom
.button
.className
+=" ColVis_MasterButton";this.dom
.wrapper
.appendChild(this.dom
.button
);this.dom
.catcher
=this._fnDomCatcher();this.dom
.collection
=this._fnDomCollection();this.dom
.background
=this._fnDomBackground();this._fnAddButtons();for(var b
=0,c
=this.s
.dt
.aoColumns
.length
;b
<c
;b
++)this.s
.abOriginal
.push(this.s
.dt
.aoColumns
[b
].bVisible
);this.s
.dt
.aoDrawCallback
.push({fn:function(){a
._fnDrawCallback
.call(a
)},
18 sName
:"ColVis"})},_fnApplyCustomisation:function(){var a
=this.s
.oInit
;if(typeof a
.activate
!="undefined")this.s
.activate
=a
.activate
;if(typeof a
.buttonText
!="undefined")this.s
.buttonText
=a
.buttonText
;if(typeof a
.aiExclude
!="undefined")this.s
.aiExclude
=a
.aiExclude
;if(typeof a
.bRestore
!="undefined")this.s
.bRestore
=a
.bRestore
;if(typeof a
.sRestore
!="undefined")this.s
.sRestore
=a
.sRestore
;if(typeof a
.sAlign
!="undefined")this.s
.sAlign
=a
.sAlign
;if(typeof a
.fnStateChange
!="undefined")this.s
.fnStateChange
=a
.fnStateChange
;
19 if(typeof a
.iOverlayFade
!="undefined")this.s
.iOverlayFade
=a
.iOverlayFade
;if(typeof a
.fnLabel
!="undefined")this.s
.fnLabel
=a
.fnLabel
},_fnDrawCallback:function(){for(var a
=this.s
.dt
.aoColumns
,b
=0,c
=a
.length
;b
<c
;b
++)if(this.dom
.buttons
[b
]!==null)a
[b
].bVisible
?d("input",this.dom
.buttons
[b
]).attr("checked","checked"):d("input",this.dom
.buttons
[b
]).removeAttr("checked")},_fnAddButtons:function(){for(var a
,b
=","+this.s
.aiExclude
.join(",")+",",c
=0,e
=this.s
.dt
.aoColumns
.length
;c
<e
;c
++)if(b
.indexOf(","+c
+",")==
20 -1){a
=this._fnDomColumnButton(c
);this.dom
.buttons
.push(a
);this.dom
.collection
.appendChild(a
)}else this.dom
.buttons
.push(null);if(this.s
.bRestore
){a
=this._fnDomRestoreButton();a
.className
+=" ColVis_Restore";this.dom
.buttons
.push(a
);this.dom
.collection
.appendChild(a
)}},_fnDomRestoreButton:function(){var a
=this,b
=document
.createElement("button"),c
=document
.createElement("span");b
.className
=!this.s
.dt
.bJUI
?"ColVis_Button TableTools_Button":"ColVis_Button TableTools_Button ui-button ui-state-default";
21 b
.appendChild(c
);d(c
).html('<span class="ColVis_title">'+this.s
.sRestore
+"</span>");d(b
).click(function(){for(var e
=0,f
=a
.s
.abOriginal
.length
;e
<f
;e
++)a
.s
.dt
.oInstance
.fnSetColumnVis(e
,a
.s
.abOriginal
[e
],false);a
.s
.dt
.oInstance
.fnDraw(false)});return b
},_fnDomColumnButton:function(a
){var b
=this,c
=this.s
.dt
.aoColumns
[a
],e
=document
.createElement("button"),f
=document
.createElement("span");e
.className
=!this.s
.dt
.bJUI
?"ColVis_Button TableTools_Button":"ColVis_Button TableTools_Button ui-button ui-state-default";
22 e
.appendChild(f
);c
=this.s
.fnLabel
===null?c
.sTitle
:this.s
.fnLabel(a
,c
.sTitle
,c
.nTh
);d(f
).html('<span class="ColVis_radio"><input type="checkbox"></span><span class="ColVis_title">'+c
+"</span>");d(e
).click(function(g
){var h
=!d("input",this).is(":checked");if(g
.target
.nodeName
.toLowerCase()=="input")h
=d("input",this).is(":checked");g
=d
.fn
.dataTableExt
.iApiIndex
;d
.fn
.dataTableExt
.iApiIndex
=b
._fnDataTablesApiIndex
.call(b
);b
.s
.dt
.oInstance
.fnSetColumnVis(a
,h
);d
.fn
.dataTableExt
.iApiIndex
=g
;b
.s
.fnStateChange
!==
23 null&&b
.s
.fnStateChange
.call(b
,a
,h
)});return e
},_fnDataTablesApiIndex:function(){for(var a
=0,b
=this.s
.dt
.oInstance
.length
;a
<b
;a
++)if(this.s
.dt
.oInstance
[a
]==this.s
.dt
.nTable
)return a
;return 0},_fnDomBaseButton:function(a
){var b
=this,c
=document
.createElement("button"),e
=document
.createElement("span"),f
=this.s
.activate
=="mouseover"?"mouseover":"click";c
.className
=!this.s
.dt
.bJUI
?"ColVis_Button TableTools_Button":"ColVis_Button TableTools_Button ui-button ui-state-default";c
.appendChild(e
);e
.innerHTML
=
24 a
;d(c
).bind(f
,function(g
){b
._fnCollectionShow();g
.preventDefault()});return c
},_fnDomCollection:function(){var a
=document
.createElement("div");a
.style
.display
="none";a
.className
=!this.s
.dt
.bJUI
?"ColVis_collection TableTools_collection":"ColVis_collection TableTools_collection ui-buttonset ui-buttonset-multi";a
.style
.position
="absolute";d(a
).css("opacity",0);return a
},_fnDomCatcher:function(){var a
=this,b
=document
.createElement("div");b
.className
="ColVis_catcher TableTools_catcher";d(b
).click(function(){a
._fnCollectionHide
.call(a
,
25 null,null)});return b
},_fnDomBackground:function(){var a
=this,b
=document
.createElement("div");b
.style
.position
="absolute";b
.style
.left
="0px";b
.style
.top
="0px";b
.className
="ColVis_collectionBackground TableTools_collectionBackground";d(b
).css("opacity",0);d(b
).click(function(){a
._fnCollectionHide
.call(a
,null,null)});this.s
.activate
=="mouseover"&&d(b
).mouseover(function(){a
.s
.overcollection
=false;a
._fnCollectionHide
.call(a
,null,null)});return b
},_fnCollectionShow:function(){var a
=this,b
,c
;b
=d(this.dom
.button
).offset();
26 var e
=this.dom
.collection
,f
=this.dom
.background
,g
=parseInt(b
.left
,10),h
=parseInt(b
.top
+d(this.dom
.button
).outerHeight(),10);e
.style
.top
=h
+"px";e
.style
.left
=g
+"px";e
.style
.display
="block";d(e
).css("opacity",0);c
=d(window
).height();var i
=d(document
).height(),j
=d(window
).width();h
=d(document
).width();f
.style
.height
=(c
>i
?c
:i
)+"px";f
.style
.width
=(j
<h
?j
:h
)+"px";c
=this.dom
.catcher
.style
;c
.height
=d(this.dom
.button
).outerHeight()+"px";c
.width
=d(this.dom
.button
).outerWidth()+"px";c
.top
=b
.top
+"px";c
.left
=g
+
27 "px";document
.body
.appendChild(f
);document
.body
.appendChild(e
);document
.body
.appendChild(this.dom
.catcher
);if(this.s
.sSize
=="auto"){i
=[];this.dom
.collection
.style
.width
="auto";b
=0;for(c
=this.dom
.buttons
.length
;b
<c
;b
++)if(this.dom
.buttons
[b
]!==null){this.dom
.buttons
[b
].style
.width
="auto";i
.push(d(this.dom
.buttons
[b
]).outerWidth())}iMax
=Math
.max
.apply(window
,i
);b
=0;for(c
=this.dom
.buttons
.length
;b
<c
;b
++)if(this.dom
.buttons
[b
]!==null)this.dom
.buttons
[b
].style
.width
=iMax
+"px";this.dom
.collection
.style
.width
=
28 iMax
+"px"}e
.style
.left
=this.s
.sAlign
=="left"?g
+"px":g
-d(e
).outerWidth()+d(this.dom
.button
).outerWidth()+"px";b
=d(e
).outerWidth();d(e
).outerHeight();if(g
+b
>h
)e
.style
.left
=h
-b
+"px";setTimeout(function(){d(e
).animate({opacity
:1},a
.s
.iOverlayFade
);d(f
).animate({opacity
:0.1},a
.s
.iOverlayFade
,"linear",function(){jQuery
.browser
.msie
&&jQuery
.browser
.version
=="6.0"&&a
._fnDrawCallback()})},10);this.s
.hidden
=false},_fnCollectionHide:function(){var a
=this;if(!this.s
.hidden
&&this.dom
.collection
!==null){this.s
.hidden
=
29 true;d(this.dom
.collection
).animate({opacity
:0},a
.s
.iOverlayFade
,function(){this.style
.display
="none"});d(this.dom
.background
).animate({opacity
:0},a
.s
.iOverlayFade
,function(){document
.body
.removeChild(a
.dom
.background
);document
.body
.removeChild(a
.dom
.catcher
)})}}};ColVis
.fnRebuild=function(a
){var b
=null;if(typeof a
!="undefined")b
=a
.fnSettings().nTable
;for(var c
=0,e
=ColVis
.aInstances
.length
;c
<e
;c
++)if(typeof a
=="undefined"||b
==ColVis
.aInstances
[c
].s
.dt
.nTable
)ColVis
.aInstances
[c
].fnRebuild()};ColVis
.aInstances
=
30 [];ColVis
.prototype.CLASS
="ColVis";ColVis
.VERSION
="1.0.6";ColVis
.prototype.VERSION
=ColVis
.VERSION
;typeof d
.fn
.dataTable
=="function"&&typeof d
.fn
.dataTableExt
.fnVersionCheck
=="function"&&d
.fn
.dataTableExt
.fnVersionCheck("1.7.0")?d
.fn
.dataTableExt
.aoFeatures
.push({fnInit:function(a
){return(new ColVis(a
,typeof a
.oInit
.oColVis
=="undefined"?{}:a
.oInit
.oColVis
)).dom
.wrapper
},cFeature
:"C",sFeature
:"ColVis"}):alert("Warning: ColVis requires DataTables 1.7 or greater - www.datatables.net/download")})(jQuery
);