forked from HTMLElements/smart-webcomponents-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmart.filter.js
6 lines (4 loc) · 14.7 KB
/
smart.filter.js
1
2
3
4
5
6
/* Smart UI v15.2.0 (2023-04-20)
Copyright (c) 2011-2023 jQWidgets.
License: https://htmlelements.com/license/ */ //
Smart.Utilities.Assign("FilterGroup",class t{constructor(){const t=this;t.stringConditions=["EMPTY","NOT_EMPTY","CONTAINS","CONTAINS_CASE_SENSITIVE","DOES_NOT_CONTAIN","DOES_NOT_CONTAIN_CASE_SENSITIVE","STARTS_WITH","STARTS_WITH_CASE_SENSITIVE","ENDS_WITH","ENDS_WITH_CASE_SENSITIVE","EQUAL","NOT_EQUAL","EQUAL_CASE_SENSITIVE","NOT_EQUAL_CASE_SENSITIVE","NULL","NOT_NULL"],t.numericConditions=["EQUAL","NOT_EQUAL","LESS_THAN","LESS_THAN_OR_EQUAL","GREATER_THAN","GREATER_THAN_OR_EQUAL","NULL","NOT_NULL"],t.dateConditions=["EQUAL","NOT_EQUAL","LESS_THAN","LESS_THAN_OR_EQUAL","GREATER_THAN","GREATER_THAN_OR_EQUAL","NULL","NOT_NULL"],t.booleanConditions=["EQUAL","NOT_EQUAL","NULL","NOT_NULL"],t.filters=new Array,t.logicalOperators=new Array}toString(){const t=this;let e="";for(let r=0;r<t.filters.length;r++)r>0&&(e+=" "+t.logicalOperators[r-1]+" "),e+=t.filters[r].toString();return e}evaluate(t){const e=this;let r=!0;for(let n=0;n<e.filters.length;n++){if(!e.filters[n].evaluate)continue;let i=e.filters[n].evaluate(t);r=0===n?i:1===e.logicalOperators[n]||"or"===e.logicalOperators[n]?r||i:r&&i}return r}getFiltersCount(){return this.filters.length}setConditions(t,e){const r=this;switch(t){case"numeric":r.numericConditions=e;break;case"string":r.stringConditions=e;break;case"date":case"time":r.dateConditions=e;break;case"bool":case"boolean":r.booleanConditions=e}}getConditions(t){const e=this;let r=new Array;switch(t){case"numeric":r=e.numericConditions.slice(0);break;case"string":r=e.stringConditions.slice(0);break;case"date":case"time":r=e.dateConditions.slice(0);break;case"bool":case"boolean":r=e.booleanConditions.slice(0)}return r}generateFilterKey(){const t=function(){return(65536*(1+Math.random())|0).toString(16).substring(1)};return t()+"-"+t()+"-"+t()}createFilter(t,e,r,n,i,s,l){if(null==t)return null;switch(t){case"int":case"float":case"int64":case"double":case"numeric":case"number":case"numericFilter":return new Smart.Utilities.NumericFilter(e,r.toUpperCase());case"string":case"stringFilter":return new Smart.Utilities.StringFilter(e,r.toUpperCase(),s);case"date":case"time":case"dateFilter":return new Smart.Utilities.DateFilter(e,r.toUpperCase(),i,s,l);case"bool":case"boolean":case"booleanFilter":return new Smart.Utilities.BooleanFilter(e,r.toUpperCase());case"custom":return new Smart.Utilities.CustomFilter(e,r.toUpperCase(),n)}throw new Error('smartGrid: There is no such filter type. The available filter types are: "numericFilter", "stringFilter", "dateFilter" and "booleanFilter"')}getFilters(){const e=this;let r=new Array;for(let n=0;n<e.filters.length;n++){const i=e.filters[n];if(i instanceof t){let t=i.getFilters();r.push({value:t,logicalOperator:e.logicalOperators[n],type:"FilterGroup"})}else{const t={value:i.value,condition:i.condition,logicalOperator:e.logicalOperators[n],type:i.type};i.data&&(t.id=i.data),r.push(t)}}return r}addFilter(t,e){const r=this;r.filters[r.filters.length]=e,e.key=r.generateFilterKey(),r.logicalOperators[r.logicalOperators.length]=t}removeFilter(t){const e=this;for(let r=0;r<e.filters.length;r++)if(e.filters[r].key===t.key){e.filters.splice(r,1),e.logicalOperators.splice(r,1);break}}getOperatorAt(t){return null==t||t<0||t>this.filters.length?null:this.logicalOperators[t]}setOperatorAt(t,e){return null==t||t<0||t>this.filters.length?null:void(this.logicalOperators[e]=e)}getFilterAt(t){return null==t||t<0||t>this.filters.length?null:this.filters[t]}setFilterAt(t,e){const r=this;return null==t||t<0||t>r.filters.length?null:(e.key=r.generateFilterKey(),void(r.filters[t]=e))}clear(){this.filters=new Array,this.logicalOperators=new Array}getUniqueValues(t,e){const r=t.data,n=[],i=[],s="dateFilter"===t.filterType&&"timePicker"===t.displayMode;let l,a=!1;for(let e=0;e<r.length;e++){let l,o=r[e][t.dataField];""!==o&&null!=o?(l=s?new Smart.Utilities.DateTime(o).toString(t.formatString):o.toString(),-1===n.indexOf(l)&&(n.push(l),i.push({label:l,value:o,customAttribute:"default-item"}))):a=!0}switch(t.filterType){case"numericFilter":case"booleanFilter":l=function(t,e){return t.value-e.value};break;case"stringFilter":l=function(t,e){return(new Intl.Collator).compare(t.value,e.value)};break;case"dateFilter":l=s?function(t,e){try{const r=t.value.getHours(),n=e.value.getHours();if(r!==n)return r-n;const i=t.value.getMinutes(),s=e.value.getMinutes();if(i!==s)return i-s;const l=t.value.getSeconds(),a=e.value.getSeconds();return l!==a?l-a:0}catch(t){return-1}}:function(t,e){try{return t.value.getTime()-e.value.getTime()}catch(t){return-1}}}return i.sort(l),"booleanFilter"===t.filterType&&i.map((function(t){t.label=t.label.toUpperCase()})),a&&i.push({label:e.localize("blanks"),value:"",customAttribute:"default-item"}),i}}),Smart.Utilities.Assign("StringFilter",class{constructor(t,e,r){const n=this;n.value=t,n.condition=e,n.locale=r||"en",n.type="stringFilter"}toString(){return this.condition+" "+this.value}evaluate(t){const e=this,r=e.value,n=e.condition;if(null==t||""===t){if("NULL"===n)return!0;if("EQUAL"===n&&t===r)return!0;if("NOT_EQUAL"===n&&t!==r)return!0;if("CONTAINS"===n&&t===r)return!0;if("EMPTY"!==n)return!1;if(""===t)return!0}let i="";try{i=t.toString()}catch(t){return!0}const s=function(t,r){const i=e.locale;switch(n){case"EQUAL":return 0===t.localeCompare(r,i,{sensitivity:"accent"});case"EQUAL_CASE_SENSITIVE":return 0===t.localeCompare(r,i,{sensitivity:"variant"});case"NOT_EQUAL":return 0!==t.localeCompare(r,i,{sensitivity:"accent"});case"NOT_EQUAL_CASE_SENSITIVE":return 0!==t.localeCompare(r,i,{sensitivity:"variant"});case"CONTAINS":return"number"==typeof r?t===r:-1!==t.toLowerCase().indexOf(r.toLowerCase());case"CONTAINS_CASE_SENSITIVE":return-1!==t.indexOf(r);case"DOES_NOT_CONTAIN":return-1===t.toLowerCase().indexOf(r.toLowerCase());case"DOES_NOT_CONTAIN_CASE_SENSITIVE":return-1===t.indexOf(r);case"EMPTY":return""===t;case"NOT_EMPTY":return""!==t;case"NOT_NULL":return null!==t;case"STARTS_WITH":return 0===(t=t.substring(0,r.length)).localeCompare(r,i,{sensitivity:"accent"});case"ENDS_WITH":return 0===(t=t.substring(t.length-r.length,t.length)).localeCompare(r,i,{sensitivity:"accent"});case"ENDS_WITH_CASE_SENSITIVE":return 0===(t=t.substring(t.length-r.length,t.length)).localeCompare(r,i,{sensitivity:"variant"});case"STARTS_WITH_CASE_SENSITIVE":return 0===(t=t.substring(0,r.length)).localeCompare(r,i,{sensitivity:"variant"});default:return!1}},l=new Array;if(r&&r.indexOf&&(r.indexOf("|")>=0||r.indexOf(" AND ")>=0||r.indexOf(" OR ")>=0||r.indexOf(" and ")>=0||r.indexOf(" or ")>=0)){const t=s(i,r);if(t)return t;const e=r.indexOf(" and ")>=0?r.split(" and "):new Array,n=r.indexOf(" or ")>=0?r.split(" or "):new Array,a=r.indexOf("|")>=0?r.split("|"):new Array;let o=r.indexOf(" AND ")>=0?r.split(" AND "):new Array,c=r.indexOf(" OR ")>=0?r.split(" OR "):new Array;if(a.length>0)for(let t=0;t<a.length;t++)a[t]=a[t].trim;const u=r.indexOf(" ")>=0?r.split(" "):new Array;if(u.length>0)for(let t=0;t<u.length;t++)u[t]=u[t].trim;if(o=o.concat(u),o=o.concat(e),c=c.concat(a),c=c.concat(n),o.length>0)for(let t=0;t<o.length;t++)!o[t].indexOf(" OR ")>=0&&l.push(o[t]);if(c.length>0)for(let t=0;t<c.length;t++)!c[t].indexOf(" AND ")>=0&&l.push(c[t]);let S;for(let t=0;t<l.length;t++){const e=s(i,l[t]),r=t<o.length?"and":"or";S=void 0===S?e:"or"===r?S||e:S&&e}return S}return s(i,r)}}),Smart.Utilities.Assign("BooleanFilter",class{constructor(t,e){const r=this;r.value=t,r.condition=e,r.type="booleanFilter"}toString(){return this.condition+" "+this.value}evaluate(t){const e=this.value,r=t;switch(this.condition){case"EQUAL":return r===e;case"NOT_EQUAL":return r!==e;case"NULL":return null==t||""===t;case"NOT_NULL":return!(null==t||""===t);default:return!1}}}),Smart.Utilities.Assign("NumericFilter",class{constructor(t,e){const r=this;r.value=t,r.condition=e,r.type="numericFilter"}toString(){return this.condition+" "+this.value}evaluate(t){let e=this.value,r=this.condition;if(null==t||""===t){if("NOT_NULL"===r)return!1;if("NULL"===r)return!0;switch(r){case"EQUAL":return t===e;case"NOT_EQUAL":return t!==e}return!1}if("NULL"===r)return!1;if("NOT_NULL"===r)return!0;let n=t;try{n=parseFloat(n)}catch(e){if(null!==t&&""!==t.toString())return!1}const i=function(t,e){switch("number"==typeof t&&"number"!=typeof e&&(e=parseFloat(e)),r){case"EQUAL":return t===e;case"NOT_EQUAL":return t!==e;case"GREATER_THAN":return t>e;case"GREATER_THAN_OR_EQUAL":return t>=e;case"LESS_THAN":return t<e;case"LESS_THAN_OR_EQUAL":return t<=e;case"STARTS_WITH":return e=e.toString().toLowerCase(),t.toString().toLowerCase().substring(0,e.length)===e;case"ENDS_WITH":return t=t.toString().toLowerCase(),e=e.toString().toLowerCase(),t.substring(t.length-e.length,t.length)===e;case"ENDS_WITH_CASE_SENSITIVE":return t=t.toString(),e=e.toString(),t.substring(t.length-e.length,t.length)===e;case"STARTS_WITH_CASE_SENSITIVE":return e=e.toString(),t.toString().substring(0,e.length)===e;case"CONTAINS":return-1!==t.toString().toLowerCase().indexOf(e.toString().toLowerCase());case"CONTAINS_CASE_SENSITIVE":return-1!==t.toString().indexOf(e.toString());case"DOES_NOT_CONTAIN":return-1===t.toString().toLowerCase().indexOf(e.toString().toLowerCase());case"DOES_NOT_CONTAIN_CASE_SENSITIVE":return-1===t.toString().indexOf(e.toString());default:return!0}};let s=new Array;if(e&&e.indexOf&&(e.indexOf("|")>=0||e.indexOf(" AND ")>=0||e.indexOf(" OR ")>=0||e.indexOf(" and ")>=0||e.indexOf(" or ")>=0)){let t=i(n,e);if(t)return t;e=e.toString();const r=e.indexOf(" and ")>=0?e.split(" and "):new Array,l=e.indexOf(" or ")>=0?e.split(" or "):new Array,a=e.indexOf("|")>=0?e.split("|"):new Array;let o,c=e.indexOf(" AND ")>=0?e.split(" AND "):new Array,u=e.indexOf(" OR ")>=0?e.split(" OR "):new Array;if(c=c.concat(r),u=u.concat(l),a.length>0)for(let t=0;t<a.length;t++)a[t]=a[t].trim;if(u=u.concat(a),c.length>0)for(let t=0;t<c.length;t++)!c[t].indexOf(" OR ")>=0&&s.push(c[t]);if(u.length>0)for(let t=0;t<u.length;t++)!u[t].indexOf(" AND ")>=0&&s.push(u[t]);for(let e=0;e<s.length;e++){const r=s[e];if(r&&r.indexOf&&r.indexOf("..")>=0){const e=r.toString().split("..");2===e.length&&(t=n>=e[0]&&n<=e[1])}else t=i(n,r);const l=e<c.length?"and":"or";o=void 0===o?t:"or"===l?o||t:o&&t}return o}return e&&e.indexOf&&e.indexOf("..")>=0&&(s=e.toString().split(".."),2===s.length)?n>=s[0]&&n<=s[1]:i(n,e)}}),Smart.Utilities.Assign("DateFilter",class{constructor(t,e,r,n,i){const s=this,l=new Smart.Utilities.DateTime;if(s.value=t,s.type="dateFilter",void 0!==r){const e=l.parseDate(t,r);if(null!==e)s.filterdate=e;else{const e=l.tryparseDate(t);null!==e&&(s.filterdate=e)}}else{const e=new Date(t);"NaN"===e.toString()||"Invalid Date"===e.toString()?s.filterdate=l.tryparseDate(t):s.filterdate=e}if(!s.filterdate){const e=new Date(t);"NaN"===e.toString()||"Invalid Date"===e.toString()?s.filterdate=l.tryparseDate(t):s.filterdate=e}s.condition=e,s.formatString=r,s.timeOnly=i}toString(){return this.condition+" "+this.value}evaluate(t){const e=this,r=e.condition,n=e.formatString;let i=e.value;if(e.timeOnly)return e.evaluateTimeOnly(t);if(null==t||""===t){if("NOT_NULL"===r)return!1;if("NULL"===r)return!0;switch(r){case"EQUAL":return t===i;case"NOT_EQUAL":return t!==i}return!1}if("NULL"===r)return!1;if("NOT_NULL"===r)return!0;let s,l=new Date;l.setFullYear(1900,0,1),l.setHours(12,0,0,0);try{const r=new Smart.Utilities.DateTime,a=new Date(t);if(l=t="NaN"===a.toString()||"Invalid Date"===a.toString()?r.tryparseDate(t):a,s=!1,void 0!==n&&(n.indexOf("t")>=0||n.indexOf("T")>=0||n.indexOf(":")>=0||n.indexOf("f")>=0)&&(s=!0,i&&-1===i.toString().indexOf(":"))){const t=r.tryparseDate(i.toString()+":00");null!==t&&(e.filterdate=t)}s||(l.setHours(0),l.setMinutes(0),l.setSeconds(0))}catch(e){if(null!==t&&""!==t.toString())return!1}if(null!==e.filterdate)i=e.filterdate;else if(i&&i.indexOf&&(-1!==i.indexOf(":")||!isNaN(parseInt(i)))){const t=new Date(l);t.setHours(12,0,0,0);const e=i.split(":");for(let r=0;r<e.length;r++)0===r&&t.setHours(e[r]),1===r&&t.setMinutes(e[r]),2===r&&t.setSeconds(e[r]);i=t}s&&i&&i.setFullYear&&l&&l.getFullYear&&-1===n.indexOf("d")&&-1===n.indexOf("M")&&-1===n.indexOf("y")&&i.setFullYear(l.getFullYear(),l.getMonth(),l.getDate());const a=function(t,e){switch(null===t&&(t=""),r){case"EQUAL":return t.toString()===e.toString();case"NOT_EQUAL":return t.toString()!==e.toString();case"GREATER_THAN":return t>e;case"GREATER_THAN_OR_EQUAL":return t>=e;case"LESS_THAN":return t<e;case"LESS_THAN_OR_EQUAL":return t<=e;case"STARTS_WITH":return e=e.toString().toLowerCase(),t.toString().toLowerCase().substring(0,e.length)===e;case"ENDS_WITH":return t=t.toString().toLowerCase(),e=e.toString().toLowerCase(),t.substring(t.length-e.length,t.length)===e;case"ENDS_WITH_CASE_SENSITIVE":return t=t.toString(),e=e.toString(),t.substring(t.length-e.length,t.length)===e;case"STARTS_WITH_CASE_SENSITIVE":return e=e.toString(),t.toString().substring(0,e.length)===e;case"CONTAINS":return-1!==t.toString().toLowerCase().indexOf(e.toString().toLowerCase());case"CONTAINS_CASE_SENSITIVE":return-1!==t.toString().indexOf(e.toString());case"DOES_NOT_CONTAIN":return-1===t.toString().toLowerCase().indexOf(e.toString().toLowerCase());case"DOES_NOT_CONTAIN_CASE_SENSITIVE":return-1===t.toString().indexOf(e.toString());default:return!0}};let o=new Array;if(i&&i.indexOf&&(i.indexOf("|")>=0||i.indexOf(" AND ")>=0||i.indexOf(" OR ")>=0||i.indexOf(" and ")>=0||i.indexOf(" or ")>=0)){let t=a(l,i);if(t)return t;const e=i.indexOf(" and ")>=0?i.split(" and "):new Array,r=i.indexOf(" or ")>=0?i.split(" or "):new Array,n=i.indexOf("|")>=0?i.split("|"):new Array;let s,c=i.indexOf(" AND ")>=0?i.split(" AND "):new Array,u=i.indexOf(" OR ")>=0?i.split(" OR "):new Array;if(c=c.concat(e),u=u.concat(r),n.length>0)for(let t=0;t<n.length;t++)n[t]=n[t].trim;if(u=u.concat(n),c.length>0)for(let t=0;t<c.length;t++)!c[t].indexOf(" OR ")>=0&&o.push(c[t]);if(u.length>0)for(let t=0;t<u.length;t++)!u[t].indexOf(" AND ")>=0&&o.push(u[t]);for(let e=0;e<o.length;e++){const r=o[e];if(r&&r.indexOf&&r.indexOf("..")>=0){const e=r.toString().split("..");2===e.length&&(t=l>=e[0]&&l<=e[1])}else t=a(l,r);const n=e<c.length?"and":"or";s=void 0===s?t:"or"===n?s||t:s&&t}return s}return i&&i.indexOf&&i.indexOf("..")>=0&&(o=i.toString().split(".."),2===o.length)?l>=o[0]&&l<=o[1]:a(l,i)}evaluateTimeOnly(t){const e=this.value;return e?!!(t&&t instanceof Date)&&(t.getHours()===e.getHours()&&(t.getMinutes()===e.getMinutes()&&t.getSeconds()===e.getSeconds())):!t}}),Smart.Utilities.Assign("CustomFilter",class{constructor(t,e,r){const n=this;n.value=t,n.condition=e,n.customfilter=r}toString(){return this.condition+" "+this.value}evaluate(t){const e=this;return e.customfilter(e.value,t,e.condition)}}),Smart.FilterGroup=Smart.Utilities.FilterGroup,Smart.StringFilter=Smart.Utilities.StringFilter,Smart.NumericFilter=Smart.Utilities.NumericFilter,Smart.DateFilter=Smart.Utilities.DateFilter,Smart.CustomFilter=Smart.Utilities.CustomFilter;