all files / workbook/actions/ conditional-formatting.js

89.35% Statements 151/169
85.62% Branches 131/153
100% Functions 10/10
89.35% Lines 151/169
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   886× 886×   885× 885×   886× 886×   885× 13× 13×     126× 126× 126× 126× 126× 126× 21×   21× 21×   20× 20× 20× 20×       20×   125×     125× 125×       122×   125× 125× 20×       87× 13×   87× 87× 87× 13×       13× 13× 13×   13×   74× 25×   49× 49× 49× 49× 49× 49× 49× 49× 49× 49× 49× 49× 49× 49× 49× 49× 191× 191× 191× 197× 197× 195× 43× 43×     152× 152× 152× 152× 152× 152× 98×       98×   98×     54×               48× 28×                     28×                       152×     143×           54× 51×     191× 191× 54×         54× 54× 45× 45× 45×             49×     46×     49× 27×   27×   27× 14×     13×       126650×        
define(["require", "exports", "../index", "../common/index", "../common/index", "../common/index"], function (require, exports, index_1, index_2, index_3, index_4) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var WorkbookConditionalFormat = (function () {
        function WorkbookConditionalFormat(parent) {
            this.parent = parent;
            this.addEventListener();
        }
        WorkbookConditionalFormat.prototype.destroy = function () {
            this.removeEventListener();
            this.parent = null;
        };
        WorkbookConditionalFormat.prototype.addEventListener = function () {
            this.parent.on(index_2.setCFRule, this.setCFRule, this);
            this.parent.on(index_2.clearCFRule, this.clearCFRule, this);
        };
        WorkbookConditionalFormat.prototype.removeEventListener = function () {
            if (!this.parent.isDestroyed) {
                this.parent.off(index_2.setCFRule, this.setCFRule);
                this.parent.off(index_2.clearCFRule, this.clearCFRule);
            }
        };
        WorkbookConditionalFormat.prototype.setCFRule = function (e) {
            var cf = e.cfModel;
            var sheetIndex = e.sheetIdx === undefined ? index_2.getSheetIndexFromAddress(this.parent, cf.range) : e.sheetIdx;
            var sheet = index_1.getSheet(this.parent, sheetIndex);
            var indexes = index_3.getSwapRange(index_3.getRangeIndexes(cf.range || sheet.selectedRange));
            cf.range = index_2.getRangeAddress(indexes);
            if (e.isAction) {
                var eventArgs = { range: cf.range, type: cf.type, cancel: false, cFColor: cf.cFColor, value: cf.value,
                    sheetIdx: sheetIndex };
                this.parent.notify(index_4.beginAction, { eventArgs: eventArgs, action: 'conditionalFormat' });
                if (eventArgs.cancel) {
                    return;
                }
                cf.type = eventArgs.type;
                cf.cFColor = eventArgs.cFColor;
                cf.value = eventArgs.value;
                Iif (eventArgs.range !== cf.range) {
                    cf.range = eventArgs.range;
                    indexes = index_3.getSwapRange(index_3.getRangeIndexes(eventArgs.range));
                }
                delete eventArgs.cancel;
            }
            Iif (!sheet.conditionalFormats) {
                this.parent.setSheetPropertyOnMute(sheet, 'conditionalFormats', []);
            }
            sheet.conditionalFormats.push(cf);
            if (sheetIndex !== this.parent.activeSheetIndex) {
                if (e.isUndoRedo && !e.isFromUpdateAction) {
                    this.parent.notify(index_4.goto, { address: sheet.name + '!' + cf.range });
                }
            }
            else {
                this.parent.notify(index_4.applyCF, { cfModel: [cf], isAction: true });
            }
            this.parent.setUsedRange(indexes[2], indexes[3]);
            if (e.isAction) {
                this.parent.notify('actionComplete', { eventArgs: { range: cf.range, type: cf.type, cFColor: cf.cFColor, value: cf.value, sheetIdx: sheetIndex },
                    action: 'conditionalFormat' });
            }
        };
        WorkbookConditionalFormat.prototype.clearCFRule = function (args) {
            if (args.sheetIdx === undefined) {
                args.sheetIdx = this.parent.activeSheetIndex;
            }
            var sheet = index_1.getSheet(this.parent, args.sheetIdx);
            var cfRule = sheet.conditionalFormats;
            if (args.isUndo) {
                if (args.updatedCFModel) {
                    var idx_1;
                    args.updatedCFModel.forEach(function (cf) {
                        idx_1 = cfRule.indexOf(cf);
                        Eif (idx_1 > -1) {
                            cfRule.splice(idx_1, 1);
                        }
                    });
                }
                cfRule.splice.apply(cfRule, [cfRule.length, 0].concat(args.oldCFModel));
                this.parent.notify(index_4.applyCF, { cfModel: args.oldCFModel, isAction: true });
                if (args.sheetIdx !== this.parent.activeSheetIndex) {
                    this.parent.notify(index_4.goto, { address: sheet.name + '!' + args.range });
                }
                return;
            }
            if (!cfRule || !cfRule.length) {
                return;
            }
            var cf;
            var cfRange;
            var cfIdx;
            var newRange;
            var left;
            var right;
            var top;
            var bottom;
            var range;
            var idx = args.range && (typeof args.range === 'string' ? index_3.getRangeIndexes(args.range) : args.range);
            args.oldCFModel = [];
            args.updatedCFModel = [];
            var updatedCFModel = [];
            var oldRange = [];
            var refreshCF = [];
            for (var i = 0; i < cfRule.length; i++) {
                cf = cfRule[i];
                cfRange = cf.range.split(',');
                for (var j = 0; j < cfRange.length; j++) {
                    cfIdx = index_3.getRangeIndexes(cfRange[j]);
                    if (args.range) {
                        if (idx[0] <= cfIdx[0] && idx[1] <= cfIdx[1] && idx[2] >= cfIdx[2] && idx[3] >= cfIdx[3]) {
                            cfRange.splice(j, 1);
                            j--;
                        }
                        else {
                            top = idx[0] >= cfIdx[0] && idx[0] <= cfIdx[2];
                            bottom = idx[2] >= cfIdx[0] && idx[2] <= cfIdx[2];
                            left = idx[1] >= cfIdx[1] && idx[1] <= cfIdx[3];
                            right = idx[3] >= cfIdx[1] && idx[3] <= cfIdx[3];
                            newRange = [];
                            if (top && bottom) {
                                if (left || right || (idx[1] < cfIdx[1] && idx[3] > cfIdx[3])) {
                                    if (idx[0] - cfIdx[0] > 0) {
                                        newRange.push(index_2.getRangeAddress([cfIdx[0], cfIdx[1], idx[0] - 1, cfIdx[3]]));
                                    }
                                    if (cfIdx[2] - idx[2] > 0) {
                                        newRange.push(index_2.getRangeAddress([idx[2] + 1, cfIdx[1], cfIdx[2], cfIdx[3]]));
                                    }
                                }
                                if (left && idx[1] !== cfIdx[1]) {
                                    newRange.push(index_2.getRangeAddress([idx[0], cfIdx[1], idx[2], idx[1] - 1]));
                                }
                                if (right && idx[3] !== cfIdx[3]) {
                                    newRange.push(index_2.getRangeAddress([idx[0], idx[3] + 1, idx[2], cfIdx[3]]));
                                }
                            }
                            else if (left && right) {
                                if (top || bottom || (idx[0] < cfIdx[0] && idx[2] > cfIdx[2])) {
                                    if (idx[1] - cfIdx[1] > 0) {
                                        newRange.push(index_2.getRangeAddress([cfIdx[0], cfIdx[1], cfIdx[2], idx[1] - 1]));
                                    }
                                    if (cfIdx[3] - idx[3] > 0) {
                                        newRange.push(index_2.getRangeAddress([cfIdx[0], idx[3] + 1, cfIdx[2], cfIdx[3]]));
                                    }
                                }
                                if (top) {
                                    Eif (idx[0] !== cfIdx[0]) {
                                        newRange.push(index_2.getRangeAddress([cfIdx[0], idx[1], idx[0] - 1, idx[3]]));
                                    }
                                }
                                else if (bottom && idx[2] !== cfIdx[2]) {
                                    newRange.push(index_2.getRangeAddress([idx[2] + 1, idx[1], cfIdx[2], idx[3]]));
                                }
                            }
                            else if (top || bottom) {
                                Iif (left) {
                                    if (idx[1] !== cfIdx[1]) {
                                        newRange.push(index_2.getRangeAddress([cfIdx[0], cfIdx[1], cfIdx[2], idx[1] - 1]));
                                    }
                                    if (idx[0] - cfIdx[0] > 0) {
                                        newRange.push(index_2.getRangeAddress([cfIdx[0], idx[1], idx[0] - 1, cfIdx[3]]));
                                    }
                                    else if (cfIdx[2] - idx[2] > 0) {
                                        newRange.push(index_2.getRangeAddress([idx[2] + 1, idx[1], cfIdx[2], cfIdx[3]]));
                                    }
                                }
                                else Iif (right) {
                                    if (idx[3] !== cfIdx[3]) {
                                        newRange.push(index_2.getRangeAddress([cfIdx[0], idx[3] + 1, cfIdx[2], cfIdx[3]]));
                                    }
                                    if (idx[0] - cfIdx[0] > 0) {
                                        newRange.push(index_2.getRangeAddress([cfIdx[0], cfIdx[1], idx[0] - 1, idx[3]]));
                                    }
                                    else if (cfIdx[2] - idx[2] > 0) {
                                        newRange.push(index_2.getRangeAddress([idx[2] + 1, cfIdx[1], cfIdx[2], idx[3]]));
                                    }
                                }
                            }
                            if (newRange.length) {
                                cfRange[j] = newRange.join(',');
                            }
                            else {
                                continue;
                            }
                        }
                    }
                    else {
                        idx = cfIdx;
                        cfRange.splice(j, 1);
                        j--;
                    }
                    if (args.sheetIdx === this.parent.activeSheetIndex) {
                        this.parent.notify(index_4.clearCF, { indexes: idx });
                    }
                }
                range = cfRange.join(',');
                if (range !== cf.range) {
                    Iif (args.cfModel && (args.cfModel.cFColor !== cf.cFColor || args.cfModel.type !== cf.type ||
                        args.cfModel.value !== cf.value)) {
                        refreshCF.push(cf);
                        continue;
                    }
                    oldRange.push(cf.range);
                    if (!range) {
                        args.oldCFModel.push(cf);
                        updatedCFModel.concat(cfRule.splice(i, 1));
                        i--;
                    }
                    else {
                        args.oldCFModel.push({ type: cf.type, cFColor: cf.cFColor, format: cf.format, range: cf.range, value: cf.value });
                        cf.range = range;
                        Eif (cf.result) {
                            delete cf.result;
                            this.parent.notify(index_4.applyCF, { cfModel: [cf], isAction: true });
                        }
                        args.updatedCFModel.push(cf);
                        updatedCFModel.push(cf);
                    }
                }
            }
            if (args.sheetIdx !== this.parent.activeSheetIndex) {
                if (args.isUndoRedo && !args.isFromUpdateAction) {
                    this.parent.notify(index_4.goto, { address: sheet.name + '!' + args.range });
                }
            }
            else Iif (refreshCF.length) {
                this.parent.notify(index_4.applyCF, { cfModel: refreshCF, isAction: true });
            }
            if ((args.isAction || args.isClear) && args.oldCFModel.length) {
                var eventArgs = { cFormats: updatedCFModel, oldRange: oldRange,
                    previousConditionalFormats: args.oldCFModel, sheetIdx: args.sheetIdx, selectedRange: args.range };
                if (args.updatedCFModel.length) {
                    eventArgs.conditionalFormats = args.updatedCFModel;
                }
                if (args.isClear) {
                    args.cfClearActionArgs = eventArgs;
                }
                else {
                    this.parent.notify('actionComplete', { eventArgs: eventArgs, action: 'clearCF' });
                }
            }
        };
        WorkbookConditionalFormat.prototype.getModuleName = function () {
            return 'workbookConditionalFormatting';
        };
        return WorkbookConditionalFormat;
    }());
    exports.WorkbookConditionalFormat = WorkbookConditionalFormat;
});