all files / workbook/integrations/ data-bind.js

92.44% Statements 416/450
90.5% Branches 362/400
95.45% Functions 42/44
92.44% Lines 416/450
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 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690   886× 886× 886×   886× 886× 886× 886×   885× 12× 12× 12× 12×     19444× 19444× 19444× 19444× 19444× 19444× 19444× 19444× 19444× 19444× 19444× 19444× 19444× 19444× 19444× 19444× 19444× 19444× 19444× 19444× 19444× 16984× 16984× 17348× 17348× 17348×   17348× 17348× 17348× 17348×   17348× 17348× 17348× 16924×     424× 195× 195×     229×     17348× 12440×   17348× 17348× 17348× 17348× 17348× 17348× 17348× 17348×             17348× 3739×   13609× 422×   17348× 17340×   17348× 666× 666× 666× 666×   666× 666× 650×   666× 666×   664× 664× 664× 664× 664× 650× 619× 619×   650× 650× 619× 619×   650×                           650× 622× 622× 4919× 4919× 4836×     83× 83×       650× 18935× 18935× 328167× 328167× 328167× 798× 798× 798×       327369× 327369×     328167× 160×   328167× 24×           14×   664× 664× 655×       664× 664× 16×   664× 664× 39× 39×   664× 574×   664× 557×   664×                             664×   664× 664× 658× 658× 28× 26×   28× 28× 28×   28×       28× 28× 25×     25× 25× 24×   25×   21×         630×     630×   658× 656× 656×           16682× 16324×   16324×     16984× 16984× 17348×       2460×       16985× 16985× 16985× 16985× 17396× 17396× 17375× 16952× 16952× 16952×     423× 360× 360×   63× 25× 25×   38× 17×     21×         16985×   328167× 328167× 53× 40×   13× 12×                         328114× 175×     327939× 36540×       36538×       291399×       328167×   17348× 17348× 16909× 16140× 15874×     266×     769× 227×     17348×   17348×               17348×   17348× 630×     13× 13× 13× 13× 13× 13× 13× 13× 11× 263× 263× 263× 2533× 2438×             13×   13×   13× 13× 13×   13×       13×               13×     6846× 6846× 6846× 6846× 6846× 6846× 6846× 6846× 6846× 6846× 6846× 6846× 6846× 6846× 6163× 6137× 6137× 6137×   6137× 286× 149× 82× 82× 82× 44×     43× 34×       38× 49×   38×       67× 67× 34× 47× 45× 45×         149×     137× 81×   81× 81× 35×   31× 29×       46× 46×   81× 17×       56× 56×     55×           5851× 5851×   5851× 5851× 756× 779× 74×       5851× 5851× 44× 44× 23× 28× 23×     23×   44×     5971× 512× 35× 60× 60× 405× 325× 325×     80×     60×   35×   29×     477× 477× 477× 30× 30× 197× 197× 1683× 1404× 1404×       30× 197×     477× 452× 452× 702×   694× 694× 5680× 5374× 5374×     694× 694×   452× 452×   702× 702×       506×     505×     5459×         7103× 7103× 1051×   6052× 3543×   2509×   8273× 8273× 8273× 37×   8236× 11×   8225×     8273× 6845× 6845× 66× 66× 22× 22× 22×     44× 44× 44× 44×       6779×   6845×     126650×   885× 885× 885×        
define(["require", "exports", "@syncfusion/ej2-data", "../base/index", "../common/index", "../common/index", "../index", "@syncfusion/ej2-base"], function (require, exports, ej2_data_1, index_1, index_2, index_3, index_4, ej2_base_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var DataBind = (function () {
        function DataBind(parent) {
            this.parent = parent;
            this.requestedInfo = [];
            this.addEventListener();
        }
        DataBind.prototype.addEventListener = function () {
            this.parent.on(index_2.updateSheetFromDataSource, this.updateSheetFromDataSourceHandler, this);
            this.parent.on(index_2.dataSourceChanged, this.dataSourceChangedHandler, this);
            this.parent.on(index_3.dataChanged, this.dataChangedHandler, this);
            this.parent.on(index_4.triggerDataChange, this.triggerDataChangeHandler, this);
        };
        DataBind.prototype.removeEventListener = function () {
            if (!this.parent.isDestroyed) {
                this.parent.off(index_2.updateSheetFromDataSource, this.updateSheetFromDataSourceHandler);
                this.parent.off(index_2.dataSourceChanged, this.dataSourceChangedHandler);
                this.parent.off(index_3.dataChanged, this.dataChangedHandler);
                this.parent.off(index_4.triggerDataChange, this.triggerDataChangeHandler);
            }
        };
        DataBind.prototype.updateSheetFromDataSourceHandler = function (args) {
            var _this = this;
            var cell;
            var flds;
            var sCellIdx;
            var result;
            var remoteUrl;
            var isLocal;
            var dataManager;
            var requestedRange = [];
            var sRanges = [];
            var rowIdx;
            var colIdx;
            var deferred = new ej2_data_1.Deferred();
            var sRowIdx;
            var sColIdx;
            var loadedInfo;
            args.promise = deferred.promise;
            var startCellIndexes;
            var autoDetectFormat = args.autoDetectFormat;
            var autoDetectFormatFn = autoDetectFormat && index_4.getAutoDetectFormatParser(this.parent);
            if (args.sheet && args.sheet.ranges.length) {
                var prevVal_1;
                var _loop_1 = function (k) {
                    var range = args.sheet.ranges[k];
                    startCellIndexes = index_2.getRangeIndexes(range.startCell);
                    if (args.loadFromStartCell) {
                        args.indexes = startCellIndexes;
                    }
                    var sRange = args.indexes[0];
                    var eRange = args.indexes[2];
                    sRowIdx = startCellIndexes[0];
                    dataManager = range.dataSource instanceof ej2_data_1.DataManager ? range.dataSource
                        : range.dataSource ? new ej2_data_1.DataManager(range.dataSource) : new ej2_data_1.DataManager();
                    remoteUrl = remoteUrl || dataManager.dataSource.url;
                    args.sheet.isLocalData = isLocal || !dataManager.dataSource.url;
                    if (sRowIdx <= sRange) {
                        sRange = sRange - sRowIdx;
                    }
                    else {
                        if (sRowIdx <= eRange) {
                            eRange = eRange - sRowIdx;
                            sRange = 0;
                        }
                        else {
                            sRange = -1;
                        }
                    }
                    if (range.showFieldAsHeader && sRange !== 0) {
                        sRange -= 1;
                    }
                    var isEndReached = false;
                    var insertRowCount = 0;
                    this_1.initRangeInfo(range);
                    var count = this_1.getMaxCount(range);
                    loadedInfo = this_1.getLoadedInfo(sRange, eRange, range);
                    sRange = loadedInfo.unloadedRange[0];
                    eRange = loadedInfo.unloadedRange[1];
                    Iif (range.info.insertRowRange) {
                        range.info.insertRowRange.forEach(function (range) {
                            insertRowCount += ((range[1] - range[0]) + 1);
                        });
                        sRange -= insertRowCount;
                        eRange -= insertRowCount;
                    }
                    if (sRange >= count) {
                        isEndReached = true;
                    }
                    else if (eRange > count) {
                        eRange = count;
                    }
                    if (!args.loadComplete) {
                        this_1.requestedInfo.push({ deferred: deferred, indexes: args.indexes, isNotLoaded: loadedInfo.isNotLoaded });
                    }
                    if (sRange >= 0 && loadedInfo.isNotLoaded && !isEndReached) {
                        sRanges[k] = sRange;
                        requestedRange[k] = false;
                        var fieldsOrder_1;
                        if (range.fieldsOrder) {
                            fieldsOrder_1 = [].slice.call(range.fieldsOrder);
                        }
                        var query = (range.query ? range.query : new ej2_data_1.Query()).clone();
                        if (!args.loadFullData) {
                            query = query.range(sRange, eRange >= count ? eRange : eRange + 1);
                        }
                        dataManager.executeQuery(query.requiresCount()).then(function (e) {
                            if (!_this.parent || _this.parent.isDestroyed) {
                                return;
                            }
                            result = (e.result && e.result.result ? e.result.result : e.result);
                            sCellIdx = index_2.getRangeIndexes(range.startCell);
                            sRowIdx = sCellIdx[0];
                            sColIdx = sCellIdx[1];
                            if (result && result.length) {
                                if (!range.info.count) {
                                    count = e.count;
                                    range.info.count = e.count;
                                }
                                flds = range.info.flds || fieldsOrder_1 || Object.keys(result[0]);
                                if (!range.info.fldLen) {
                                    range.info.fldLen = flds.length;
                                    range.info.flds = flds;
                                }
                                Iif (range.info.insertColumnRange) {
                                    var insertCount_1 = 0;
                                    range.info.insertColumnRange.forEach(function (insertRange) {
                                        for (var i = insertRange[0]; i <= insertRange[1]; i++) {
                                            if (i <= sColIdx) {
                                                flds.splice(0, 0, "emptyCell" + insertCount_1);
                                            }
                                            else {
                                                flds.splice(i - sColIdx, 0, "emptyCell" + insertCount_1);
                                            }
                                            insertCount_1++;
                                        }
                                    });
                                }
                                if (sRanges[k] === 0 && range.showFieldAsHeader) {
                                    rowIdx = sRowIdx + sRanges[k] + insertRowCount;
                                    flds.forEach(function (field, i) {
                                        cell = index_1.getCell(rowIdx, sColIdx + i, args.sheet, true);
                                        if (!cell) {
                                            args.sheet.rows[sRowIdx + sRanges[k]].cells[sColIdx + i] = field.includes('emptyCell') ? {}
                                                : { value: field };
                                        }
                                        else Eif (!field.includes('emptyCell')) {
                                            cell.value = field;
                                        }
                                    });
                                }
                                result.forEach(function (item, i) {
                                    rowIdx = sRowIdx + sRanges[k] + i + (range.showFieldAsHeader ? 1 : 0) + insertRowCount;
                                    flds.forEach(function (field, idx) {
                                        colIdx = sColIdx + idx;
                                        cell = index_1.getCell(rowIdx, colIdx, args.sheet, true);
                                        if (cell) {
                                            prevVal_1 = cell.value;
                                            Eif (!field.includes('emptyCell')) {
                                                index_1.setCell(rowIdx, colIdx, args.sheet, _this.getCellDataFromProp(item[field]), true);
                                            }
                                        }
                                        else {
                                            prevVal_1 = undefined;
                                            cell = args.sheet.rows[rowIdx].cells[colIdx] =
                                                field.includes('emptyCell') ? {} : _this.getCellDataFromProp(item[field]);
                                        }
                                        if (autoDetectFormat) {
                                            autoDetectFormatFn(cell);
                                        }
                                        if (args.updateDependentCellsCallback && prevVal_1 !== cell.value) {
                                            args.updateDependentCellsCallback(rowIdx, colIdx);
                                        }
                                    });
                                });
                            }
                            else {
                                flds = [];
                            }
                            var totalRows;
                            if ((sRowIdx + (count || e.count)) > 0) {
                                totalRows = (sRowIdx + (count || e.count) + (range.showFieldAsHeader ? 1 : 0) + insertRowCount) - 1;
                            }
                            else {
                                totalRows = args.sheet.usedRange.rowIndex;
                            }
                            var totalCols = sColIdx + flds.length - 1 < 0 ? args.sheet.usedRange.colIndex : sColIdx + flds.length - 1;
                            if (args.loadFullData) {
                                eRange = totalRows;
                            }
                            var usedRange = { rowIndex: totalRows, colIndex: totalCols };
                            if (_this.parent.scrollSettings && _this.parent.scrollSettings.isFinite) {
                                usedRange.rowIndex = totalRows < args.sheet.rowCount ? totalRows : args.sheet.rowCount - 1;
                                usedRange.colIndex = totalCols < args.sheet.colCount ? totalCols : args.sheet.colCount - 1;
                            }
                            if (args.sheet.usedRange.rowIndex < usedRange.rowIndex) {
                                _this.parent.setSheetPropertyOnMute(args.sheet, 'usedRange', { rowIndex: usedRange.rowIndex, colIndex: args.sheet.usedRange.colIndex });
                            }
                            if (args.sheet.usedRange.colIndex < usedRange.colIndex) {
                                _this.parent.setSheetPropertyOnMute(args.sheet, 'usedRange', { rowIndex: args.sheet.usedRange.rowIndex, colIndex: usedRange.colIndex });
                            }
                            Iif (insertRowCount) {
                                loadedInfo = _this.getLoadedInfo(sRange, eRange, range);
                                sRange = loadedInfo.unloadedRange[0];
                                eRange = loadedInfo.unloadedRange[1];
                                if (sRange > count) {
                                    loadedInfo.isNotLoaded = false;
                                }
                                if (loadedInfo.isNotLoaded) {
                                    if (eRange > count) {
                                        eRange = count;
                                    }
                                    range.info.loadedRange.push([sRange, eRange]);
                                }
                            }
                            else {
                                range.info.loadedRange.push([sRange, eRange]);
                            }
                            requestedRange[k] = true;
                            if (requestedRange.indexOf(false) === -1) {
                                var dataLoading = void 0;
                                if (eRange + sRowIdx < sRowIdx + range.info.count) {
                                    if (!args.rangeSettingCount) {
                                        args.rangeSettingCount = [];
                                    }
                                    args.rangeSettingCount.push(k);
                                    dataLoading = true;
                                    var unloadedArgs = {
                                        sheet: args.sheet, indexes: [0, 0, totalRows, totalCols],
                                        promise: new Promise(function (resolve) { resolve((function () { })()); }),
                                        rangeSettingCount: args.rangeSettingCount, loadComplete: args.loadComplete,
                                        autoDetectFormat: args.autoDetectFormat, resolveAfterFullDataLoaded: args.resolveAfterFullDataLoaded
                                    };
                                    _this.updateSheetFromDataSourceHandler(unloadedArgs);
                                    unloadedArgs.promise.then(function () {
                                        Iif (_this.parent.getModuleName() === 'workbook') {
                                            return;
                                        }
                                        args.rangeSettingCount.pop();
                                        if (!args.rangeSettingCount.length) {
                                            _this.parent.notify('created', null);
                                        }
                                        if (args.formulaCellRef) {
                                            _this.notfyFormulaCellRefresh(args.formulaCellRef, args.sheetIndex);
                                        }
                                        else Iif (args.loadComplete) {
                                            args.loadComplete();
                                        }
                                    });
                                }
                                else Iif (args.formulaCellRef) {
                                    _this.notfyFormulaCellRefresh(args.formulaCellRef, args.sheetIndex);
                                }
                                else if (args.loadComplete) {
                                    args.loadComplete();
                                }
                                if (!(dataLoading && args.resolveAfterFullDataLoaded)) {
                                    _this.checkResolve(args.indexes);
                                    if (args.resolveAfterFullDataLoaded) {
                                        _this.checkResolve([0, 0, 0, 0]);
                                    }
                                }
                            }
                        });
                    }
                    else if (k === 0 && requestedRange.indexOf(false) === -1) {
                        if (args.loadComplete) {
                            args.loadComplete();
                        }
                        this_1.checkResolve(args.indexes);
                    }
                };
                var this_1 = this;
                for (var k = args.sheet.ranges.length - 1; k >= 0; k--) {
                    _loop_1(k);
                }
            }
            else {
                deferred.resolve();
            }
        };
        DataBind.prototype.notfyFormulaCellRefresh = function (formulaCellRef, sheetIndex) {
            this.parent.formulaRefCell = null;
            this.parent.notify(index_4.updateView, { indexes: index_2.getRangeIndexes(formulaCellRef), sheetIndex: sheetIndex, refreshing: true });
        };
        DataBind.prototype.checkResolve = function (indexes) {
            var resolved;
            var isSameRng;
            var cnt = 0;
            this.requestedInfo.forEach(function (info, idx) {
                isSameRng = JSON.stringify(info.indexes) === JSON.stringify(indexes);
                if (isSameRng || resolved) {
                    if (idx === 0) {
                        info.deferred.resolve();
                        cnt++;
                        resolved = true;
                    }
                    else {
                        if (resolved && (info.isLoaded || !info.isNotLoaded)) {
                            info.deferred.resolve();
                            cnt++;
                        }
                        else if (isSameRng && resolved) {
                            info.deferred.resolve();
                            cnt++;
                        }
                        else if (isSameRng) {
                            info.isLoaded = true;
                        }
                        else {
                            resolved = false;
                        }
                    }
                }
            });
            this.requestedInfo.splice(0, cnt);
        };
        DataBind.prototype.getCellDataFromProp = function (prop) {
            var data = {};
            if (Object.prototype.toString.call(prop) === '[object Object]') {
                if (prop.formula) {
                    data.formula = prop.formula;
                }
                else if (!ej2_base_1.isNullOrUndefined(prop.value)) {
                    if (typeof (prop.value) === 'string') {
                        Iif (prop.value.indexOf('http://') === 0 || prop.value.indexOf('https://') === 0 ||
                            prop.value.indexOf('ftp://') === 0 || prop.value.indexOf('www.') === 0) {
                            data.hyperlink = prop.value;
                            data.style = { textDecoration: 'underline', color: '#00e' };
                        }
                        else {
                            data.value = prop.value;
                        }
                    }
                    else {
                        data.value = prop.value;
                    }
                }
            }
            else {
                if (index_2.checkIsFormula(prop)) {
                    data.formula = prop;
                }
                else {
                    if (typeof (prop) === 'string') {
                        if (prop.indexOf('http://') === 0 || prop.indexOf('https://') === 0 ||
                            prop.indexOf('ftp://') === 0 || prop.indexOf('www.') === 0) {
                            data.hyperlink = prop;
                            data.style = { textDecoration: 'underline', color: '#00e' };
                        }
                        else {
                            data.value = prop;
                        }
                    }
                    else {
                        data.value = prop;
                    }
                }
            }
            return data;
        };
        DataBind.prototype.getLoadedInfo = function (sRange, eRange, range) {
            var isNotLoaded = true;
            range.info.loadedRange.forEach(function (range) {
                if (range[0] <= sRange && sRange <= range[1]) {
                    if (range[0] <= eRange && eRange <= range[1]) {
                        isNotLoaded = false;
                    }
                    else {
                        sRange = range[1] + 1;
                    }
                }
                else if (range[0] <= eRange && eRange <= range[1]) {
                    eRange = range[0] - 1;
                }
            });
            return { isNotLoaded: isNotLoaded, unloadedRange: [sRange, eRange] };
        };
        DataBind.prototype.getMaxCount = function (range) {
            Iif (range.query) {
                var query = range.query.queries;
                for (var i = 0; i < query.length; i++) {
                    if (query[i].fn === 'onTake') {
                        return Math.min(query[i].e.nos, range.info.count || query[i].e.nos);
                    }
                }
            }
            return range.info.count;
        };
        DataBind.prototype.initRangeInfo = function (range) {
            if (!range.info) {
                range.info = { loadedRange: [] };
            }
        };
        DataBind.prototype.dataSourceChangedHandler = function (args) {
            var _this = this;
            var row;
            var sheet = this.parent.sheets[args.sheetIdx];
            var range = sheet.ranges[args.rangeIdx];
            Eif (range && (this.checkRangeHasChanges(sheet, args.rangeIdx) || !range.info)) {
                var showFieldAsHeader_1 = range.showFieldAsHeader;
                var indexes_1 = index_3.getCellIndexes(range.startCell);
                if (range.info) {
                    range.info.loadedRange.forEach(function (loadedRange) {
                        for (var i = loadedRange[0]; i <= loadedRange[1] && (i < range.info.count + (showFieldAsHeader_1 ? 1 : 0)); i++) {
                            row = sheet.rows[i + indexes_1[0]];
                            Eif (row) {
                                for (var j = indexes_1[1]; j < indexes_1[1] + range.info.fldLen; j++) {
                                    if (row.cells && row.cells[j]) {
                                        delete row.cells[j];
                                    }
                                }
                            }
                        }
                    });
                    range.info = null;
                }
                var evtArgs = {
                    sheet: sheet, indexes: [0, 0, sheet.rowCount - 1, sheet.colCount - 1], loadFullData: true,
                    promise: new Promise(function (resolve) { resolve((function () { })()); })
                };
                this.updateSheetFromDataSourceHandler(evtArgs);
                evtArgs.promise.then(function () {
                    _this.parent.trigger('dataSourceChanged', { data: args.changedData, action: 'dataSourceChanged', rangeIndex: args.rangeIdx,
                        sheetIndex: args.sheetIdx });
                    _this.parent.notify(index_4.updateView, { sheetIndex: args.sheetIdx, checkWrap: true, checkCF: true });
                });
            }
        };
        DataBind.prototype.checkRangeHasChanges = function (sheet, rangeIdx) {
            Iif (this.parent.isAngular) {
                if (sheet['changedRangeIdx'] === rangeIdx) {
                    delete sheet['changedRangeIdx'];
                    return true;
                }
                return false;
            }
            else {
                return true;
            }
        };
        DataBind.prototype.dataChangedHandler = function (args) {
            var _this = this;
            var changedData = [{}];
            var action;
            var cell;
            var dataRange;
            var startCell;
            var inRange;
            var inRangeCut;
            var deleteRowDetails;
            var sheetIdx = args.sheetIdx === undefined ? this.parent.activeSheetIndex : args.sheetIdx;
            var sheet = this.parent.sheets[sheetIdx];
            var cellIndices;
            var cutIndices;
            sheet.ranges.forEach(function (range, idx) {
                if (range.dataSource) {
                    var isNewRow = void 0;
                    startCell = index_3.getCellIndexes(range.startCell);
                    dataRange = startCell.concat([startCell[0] + range.info.count + (range.showFieldAsHeader ? 0 : -1),
                        startCell[1] + range.info.fldLen - 1]);
                    if (args.modelType === 'Row' || args.modelType === 'Column') {
                        if (args.modelType === 'Column') {
                            if (args.insertType) {
                                inRange = dataRange[1] < args.index && dataRange[3] >= args.index;
                                cellIndices = [args.index];
                                if (!inRange) {
                                    if ((dataRange[3] + 1 === args.index && args.insertType === 'after')) {
                                        args.model.forEach(function () {
                                            range.info.flds.splice(args.index - startCell[1], 0, '');
                                        });
                                        range.info.fldLen += args.model.length;
                                    }
                                    else if (dataRange[1] >= args.index) {
                                        range.startCell = index_3.getCellAddress(startCell[0], startCell[1] + args.model.length);
                                    }
                                }
                                else {
                                    args.model.forEach(function () {
                                        range.info.flds.splice(args.index - startCell[1], 0, '');
                                    });
                                    range.info.fldLen += args.model.length;
                                }
                            }
                            else {
                                inRange = dataRange[1] <= args.startIndex && dataRange[3] >= args.startIndex;
                                if (inRange) {
                                    for (var i = args.startIndex; i <= args.endIndex; i++) {
                                        if (i <= dataRange[3]) {
                                            range.info.flds.splice(args.startIndex, 1);
                                            range.info.fldLen -= 1;
                                        }
                                    }
                                }
                            }
                            return;
                        }
                        else {
                            if (args.insertType) {
                                inRange = ((!range.showFieldAsHeader && (args.insertType === 'above' || args.isMethod)) ? dataRange[0] <=
                                    args.index : dataRange[0] < args.index) && dataRange[2] >= args.index;
                                cellIndices = [args.index];
                                if (!inRange) {
                                    if ((dataRange[2] + 1 === args.index && args.insertType === 'below')) {
                                        isNewRow = true;
                                        range.info.count += args.model.length;
                                    }
                                    else if (dataRange[0] >= args.index) {
                                        range.startCell = index_3.getCellAddress(startCell[0] + args.model.length, startCell[1]);
                                    }
                                }
                                else {
                                    isNewRow = true;
                                    range.info.count += args.model.length;
                                }
                                if (args.isMethod) {
                                    return;
                                }
                            }
                            else {
                                inRange = dataRange[0] <= args.startIndex && dataRange[2] >= args.startIndex;
                                if (args.isDataRequest) {
                                    cellIndices = [args.startIndex, dataRange[1], args.startIndex, dataRange[1]];
                                }
                                else {
                                    action = 'delete';
                                }
                            }
                        }
                    }
                    else {
                        var argsAddress = args.address || args.range || args.fillRange;
                        cellIndices = index_2.getRangeIndexes(args.requestType && args.requestType.toLowerCase().includes('paste') ? args.pastedRange.substring(args.pastedRange.lastIndexOf('!') + 1) :
                            args.sheetIdx > -1 ? args.address : argsAddress.substring(argsAddress.lastIndexOf('!') + 1));
                        var dataRangeIndices = [range.showFieldAsHeader ? dataRange[0] + 1 : dataRange[0]].concat(dataRange.slice(1, 4));
                        if (range.showFieldAsHeader && cellIndices[0] === startCell[0]) {
                            for (var i = cellIndices[1]; i <= cellIndices[3]; i++) {
                                if (i >= dataRangeIndices[1] && i <= dataRangeIndices[3]) {
                                    range.info.flds[i - startCell[1]] = index_1.getCell(startCell[0], i, sheet, false, true).value || '';
                                }
                            }
                        }
                        inRange = index_3.isInRange(dataRangeIndices, cellIndices, true);
                        if (args.requestType === 'paste' && args.copiedInfo.isCut) {
                            cutIndices = [].slice.call(args.copiedInfo.range);
                            if (range.showFieldAsHeader && cutIndices[0] === startCell[0]) {
                                for (var i = cutIndices[1]; i <= cutIndices[3]; i++) {
                                    if (i >= dataRangeIndices[1] && i <= dataRangeIndices[3]) {
                                        range.info.flds[i - startCell[1]] = '';
                                    }
                                }
                                inRange = false;
                            }
                            inRangeCut = index_3.isInRange(dataRangeIndices, cutIndices, true);
                        }
                    }
                    if (inRange || isNewRow || inRangeCut) {
                        if (args.modelType === 'Row' && !args.insertType && !args.isDataRequest) {
                            args.deletedModel.forEach(function (row, rowIdx) {
                                changedData[rowIdx] = {};
                                range.info.flds.forEach(function (fld, idx) {
                                    if (row.cells) {
                                        cell = row.cells[startCell[1] + idx];
                                        changedData[rowIdx]["" + fld] = _this.getFormattedValue(cell);
                                    }
                                    else {
                                        changedData[rowIdx]["" + fld] = null;
                                    }
                                });
                                range.info.count -= 1;
                            });
                            if (args.isMethod) {
                                return;
                            }
                            deleteRowDetails = { count: args.deletedModel.length, index: args.endIndex };
                        }
                        else {
                            action = isNewRow ? 'add' : 'edit';
                            var addedCutData_1 = 0;
                            if (inRangeCut) {
                                addedCutData_1 = cutIndices[2] - cutIndices[0] + 1;
                                var _loop_2 = function (i) {
                                    changedData[i] = {};
                                    range.info.flds.forEach(function (fld, idx) {
                                        if (fld) {
                                            cell = index_1.getCell(cutIndices[0] + i, startCell[1] + idx, sheet);
                                            changedData[i]["" + fld] = _this.getFormattedValue(cell);
                                        }
                                    });
                                };
                                for (var i = 0; i < addedCutData_1; i++) {
                                    _loop_2(i);
                                }
                            }
                            if (inRange || isNewRow) {
                                var filterCheck = !args.isDataRequest && !inRangeCut && !isNewRow && !args.skipFilterCheck;
                                var _loop_3 = function (i, count) {
                                    if (filterCheck && index_1.isFilterHidden(sheet, cellIndices[0] + i)) {
                                        return out_count_1 = count, "continue";
                                    }
                                    changedData[count + addedCutData_1] = {};
                                    range.info.flds.forEach(function (fld, idx) {
                                        if (fld) {
                                            cell = index_1.getCell(cellIndices[0] + i, startCell[1] + idx, sheet);
                                            changedData[count + addedCutData_1]["" + fld] = _this.getFormattedValue(cell);
                                        }
                                    });
                                    count++;
                                    out_count_1 = count;
                                };
                                var out_count_1;
                                for (var i = 0, count = 0; i < (isNewRow ? args.model.length : (cellIndices[2] - cellIndices[0])
                                    + 1 || 1); i++) {
                                    _loop_3(i, count);
                                    count = out_count_1;
                                }
                            }
                        }
                        if (args.isDataRequest) {
                            args.data = changedData;
                        }
                        else {
                            _this.parent.trigger('dataSourceChanged', { data: changedData, action: action, rangeIndex: idx, sheetIndex: sheetIdx });
                        }
                    }
                    else if (deleteRowDetails && deleteRowDetails.count && dataRange[0] > deleteRowDetails.index) {
                        range.startCell = index_3.getCellAddress(startCell[0] - deleteRowDetails.count, startCell[1]);
                    }
                }
            });
        };
        DataBind.prototype.getFormattedValue = function (cell) {
            var value = this.parent.getDisplayText(cell);
            if (value === '') {
                return null;
            }
            else if (cell && !cell.format && typeof cell.value === 'number') {
                return cell.value;
            }
            return value;
        };
        DataBind.prototype.triggerDataChangeHandler = function (args) {
            var dataChangingActions = ['insert', 'delete', 'edit', 'cellDelete', 'cellSave', 'autofill', 'clipboard', 'clear'];
            var triggerDataChange = true;
            if ((args.action === 'delete' || args.action === 'insert') && ['Sheet'].indexOf(args.eventArgs.modelType) > -1) {
                triggerDataChange = false;
            }
            else if (args.action === 'clear' && ['Clear Formats', 'Clear Hyperlinks'].indexOf(args.eventArgs.type) > -1) {
                triggerDataChange = false;
            }
            else Iif (args.action === 'clipboard' && args.eventArgs.requestType === 'Formats') {
                triggerDataChange = false;
            }
            if (triggerDataChange && dataChangingActions.indexOf(args.action) > -1) {
                var evtArgs = void 0;
                if (args.isUndo && (args.action === 'delete' || args.action === 'insert')) {
                    evtArgs = ej2_base_1.extend({}, args.eventArgs);
                    if (args.action === 'delete') {
                        evtArgs.index = evtArgs.startIndex;
                        evtArgs.model = evtArgs.deletedModel;
                        evtArgs.insertType = 'below';
                    }
                    else {
                        evtArgs.startIndex = evtArgs.index;
                        evtArgs.endIndex = args.eventArgs.index + args.eventArgs.model.length - 1;
                        evtArgs.deletedModel = evtArgs.model;
                        delete evtArgs.insertType;
                    }
                }
                else {
                    evtArgs = args.eventArgs;
                }
                this.parent.notify(index_3.dataChanged, evtArgs);
            }
        };
        DataBind.prototype.getModuleName = function () {
            return 'dataBind';
        };
        DataBind.prototype.destroy = function () {
            this.removeEventListener();
            this.parent = null;
            this.requestedInfo = [];
        };
        return DataBind;
    }());
    exports.DataBind = DataBind;
});