all files / treegrid/actions/ filter.js

98.45% Statements 127/129
99.01% Branches 100/101
100% Functions 13/13
98.45% Lines 127/129
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194   625× 625× 625× 625× 625× 625× 625×   11395×   608×   625× 625×   608× 608×         92× 92× 92× 92× 92× 92× 4784× 4784× 4784×   4784×   112×   4784× 4784× 902×   4784× 4784× 3922× 3922× 3922× 104×       92× 27×   92×   182× 182× 182× 176×   182× 78×     8810× 8810×   8810×   38× 17× 10×   10×       21× 21× 14× 12× 12×                 8772× 4074×   4074×   86× 17×       3988×     8772× 2874× 2874×       988× 988× 988× 3876× 3876×   3876×   2555× 2555× 2537× 2537× 2537×     3876×   42×     3869× 86×   3869× 2555×     988×   27× 27× 27× 211× 211× 211× 131× 131×     80× 80×       472× 472× 472× 472× 472× 49808× 49808× 49808× 294× 294×     472× 472×        
define(["require", "exports", "@syncfusion/ej2-grids", "@syncfusion/ej2-base", "../utils"], function (require, exports, ej2_grids_1, ej2_base_1, utils_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var Filter = (function () {
        function Filter(parent) {
            ej2_grids_1.Grid.Inject(ej2_grids_1.Filter);
            this.parent = parent;
            this.isHierarchyFilter = false;
            this.filteredResult = [];
            this.flatFilteredData = [];
            this.filteredParentRecs = [];
            this.addEventListener();
        }
        Filter.prototype.getModuleName = function () {
            return 'filter';
        };
        Filter.prototype.destroy = function () {
            this.removeEventListener();
        };
        Filter.prototype.addEventListener = function () {
            this.parent.on('updateFilterRecs', this.updatedFilteredRecord, this);
            this.parent.on('clearFilters', this.clearFilterLevel, this);
        };
        Filter.prototype.removeEventListener = function () {
            Eif (this.parent.isDestroyed) {
                return;
            }
            this.parent.off('updateFilterRecs', this.updatedFilteredRecord);
            this.parent.off('clearFilters', this.clearFilterLevel);
        };
        Filter.prototype.updatedFilteredRecord = function (dataDetails) {
            ej2_base_1.setValue('uniqueIDFilterCollection', {}, this.parent);
            this.flatFilteredData = dataDetails.data;
            this.filteredParentRecs = [];
            this.filteredResult = [];
            this.isHierarchyFilter = false;
            for (var f = 0; f < this.flatFilteredData.length; f++) {
                var rec = this.flatFilteredData[parseInt(f.toString(), 10)];
                this.addParentRecord(rec);
                var hierarchyMode = this.parent.grid.searchSettings.key === '' ? this.parent.filterSettings.hierarchyMode
                    : this.parent.searchSettings.hierarchyMode;
                if (((hierarchyMode === 'Child' || hierarchyMode === 'None') &&
                    (this.parent.grid.filterSettings.columns.length !== 0 || this.parent.grid.searchSettings.key !== ''))) {
                    this.isHierarchyFilter = true;
                }
                var ischild = ej2_grids_1.getObject('childRecords', rec);
                if (!ej2_base_1.isNullOrUndefined(ischild) && ischild.length) {
                    ej2_base_1.setValue('hasFilteredChildRecords', this.checkChildExsist(rec), rec);
                }
                var parent_1 = ej2_grids_1.getObject('parentItem', rec);
                if (!ej2_base_1.isNullOrUndefined(parent_1)) {
                    var parRecord = utils_1.getParentData(this.parent, rec.parentItem.uniqueID, true);
                    ej2_base_1.setValue('hasFilteredChildRecords', true, parRecord);
                    if (parRecord && parRecord.parentItem) {
                        this.updateParentFilteredRecord(parRecord);
                    }
                }
            }
            if (this.flatFilteredData.length > 0 && this.isHierarchyFilter) {
                this.updateFilterLevel();
            }
            this.parent.notify('updateAction', { result: this.filteredResult });
        };
        Filter.prototype.updateParentFilteredRecord = function (record) {
            var parRecord = utils_1.getParentData(this.parent, record.parentItem.uniqueID, true);
            var uniqueIDValue = ej2_base_1.getValue('uniqueIDFilterCollection', this.parent);
            if (parRecord && Object.prototype.hasOwnProperty.call(uniqueIDValue, parRecord.uniqueID)) {
                ej2_base_1.setValue('hasFilteredChildRecords', true, parRecord);
            }
            if (parRecord && parRecord.parentItem) {
                this.updateParentFilteredRecord(parRecord);
            }
        };
        Filter.prototype.addParentRecord = function (record) {
            var parent = utils_1.getParentData(this.parent, record.parentUniqueID);
            var hierarchyMode = this.parent.grid.searchSettings.key === '' ? this.parent.filterSettings.hierarchyMode
                : this.parent.searchSettings.hierarchyMode;
            if (hierarchyMode === 'None' && (this.parent.grid.filterSettings.columns.length !== 0
                || this.parent.grid.searchSettings.key !== '')) {
                if (ej2_base_1.isNullOrUndefined(parent)) {
                    if (this.flatFilteredData.indexOf(record) !== -1) {
                        if (this.filteredResult.indexOf(record) === -1) {
                            this.filteredResult.push(record);
                            ej2_base_1.setValue('uniqueIDFilterCollection.' + record.uniqueID, record, this.parent);
                            record.hasFilteredChildRecords = true;
                        }
                        return;
                    }
                }
                else {
                    this.addParentRecord(parent);
                    if (this.flatFilteredData.indexOf(parent) !== -1 || this.filteredResult.indexOf(parent) !== -1) {
                        if (this.filteredResult.indexOf(record) === -1) {
                            this.filteredResult.push(record);
                            ej2_base_1.setValue('uniqueIDFilterCollection.' + record.uniqueID, record, this.parent);
                        }
                    }
                    else {
                        if (this.filteredResult.indexOf(record) === -1 && this.flatFilteredData.indexOf(record) !== -1) {
                            this.filteredResult.push(record);
                            ej2_base_1.setValue('uniqueIDFilterCollection.' + record.uniqueID, record, this.parent);
                        }
                    }
                }
            }
            else {
                if (!ej2_base_1.isNullOrUndefined(parent)) {
                    var hierarchyMode_1 = this.parent.grid.searchSettings.key === '' ?
                        this.parent.filterSettings.hierarchyMode : this.parent.searchSettings.hierarchyMode;
                    if (hierarchyMode_1 === 'Child' && (this.parent.grid.filterSettings.columns.length !== 0
                        || this.parent.grid.searchSettings.key !== '')) {
                        if (this.flatFilteredData.indexOf(parent) !== -1) {
                            this.addParentRecord(parent);
                        }
                    }
                    else {
                        this.addParentRecord(parent);
                    }
                }
                if (this.filteredResult.indexOf(record) === -1) {
                    this.filteredResult.push(record);
                    ej2_base_1.setValue('uniqueIDFilterCollection.' + record.uniqueID, record, this.parent);
                }
            }
        };
        Filter.prototype.checkChildExsist = function (records) {
            var childRec = ej2_grids_1.getObject('childRecords', records);
            var isExist = false;
            for (var count = 0; count < childRec.length; count++) {
                var ischild = childRec[parseInt(count.toString(), 10)].childRecords;
                var hierarchyMode = this.parent.grid.searchSettings.key === '' ?
                    this.parent.filterSettings.hierarchyMode : this.parent.searchSettings.hierarchyMode;
                if (((hierarchyMode === 'Child' || hierarchyMode === 'Both') && (this.parent.grid.filterSettings.columns.length !== 0
                    || this.parent.grid.searchSettings.key !== ''))) {
                    var uniqueIDValue = ej2_base_1.getValue('uniqueIDFilterCollection', this.parent);
                    if (!Object.prototype.hasOwnProperty.call(uniqueIDValue, childRec[parseInt(count.toString(), 10)].uniqueID)) {
                        this.filteredResult.push(childRec[parseInt(count.toString(), 10)]);
                        ej2_base_1.setValue('uniqueIDFilterCollection.' + childRec[parseInt(count.toString(), 10)].uniqueID, childRec[parseInt(count.toString(), 10)], this.parent);
                        isExist = true;
                    }
                }
                if ((hierarchyMode === 'None')
                    && (this.parent.grid.filterSettings.columns.length !== 0 || this.parent.grid.searchSettings.key !== '')) {
                    if (this.flatFilteredData.indexOf(childRec[parseInt(count.toString(), 10)]) !== -1) {
                        isExist = true;
                        break;
                    }
                }
                if (!ej2_base_1.isNullOrUndefined(ischild) && ischild.length) {
                    isExist = this.checkChildExsist(childRec[parseInt(count.toString(), 10)]);
                }
                if ((hierarchyMode === 'Child' || hierarchyMode === 'Both') && childRec.length) {
                    isExist = true;
                }
            }
            return isExist;
        };
        Filter.prototype.updateFilterLevel = function () {
            var record = this.filteredResult;
            var len = this.filteredResult.length;
            for (var c = 0; c < len; c++) {
                var parent_2 = utils_1.getParentData(this.parent, record[parseInt(c.toString(), 10)].parentUniqueID);
                var isPrst = record.indexOf(parent_2) !== -1;
                if (isPrst) {
                    var parent_3 = utils_1.getParentData(this.parent, record[parseInt(c.toString(), 10)].parentUniqueID, true);
                    record[parseInt(c.toString(), 10)].filterLevel = parent_3.filterLevel + 1;
                }
                else {
                    record[parseInt(c.toString(), 10)].filterLevel = 0;
                    this.filteredParentRecs.push(record[parseInt(c.toString(), 10)]);
                }
            }
        };
        Filter.prototype.clearFilterLevel = function (data) {
            var count = 0;
            var flatData = data.flatData;
            var len = flatData.length;
            var currentRecord;
            for (count; count < len; count++) {
                currentRecord = flatData[parseInt(count.toString(), 10)];
                var fLevel = currentRecord.filterLevel;
                if (fLevel || fLevel === 0 || !ej2_base_1.isNullOrUndefined(currentRecord.hasFilteredChildRecords)) {
                    currentRecord.hasFilteredChildRecords = null;
                    currentRecord.filterLevel = null;
                }
            }
            this.filteredResult = [];
            this.parent.notify('updateResults', { result: flatData, count: flatData.length });
        };
        return Filter;
    }());
    exports.Filter = Filter;
});