all files / workbook/actions/ edit.js

99.13% Statements 114/115
97.45% Branches 153/157
100% Functions 9/9
99.13% Lines 114/115
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   1053× 1053×   1050× 1050×   1053×   1050× 13×     150870×   201221× 201221×   201221× 201221×     201221× 6643×   201221× 201221× 201221× 6615×     194606×   201221× 201221× 201221× 17397×   201221× 50723× 50723× 50723× 50723×   50723×   50723× 50723× 44632× 26×   44632×   50723×                         50723× 21980×               21980× 16135× 16135× 16135×   16041×   16135× 165×   15970× 646× 646× 83×             77×       77×       563×   77× 77×       77×               5845× 90×     50723×     50717× 50717× 50717×     61× 61×       50717× 49202× 49202×     1515×   50717× 6076× 6076×         15× 37×   15×     14×   15×         6061× 6061×     6076× 6076× 6076×   6074× 72×   6002×     44641×           150498× 150498× 1036×     201221× 201221× 10294×   201221×        
define(["require", "exports", "../base/index", "../common/event", "../common/index", "@syncfusion/ej2-base", "../../workbook/common/index", "../integrations/index"], function (require, exports, index_1, event_1, index_2, ej2_base_1, index_3, index_4) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var WorkbookEdit = (function () {
        function WorkbookEdit(workbook) {
            this.parent = workbook;
            this.addEventListener();
        }
        WorkbookEdit.prototype.destroy = function () {
            this.removeEventListener();
            this.parent = null;
        };
        WorkbookEdit.prototype.addEventListener = function () {
            this.parent.on(event_1.workbookEditOperation, this.performEditOperation, this);
        };
        WorkbookEdit.prototype.removeEventListener = function () {
            if (!this.parent.isDestroyed) {
                this.parent.off(event_1.workbookEditOperation, this.performEditOperation);
            }
        };
        WorkbookEdit.prototype.getModuleName = function () {
            return 'workbookEdit';
        };
        WorkbookEdit.prototype.performEditOperation = function (args) {
            var action = args.action;
            switch (action) {
                case 'updateCellValue':
                    args.isFormulaDependent = this.updateCellValue(args.address, args.value, args.sheetIndex, args.isValueOnly, args.skipFormatCheck, args.isRandomFormula, args.skipCellFormat, args.isDelete, args.deletedRange, args.fillType, args.cellInformation, args.isRedo, args.isDependentUpdate, args.actionName, args.isPaste);
                    break;
            }
        };
        WorkbookEdit.prototype.updateCellValue = function (address, value, sheetIdx, isValueOnly, skipFormatCheck, isRandomFormula, skipCellFormat, isDelete, deletedRange, fillType, cellInformation, isRedo, isDependentUpdate, actionName, isPaste) {
            if (sheetIdx === undefined) {
                sheetIdx = this.parent.activeSheetIndex;
            }
            var range;
            var isFormulaDependent;
            if (typeof address === 'string') {
                range = index_2.getRangeIndexes(address);
            }
            else {
                range = address;
            }
            var sheet = index_1.getSheet(this.parent, sheetIdx);
            var cell = index_1.getCell(range[0], range[1], sheet, true);
            if (!cell) {
                cell = sheet.rows[range[0]].cells[range[1]] = {};
            }
            if (!isValueOnly) {
                var isFormula = index_3.checkIsFormula(value);
                isFormula = value === '#SPILL!' ? true : isFormula;
                var skipFormula = false;
                if (cell.formula && cell.formula.indexOf('UNIQUE') > -1 && value === '') {
                    skipFormula = true;
                }
                var isNotTextFormat = index_4.getTypeFromFormat(cell.format) !== 'Text' && (!isFormula ||
                    !value.toLowerCase().startsWith('=text('));
                isFormula = index_4.getTypeFromFormat(cell.format) === 'Text' ? false : isFormula;
                if (!isFormula && !skipFormula) {
                    if (cell.formula) {
                        cell.formula = '';
                    }
                    cell.value = isNotTextFormat ? index_2.parseIntValue(value, isPaste) : value;
                }
                var eventArgs = {
                    action: 'refreshCalculate',
                    value: value,
                    rowIndex: range[0],
                    colIndex: range[1],
                    sheetIndex: sheetIdx,
                    isFormula: isFormula,
                    isRandomFormula: isRandomFormula,
                    isDelete: isDelete,
                    deletedRange: deletedRange,
                    fillType: fillType,
                    isDependentRefresh: isDependentUpdate
                };
                if (isNotTextFormat && !skipFormatCheck) {
                    var dateEventArgs = {
                        value: value,
                        rowIndex: range[0],
                        colIndex: range[1],
                        sheetIndex: sheetIdx,
                        updatedVal: '',
                        skipCellFormat: skipCellFormat
                    };
                    if (!isFormula) {
                        var cellType = index_4.getTypeFromFormat(cell.format);
                        var valArr = value ? value.toString().split('/') : [];
                        if ((cellType !== 'Number' && cellType !== 'Percentage' && cellType !== 'Fraction' && !(cellType === 'Scientific' && valArr.length === 2)) ||
                            (cellType === 'Fraction' && valArr.length !== 2)) {
                            this.parent.notify(event_1.checkDateFormat, dateEventArgs);
                        }
                        if (!ej2_base_1.isNullOrUndefined(dateEventArgs.updatedVal) && dateEventArgs.updatedVal.length > 0) {
                            cell.value = dateEventArgs.updatedVal;
                        }
                        else if (this.parent.isEdit && value && !index_2.isNumber(value)) {
                            var curSymbol = ej2_base_1.getNumberDependable(this.parent.locale, ej2_base_1.defaultCurrencyCode);
                            if (cell.format) {
                                if ((value.includes('%') || value.includes(curSymbol)) && index_1.isCustomDateTime(cell.format)) {
                                    var formatArgs = {
                                        formattedText: value, value: value, format: 'General',
                                        cell: { value: value, format: 'General' }, isEdit: true
                                    };
                                    this.parent.notify(event_1.getFormattedCellObject, formatArgs);
                                    Eif (formatArgs.format !== 'General' && ['Currency', 'Percentage'].indexOf(index_4.getTypeFromFormat(formatArgs.format)) > -1) {
                                        cell.format = formatArgs.format;
                                        cell.value = formatArgs.value;
                                    }
                                }
                                else {
                                    var evtArgs = {
                                        value: cell.value, format: cell.format, formattedText: cell.value,
                                        type: 'General', cell: cell, rowIndex: range[0], colIndex: range[1]
                                    };
                                    this.parent.notify(event_1.getFormattedCellObject, evtArgs);
                                }
                            }
                            else {
                                if (value.includes(curSymbol) || value.includes('%') ||
                                    value.includes(ej2_base_1.getNumericObject(this.parent.locale).group)) {
                                    var intl = new ej2_base_1.Internationalization();
                                    var eventArgs_1 = {
                                        intl: intl, updateValue: true, value: '', curSymbol: curSymbol,
                                        cell: cell
                                    };
                                    this.parent.notify(event_1.checkNumberFormat, {
                                        args: eventArgs_1, intl: intl, fResult: value,
                                        cell: cell
                                    });
                                }
                            }
                        }
                    }
                    else if (!ej2_base_1.isNullOrUndefined(value) && value.toLowerCase().includes('unique(') && value.length > 0) {
                        cell.value = value;
                    }
                }
                if (value === '#SPILL!') {
                    cell.value = value;
                }
                else {
                    var args = { cellIdx: range, isUnique: false };
                    this.parent.notify(event_1.checkUniqueRange, args);
                    if (this.parent.calculationMode === 'Manual' && isFormula && ej2_base_1.isNullOrUndefined(isDependentUpdate) &&
                        (actionName !== 'autofill' || cell.formula !== '') && !this.parent.isEdit &&
                        ej2_base_1.isNullOrUndefined(this.parent.element.querySelector('.e-text-replaceInp'))) {
                        skipFormula = true;
                        Iif (!isRedo && cell.value === undefined) {
                            skipFormula = false;
                        }
                    }
                    if (!skipFormula && !isDelete) {
                        this.parent.notify(event_1.workbookFormulaOperation, eventArgs);
                        isFormulaDependent = eventArgs.isFormulaDependent;
                    }
                    else {
                        value = cell.value;
                    }
                    if (isFormula) {
                        cell.formula = eventArgs.value;
                        if (this.parent.calculationMode === 'Manual' && skipFormula && isRedo && !this.parent.isEdit && cellInformation &&
                            cellInformation.cellDetails && cellInformation.cellDetails.length > 0 &&
                            cellInformation.cutCellDetails.length === 0 &&
                            (!ej2_base_1.isNullOrUndefined(cellInformation.cellDetails[0].autoFillText) ||
                                !ej2_base_1.isNullOrUndefined(cellInformation.cellDetails[0].copyCellValue))) {
                            for (var i = 0; i < cellInformation.cellDetails.length; i++) {
                                if (cellInformation.cellDetails[i].rowIndex === address[0] &&
                                    cellInformation.cellDetails[i].colIndex === address[1]) {
                                    if (cellInformation.cellDetails[i].copyCellValue) {
                                        value = cell.value = cellInformation.cellDetails[i].copyCellValue;
                                    }
                                    else {
                                        value = cell.value = cellInformation.cellDetails[i].autoFillText;
                                    }
                                    break;
                                }
                            }
                        }
                        else {
                            value = cell.value;
                            if (this.parent.calculationMode === 'Manual' && cellInformation && isRedo && cellInformation.displayText) {
                                value = cell.value = cellInformation.displayText;
                            }
                        }
                        var formula = cell.formula.toLowerCase();
                        var isNeedFormatUpdate = index_4.getTypeFromFormat(cell.format) === 'Scientific' && !skipFormatCheck && this.parent.isEdit;
                        if (formula === '=now()' && (!cell.format || cell.format === 'General' || isNeedFormatUpdate)) {
                            cell.format = index_4.getFormatFromType('ShortDate') + " h:mm";
                        }
                        else if (formula.includes('=time(') && (!cell.format || isNeedFormatUpdate)) {
                            cell.format = 'h:mm AM/PM';
                        }
                        else if (formula.includes('=date(') && isNeedFormatUpdate) {
                            cell.format = index_4.getFormatFromType('ShortDate');
                        }
                    }
                    else if (cell.value && typeof cell.value === 'string' && (cell.value.indexOf('www.') === 0 ||
                        cell.value.indexOf('https://') === 0 || cell.value.indexOf('http://') === 0 || cell.value.indexOf('ftp://') === 0)) {
                        this.parent.notify(index_2.setLinkModel, { hyperlink: cell.value, cell: sheet.name + "!" + index_2.getCellAddress(range[0], range[1]) });
                    }
                }
            }
            else {
                cell.value = value;
                if (cell.formattedText) {
                    delete cell.formattedText;
                }
            }
            this.parent.setUsedRange(range[0], range[1], sheet);
            if (this.parent.chartColl.length && !this.parent.isEdit && !isRandomFormula) {
                this.parent.notify(event_1.refreshChart, { cell: cell, rIdx: range[0], cIdx: range[1], sheetIdx: sheetIdx, isRefreshChart: true });
            }
            return isFormulaDependent;
        };
        return WorkbookEdit;
    }());
    exports.WorkbookEdit = WorkbookEdit;
});