@true-directive/base
Version:
The set of base classes for the TrueDirective Grid
156 lines (155 loc) • 6.14 kB
JavaScript
/**
* Copyright (c) 2018-2019 Aleksey Melnikov, True Directive Company.
* @link https://truedirective.com/
* @license MIT
*/
import { ColumnType } from '../classes/enums';
import { FilterOperator } from '../classes/filter.class';
var FilterPipe = /** @class */ (function () {
function FilterPipe() {
this._searchStringApplicableForNumbers = false;
this._searchStringApplicableForDates = false;
}
FilterPipe.prototype.isEmpty = function (v) {
return v === null || v === undefined || v === '';
};
FilterPipe.prototype.match = function (row, columns, filters, searchString, valueFormatter) {
var _this = this;
if (valueFormatter === void 0) { valueFormatter = null; }
var res = true;
if (searchString) {
res = false;
columns.some(function (c) {
var v = row[c.fieldName];
if (c.type === ColumnType.STRING && v && v.toLowerCase().indexOf(searchString) >= 0) {
res = true;
return true;
}
if (c.type === ColumnType.REFERENCE && v && v.toLowerCase().indexOf(searchString) >= 0) {
res = true;
return true;
}
if (c.type === ColumnType.UNSAFE_HTML || c.type === ColumnType.HTML) {
if (c.displayField !== '') {
if (v && v.toLowerCase().indexOf(searchString) >= 0) {
res = true;
return true;
}
}
}
if (c.type === ColumnType.DATETIME && _this._searchStringApplicableForDates) {
if (valueFormatter !== null) {
var vs = valueFormatter.displayedValue(c, v, row) + '';
if (vs && vs.toLowerCase().indexOf(searchString) >= 0) {
res = true;
return true;
}
}
}
if (c.type === ColumnType.NUMBER && _this._searchStringApplicableForNumbers) {
if (valueFormatter !== null) {
var vs = valueFormatter.displayedValue(c, v, row) + '';
if (vs && vs.toLowerCase().indexOf(searchString) >= 0) {
res = true;
return true;
}
}
}
return false;
});
if (!res) {
return false;
}
}
for (var i = 0; i < filters.length; i++) {
// Фильтр
var f = filters[i];
// Значение поля
var v = row[f.fieldName];
var vv = v;
var v1 = f.value;
var v2 = f.value2;
var s = v ? (v + '').toLowerCase() : '';
if (f.operator === FilterOperator.EMPTY) {
res = this.isEmpty(v);
}
if (f.operator === FilterOperator.NOT_EMPTY) {
res = !this.isEmpty(v);
}
if (f.type === ColumnType.DATETIME) {
vv = vv !== null ? vv.getTime() : null;
v1 = v1 !== null ? v1.getTime() : null;
v2 = v2 !== null ? v2.getTime() : null;
}
if (f.type === ColumnType.BOOLEAN) {
v1 = f.value;
if (f.operator === FilterOperator.EQUALS) {
if (vv !== v1) {
res = false;
}
}
if (f.operator === FilterOperator.NOT_EQUALS) {
if (vv === v1) {
res = false;
}
}
}
// Задан набор значений
if (f.operator === FilterOperator.SET) {
if (f.items.indexOf(vv) < 0) {
res = false;
}
}
if (f.operator === FilterOperator.CONTAINS) {
// Содержит подстроку
if (s.indexOf(f.txtValue.toLowerCase()) < 0) {
res = false;
}
}
if (f.operator === FilterOperator.NOT_CONTAINS) {
// НЕ содержит подстроку
if (s.indexOf(f.txtValue.toLowerCase()) >= 0) {
res = false;
}
}
if (f.operator === FilterOperator.EQUALS) {
if (s !== f.txtValue.toLowerCase()) {
res = false;
}
}
if (f.operator === FilterOperator.NOT_EQUALS) {
if (s === f.txtValue.toLowerCase()) {
res = false;
}
}
if (f.operator === FilterOperator.BETWEEN) {
if (vv < v1 || vv > v2) {
res = false;
}
}
if (f.operator === FilterOperator.NOT_BETWEEN) {
if (vv >= v1 && vv <= v2) {
res = false;
}
}
}
return res;
};
FilterPipe.prototype.transform = function (rows, columns, filters, searchString, vf) {
var _this = this;
if (!rows) {
return rows;
}
searchString = searchString.toLowerCase();
if (/\d/.test(searchString)) {
this._searchStringApplicableForDates = true;
this._searchStringApplicableForNumbers = true;
}
return !searchString && (filters === undefined || filters.length === 0) ?
rows : rows.filter(function (row) {
return _this.match(row, columns, filters, searchString, vf);
});
};
return FilterPipe;
}());
export { FilterPipe };