all files / base/ data.js

68.39% Statements 119/174
62.32% Branches 43/69
59.46% Functions 22/37
69.88% Lines 116/166
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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245   89× 89× 89× 89× 89× 89× 89×   89× 89× 89×   133×   89×         89×   44×   43× 43× 43× 43× 43×   43× 43× 43× 43× 43× 43× 16× 16× 16× 16× 16× 16× 16× 16× 16× 16× 16×                                                   27× 27×   43×   42×     41×     89× 89× 89×   88× 88×     88×         88× 88× 88× 88× 88×                   43× 43× 43× 43×       43× 43× 43× 42×     24× 24×     10× 10×   42× 42× 42× 42× 42×                                                                                                 42× 42× 42× 23× 23×   42×       42× 47×     47× 47× 47×   47×       42×   42× 42× 42× 42×        
define(["require", "exports", "@syncfusion/ej2-base", "@syncfusion/ej2-data", "./constant"], function (require, exports, ej2_base_1, ej2_data_1, events) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var Data = (function () {
        function Data(parent) {
            this.dataState = { isPending: false, resolver: null, isDataChanged: false };
            this.parent = parent;
            this.keyField = this.parent.cardSettings.headerField;
            this.dataState = { isDataChanged: false };
            this.isObservable = false;
            this.initDataManager(parent.dataSource, parent.query);
            this.refreshDataManager();
        }
        Data.prototype.initDataManager = function (dataSource, query) {
            this.dataManager = dataSource instanceof ej2_data_1.DataManager ? dataSource : new ej2_data_1.DataManager(dataSource);
            this.query = query instanceof ej2_data_1.Query ? query : new ej2_data_1.Query();
            this.kanbanData = new ej2_data_1.DataManager(this.parent.kanbanData);
        };
        Data.prototype.getQuery = function () {
            return this.query.clone();
        };
        Data.prototype.getData = function (query) {
            Iif (this.parent.dataSource && 'result' in this.parent.dataSource) {
                var def = this.eventPromise({ requestType: '' }, query);
                this.isObservable = true;
                return def.promise;
            }
            return this.dataManager.executeQuery(query);
        };
        Data.prototype.setState = function (state) {
            return this.dataState = state;
        };
        Data.prototype.getStateEventArgument = function (query) {
            var adaptr = new ej2_data_1.UrlAdaptor();
            var dm = new ej2_data_1.DataManager({ url: '', adaptor: new ej2_data_1.UrlAdaptor });
            var state = adaptr.processQuery(dm, query);
            var data = JSON.parse(state.data);
            return ej2_base_1.extend(data, state.pvtData);
        };
        Data.prototype.eventPromise = function (args, query, index) {
            var _this = this;
            var dataArgs = args;
            var state = this.getStateEventArgument(query);
            var def = new ej2_data_1.Deferred();
            var deff = new ej2_data_1.Deferred();
            if (args.requestType !== undefined && this.dataState.isDataChanged !== false) {
                state.action = args;
                Eif (args.requestType === 'cardChanged' || args.requestType === 'cardRemoved' || args.requestType === 'cardCreated') {
                    var editArgs_1 = args;
                    editArgs_1.promise = deff.promise;
                    editArgs_1.state = state;
                    editArgs_1.index = index;
                    this.setState({ isPending: true, resolver: deff.resolve });
                    dataArgs.endEdit = deff.resolve;
                    dataArgs.cancelEdit = deff.reject;
                    this.parent.trigger(events.dataSourceChanged, editArgs_1);
                    deff.promise.then(function () {
                        _this.setState({ isPending: true, resolver: def.resolve });
                        _this.parent.trigger(events.dataStateChange, state);
                        editArgs_1.addedRecords.forEach(function (data) {
                            _this.parent.kanbanData.push(data);
                        });
                        editArgs_1.changedRecords.forEach(function (changedRecord) {
                            var cardObj = _this.parent.kanbanData.filter(function (data) {
                                return data[_this.parent.cardSettings.headerField] === changedRecord[_this.parent.cardSettings.headerField];
                            })[0];
                            ej2_base_1.extend(cardObj, changedRecord);
                        });
                        editArgs_1.deletedRecords.forEach(function (deletedRecord) {
                            var index = _this.parent.kanbanData.findIndex(function (data) {
                                return data[_this.parent.cardSettings.headerField] === deletedRecord[_this.parent.cardSettings.headerField];
                            });
                            _this.parent.kanbanData.splice(index, 1);
                        });
                    }).catch(function () { _this.parent.hideSpinner(); void 0; });
                }
                else {
                    this.setState({ isPending: true, resolver: def.resolve });
                    this.parent.trigger(events.dataStateChange, state);
                }
            }
            else {
                this.setState({});
                def.resolve(this.parent.dataSource);
            }
            return def;
        };
        Data.prototype.getTable = function () {
            if (this.parent.query) {
                var query = this.getQuery();
                return query.fromTable;
            }
            else {
                return null;
            }
        };
        Data.prototype.refreshDataManager = function () {
            var _this = this;
            var dataManager = this.getData(this.getQuery());
            dataManager.then(function (e) { return _this.dataManagerSuccess(e); }).catch(function (e) { return _this.dataManagerFailure(e); });
        };
        Data.prototype.dataManagerSuccess = function (e, type, offlineArgs, index) {
            var _this = this;
            Iif (this.parent.isDestroyed) {
                return;
            }
            Iif (type) {
                var resultData = ej2_base_1.extend([], e.result, null, true);
                this.parent.kanbanData = resultData;
            }
            else {
                this.parent.trigger(events.dataBinding, e, function (args) {
                    var resultData = ej2_base_1.extend([], args.result, null, true);
                    _this.parent.kanbanData = resultData;
                    _this.parent.notify(events.dataReady, { processedData: resultData });
                    _this.parent.trigger(events.dataBound, null, function () { return _this.parent.hideSpinner(); });
                });
            }
        };
        Data.prototype.dataManagerFailure = function (e) {
            var _this = this;
            if (this.parent.isDestroyed) {
                return;
            }
            this.parent.trigger(events.actionFailure, { error: e }, function () { return _this.parent.hideSpinner(); });
        };
        Data.prototype.updateDataManager = function (updateType, params, type, data, index) {
            var _this = this;
            this.parent.showSpinner();
            var promise;
            var actionArgs = {
                requestType: type, cancel: false, addedRecords: params.addedRecords,
                changedRecords: params.changedRecords, deletedRecords: params.deletedRecords
            };
            this.eventPromise(actionArgs, this.query, index);
            this.parent.trigger(events.actionComplete, actionArgs, function (offlineArgs) {
                if (!offlineArgs.cancel) {
                    switch (updateType) {
                        case 'insert':
                            promise = _this.dataManager.insert(data, _this.getTable(), _this.getQuery());
                            break;
                        case 'update':
                            promise = _this.dataManager.update(_this.keyField, data, _this.getTable(), _this.getQuery());
                            break;
                        case 'delete':
                            promise = _this.dataManager.remove(_this.keyField, data, _this.getTable(), _this.getQuery());
                            break;
                        case 'batch':
                            promise = _this.dataManager.saveChanges(params, _this.keyField, _this.getTable(), _this.getQuery());
                            break;
                    }
                    Eif (_this.dataManager.dataSource.offline) {
                        Eif (!_this.isObservable) {
                            _this.kanbanData = _this.dataManager;
                            _this.parent.kanbanData = _this.dataManager.dataSource.json;
                            _this.refreshUI(offlineArgs, index);
                        }
                    }
                    else {
                        promise.then(function (args) {
                            if (_this.parent.isDestroyed) {
                                return;
                            }
                            var dataManager = _this.getData(_this.getQuery());
                            dataManager.then(function (e) { return _this.dataManagerSuccess(e, 'DataSourceChange', offlineArgs, index); }).catch(function (e) { return _this.dataManagerFailure(e); });
                            if (offlineArgs.requestType === 'cardCreated') {
                                if (!Array.isArray(args)) {
                                    offlineArgs.addedRecords[0] = ej2_base_1.extend(offlineArgs.addedRecords[0], args);
                                }
                                else {
                                    _this.modifyArrayData(offlineArgs.addedRecords, args);
                                }
                            }
                            else if (offlineArgs.requestType === 'cardChanged') {
                                if (!Array.isArray(args)) {
                                    offlineArgs.changedRecords[0] = ej2_base_1.extend(offlineArgs.changedRecords[0], args);
                                }
                                else {
                                    _this.modifyArrayData(offlineArgs.changedRecords, args);
                                }
                            }
                            else if (offlineArgs.requestType === 'cardRemoved') {
                                if (!Array.isArray(args)) {
                                    offlineArgs.deletedRecords[0] = ej2_base_1.extend(offlineArgs.deletedRecords[0], args);
                                }
                                else {
                                    _this.modifyArrayData(offlineArgs.deletedRecords, args);
                                }
                            }
                            _this.refreshUI(offlineArgs, index);
                        }).catch(function (e) {
                            _this.dataManagerFailure(e);
                        });
                    }
                }
            });
        };
        Data.prototype.modifyArrayData = function (onLineData, e) {
            if (onLineData.length === e.length) {
                for (var i = 0; i < e.length; i++) {
                    onLineData[i] = ej2_base_1.extend(onLineData[i], e[i]);
                }
            }
            return onLineData;
        };
        Data.prototype.refreshUI = function (args, position) {
            var _this = this;
            this.parent.layoutModule.columnData = this.parent.layoutModule.getColumnCards();
            if (this.parent.swimlaneSettings.keyField) {
                this.parent.layoutModule.kanbanRows = this.parent.layoutModule.getRows();
                this.parent.layoutModule.swimlaneData = this.parent.layoutModule.getSwimlaneCards();
            }
            args.addedRecords.forEach(function (data, index) {
                Iif (_this.parent.swimlaneSettings.keyField && !data[_this.parent.swimlaneSettings.keyField]) {
                    data[_this.parent.swimlaneSettings.keyField] = '';
                }
                _this.parent.layoutModule.renderCardBasedOnIndex(data, position + index);
            });
            args.changedRecords.forEach(function (data) {
                Iif (_this.parent.swimlaneSettings.keyField && !data[_this.parent.swimlaneSettings.keyField]) {
                    data[_this.parent.swimlaneSettings.keyField] = '';
                }
                _this.parent.layoutModule.removeCard(data);
                _this.parent.layoutModule.renderCardBasedOnIndex(data, position);
                if (_this.parent.layoutModule.isSelectedCard) {
                    _this.parent.actionModule.SingleCardSelection(data);
                }
                if (_this.parent.sortSettings.field && _this.parent.sortSettings.sortBy === 'Index'
                    && _this.parent.sortSettings.direction === 'Descending' && position > 0) {
                    --position;
                }
            });
            args.deletedRecords.forEach(function (data) {
                _this.parent.layoutModule.removeCard(data);
            });
            this.parent.layoutModule.refresh();
            this.parent.renderTemplates();
            this.parent.notify(events.contentReady, {});
            this.parent.trigger(events.dataBound, args, function () { return _this.parent.hideSpinner(); });
        };
        return Data;
    }());
    exports.Data = Data;
});