1
0
Fork 0
mirror of https://github.com/koalyptus/TableFilter.git synced 2024-06-07 16:32:39 +02:00
TableFilter/dist/tablefilter_all.js

10 lines
112 KiB
JavaScript
Raw Normal View History

/*------------------------------------------------------------------------
- TableFilter v1.0 by Max Guglielmi
- build date: Sun Sep 07 2014 17:47:35
- http://tablefilter.free.fr
- Copyright (c) 2014, Licensed under the MIT License
------------------------------------------------------------------------*/
function tf_GetNodeText(a){var b=a.textContent||a.innerText||a.innerHTML.replace(/\<[^<>]+>/g,"");return b=b.replace(/^\s+/,"").replace(/\s+$/,""),b.tf_Trim()}function tf_IsObj(a){var b=!1;return"string"==(typeof a).tf_LCase()?window[a]&&"object"==(typeof window[a]).tf_LCase()&&(b=!0):a&&"object"==(typeof a).tf_LCase()&&(b=!0),b}function tf_IsFn(a){return a&&a.constructor==Function}function tf_IsArray(a){return a.constructor==Array}function tf_Id(a){return document.getElementById(a)}function tf_Tag(a,b){return a.getElementsByTagName(b)}function tf_RegexpEscape(b){function c(c){a=new RegExp("\\"+c,"g"),b=b.replace(a,"\\"+c)}chars=new Array("\\","[","^","$",".","|","?","*","+","(",")");for(var d=0;d<chars.length;d++)c(chars[d]);return b}function tf_CreateElm(a){if(void 0!=a&&null!=a&&""!=a){var b=document.createElement(a);if(arguments.length>1)for(var c=0;c<arguments.length;c++){var d=typeof arguments[c];switch(d.tf_LCase()){case"object":2==arguments[c].length&&b.setAttribute(arguments[c][0],arguments[c][1])}}return b}}function tf_CreateText(a){return document.createTextNode(a)}function tf_CreateOpt(a,b,c){var d=c?!0:!1,e=d?tf_CreateElm("option",["value",b],["selected","true"]):tf_CreateElm("option",["value",b]);return e.appendChild(tf_CreateText(a)),e}function tf_CreateCheckItem(a,b,c){if(void 0!=a&&void 0!=b&&void 0!=c){var d=tf_CreateElm("li"),e=tf_CreateElm("label",["for",a]),f=tf_CreateElm("input",["id",a],["name",a],["type","checkbox"],["value",b]);return e.appendChild(f),e.appendChild(tf_CreateText(c)),d.appendChild(e),d.label=e,d.check=f,d}}function tf_AddEvent(a,b,c,d){a.attachEvent?a.attachEvent("on"+b,c):a.addEventListener?a.addEventListener(b,c,void 0==d?!1:d):a["on"+b]=c}function tf_RemoveEvent(a,b,c,d){a.detachEvent?a.detachEvent("on"+b,c):a.removeEventListener?a.removeEventListener(b,c,void 0==d?!1:d):a["on"+b]=null}function tf_StopEvent(a){a||(a=window.event),a.stopPropagation?a.stopPropagation():a.cancelBubble=!0}function tf_CancelEvent(a){a||(a=window.event),a.preventDefault?a.preventDefault():a.returnValue=!1}function tf_ObjPosition(a,b){var c=0,d=0;if(a&&a.offsetParent&&b.tf_Has(a.nodeName.tf_LCase()))do c+=a.offsetLeft,d+=a.offsetTop;while(a=a.offsetParent);return[c,d]}function tf_NumSortAsc(a,b){return a-b}function tf_NumSortDesc(a,b){return b-a}function tf_IgnoreCaseSort(a,b){var c=a.tf_LCase(),d=b.tf_LCase();return d>c?-1:c>d?1:0}function tf_HasClass(a,b){return a?a.className.match(new RegExp("(\\s|^)"+b+"(\\s|$)")):!1}function tf_AddClass(a,b){a&&(tf_HasClass(a,b)||(a.className+=" "+b))}function tf_RemoveClass(a,b){if(a&&tf_HasClass(a,b)){var c=new RegExp("(\\s|^)"+b+"(\\s|$)");a.className=a.className.replace(c,"")}}function tf_IsValidDate(a,b){if(null==b&&(b="DMY"),b=b.toUpperCase(),3!=b.length&&"DDMMMYYYY"==b){var c=tf_FormatDate(a,b);a=c.getDate()+"/"+(c.getMonth()+1)+"/"+c.getFullYear(),b="DMY"}if((-1==b.indexOf("M")||-1==b.indexOf("D")||-1==b.indexOf("Y"))&&(b="DMY"),"Y"==b.substring(0,1))var d=/^\d{2}(\-|\/|\.)\d{1,2}\1\d{1,2}$/,e=/^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/;else if("Y"==b.substring(1,2))var d=/^\d{1,2}(\-|\/|\.)\d{2}\1\d{1,2}$/,e=/^\d{1,2}(\-|\/|\.)\d{4}\1\d{1,2}$/;else var d=/^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2}$/,e=/^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;if(0==d.test(a)&&0==e.test(a))return!1;var f=a.split(RegExp.$1);if("M"==b.substring(0,1))var g=f[0];else if("M"==b.substring(1,2))var g=f[1];else var g=f[2];if("D"==b.substring(0,1))var h=f[0];else if("D"==b.substring(1,2))var h=f[1];else var h=f[2];if("Y"==b.substring(0,1))var i=f[0];else if("Y"==b.substring(1,2))var i=f[1];else var i=f[2];parseFloat(i)<=50&&(i=(parseFloat(i)+2e3).toString()),parseFloat(i)<=99&&(i=(parseFloat(i)+1900).toString());var j=new Date(parseFloat(i),parseFloat(g)-1,parseFloat(h),0,0,0,0);return parseFloat(h)!=j.getDate()?!1:parseFloat(g)-1!=j.getMonth()?!1:!0}function tf_FormatDate(a,b){function c(a){if(void 0==a)return 0;if(a.length>2)return a;var b;return 99>=a&&a>50&&(b="19"+a),(50>a||"00"==a)&&(b="20"+a),b}function d(a){if(void 0==a)return 0;for(var b,c=new Array("january","february","march","april","may","j
" "!=o[f]&&this.SetFilterValue(f,o[f],!1)}}if(f==c-1&&this.displayBtn){var p=tf_CreateElm(this.fltTypeInp,["id",this.prfxValButton+f+"_"+this.id],["type","button"],["value",this.btnText]);p.className=this.btnCssClass,this.isExternalFlt&&this.externalFltTgtIds&&tf_Id(this.externalFltTgtIds[f])?tf_Id(this.externalFltTgtIds[f]).appendChild(p):g.appendChild(p),p.onclick=this.Evt._OnBtnClick}}}else this.__resetGrid();else this.refRow=this.refRow-1,this.gridLayout&&(this.refRow=0),this.nbFilterableRows=this.GetRowsNb(),this.nbVisibleRows=this.nbFilterableRows,this.nbRows=this.nbFilterableRows+this.refRow;if(this.rowsCounter&&this.SetRowsCounter(),this.statusBar&&this.SetStatusBar(),this.fixedHeaders&&!this.isFirstLoad&&this.SetFixedHeaders(),this.paging&&this.SetPaging(),this.hasResultsPerPage&&this.paging&&this.SetResultsPerPage(),this.btnReset&&this.SetResetBtn(),this.helpInstructions&&this.SetHelpInstructions(),this.hasColWidth&&!this.gridLayout&&this.SetColWidths(),this.alternateBgs&&this.isStartBgAlternate&&this.SetAlternateRows(),this.hasColOperation&&this.fltGrid&&(this.colOperation=b.col_operation,this.SetColOperation()),(this.sort||this.gridLayout)&&this.SetSort(),(this.selectable||this.editable)&&this.SetEditable(),this.hasBindScript&&void 0!=this.bindScript.src){var q=this.bindScript.src,r=void 0!=this.bindScript.name?this.bindScript.name:"";this.IncludeFile(r,q,this.bindScript.target_fn)}this.isFirstLoad=!1,this.hasGrid=!0,(this.rememberGridValues||this.rememberPageLen||this.rememberPageNb)&&this.ResetValues(),this.gridLayout||tf_AddClass(this.tbl,this.prfxTf),this.loader&&this.ShowLoader("none"),this.hasExtensions&&this.LoadExtensions(),this.onFiltersLoaded&&this.onFiltersLoaded.call(null,this)}},EvtManager:function(a,b){function c(){if(void 0!=a)switch(a){case d.Evt.name.filter:d.isModFilterFn?d.modFilterFn.call(null,d):d._Filter();break;case d.Evt.name.populateselect:d.refreshFilters?d._PopulateSelect(e,!0):d._PopulateSelect(e,!1,f,g);break;case d.Evt.name.populatechecklist:d._PopulateCheckList(e,f,g);break;case d.Evt.name.changepage:d._ChangePage(h);break;case d.Evt.name.clear:d._ClearFilters(),d._Filter();break;case d.Evt.name.changeresultsperpage:d._ChangeResultsPerPage();break;case d.Evt.name.resetvalues:d._ResetValues(),d._Filter();break;case d.Evt.name.resetpage:d._ResetPage(d.pgNbCookie);break;case d.Evt.name.resetpagelength:d._ResetPageLength(d.pgLenCookie);break;case d.Evt.name.sort:break;case d.Evt.name.loadextensions:d._LoadExtensions();break;case d.Evt.name.loadthemes:d._LoadThemes();break;default:d["_"+a].call(null,d,b)}(d.status||d.statusBar)&&d.StatusMsg(""),d.loader&&d.ShowLoader("none")}var d=this,e=void 0!=b&&void 0!=b.slcIndex?b.slcIndex:null,f=void 0!=b&&void 0!=b.slcExternal?b.slcExternal:!1,g=void 0!=b&&void 0!=b.slcId?b.slcId:null,h=void 0!=b&&void 0!=b.pgIndex?b.pgIndex:null;if(this.loader||this.status||this.statusBar){try{this.ShowLoader(""),this.StatusMsg(d["msg"+a])}catch(i){}window.setTimeout(c,this.execDelay)}else c()},ImportModule:function(a){a.path&&a.name&&this.IncludeFile(a.name,a.path,a.init)},LoadExtensions:function(){if(!this.Ext){var a=this;this.Ext={list:{},add:function(b,c,d,e){var f=d.split("/")[d.split("/").length-1],g=new RegExp(f),h=d.replace(g,"");a.Ext.list[b]={name:b,description:c,file:f,path:h,callback:e}}}}this.EvtManager(this.Evt.name.loadextensions)},_LoadExtensions:function(){if(this.hasExtensions&&tf_IsArray(this.extensions.name)&&tf_IsArray(this.extensions.src))for(var a=this.extensions,b=0;b<a.name.length;b++){var c=a.src[b],d=a.name[b],e=a.initialize&&a.initialize[b]?a.initialize[b]:null,f=a.description&&a.description[b]?a.description[b]:null;this.Ext.add(d,f,c,e),tf_IsImported(c)?e.call(null,this):this.IncludeFile(d,c,e)}},LoadThemes:function(){this.EvtManager(this.Evt.name.loadthemes)},_LoadThemes:function(){if(this.hasThemes){if(!this.Thm){var a=this;this.Thm={list:{},add:function(b,c,d,e){var f=d.split("/")[d.split("/").length-1],g=new RegExp(f),h=d.replace(g,"");a.Thm.list[b]={name:b,description:c,file:f,path:h,callback:e}}}}if(this.enableDefaultTheme&
if(b&&j.disableExcludedOptions&&s.tf_Has(f.tf_MatchCase(j.matchCase),j.matchCase)&&(h=!0),"innerhtml"==l){var k="";j.fillSlcOnDemand&&c==m[d]&&(k='selected="selected"'),n+='<option value="'+f+'" '+k+(h?'disabled="disabled"':"")+">"+g+"</option>"}else{var t;t=j.fillSlcOnDemand&&c==m[d]&&j["col"+a]==j.fltTypeSlc?tf_CreateOpt(g,f,!0):j["col"+a]!=j.fltTypeMulti?tf_CreateOpt(g,f," "!=q[a]&&f==q[a]?!0:!1):tf_CreateOpt(g,f,r.tf_Has(m[d].tf_MatchCase(j.matchCase),j.matchCase)||-1!=r.toString().indexOf(f)?!0:!1),h&&(t.disabled=!0),i.appendChild(t)}}"innerhtml"==l&&(i.innerHTML+=n),i.setAttribute("filled","1")}c=void 0==c?!1:c;var g=this.fltIds[a];if(!(null==tf_Id(g)&&!c||null==tf_Id(d)&&c)){var h,i=tf_Id(c?d:g),j=this,k=this.tbl.rows,l=this.slcFillingMethod.tf_LCase(),m=[],n="",o=this.hasCustomSlcOptions&&this.customSlcOptions.cols.tf_Has(a),p=[];b&&this.activeFilterId&&(h=this.activeFilterId.split("_")[0],h=h.split(this.prfxFlt)[1]);var q=[],r=[];this.rememberGridValues&&(q=tf_CookieValueArray(this.fltsValuesCookie,this.separator),void 0!=q&&""!=q.toString().tf_Trim()&&(this.hasCustomSlcOptions&&this.customSlcOptions.cols.tf_Has(a)?r.push(q[a]):r=q[a].split(" "+j.orOperator+" ")));var s=null,t=null;b&&this.disableExcludedOptions&&(s=[],t=[]);for(var u=this.refRow;u<this.nbRows;u++)if(!this.hasVisibleRows||!this.visibleRows.tf_Has(u)||this.paging){var v=k[u].cells,w=v.length;if(w==this.nbCells&&!o)for(var x=0;w>x;x++)if(a==x&&(!b||b&&this.disableExcludedOptions)||a==x&&b&&(""==k[u].style.display&&!this.paging||this.paging&&(!this.validRowsIndex||this.validRowsIndex&&this.validRowsIndex.tf_Has(u))&&(void 0==h||h==a||h!=a&&this.validRowsIndex.tf_Has(u)))){var y=this.GetCellData(x,v[x]),z=y.tf_MatchCase(this.matchCase);m.tf_Has(z,this.matchCase)||m.push(y),b&&this.disableExcludedOptions&&(t[x]||(t[x]=this.GetFilteredDataCol(x)),t[x].tf_Has(z,this.matchCase)||s.tf_Has(z,this.matchCase)||this.isFirstLoad||s.push(y))}}if(o){var A=this.__getCustomValues(a);m=A[0],p=A[1]}if(this.sortSlc&&!o&&(this.matchCase?(m.sort(),s&&s.sort()):(m.sort(tf_IgnoreCaseSort),s&&s.sort(tf_IgnoreCaseSort))),this.sortNumAsc&&this.sortNumAsc.tf_Has(a))try{m.sort(tf_NumSortAsc),s&&s.sort(tf_NumSortAsc),o&&p.sort(tf_NumSortAsc)}catch(B){m.sort(),s&&s.sort(),o&&p.sort()}if(this.sortNumDesc&&this.sortNumDesc.tf_Has(a))try{m.sort(tf_NumSortDesc),s&&s.sort(tf_NumSortDesc),o&&p.sort(tf_NumSortDesc)}catch(B){m.sort(),s&&s.sort(),o&&p.sort()}f()}},__deferMultipleSelection:function(a,b,c){if("select"==a.nodeName.tf_LCase()){var d=void 0==c?!1:c,e=this;window.setTimeout(function(){a.options[0].selected=!1,a.options[b].selected=""==a.options[b].value?!1:!0,d&&e.Filter()},.1)}},__getCustomValues:function(a){if(void 0!=a){var b=this.hasCustomSlcOptions&&this.customSlcOptions.cols.tf_Has(a);if(b){for(var c=[],d=[],e=this.customSlcOptions.cols.tf_IndexByValue(a),f=this.customSlcOptions.values[e],g=this.customSlcOptions.texts[e],h=this.customSlcOptions.sorts[e],i=0;i<f.length;i++)d.push(f[i]),c.push(void 0!=g[i]?g[i]:f[i]);return h&&(d.sort(),c.sort()),[d,c]}}},PopulateCheckList:function(a,b,c){this.EvtManager(this.Evt.name.populatechecklist,{slcIndex:a,slcExternal:b,slcId:c})},_PopulateCheckList:function(a,b,c){function d(){var b=1,c=tf_CreateCheckItem(j.fltIds[a]+"_0","",j.displayAllText);if(c.className=j.checkListItemCssClass,h.appendChild(c),c.check.onclick=function(a){j.__setCheckListValues(this),h.onchange.call(null,a)},j.enableCheckListResetFilter||(c.style.display="none"),tf_isIE&&(c.label.onclick=function(){c.check.click()}),j.enableEmptyOption){var d=tf_CreateCheckItem(j.fltIds[a]+"_1",j.emOperator,j.emptyText);d.className=j.checkListItemCssClass,h.appendChild(d),d.check.onclick=function(a){j.__setCheckListValues(this),h.onchange.call(null,a)},tf_isIE&&(d.label.onclick=function(){d.check.click()}),b++}if(j.enableNonEmptyOption){var e=tf_CreateCheckItem(j.fltIds[a]+"_2",j.nmOperator,j.nonEmptyText);e.className=j.checkListItemCssClass,h.appendChild(e),e.check.onclick=function(a){j.__setCheckListValues(this),h.onchange.call(null,a)},tf_isIE&&(e.label.onclick=function()
else if(G)d=""!=b.tf_Trim()?!0:!1;else if(H)try{var Q=a.replace(u,""),R=new RegExp(Q);d=R.test(b)}catch(S){d=!1}else d=e.__containsStr(a,b,void 0==f["col_"+c]?this.fltTypeInp:f["col_"+c]);return d}if(this.fltGrid&&(this.hasGrid||this.isFirstLoad)){this.onBeforeFilter&&this.onBeforeFilter.call(null,this),""!=this.inpWatermark&&this.SetWatermark(!1);var c=this.tbl.rows;f=void 0!=this.fObj?this.fObj:[];var d=0;this.validRowsIndex=[];var e=this;this.highlightKeywords&&this.UnhighlightAll(),this.popUpFilters&&this.SetAllPopupFiltersIcon(),this.markActiveColumns&&this.ClearActiveColumns(),this.searchArgs=this.GetFiltersValue();for(var g,h,i=new RegExp(this.leOperator),j=new RegExp(this.geOperator),k=new RegExp(this.lwOperator),l=new RegExp(this.grOperator),m=new RegExp(this.dfOperator),n=new RegExp(tf_RegexpEscape(this.lkOperator)),o=new RegExp(this.eqOperator),p=new RegExp(this.stOperator),q=new RegExp(this.enOperator),r=new RegExp(this.anOperator),s=(new RegExp(this.curExp),this.emOperator),t=this.nmOperator,u=new RegExp(tf_RegexpEscape(this.rgxOperator)),v=this.refRow;v<this.nbRows;v++){"none"==c[v].style.display&&(c[v].style.display="");var w=c[v].cells,x=w.length;if(x==this.nbCells){for(var y=[],z="include"==this.searchType?!0:!1,A=!1,B=0;x>B;B++){var C=this.searchArgs[this.singleSearchFlt?0:B],D=this.hasColDateType?this.colDateType[B]:this.defaultDateType;if(""!=C){var E=this.GetCellData(B,w[B]).tf_MatchCase(this.matchCase),F=C.split(this.orOperator),G=F.length>1?!0:!1,H=C.split(this.anOperator),I=H.length>1?!0:!1;if(G||I){for(var J,K=!1,L=G?F:H,M=0;M<L.length&&(J=L[M].tf_Trim(),K=b(J,E,B),a(J,K,w[B]),!G||!K)&&(!I||K);M++);y[B]=K}else y[B]=b(C.tf_Trim(),E,B),a(C,y[B],w[B]);y[B]||(z="include"==this.searchType?!1:!0),this.singleSearchFlt&&y[B]&&(A=!0),this.popUpFilters&&this.SetPopupFilterIcon(B,!0),this.markActiveColumns&&v==this.refRow&&(this.onBeforeActiveColumn&&this.onBeforeActiveColumn.call(null,this,B),tf_AddClass(this.GetHeaderElement(B),this.activeColumnsCssClass),this.onAfterActiveColumn&&this.onAfterActiveColumn.call(null,this,B))}}this.singleSearchFlt&&A&&(z=!0),z?(this.SetRowValidation(v,!0),this.validRowsIndex.push(v),this.alternateBgs&&this.SetRowBg(v,this.validRowsIndex.length),this.onRowValidated&&this.onRowValidated.call(null,this,v)):(this.SetRowValidation(v,!1),this.hasVisibleRows&&this.visibleRows.tf_Has(v)&&!this.paging?this.validRowsIndex.push(v):d++)}}this.nbVisibleRows=this.validRowsIndex.length,this.nbHiddenRows=d,this.isStartBgAlternate=!1,this.rememberGridValues&&this.RememberFiltersValue(this.fltsValuesCookie),this.paging||this.ApplyGridProps(),this.paging&&(this.startPagingRow=0,this.currentPageNb=1,this.SetPagingInfo(this.validRowsIndex)),this.onAfterFilter&&this.onAfterFilter.call(null,this)}},ApplyGridProps:function(){this.activeFlt&&this.activeFlt.nodeName.tf_LCase()==this.fltTypeSlc&&!this.popUpFilters&&(this.activeFlt.blur(),this.activeFlt.parentNode&&this.activeFlt.parentNode.focus()),this.visibleRows&&this.SetVisibleRows(),this.colOperation&&this.SetColOperation(),this.refreshFilters&&this.RefreshFiltersGrid();var a=!this.paging&&this.hasVisibleRows?this.nbVisibleRows-this.visibleRows.length:this.nbVisibleRows;this.rowsCounter&&this.RefreshNbRows(a),""!=this.inpWatermark&&this.SetWatermark(!0),this.popUpFilters&&this.CloseAllPopupFilters()},GetColValues:function(a,b,c){if(this.fltGrid){for(var d=this.tbl.rows,e=[],f=this.refRow;f<this.nbRows;f++){var g=!1;void 0!=c&&tf_IsObj(c)&&(g=c.tf_Has(f));var h=d[f].cells,i=h.length;if(i==this.nbCells&&!g)for(var j=0;i>j;j++)if(j==a&&""==d[f].style.display){var k=this.GetCellData(j,h[j]).tf_LCase(),l=this.colNbFormat?this.colNbFormat[a]:null;e.push(b?tf_RemoveNbFormat(k,l):k)}}return e}},GetFilterValue:function(a){if(this.fltGrid){var b,c=this.GetFilterElement(a);if(null==c)return b="";if(this["col"+a]!=this.fltTypeMulti&&this["col"+a]!=this.fltTypeCheckList)b=c.value;else if(this["col"+a]==this.fltTypeMulti){b="";for(var d=0;d<c.options.length;d++)c.options[d].selected&&(b=b.concat(c.options[d].value+" "+this.orOperator+" "));b=b.substr(0,b.len