/*
* File: ColVis.js
* Version: 1.1.0-dev
* CVS: $Id$
* Description: Controls for column visiblity in DataTables
* Author: Allan Jardine (www.sprymedia.co.uk)
* Created: Wed Sep 15 18:23:29 BST 2010
* Modified: $Date$ by $Author$
* Language: Javascript
* License: GPL v2 or BSD 3 point style
* Project: Just a little bit of fun :-)
* Contact: www.sprymedia.co.uk/contact
*
* Copyright 2010-2011 Allan Jardine, all rights reserved.
*
* This source file is free software, under either the GPL v2 license or a
* BSD style license, available at:
* http://datatables.net/license_gpl2
* http://datatables.net/license_bsd
*/(function(e){var t=function(e,n){(!this.CLASS||this.CLASS!="ColVis")&&alert("Warning: ColVis must be initialised with the keyword 'new'");typeof n=="undefined"&&(n={});this.s={dt:null,oInit:n,fnStateChange:null,activate:"click",sAlign:"left",buttonText:"Show / hide columns",hidden:!0,aiExclude:[],aoGroups:[],abOriginal:[],bShowAll:!1,sShowAll:"Show All",bRestore:!1,sRestore:"Restore original",iOverlayFade:500,fnLabel:null,sSize:"auto",bCssPosition:!1};this.dom={wrapper:null,button:null,collection:null,background:null,catcher:null,buttons:[],groupButtons:[],restore:null};t.aInstances.push(this);this.s.dt=e;this._fnConstruct();return this};t.prototype={fnRebuild:function(){for(var e=this.dom.buttons.length-1;e>=0;e--)this.dom.buttons[e]!==null&&this.dom.collection.removeChild(this.dom.buttons[e]);this.dom.buttons.splice(0,this.dom.buttons.length);this.dom.restore&&this.dom.restore.parentNode(this.dom.restore);this._fnAddGroups();this._fnAddButtons();this._fnDrawCallback()},_fnConstruct:function(){this._fnApplyCustomisation();var t=this,n,r;this.dom.wrapper=document.createElement("div");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._fnAddGroups();this._fnAddButtons();for(n=0,r=this.s.dt.aoColumns.length;n<r;n++)this.s.abOriginal.push(this.s.dt.aoColumns[n].bVisible);this.s.dt.aoDrawCallback.push({fn:function(){t._fnDrawCallback.call(t)},sName:"ColVis"});e(this.s.dt.oInstance).bind("column-reorder",function(e,s,o){for(n=0,r=t.s.aiExclude.length;n<r;n++)t.s.aiExclude[n]=o.aiInvertMapping[t.s.aiExclude[n]];var u=t.s.abOriginal.splice(o.iFrom,1)[0];t.s.abOriginal.splice(o.iTo,0,u);t.fnRebuild()})},_fnApplyCustomisation:function(){var e=this.s.oInit;typeof e.activate!="undefined"&&(this.s.activate=e.activate);typeof e.buttonText!="undefined"&&(this.s.buttonText=e.buttonText);typeof e.aiExclude!="undefined"&&(this.s.aiExclude=e.aiExclude);typeof e.bRestore!="undefined"&&(this.s.bRestore=e.bRestore);typeof e.sRestore!="undefined"&&(this.s.sRestore=e.sRestore);typeof e.bShowAll!="undefined"&&(this.s.bShowAll=e.bShowAll);typeof e.sShowAll!="undefined"&&(this.s.sShowAll=e.sShowAll);typeof e.sAlign!="undefined"&&(this.s.sAlign=e.sAlign);typeof e.fnStateChange!="undefined"&&(this.s.fnStateChange=e.fnStateChange);typeof e.iOverlayFade!="undefined"&&(this.s.iOverlayFade=e.iOverlayFade);typeof e.fnLabel!="undefined"&&(this.s.fnLabel=e.fnLabel);typeof e.sSize!="undefined"&&(this.s.sSize=e.sSize);typeof e.bCssPosition!="undefined"&&(this.s.bCssPosition=e.bCssPosition);typeof e.aoGroups!="undefined"&&(this.s.aoGroups=e.aoGroups)},_fnDrawCallback:function(){var t=this.s.dt.aoColumns,n=this.dom.buttons,r=this.s.aoGroups;for(var i=0,s=t.length;i<s;i++)n[i]!==null&&e("input",n[i]).prop("checked",t[i].bVisible);var o=function(e){for(var n=0,r=e.length;n<r;n++)if(t[e[n]].bVisible===!1)return!1;return!0},u=function(e){for(var n=0,r=e.length;n<r;n++)if(t[e[n]].bVisible===!0)return!1;return!0};for(var a=0,f=r.length;a<f;a++)if(o(r[a].aiColumns)){e("input",this.dom.groupButtons[a]).prop("checked",!0);e("input",this.dom.groupButtons[a]).prop("indeterminate",!1)}else if(u(r[a].aiColumns)){e("input",this.dom.groupButtons[a]).prop("checked",!1);e("input",this.dom.groupButtons[a]).prop("indeterminate",!1)}else e("input",this.dom.groupButtons[a]).prop("indeterminate",!0)},_fnAddGroups:function(){var e;if(typeof this.s.aoGroups!="undefined")for(var t=0,n=this.s.aoGroups.length;t<n;t++){e=this._fnDomGroupButton(t);this.dom.groupButtons.push(e);this.dom.collection.appendChild(e)}},_fnAddButtons:function(){var t,n=","+this.s.aiExclude.join(",")+",";if(e.inArray("all",this.s.aiExclude)===-1)for(var r=0,i=this.s.dt.aoColumns.length;r<i;r++)if(n.indexOf(","+r+",")==-1){t=this._fnDomColumnButton(r);this.dom.buttons.push(t);this.dom.collection.appendChild(t)}else this.dom.buttons.push(null);if(this.s.bRestore){t=this._fnDomRestoreButton();t.className+=" ColVis_Restore";this.dom.buttons.push(t);this.dom.collection.appendChild(t)}if(this.s.bShowAll){t=this._fnDomShowAllButton();t.className+=" ColVis_ShowAll";this.dom.buttons.push(t);this.dom.collection.appendChild(t)}},_fnDomRestoreButton:function(){var t=this,n=document.createElement("button"),r=document.createElement("span");n.className=this.s.dt.bJUI?"ColVis_Button TableTools_Button ui-button ui-state-default":"ColVis_Button TableTools_Button";n.appendChild(r);e(r).html('<span class="ColVis_title">'+this.s.sRestore+"</span>");e(n).click(function(e){for(var n=0,r=t.s.abOriginal.length;n<r;n++)t.s.dt.oInstance.fnSetColumnVis(n,t.s.abOriginal[n],!1);t._fnAdjustOpenRows();t.s.dt.oInstance.fnAdjustColumnSizing(!1);t.s.dt.oInstance.fnDraw(!1)});return n},_fnDomShowAllButton:function(){var t=this,n=document.createElement("button"),r=document.createElement("span");n.className=this.s.dt.bJUI?"ColVis_Button TableTools_Button ui-button ui-state-default":"ColVis_Button TableTools_Button";n.appendChild(r);e(r).html('<span class="ColVis_title">'+this.s.sShowAll+"</span>");e(n).click(function(e){for(var n=0,r=t.s.abOriginal.length;n<r;n++)t.s.aiExclude.indexOf(n)===-1&&t.s.dt.oInstance.fnSetColumnVis(n,!0,!1);t._fnAdjustOpenRows();t.s.dt.oInstance.fnAdjustColumnSizing(!1);t.s.dt.oInstance.fnDraw(!1)});return n},_fnDomGroupButton:function(t){var n=this,r=this.s.aoGroups[t],i=this.s.dt.aoColumns,s=document.createElement("button"),o=document.createElement("span"),u=this.s.dt;s.className=u.bJUI?"ColVis_Group ColVis_Button TableTools_Button ui-button ui-state-default":"ColVis_Group ColVis_Button TableTools_Button";s.appendChild(o);var a=r.sTitle;e(o).html('<span class="ColVis_radio"><input type="checkbox"/></span><span class="ColVis_title">'+a+"</span>");e(s).click(function(t){var i=!e("input",this).is(":checked");t.target.nodeName.toLowerCase()=="input"&&(i=e("input",this).is(":checked"));for(var s=0;s<r.aiColumns.length;s++)n.s.dt.oInstance.fnSetColumnVis(r.aiColumns[s],i)});return s},_fnDomColumnButton:function(t){var n=this,r=this.s.dt.aoColumns[t],i=document.createElement("button"),s=document.createElement("span"),o=this.s.dt;i.className=o.bJUI?"ColVis_Button TableTools_Button ui-button ui-state-default":"ColVis_Button TableTools_Button";i.appendChild(s);var u=this.s.fnLabel===null?r.sTitle:this.s.fnLabel(t,r.sTitle,r.nTh);e(s).html('<span class="ColVis_radio"><input type="checkbox" checked=""/></span><span class="ColVis_title">'+u+"</span>");e(i).click(function(r){var i=!e("input",this).is(":checked");r.target.nodeName.toLowerCase()=="input"&&(i=e("input",this).is(":checked"));var s=e.fn.dataTableExt.iApiIndex;e.fn.dataTableExt.iApiIndex=n._fnDataTablesApiIndex.call(n);if(!o.oFeatures.bServerSide||o.oScroll.sX===""&&o.oScroll.sY==="")n.s.dt.oInstance.fnSetColumnVis(t,i);else{n.s.dt.oInstance.fnSetColumnVis(t,i,!1);n.s.dt.oInstance.fnAdjustColumnSizing(!1);n.s.dt.oInstance.oApi._fnScrollDraw(n.s.dt);n._fnDrawCallback()}e.fn.dataTableExt.iApiIndex=s;n.s.fnStateChange!==null&&n.s.fnStateChange.call(n,t,i)});return i},_fnDataTablesApiIndex:function(){for(var e=0,t=this.s.dt.oInstance.length;e<t;e++)if(this.s.dt.oInstance[e]==this.s.dt.nTable)return e;return 0},_fnDomBaseButton:function(t){var n=this,r=document.createElement("button"),i=document.createElement("span"),s=this.s.activate=="mouseover"?"mouseover":"click";r.className=this.s.dt.bJUI?"ColVis_Button TableTools_Button ui-button ui-state-default":"ColVis_Button TableTools_Button";r.appendChild(i);i.innerHTML=t;e(r).bind(s,function(e){n._fnCollectionShow();e.preventDefault()});return r},_fnDomCollection:function(){var t=this,n=document.createElement("div");n.style.display="none";n.className=this.s.dt.bJUI?"ColVis_collection TableTools_collection ui-buttonset ui-buttonset-multi":"ColVis_collection TableTools_collection";this.s.bCssPosition||(n.style.position="absolute");e(n).css("opacity",0);return n},_fnDomCatcher:function(){var t=this,n=document.createElement("div");n.className="ColVis_catcher TableTools_catcher";e(n).click(function(){t._fnCollectionHide.call(t,null,null)});return n},_fnDomBackground:function(){var t=this,n=e("<div></div>").addClass("ColVis_collectionBackground TableTools_collectionBackground").css("opacity",0).click(function(){t._fnCollectionHide.call(t,null,null)});this.s.activate=="mouseover"&&n.mouseover(function(){t.s.overcollection=!1;t._fnCollectionHide.call(t,null,null)});return n[0]},_fnCollectionShow:function(){var t=this,n,r,i,s=e(this.dom.button).offset(),o=this.dom.collection,u=this.dom.background,a=parseInt(s.left,10),f=parseInt(s.top+e(this.dom.button).outerHeight(),10);if(!this.s.bCssPosition){o.style.top=f+"px";o.style.left=a+"px"}e(o).css({display:"block",opacity:0});u.style.bottom="0px";u.style.right="0px";var l=this.dom.catcher.style;l.height=e(this.dom.button).outerHeight()+"px";l.width=e(this.dom.button).outerWidth()+"px";l.top=s.top+"px";l.left=a+"px";document.body.appendChild(u);document.body.appendChild(o);document.body.appendChild(this.dom.catcher);if(this.s.sSize=="auto"){var c=[];this.dom.collection.style.width="auto";for(n=0,r=this.dom.buttons.length;n<r;n++)if(this.dom.buttons[n]!==null){this.dom.buttons[n].style.width="auto";c.push(e(this.dom.buttons[n]).outerWidth())}var h=Math.max.apply(window,c);for(n=0,r=this.dom.buttons.length;n<r;n++)this.dom.buttons[n]!==null&&(this.dom.buttons[n].style.width=h+"px");this.dom.collection.style.width=h+"px"}e(o).animate({opacity:1},t.s.iOverlayFade);e(u).animate({opacity:.1},t.s.iOverlayFade,"linear",function(){e.browser&&e.browser.msie&&e.browser.version=="6.0"&&t._fnDrawCallback()});if(!this.s.bCssPosition){i=this.s.sAlign=="left"?a:a-e(o).outerWidth()+e(this.dom.button).outerWidth();o.style.left=i+"px";var p=e(o).outerWidth(),d=e(o).outerHeight(),v=e(document).width();i+p>v&&(o.style.left=v-p+"px")}this.s.hidden=!1},_fnCollectionHide:function(){var t=this;if(!this.s.hidden&&this.dom.collection!==null){this.s.hidden=!0;e(this.dom.collection).animate({opacity:0},t.s.iOverlayFade,function(e){this.style.display="none"});e(this.dom.background).animate({opacity:0},t.s.iOverlayFade,function(e){document.body.removeChild(t.dom.background);document.body.removeChild(t.dom.catcher)})}},_fnAdjustOpenRows:function(){var e=this.s.dt.aoOpenRows,t=this.s.dt.oApi._fnVisbleColumns(this.s.dt);for(var n=0,r=e.length;n<r;n++)e[n].nTr.getElementsByTagName("td")[0].colSpan=t}};t.fnRebuild=function(e){var n=null;typeof e!="undefined"&&(n=e.fnSettings().nTable);for(var r=0,i=t.aInstances.length;r<i;r++)(typeof e=="undefined"||n==t.aInstances[r].s.dt.nTable)&&t.aInstances[r].fnRebuild()};t.aInstances=[];t.prototype.CLASS="ColVis";t.VERSION="1.1.0-dev";t.prototype.VERSION=t.VERSION;typeof e.fn.dataTable=="function"&&typeof e.fn.dataTableExt.fnVersionCheck=="function"&&e.fn.dataTableExt.fnVersionCheck("1.7.0")?e.fn.dataTableExt.aoFeatures.push({fnInit:function(e){var n=typeof e.oInit.oColVis=="undefined"?{}:e.oInit.oColVis,r=new t(e,n);return r.dom.wrapper},cFeature:"C",sFeature:"ColVis"}):alert("Warning: ColVis requires DataTables 1.7 or greater - www.datatables.net/download");e.fn.dataTable.ColVis=t})(jQuery);
|