all files / grid/actions/ foreign-key.js

97.67% Statements 126/129
92.13% Branches 82/89
100% Functions 27/27
97.6% Lines 122/125
11 statements, 6 functions, 9 branches Ignored     
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 195 196 197          22× 22× 22× 22× 22×   23×   22× 22× 22×   24× 31×                           72× 72× 72×   72× 72× 89× 89×   89× 89× 89×   85× 83×         89×   72× 72× 89×   72× 71× 88× 88×                       71×             50×   117× 117× 117× 117× 117× 85×   85× 85× 85× 438×       438×       117× 66×   51×   26× 26× 26× 26× 18×   26×           26×   27× 40×   27×       20557×   24×   25× 24×          
/* istanbul ignore next */ 
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
define(["require", "exports", "@syncfusion/ej2-base", "@syncfusion/ej2-data", "../base/constant", "../base/util", "./data", "../base/constant"], function (require, exports, ej2_base_1, ej2_data_1, constant_1, util_1, data_1, events) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var ForeignKey = (function (_super) {
        __extends(ForeignKey, _super);
        function ForeignKey(parent, serviceLocator) {
            var _this = _super.call(this, parent, serviceLocator) || this;
            _this.parent = parent;
            _this.serviceLocator = serviceLocator;
            _this.initEvent();
            return _this;
        }
        ForeignKey.prototype.initEvent = function () {
            if (this.parent.isDestroyed) {
                return;
            }
            this.parent.on(constant_1.initForeignKeyColumn, this.initForeignKeyColumns, this);
            this.parent.on(constant_1.getForeignKeyData, this.getForeignKeyData, this);
            this.parent.on(constant_1.generateQuery, this.generateQueryFormData, this);
        };
        ForeignKey.prototype.initForeignKeyColumns = function (columns) {
            for (var i = 0; i < columns.length; i++) {
                columns[parseInt(i.toString(), 10)].dataSource = (columns[parseInt(i.toString(), 10)].dataSource instanceof ej2_data_1.DataManager ?
                    columns[parseInt(i.toString(), 10)].dataSource :
                    (ej2_base_1.isNullOrUndefined(columns[parseInt(i.toString(), 10)].dataSource) ? new ej2_data_1.DataManager() :
                        'result' in columns[parseInt(i.toString(), 10)].dataSource ? columns[parseInt(i.toString(), 10)].dataSource :
                            new ej2_data_1.DataManager(columns[parseInt(i.toString(), 10)].dataSource)));
            }
        };
        ForeignKey.prototype.eventfPromise = function (args, query, key, column) {
            var state = this.getStateEventArgument(query);
            var def = new ej2_data_1.Deferred();
            var deff = new ej2_data_1.Deferred();
            state.action = args.action;
            var dataModule = this.parent.getDataModule();
            if (!ej2_base_1.isNullOrUndefined(args.action) && args.action.requestType && dataModule.foreignKeyDataState.isDataChanged !== false) {
                dataModule.setForeignKeyDataState({
                    isPending: true, resolver: deff.resolve
                });
                deff.promise.then(function () {
                    def.resolve(column.dataSource);
                });
                state.setColumnData = this.parent.setForeignKeyData.bind(this.parent);
                this.parent.trigger(events.columnDataStateChange, state);
            }
            else {
                dataModule.setForeignKeyDataState({});
                def.resolve(key);
            }
            return def;
        };
        ForeignKey.prototype.getForeignKeyData = function (args) {
            var _this = this;
            var foreignColumns = args.column ? [args.column] : this.parent.getForeignKeyColumns();
            if (this.parent.columnQueryMode === 'ExcludeHidden') {
                foreignColumns = foreignColumns.filter(function (col) { return col.visible !== false; });
            }
            var allPromise = [];
            var _loop_1 = function (i) {
                var promise = void 0;
                var query = args.isComplex ? this_1.genarateColumnQuery(foreignColumns[parseInt(i.toString(), 10)]) :
                    this_1.genarateQuery(foreignColumns[parseInt(i.toString(), 10)], args.result.result, false, true);
                query.params = this_1.parent.query.params;
                var dataSource = foreignColumns[parseInt(i.toString(), 10)].dataSource;
                if (dataSource && 'result' in dataSource) {
                    var def = this_1.eventfPromise(args, query, dataSource, foreignColumns[parseInt(i.toString(), 10)]);
                    promise = def.promise;
                }
                else if (!dataSource.ready || dataSource.dataSource.offline) {
                    promise = dataSource.executeQuery(query);
                }
                else {
                    promise = dataSource.ready.then(function () {
                        return dataSource.executeQuery(query);
                    });
                }
                allPromise.push(promise);
            };
            var this_1 = this;
            for (var i = 0; i < foreignColumns.length; i++) {
                _loop_1(i);
            }
            Promise.all(allPromise).then(function (responses) {
                for (var i = 0; i < responses.length; i++) {
                    foreignColumns[parseInt(i.toString(), 10)].columnData = responses[parseInt(i.toString(), 10)].result;
                    Iif (foreignColumns[parseInt(i.toString(), 10)].editType === 'dropdownedit' && 'result' in foreignColumns[parseInt(i.toString(), 10)].dataSource) {
                        foreignColumns[parseInt(i.toString(), 10)].edit.params = ej2_base_1.extend(foreignColumns[parseInt(i.toString(), 10)]
                            .edit.params, {
                            dataSource: responses[parseInt(i.toString(), 10)].result,
                            query: new ej2_data_1.Query(), fields: {
                                value: foreignColumns[parseInt(i.toString(), 10)].foreignKeyField ||
                                    foreignColumns[parseInt(i.toString(), 10)].field,
                                text: foreignColumns[parseInt(i.toString(), 10)].foreignKeyValue
                            }
                        });
                    }
                }
                args.promise.resolve(args.result);
            }).catch(function (e) {
                var errorMsg = e;
                Eif (!errorMsg.error) {
                    errorMsg = { error: errorMsg };
                }
                _this.parent.log(['actionfailure', 'foreign_key_failure'], errorMsg);
                Iif (args.promise && args.promise.reject) {
                    args.promise.reject(e);
                }
                return e;
            });
        };
        ForeignKey.prototype.generateQueryFormData = function (args) {
            args.predicate.predicate = this.genarateQuery(args.column, args.column.columnData, true);
        };
        ForeignKey.prototype.genarateQuery = function (column, e, fromData, needQuery) {
            var gObj = this.parent;
            var predicates = [];
            var query = new ej2_data_1.Query();
            var field = fromData ? column.foreignKeyField : column.field;
            if (gObj.allowPaging || gObj.enableVirtualization || fromData) {
                e = new ej2_data_1.DataManager(((gObj.allowGrouping && gObj.groupSettings.columns.length && !fromData) ?
                    e.records : e)).executeLocal(new ej2_data_1.Query().select(field));
                var filteredValue = ej2_data_1.DataUtil.distinct(e, field, false);
                field = fromData ? column.field : column.foreignKeyField;
                for (var i = 0; i < filteredValue.length; i++) {
                    Iif (filteredValue[parseInt(i.toString(), 10)] && filteredValue[parseInt(i.toString(), 10)].getDay) {
                        predicates.push(util_1.getDatePredicate({ field: field, operator: 'equal', value: filteredValue[parseInt(i.toString(), 10)], matchCase: false }));
                    }
                    else {
                        predicates.push(new ej2_data_1.Predicate(field, 'equal', filteredValue[parseInt(i.toString(), 10)], false));
                    }
                }
            }
            if (needQuery) {
                return predicates.length ? query.where(ej2_data_1.Predicate.or(predicates)) : query;
            }
            return (predicates.length ? ej2_data_1.Predicate.or(predicates) : { predicates: [] });
        };
        ForeignKey.prototype.genarateColumnQuery = function (column) {
            var gObj = this.parent;
            var query = new ej2_data_1.Query();
            var queryColumn = this.isFiltered(column);
            if (queryColumn.isTrue) {
                query = this.filterQuery(query, queryColumn.column, true);
            }
            if (gObj.searchSettings.key.length) {
                var sSettings = gObj.searchSettings;
                if (column.dataSource instanceof ej2_data_1.DataManager && (column.dataSource.adaptor.getModuleName &&
                    column.dataSource.adaptor.getModuleName() === 'ODataV4Adaptor')) {
                    query = this.searchQuery(query, column, true);
                }
                else {
                    query.search(sSettings.key, column.foreignKeyValue, sSettings.operator, sSettings.ignoreCase);
                }
            }
            return query;
        };
        ForeignKey.prototype.isFiltered = function (column) {
            var filterColumn = this.parent.filterSettings.columns.filter(function (fColumn) {
                return (fColumn.field === column.foreignKeyValue && fColumn.uid === column.uid);
            });
            return {
                column: filterColumn, isTrue: !!filterColumn.length
            };
        };
        ForeignKey.prototype.getModuleName = function () {
            return 'foreignKey';
        };
        ForeignKey.prototype.destroy = function () {
            this.destroyEvent();
        };
        ForeignKey.prototype.destroyEvent = function () {
            if (this.parent.isDestroyed) {
                return;
            }
            this.parent.off(constant_1.initForeignKeyColumn, this.initForeignKeyColumns);
            this.parent.off(constant_1.getForeignKeyData, this.getForeignKeyData);
            this.parent.off(constant_1.generateQuery, this.generateQueryFormData);
        };
        return ForeignKey;
    }(data_1.Data));
    exports.ForeignKey = ForeignKey;
});