all files / base/ data.js

73.28% Statements 181/247
61.94% Branches 83/134
71.74% Functions 33/46
73.86% Lines 178/241
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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369   210× 210× 210× 210× 210× 210× 210× 210×   210× 210× 210×   873×                       365× 365×           365×   210×         210×   125×   62× 62× 62× 62× 62×   62× 62× 62× 62× 62× 62× 62× 62× 62× 62× 62× 62× 62× 62× 62× 62× 62×                                                           62×   149×     144×     210× 210× 210× 207×   207× 207× 90×   117×             117× 117× 117× 117×     117×       117×   117×   117×                 117× 117×       62× 62× 62× 62×       62× 62× 62× 62× 60× 60× 60× 60× 60× 60× 60× 13×                                                                                             121× 121× 121×   11×   76× 18× 18× 16002×   18×     58×     10×   24×   1994×         22×                         60× 60× 13× 13×   13× 13× 13× 13×   13×         13×     13×     13×     47× 47× 23× 23×   47×       47× 52×     52× 52× 52× 10×   52×       44×     47×   47×   60× 60× 60×        
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.initload = false;
            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.isRemote = function () {
            return this.dataManager.dataSource.offline !== true && this.dataManager.dataSource.url !== undefined &&
                this.dataManager.dataSource.url !== '';
        };
        Data.prototype.columnKeyFields = function () {
            var columns = [];
            for (var _i = 0, _a = this.parent.columns; _i < _a.length; _i++) {
                var column = _a[_i];
                Iif (column.keyField.toString().split(',').length > 1) {
                    for (var _b = 0, _c = column.keyField.toString().split(','); _b < _c.length; _b++) {
                        var innerColumns = _c[_b];
                        columns.push(innerColumns.trim());
                    }
                }
                else {
                    columns.push(column.keyField.toString());
                }
            }
            return columns;
        };
        Data.prototype.getQuery = function (parameter) {
            var query = this.query.clone();
            if (this.isRemote() && this.parent.enableVirtualization) {
                var cardHeight = this.parent.cardHeight === 'auto' ? 100 :
                    parseInt(ej2_base_1.formatUnit(this.parent.cardHeight).split('px')[0], 10);
                var take = this.parent.height === 'auto' ? (Math.ceil(window.innerHeight / cardHeight) * 2) :
                    (Math.ceil(parseInt(ej2_base_1.formatUnit(this.parent.height).split('px')[0], 10) / cardHeight) * 2);
                var columns = this.columnKeyFields();
                for (var i = 0; i < columns.length; i++) {
                    query.where(this.parent.keyField, 'equal', columns[i]);
                }
                query.take(take);
                Eif (ej2_base_1.isNullOrUndefined(parameter)) {
                    parameter = 'KanbanVirtualization';
                }
                query.addParams('KanbanVirtualization', parameter);
            }
            return query;
        };
        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();
            Eif (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;
            if (this.parent.isDestroyed) {
                return;
            }
            Iif (type) {
                this.updateKanbanData(e);
                if (this.parent.enableVirtualization && this.isRemote()) {
                    this.parent.virtualLayoutModule.refresh();
                }
            }
            else {
                this.parent.trigger(events.dataBinding, e, function (args) {
                    _this.updateKanbanData(args);
                    _this.parent.notify(events.dataReady, { processedData: _this.parent.kanbanData });
                    _this.parent.trigger(events.dataBound, null, function () { return _this.parent.hideSpinner(); });
                });
            }
            Iif (this.initload) {
                this.parent.layoutModule.refresh();
                this.parent.renderTemplates();
            }
            this.initload = true;
        };
        Data.prototype.updateKanbanData = function (args) {
            var resultData = ej2_base_1.extend([], !ej2_base_1.isNullOrUndefined(args.result.result) ?
                args.result.result : args.result, null, true);
            Iif (this.isRemote() && this.parent.enableVirtualization && resultData.length > 0
                && !ej2_base_1.isNullOrUndefined(args.result.count)) {
                var columnsKeyFields = this.columnKeyFields();
                for (var i = 0; i < columnsKeyFields.length; i++) {
                    if (args.result.count[i].Key === columnsKeyFields[i]) {
                        this.parent.columnDataCount[columnsKeyFields[i]] = args.result.count[i].Value;
                    }
                }
            }
            this.parent.kanbanData = resultData;
            this.kanbanData = new ej2_data_1.DataManager(this.parent.kanbanData);
        };
        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, isDropped, dataDropIndexKeyFieldValue, draggedKey, droppedKey, isMultipleDrag) {
            var _this = this;
            this.parent.showSpinner();
            var promise;
            var actionArgs = {
                requestType: type, cancel: false, addedRecords: params.addedRecords,
                changedRecords: params.changedRecords, deletedRecords: params.deletedRecords
            };
            this.setState({ isDataChanged: true });
            this.eventPromise(actionArgs, this.query, index);
            this.parent.trigger(events.actionComplete, actionArgs, function (offlineArgs) {
                if (!offlineArgs.cancel) {
                    promise = _this.syncDataSource(_this.dataManager, updateType, params, data, isDropped, dataDropIndexKeyFieldValue);
                    Eif (_this.dataManager.dataSource.offline) {
                        Eif (!_this.isObservable) {
                            _this.syncDataSource(_this.kanbanData, updateType, params, data, isDropped, dataDropIndexKeyFieldValue);
                            index = draggedKey === droppedKey && isMultipleDrag ? index - 1 : index;
                            _this.refreshUI(offlineArgs, index, isDropped);
                            if (_this.parent.enableVirtualization) {
                                _this.parent.virtualLayoutModule.refreshColumnData(draggedKey, droppedKey, offlineArgs.requestType, data[_this.parent.keyField]);
                            }
                        }
                    }
                    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);
                                }
                            }
                            index = draggedKey === droppedKey && isMultipleDrag ? index - 1 : index;
                            _this.refreshUI(offlineArgs, index, isDropped);
                            if (_this.parent.enableVirtualization) {
                                _this.parent.virtualLayoutModule.refreshColumnData(draggedKey, droppedKey, offlineArgs.requestType, data[_this.parent.keyField]);
                            }
                        }).catch(function (e) {
                            _this.dataManagerFailure(e);
                        });
                    }
                }
            });
        };
        Data.prototype.syncDataSource = function (dataManager, updateType, params, data, isDropped, dataDropIndexKeyFieldValue) {
            var _this = this;
            var promise;
            switch (updateType) {
                case 'insert':
                    return dataManager.insert(data, this.getTable(), this.getQuery());
                case 'update':
                    if (this.parent.enableVirtualization && !this.parent.dataModule.isRemote() && isDropped) {
                        promise = dataManager.remove(this.keyField, data, this.getTable(), this.getQuery());
                        promise = dataManager.insert(data, this.getTable(), this.getQuery(), dataManager.dataSource.json.findIndex(function (data) {
                            return data[_this.parent.cardSettings.headerField] === dataDropIndexKeyFieldValue;
                        }));
                        return promise;
                    }
                    else {
                        return dataManager.update(this.keyField, data, this.getTable(), this.getQuery());
                    }
                case 'delete':
                    return dataManager.remove(this.keyField, data, this.getTable(), this.getQuery());
                case 'batch':
                    if (!this.parent.dataModule.isRemote() && isDropped && this.parent.enableVirtualization && data) {
                        for (var i = 0; i < data.length; i++) {
                            promise = dataManager.remove(this.keyField, data[i], this.getTable(), this.getQuery());
                        }
                        var currentIndex = dataManager.dataSource.json.findIndex(function (data) {
                            return data[_this.parent.cardSettings.headerField] === dataDropIndexKeyFieldValue;
                        });
                        for (var i = 0; i < data.length; i++, currentIndex++) {
                            promise = dataManager.insert(data[i], this.getTable(), this.getQuery(), currentIndex);
                        }
                        return promise;
                    }
                    else {
                        return dataManager.saveChanges(params, this.keyField, this.getTable(), this.getQuery());
                    }
                default:
                    return promise;
            }
        };
        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, isDropped) {
            var _this = this;
            if (this.parent.enableVirtualization) {
                this.parent.virtualLayoutModule.columnData = this.parent.virtualLayoutModule.getColumnCards();
                args.addedRecords.forEach(function (data, index) {
                    _this.parent.virtualLayoutModule.renderCardBasedOnIndex(data, position + index, isDropped, args.requestType);
                });
                args.changedRecords.forEach(function (data) {
                    _this.parent.virtualLayoutModule.removeCard(data);
                    _this.parent.virtualLayoutModule.renderCardBasedOnIndex(data, position, isDropped, args.requestType);
                    if (_this.parent.virtualLayoutModule.isSelectedCard) {
                        _this.parent.actionModule.SingleCardSelection(data);
                    }
                    Iif (_this.parent.sortSettings.field && _this.parent.sortSettings.sortBy === 'Index'
                        && _this.parent.sortSettings.direction === 'Descending' && position > 0) {
                        --position;
                    }
                    else {
                        position++;
                    }
                });
                args.deletedRecords.forEach(function (data) {
                    _this.parent.virtualLayoutModule.removeCard(data);
                });
                this.parent.virtualLayoutModule.refresh();
            }
            else {
                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;
                    }
                    else {
                        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;
});