all files / workbook/actions/ data-validation.js

95.11% Statements 214/225
86.08% Branches 167/194
100% Functions 17/17
95.11% Lines 214/225
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   884× 884×   883× 883×   884× 884× 884× 884× 884×   883× 13× 13× 13× 13× 13×     147× 147× 147× 147× 63× 63× 63×   147× 147× 147× 63× 63× 63×     84× 84×   147× 147× 147× 147× 147×   147× 147× 147× 147× 147× 147×   147× 54× 54×     147× 147× 147× 259× 14× 14×   51× 51×             13× 13× 13×   13×     245× 1356× 277× 49× 49×     277× 277× 14×   14× 14× 14×                   1079× 1079× 1079× 50×         147× 147× 259×   147× 22× 22× 22×     20× 20×     22×     504×   21×   525× 525× 525× 525× 525×   521×     525×   525× 525× 525× 525× 525× 525× 525× 525× 525× 525× 525× 525× 525× 525× 8420× 8153× 8153× 8153× 8339× 8339× 8235× 8235× 8235× 7748× 7748×     7748× 138× 101×       7610×     487× 45× 45×   44×         44× 44×     8234× 8234× 8234× 7792× 7792× 7792× 7792× 694× 694×                     405× 40×   365× 365× 365× 441× 441× 7411× 7135× 65627× 65627× 691× 691× 691× 691× 691× 509×               182×         691× 534× 534× 534×   691×         691× 509×               672× 672× 672× 18× 18× 18× 18×   654×         672×   509× 509× 126× 20× 36×       383× 383× 41× 49×         126650×        
define(["require", "exports", "../base/index", "../common/index", "../common/index", "../common/index", "../common/index", "@syncfusion/ej2-base"], function (require, exports, index_1, index_2, index_3, index_4, index_5, ej2_base_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var WorkbookDataValidation = (function () {
        function WorkbookDataValidation(parent) {
            this.parent = parent;
            this.addEventListener();
        }
        WorkbookDataValidation.prototype.destroy = function () {
            this.removeEventListener();
            this.parent = null;
        };
        WorkbookDataValidation.prototype.addEventListener = function () {
            this.parent.on(index_2.cellValidation, this.validationHandler, this);
            this.parent.on(index_2.addHighlight, this.addHighlightHandler, this);
            this.parent.on(index_3.removeHighlight, this.removeHighlightHandler, this);
            this.parent.on(index_5.beforeInsert, this.beforeInsertDeleteHandler, this);
            this.parent.on(index_5.beforeDelete, this.beforeInsertDeleteHandler, this);
        };
        WorkbookDataValidation.prototype.removeEventListener = function () {
            if (!this.parent.isDestroyed) {
                this.parent.off(index_2.cellValidation, this.validationHandler);
                this.parent.off(index_2.addHighlight, this.addHighlightHandler);
                this.parent.off(index_3.removeHighlight, this.removeHighlightHandler);
                this.parent.off(index_5.beforeInsert, this.beforeInsertDeleteHandler);
                this.parent.off(index_5.beforeDelete, this.beforeInsertDeleteHandler);
            }
        };
        WorkbookDataValidation.prototype.validationHandler = function (args) {
            var _this = this;
            var onlyRange = args.range;
            var sheetName = '';
            if (args.range.lastIndexOf('!') > -1) {
                var lastIndex = args.range.lastIndexOf('!');
                onlyRange = args.range.substring(lastIndex + 1);
                sheetName = args.range.substring(0, lastIndex);
            }
            var sheet;
            var isActiveSheet;
            if (sheetName) {
                var sheetIdx = index_1.getSheetIndex(this.parent, sheetName);
                sheet = index_1.getSheet(this.parent, sheetIdx);
                isActiveSheet = sheetIdx === this.parent.activeSheetIndex;
            }
            else {
                sheet = this.parent.getActiveSheet();
                isActiveSheet = true;
            }
            this.parent.dataValidationRange = (this.parent.dataValidationRange.indexOf('!') > -1 ? '' : sheet.name + '!') + this.parent.dataValidationRange + onlyRange + ',';
            var rangeInfo = this.getRangeWhenColumnSelected(onlyRange, sheet);
            onlyRange = rangeInfo.range;
            Eif (!ej2_base_1.isNullOrUndefined(sheetName)) {
                args.range = sheetName + '!' + onlyRange;
            }
            args.range = args.range || sheet.selectedRange;
            var indexes = index_2.getSwapRange(index_4.getRangeIndexes(args.range));
            var cell;
            var column;
            var viewportIndexes;
            if (args.viewport && rangeInfo.isFullCol) {
                viewportIndexes = index_2.getViewportIndexes(this.parent, args.viewport);
            }
            if (!args.isRemoveValidation && args.rules.type === 'List' && args.rules.value1) {
                args.rules.value1 = args.rules.value1.trim();
                if (args.rules.value1[args.rules.value1.length - 1] === this.parent.listSeparator) {
                    args.rules.value1 = args.rules.value1.substring(0, args.rules.value1.length - 1);
                }
            }
            var activeCellIndex = index_2.getSwapRange(index_4.getRangeIndexes(sheet.activeCell));
            var isListValidation;
            var _loop_1 = function (colIdx) {
                if (rangeInfo.isFullCol) {
                    column = index_1.getColumn(sheet, colIdx);
                    if (args.isRemoveValidation) {
                        Eif (column && column.validation) {
                            Eif (colIdx === activeCellIndex[1]) {
                                isListValidation = column.validation.type === 'List';
                            }
                            delete column.validation;
                            Eif (viewportIndexes) {
                                viewportIndexes.forEach(function (viewportIndex) {
                                    for (var rowIdx = viewportIndex[0]; rowIdx <= viewportIndex[2]; rowIdx++) {
                                        cell = index_1.getCell(rowIdx, colIdx, sheet);
                                        _this.parent.notify(index_2.applyCellFormat, { rowIdx: rowIdx, colIdx: colIdx, style: _this.parent.getCellStyleValue(['backgroundColor', 'color'], [rowIdx, colIdx]) });
                                    }
                                });
                            }
                            return "continue";
                        }
                    }
                    else {
                        column = index_1.getColumn(sheet, colIdx);
                        column.validation = { operator: args.rules.operator, type: args.rules.type, value1: args.rules.value1, value2: args.rules.value2, inCellDropDown: args.rules.inCellDropDown, ignoreBlank: args.rules.ignoreBlank };
                        if (colIdx === activeCellIndex[1]) {
                            isListValidation = column.validation.type === 'List';
                        }
                        return "continue";
                    }
                }
                for (var rowIdx = indexes[0]; rowIdx <= indexes[2]; rowIdx++) {
                    if (args.isRemoveValidation) {
                        if (rowIdx === indexes[2]) {
                            column = index_1.getColumn(sheet, colIdx);
                            if (column && column.validation) {
                                column.validation.address = index_2.getSplittedAddressForColumn(column.validation.address, [indexes[0], colIdx, indexes[2], colIdx], colIdx);
                            }
                        }
                        cell = index_1.getCell(rowIdx, colIdx, sheet);
                        if (cell && cell.validation) {
                            if (rowIdx === activeCellIndex[0] && colIdx === activeCellIndex[1]) {
                                isListValidation = cell.validation.type === 'List';
                            }
                            Eif (!index_5.updateCell(this_1.parent, sheet, { cell: { validation: {} }, rowIdx: rowIdx, colIdx: colIdx })) {
                                delete cell.validation;
                                this_1.parent.notify(index_2.applyCellFormat, {
                                    rowIdx: rowIdx, colIdx: colIdx, style: this_1.parent.getCellStyleValue(['backgroundColor', 'color'], [rowIdx, colIdx])
                                });
                            }
                            else {
                                isListValidation = false;
                            }
                        }
                    }
                    else {
                        cell = { validation: Object.assign({}, args.rules) };
                        index_5.updateCell(this_1.parent, sheet, { cell: cell, rowIdx: rowIdx, colIdx: colIdx });
                        if (rowIdx === activeCellIndex[0] && colIdx === activeCellIndex[1]) {
                            isListValidation = cell.validation.type === 'List';
                        }
                    }
                }
            };
            var this_1 = this;
            for (var colIdx = indexes[1]; colIdx <= indexes[3]; colIdx++) {
                _loop_1(colIdx);
            }
            if (isActiveSheet && isListValidation) {
                var validation = void 0;
                var cell_1 = index_1.getCell(activeCellIndex[0], activeCellIndex[1], sheet, false, true);
                if (args.isRemoveValidation) {
                    validation = {};
                }
                else {
                    validation = cell_1.validation;
                    if (!validation) {
                        column = index_1.getColumn(sheet, activeCellIndex[1]);
                        validation = (column && column.validation) || {};
                    }
                }
                this.parent.notify(index_5.addListValidationDropdown, { validation: validation, cell: cell_1, rowIdx: activeCellIndex[0], colIdx: activeCellIndex[1], isRefresh: true });
            }
        };
        WorkbookDataValidation.prototype.addHighlightHandler = function (args) {
            this.invalidDataHandler(args.range, false, args.td, args.isclearFormat);
        };
        WorkbookDataValidation.prototype.removeHighlightHandler = function (args) {
            this.invalidDataHandler(args.range, true);
        };
        WorkbookDataValidation.prototype.getRange = function (range) {
            var indexes = index_4.getRangeIndexes(range);
            var sheet = this.parent.getActiveSheet();
            var maxColCount = sheet.colCount;
            var maxRowCount = sheet.rowCount;
            if (indexes[2] === maxRowCount - 1 && indexes[0] === 0) {
                range = range.replace(/[0-9]/g, '');
            }
            else Iif (indexes[3] === maxColCount - 1 && indexes[2] === 0) {
                range = range.replace(/\D/g, '');
            }
            return range;
        };
        WorkbookDataValidation.prototype.invalidDataHandler = function (range, isRemoveHighlightedData, td, isclearFormat) {
            var cell;
            var value;
            var sheetIdx = range ? index_2.getSheetIndexFromAddress(this.parent, range) : this.parent.activeSheetIndex;
            var sheet = index_1.getSheet(this.parent, sheetIdx);
            range = range || sheet.selectedRange;
            var sheetName = range.includes('!') ? index_1.getSheetNameFromAddress(range) : sheet.name;
            var rangeInfo = this.getRangeWhenColumnSelected(index_2.getRangeFromAddress(range), sheet);
            var isFullCol = rangeInfo.isFullCol;
            range = sheetName + '!' + rangeInfo.range;
            var indexes = range ? index_2.getSwapRange(index_4.getRangeIndexes(range)) : [];
            range = this.getRange(range);
            var rowIdx = range ? indexes[0] : 0;
            var lastRowIdx = range ? indexes[2] : sheet.rows.length;
            for (rowIdx; rowIdx <= lastRowIdx; rowIdx++) {
                if (sheet.rows[rowIdx]) {
                    var colIdx = range ? indexes[1] : 0;
                    var lastColIdx = range ? indexes[3] : sheet.rows[rowIdx].cells.length;
                    for (colIdx; colIdx <= lastColIdx; colIdx++) {
                        var validation = void 0;
                        if (sheet.rows[rowIdx].cells && sheet.rows[rowIdx].cells[colIdx]) {
                            var column = index_1.getColumn(sheet, colIdx);
                            cell = sheet.rows[rowIdx].cells[colIdx];
                            if (cell && cell.validation) {
                                validation = cell.validation;
                                Iif (isclearFormat && !validation.isHighlighted) {
                                    return;
                                }
                                if (isRemoveHighlightedData) {
                                    if (validation.isHighlighted) {
                                        cell.validation.isHighlighted = false;
                                    }
                                }
                                else {
                                    cell.validation.isHighlighted = true;
                                }
                            }
                            else if (column && column.validation) {
                                validation = column.validation;
                                if (isclearFormat && !validation.isHighlighted) {
                                    return;
                                }
                                Iif (isRemoveHighlightedData && isFullCol) {
                                    if (validation.isHighlighted) {
                                        column.validation.isHighlighted = false;
                                    }
                                }
                                else Eif (isFullCol) {
                                    column.validation.isHighlighted = true;
                                }
                            }
                            value = cell.value ? cell.value : '';
                            var range_1 = [rowIdx, colIdx];
                            if (validation && this.parent.allowDataValidation) {
                                var validEventArgs = { value: value, range: range_1, sheetIdx: sheetIdx, td: td, isValid: true };
                                this.parent.notify(index_2.isValidation, validEventArgs);
                                var isValid = validEventArgs.isValid;
                                if (!isValid) {
                                    Eif (!index_1.isHiddenRow(sheet, rowIdx) && sheetIdx === this.parent.activeSheetIndex) {
                                        this.parent.notify(index_2.validationHighlight, {
                                            isRemoveHighlightedData: isRemoveHighlightedData, rowIdx: rowIdx, colIdx: colIdx, td: td
                                        });
                                    }
                                }
                            }
                        }
                    }
                }
            }
        };
        WorkbookDataValidation.prototype.beforeInsertDeleteHandler = function (args) {
            if (args.modelType === 'Sheet') {
                return;
            }
            var cell;
            var sheet;
            for (var i = 0, sheetLen = this.parent.sheets.length; i < sheetLen; i++) {
                sheet = this.parent.sheets[i];
                for (var j = 0, rowLen = sheet.rows.length; j < rowLen; j++) {
                    if (sheet.rows[j] && sheet.rows[j].cells) {
                        for (var k = 0, cellLen = sheet.rows[j].cells.length; k < cellLen; k++) {
                            cell = sheet.rows[j].cells[k];
                            if (cell && cell.validation) {
                                var isInsert = args.name === 'beforeInsert';
                                var endIndex = args.index + (args.model.length - 1);
                                var isNewlyInsertedModel = args.modelType === 'Row' ? (j >= args.index && j <= endIndex) : (k >= args.index && k <= endIndex);
                                var eventArgs = void 0;
                                if (isInsert) {
                                    eventArgs = {
                                        insertDeleteArgs: {
                                            startIndex: args.index, endIndex: args.index + args.model.length - 1, modelType: args.modelType,
                                            isInsert: true, sheet: index_1.getSheet(this.parent, args.activeSheetIndex)
                                        }, row: j, col: k, sheetIdx: i
                                    };
                                }
                                else {
                                    eventArgs = { insertDeleteArgs: {
                                            startIndex: args.start, modelType: args.modelType,
                                            endIndex: args.end, sheet: args.model
                                        }, row: j, col: k, sheetIdx: i };
                                }
                                if (index_3.checkIsFormula(cell.validation.value1) && !isNewlyInsertedModel) {
                                    eventArgs.cell = { formula: cell.validation.value1 };
                                    this.parent.notify(index_4.getUpdatedFormulaOnInsertDelete, eventArgs);
                                    cell.validation.value1 = eventArgs.cell.formula;
                                }
                                Iif (index_3.checkIsFormula(cell.validation.value2) && !isNewlyInsertedModel) {
                                    eventArgs.cell = { formula: cell.validation.value2 };
                                    this.parent.notify(index_4.getUpdatedFormulaOnInsertDelete, eventArgs);
                                    cell.validation.value2 = eventArgs.cell.formula;
                                }
                                if (args.activeSheetIndex === i && isInsert) {
                                    this.updateValidationForInsertedModel(args, sheet, j, k, cell.validation);
                                }
                            }
                        }
                    }
                }
            }
        };
        WorkbookDataValidation.prototype.getRangeWhenColumnSelected = function (range, sheet) {
            var isFullCol;
            var colNames = range.split(':');
            if (range.match(/\D/g) && !range.match(/[0-9]/g)) {
                colNames[0] += 1;
                colNames[1] += sheet.rowCount;
                range = colNames[0] + ':' + colNames[1];
                isFullCol = true;
            }
            else Iif (!range.match(/\D/g) && range.match(/[0-9]/g)) {
                colNames[0] = 'A' + colNames[0];
                colNames[1] = index_2.getCellAddress(0, sheet.colCount - 1).replace(/[0-9]/g, '') + colNames[1];
                range = colNames[0] + ':' + colNames[1];
            }
            return { range: range, isFullCol: isFullCol };
        };
        WorkbookDataValidation.prototype.updateValidationForInsertedModel = function (args, sheet, rowIndex, colIndex, validation) {
            var endIndex = args.index + (args.model.length - 1);
            if (args.modelType === 'Column') {
                if ((args.insertType === 'before' && endIndex === colIndex - 1) || (args.insertType === 'after' && args.index - 1 === colIndex)) {
                    for (var l = args.index; l <= endIndex; l++) {
                        index_1.setCell(rowIndex, l, sheet, { validation: ej2_base_1.extend({}, validation) }, true);
                    }
                }
            }
            else Eif (args.modelType === 'Row') {
                if ((args.insertType === 'above' && endIndex === rowIndex - 1) || (args.insertType === 'below' && args.index - 1 === rowIndex)) {
                    for (var l = args.index; l <= endIndex; l++) {
                        index_1.setCell(l, colIndex, sheet, { validation: ej2_base_1.extend({}, validation) }, true);
                    }
                }
            }
        };
        WorkbookDataValidation.prototype.getModuleName = function () {
            return 'workbookDataValidation';
        };
        return WorkbookDataValidation;
    }());
    exports.WorkbookDataValidation = WorkbookDataValidation;
});