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

97.96% Statements 96/98
95% Branches 57/60
100% Functions 26/26
97.89% Lines 93/95
9 statements, 5 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              10×       18× 18× 18× 18× 35× 35×   35× 35× 34×         35×   18× 18× 35×   18×           21×   45× 45× 45× 45× 45× 45× 44×   44× 44× 44× 287×       287×       45× 23×   22× 22×   14× 14× 14× 14× 14×   14×   14×   15× 62×   15×       7966×              
/* istanbul ignore next */ 
var __extends = (this && this.__extends) || (function () {
    var 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 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"], function (require, exports, ej2_base_1, ej2_data_1, constant_1, util_1, data_1) {
    "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) {
            columns.forEach(function (column) {
                column.dataSource = (column.dataSource instanceof ej2_data_1.DataManager ? column.dataSource :
                    (ej2_base_1.isNullOrUndefined(column.dataSource) ? new ej2_data_1.DataManager() : new ej2_data_1.DataManager(column.dataSource)));
            });
        };
        ForeignKey.prototype.getForeignKeyData = function (args) {
            var _this = this;
            var foreignColumns = args.column ? [args.column] : this.parent.getForeignKeyColumns();
            var allPromise = [];
            foreignColumns.forEach(function (col) {
                var promise;
                var query = args.isComplex ? _this.genarateColumnQuery(col) :
                    _this.genarateQuery(col, args.result.result, false, true);
                var dataSource = col.dataSource;
                if (!dataSource.ready || dataSource.dataSource.offline) {
                    promise = dataSource.executeQuery(query);
                }
                else {
                    promise = dataSource.ready.then(function () {
                        return dataSource.executeQuery(query);
                    });
                }
                allPromise.push(promise);
            });
            Promise.all(allPromise).then(function (responses) {
                responses.forEach(function (data, index) {
                    foreignColumns[index].columnData = data.result;
                });
                args.promise.resolve(args.result);
            }).catch(function (e) {
                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 predicate;
            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) ?
                    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;
                filteredValue.forEach(function (obj) {
                    Iif (obj && obj.getDay) {
                        predicates.push(util_1.getDatePredicate({ field: field, operator: 'equal', value: obj, matchCase: false }));
                    }
                    else {
                        predicates.push(new ej2_data_1.Predicate(field, 'equal', obj, false));
                    }
                });
            }
            if (needQuery) {
                return predicates.length ? query.where(ej2_data_1.Predicate.or(predicates)) : query;
            }
            predicate = (predicates.length ? ej2_data_1.Predicate.or(predicates) : { predicates: [] });
            return predicate;
        };
        ForeignKey.prototype.genarateColumnQuery = function (column) {
            var gObj = this.parent;
            var query = new ej2_data_1.Query();
            var predicate = [];
            var queryColumn = this.isFiltered(column);
            if (queryColumn.isTrue) {
                query = this.filterQuery(query, queryColumn.column, true);
            }
            if (gObj.searchSettings.key.length) {
                var sSettings = gObj.searchSettings;
                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);
            });
            return {
                column: filterColumn, isTrue: !!filterColumn.length
            };
        };
        ForeignKey.prototype.getModuleName = function () {
            return 'foreignKey';
        };
        ForeignKey.prototype.destroy = function () {
            _super.prototype.destroy.call(this);
            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;
});