1
0
Fork 0
mirror of https://github.com/koalyptus/TableFilter.git synced 2024-05-18 14:26:40 +02:00

Cleaned code

This commit is contained in:
Max Guglielmi 2015-06-05 17:46:48 +10:00
parent b76cc47666
commit 289579245d
2 changed files with 59 additions and 49 deletions

File diff suppressed because one or more lines are too long

View file

@ -75,7 +75,7 @@ export class TableFilter{
} }
if(arguments.length > 1){ if(arguments.length > 1){
for(let i=0; i<arguments.length; i++){ for(let i=0, len=arguments.length; i<len; i++){
let arg = arguments[i]; let arg = arguments[i];
let argtype = typeof arg; let argtype = typeof arg;
switch(Str.lower(argtype)){ switch(Str.lower(argtype)){
@ -110,8 +110,9 @@ export class TableFilter{
this.fltCol = []; //filter type of each column this.fltCol = []; //filter type of each column
for(let j=0; j<this.nbCells; j++){ for(let j=0; j<this.nbCells; j++){
let cfgCol = f['col_'+j]; // let cfgCol = f['col_'+j];
let col = !cfgCol ? this.fltTypeInp : Str.lower(cfgCol); // let col = !cfgCol ? this.fltTypeInp : Str.lower(cfgCol);
let col = this.getFilterType(j);
this.fltCol.push(col); this.fltCol.push(col);
this['col'+j] = col; this['col'+j] = col;
} }
@ -658,7 +659,7 @@ export class TableFilter{
/*==================================================== /*====================================================
- onchange event for select filters - onchange event for select filters
=====================================================*/ =====================================================*/
onSlcChange(e) {console.log('onSlcChange',this.activeFlt); onSlcChange(e) {
if(!this.activeFlt){ return; } if(!this.activeFlt){ return; }
// let colIndex = o.activeFlt.getAttribute('colIndex'); // let colIndex = o.activeFlt.getAttribute('colIndex');
//Checks filter is a checklist and caller is not null //Checks filter is a checklist and caller is not null
@ -712,7 +713,7 @@ export class TableFilter{
this.gridLayout)){ this.gridLayout)){
this.headersRow = 0; this.headersRow = 0;
} }
let f = this.cfg, let /*f = this.cfg,*/
n = this.singleSearchFlt ? 1 : this.nbCells, n = this.singleSearchFlt ? 1 : this.nbCells,
inpclass; inpclass;
@ -812,7 +813,8 @@ export class TableFilter{
} }
let fltcell = Dom.create(this.fltCellTag), let fltcell = Dom.create(this.fltCellTag),
col = this['col'+i], // col = this['col'+i],
col = this.getFilterType(i),
externalFltTgtId = externalFltTgtId =
this.isExternalFlt && this.externalFltTgtIds ? this.isExternalFlt && this.externalFltTgtIds ?
this.externalFltTgtIds[i] : null; this.externalFltTgtIds[i] : null;
@ -826,10 +828,10 @@ export class TableFilter{
inpclass = (i==n-1 && this.displayBtn) ? inpclass = (i==n-1 && this.displayBtn) ?
this.fltSmallCssClass : this.fltCssClass; this.fltSmallCssClass : this.fltCssClass;
if(col===undefined){ // if(col===undefined){
col = f['col_'+i]===undefined ? // col = f['col_'+i]===undefined ?
this.fltTypeInp : Str.lower(f['col_'+i]); // this.fltTypeInp : Str.lower(f['col_'+i]);
} // }
//only 1 input for single search //only 1 input for single search
if(this.singleSearchFlt){ if(this.singleSearchFlt){
@ -1068,12 +1070,11 @@ export class TableFilter{
} }
/*==================================================== /**
- TF events manager * Manages state messages
- Params: * @param {String} evt Event name
- event name (string) * @param {Object} cfg Config object
- config object (optional literal object) */
=====================================================*/
EvtManager(evt, EvtManager(evt,
cfg={ slcIndex: null, slcExternal: false, slcId: null, pgIndex: null }){ cfg={ slcIndex: null, slcExternal: false, slcId: null, pgIndex: null }){
let slcIndex = cfg.slcIndex; let slcIndex = cfg.slcIndex;
@ -1299,10 +1300,6 @@ export class TableFilter{
if(this.highlightKeywords){ if(this.highlightKeywords){
Cpt.highlightKeyword.unhighlightAll(); Cpt.highlightKeyword.unhighlightAll();
} }
// if(this.sort){
// console.log('sort.destroy',this.ExtRegistry, ExtRegistry.sort);
// ExtRegistry.sort.destroy();
// }
if(this.loader){ if(this.loader){
Cpt.loader.destroy(); Cpt.loader.destroy();
} }
@ -1327,7 +1324,7 @@ export class TableFilter{
//ie<=6 doesn't support hasAttribute method //ie<=6 doesn't support hasAttribute method
let row = rows[j]; let row = rows[j];
let attribs = row.attributes; let attribs = row.attributes;
for(let x = 0; x < attribs.length; x++){ for(let x=0, len=attribs.length; x<len; x++){
if(Str.lower(attribs.nodeName)==='validrow'){ if(Str.lower(attribs.nodeName)==='validrow'){
row.removeAttribute('validRow'); row.removeAttribute('validRow');
} }
@ -1665,7 +1662,7 @@ export class TableFilter{
/** /**
* Filter the table by retrieving the data from each cell in every single * Filter the table by retrieving the data from each cell in every single
* row and comparing it to the search term for current column. A row is * row and comparing it to the search term for current column. A row is
* hidden when all the search terms are not found in current row. * hidden when all the search terms are not found in inspected row.
*/ */
_filter(){ _filter(){
if(!this.fltGrid || (!this._hasGrid && !this.isFirstLoad)){ if(!this.fltGrid || (!this._hasGrid && !this.isFirstLoad)){
@ -1678,7 +1675,7 @@ export class TableFilter{
let row = this.tbl.rows, let row = this.tbl.rows,
Cpt = this.Cpt, Cpt = this.Cpt,
f = this.cfg, // f = this.cfg,
hiddenrows = 0; hiddenrows = 0;
this.validRowsIndex = []; this.validRowsIndex = [];
@ -1862,12 +1859,12 @@ export class TableFilter{
//like //like
else if(hasLK){ else if(hasLK){
occurence = this._containsStr( occurence = this._containsStr(
sA.replace(re_lk,''),cell_data,null,false); sA.replace(re_lk,''), cell_data, null, false);
} }
//equal //equal
else if(hasEQ){ else if(hasEQ){
occurence = this._containsStr( occurence = this._containsStr(
sA.replace(re_eq,''),cell_data,null,true); sA.replace(re_eq,''), cell_data, null, true);
} }
//starts with //starts with
else if(hasST){ else if(hasST){
@ -1881,7 +1878,7 @@ export class TableFilter{
cell_data.lastIndexOf(searchArg,cell_data.length-1) === cell_data.lastIndexOf(searchArg,cell_data.length-1) ===
(cell_data.length-1)-(searchArg.length-1) && (cell_data.length-1)-(searchArg.length-1) &&
cell_data.lastIndexOf( cell_data.lastIndexOf(
searchArg,cell_data.length-1) > -1 ? true : false; searchArg, cell_data.length-1) > -1 ? true : false;
} }
//empty //empty
else if(hasEM){ else if(hasEM){
@ -1902,9 +1899,11 @@ export class TableFilter{
} catch(e) { occurence = false; } } catch(e) { occurence = false; }
} }
else{ else{
let fCol = f['col_'+j]; // let fCol = f['col_'+j];
// occurence = this._containsStr(
// sA, cell_data, !fCol ? this.fltTypeInp : fCol);
occurence = this._containsStr( occurence = this._containsStr(
sA, cell_data, !fCol ? this.fltTypeInp : fCol); sA, cell_data, this.getFilterType(j));
} }
}//else }//else
@ -1957,7 +1956,7 @@ export class TableFilter{
let cS, let cS,
occur = false, occur = false,
s = hasMultiOrSA ? sAOrSplit : sAAndSplit; s = hasMultiOrSA ? sAOrSplit : sAAndSplit;
for(let w=0; w<s.length; w++){ for(let w=0, len=s.length; w<len; w++){
cS = Str.trim(s[w]); cS = Str.trim(s[w]);
occur = hasArg.call(this, cS, cell_data, j); occur = hasArg.call(this, cS, cell_data, j);
highlight.call(this, cS, occur, cell[j]); highlight.call(this, cS, occur, cell[j]);
@ -2040,7 +2039,7 @@ export class TableFilter{
this.startPagingRow = 0; this.startPagingRow = 0;
this.currentPageNb = 1; this.currentPageNb = 1;
Cpt.paging.setPagingInfo(this.validRowsIndex); Cpt.paging.setPagingInfo(this.validRowsIndex);
}//starts paging process }
//invokes onafter callback //invokes onafter callback
if(this.onAfterFilter){ if(this.onAfterFilter){
this.onAfterFilter.call(null,this); this.onAfterFilter.call(null,this);
@ -2154,7 +2153,7 @@ export class TableFilter{
//mutiple select //mutiple select
else if(fltColType === this.fltTypeMulti){ else if(fltColType === this.fltTypeMulti){
fltValue = ''; fltValue = '';
for(let j=0; j<flt.options.length; j++){ for(let j=0, len=flt.options.length; j<len; j++){
if(flt.options[j].selected){ if(flt.options[j].selected){
fltValue = fltValue.concat( fltValue = fltValue.concat(
flt.options[j].value+' ' + flt.options[j].value+' ' +
@ -2187,7 +2186,7 @@ export class TableFilter{
return; return;
} }
let searchArgs = []; let searchArgs = [];
for(let i=0; i<this.fltIds.length; i++){ for(let i=0, len=this.fltIds.length; i<len; i++){
searchArgs.push( searchArgs.push(
Str.trim( Str.trim(
Str.matchCase(this.getFilterValue(i), this.caseSensitive)) Str.matchCase(this.getFilterValue(i), this.caseSensitive))
@ -2222,7 +2221,7 @@ export class TableFilter{
return; return;
} }
let arr = []; let arr = [];
for(let i=0; i<this.fltIds.length; i++){ for(let i=0, len=this.fltIds.length; i<len; i++){
let fltType = this['col'+i]; let fltType = this['col'+i];
if(fltType === Str.lower(type)){ if(fltType === Str.lower(type)){
let a = bool ? i : this.fltIds[i]; let a = bool ? i : this.fltIds[i];
@ -2299,7 +2298,7 @@ export class TableFilter{
let rowData = [k,[]]; let rowData = [k,[]];
let cells = row[k].cells; let cells = row[k].cells;
// this loop retrieves cell data // this loop retrieves cell data
for(let j=0; j<cells.length; j++){ for(let j=0, len=cells.length; j<len; j++){
let cell_data = this.getCellData(j, cells[j]); let cell_data = this.getCellData(j, cells[j]);
rowData[1].push(cell_data); rowData[1].push(cell_data);
} }
@ -2329,7 +2328,7 @@ export class TableFilter{
let table = this.gridLayout ? let table = this.gridLayout ?
this.Cpt.gridLayout.headTbl : this.tbl, this.Cpt.gridLayout.headTbl : this.tbl,
r = table.rows[this.headersRow], r = table.rows[this.headersRow],
rowData = [r.rowIndex,[]]; rowData = [r.rowIndex, []];
for(let j=0; j<this.nbCells; j++){ for(let j=0; j<this.nbCells; j++){
let headerText = this.getCellData(j, r.cells[j]); let headerText = this.getCellData(j, r.cells[j]);
rowData[1].push(headerText); rowData[1].push(headerText);
@ -2363,7 +2362,7 @@ export class TableFilter{
} }
let data = this.getFilteredData(), let data = this.getFilteredData(),
colData = []; colData = [];
for(let i=0; i<data.length; i++){ for(let i=0, len=data.length; i<len; i++){
let r = data[i], let r = data[i],
//cols values of current row //cols values of current row
d = r[1], d = r[1],
@ -2431,15 +2430,15 @@ export class TableFilter{
* @param {Number} index Column's index * @param {Number} index Column's index
* @param {String} searcharg Search term * @param {String} searcharg Search term
*/ */
setFilterValue(index, searcharg/*, doFilter*/){ setFilterValue(index, searcharg=''/*, doFilter*/){
if((!this.fltGrid && !this.isFirstLoad) || if((!this.fltGrid && !this.isFirstLoad) ||
!this.getFilterElement(index)){ !this.getFilterElement(index)){
return; return;
} }
let slc = this.getFilterElement(index), let slc = this.getFilterElement(index),
// execFilter = doFilter===undefined ? true : doFilter, // execFilter = doFilter===undefined ? true : doFilter,
fltColType = this['col'+index]; // fltColType = this['col'+index];
searcharg = searcharg===undefined ? '' : searcharg; fltColType = this.getFilterType(index);
if(fltColType !== this.fltTypeMulti && if(fltColType !== this.fltTypeMulti &&
fltColType != this.fltTypeCheckList){ fltColType != this.fltTypeCheckList){
@ -2449,7 +2448,7 @@ export class TableFilter{
else if(fltColType === this.fltTypeMulti){ else if(fltColType === this.fltTypeMulti){
let s = searcharg.split(' '+this.orOperator+' '); let s = searcharg.split(' '+this.orOperator+' ');
// let ct = 0; //keywords counter // let ct = 0; //keywords counter
for(let j=0; j<slc.options.length; j++){ for(let j=0, len=slc.options.length; j<len; j++){
if(s==='' || s[0]===''){ if(s==='' || s[0]===''){
slc.options[j].selected = false; slc.options[j].selected = false;
} }
@ -2562,7 +2561,7 @@ export class TableFilter{
if(this.onBeforeReset){ if(this.onBeforeReset){
this.onBeforeReset.call(null, this, this.getFiltersValue()); this.onBeforeReset.call(null, this, this.getFiltersValue());
} }
for(let i=0; i<this.fltIds.length; i++){ for(let i=0, len=this.fltIds.length; i<len; i++){
this.setFilterValue(i, ''); this.setFilterValue(i, '');
} }
if(this.linkedFilters){ if(this.linkedFilters){
@ -2634,7 +2633,7 @@ export class TableFilter{
let activeFlt = this.activeFilterId.split('_')[0]; let activeFlt = this.activeFilterId.split('_')[0];
activeFlt = activeFlt.split(this.prfxFlt)[1]; activeFlt = activeFlt.split(this.prfxFlt)[1];
let slcSelectedValue; let slcSelectedValue;
for(let i=0; i<slcIndex.length; i++){ for(let i=0, len=slcIndex.length; i<len; i++){
let curSlc = Dom.id(this.fltIds[slcIndex[i]]); let curSlc = Dom.id(this.fltIds[slcIndex[i]]);
slcSelectedValue = this.getFilterValue(slcIndex[i]); slcSelectedValue = this.getFilterValue(slcIndex[i]);
if(activeFlt!==slcIndex[i] || if(activeFlt!==slcIndex[i] ||
@ -2790,7 +2789,7 @@ export class TableFilter{
importType = !type ? 'script' : type, importType = !type ? 'script' : type,
attr = importType == 'script' ? 'src' : 'href', attr = importType == 'script' ? 'src' : 'href',
files = Dom.tag(doc, importType); files = Dom.tag(doc, importType);
for (let i=0; i<files.length; i++){ for (let i=0, len=files.length; i<len; i++){
if(files[i][attr] === undefined){ if(files[i][attr] === undefined){
continue; continue;
} }
@ -2967,11 +2966,13 @@ export class TableFilter{
} }
/** /**
* Get the configuration object (literal object) * Return the filter type for a specified column
* @return {Object} * @param {Number} colIndex Column's index
* @return {String}
*/ */
config(){ getFilterType(colIndex){
return this.cfg; let colType = this.cfg['col_'+colIndex];
return !colType ? this.fltTypeInp : Str.lower(colType);
} }
/** /**
@ -2981,6 +2982,14 @@ export class TableFilter{
getFilterableRowsNb(){ getFilterableRowsNb(){
return this.getRowsNb(false); return this.getRowsNb(false);
} }
/**
* Get the configuration object (literal object)
* @return {Object}
*/
config(){
return this.cfg;
}
} }
TableFilter.Cookie = Cookie; TableFilter.Cookie = Cookie;