all files / workbook/actions/ protect-sheet.js

100% Statements 69/69
96.15% Branches 25/26
100% Functions 14/14
100% Lines 69/69
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   1259× 1259×   79× 79× 79× 79× 79×         79× 13× 13× 13×     79× 79×       78×       78×   79× 323× 203×       15× 15× 15× 15×   15× 15× 15× 15× 15× 15×   15×     14×     1256× 1256×   1259× 1259× 1259×   1256× 25× 25× 25×     38× 38× 38× 38× 38×     38× 1226× 8772×     38× 37×     181551×        
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 WorkbookProtectSheet = (function () {
        function WorkbookProtectSheet(workbook) {
            this.parent = workbook;
            this.addEventListener();
        }
        WorkbookProtectSheet.prototype.protectsheetHandler = function (args) {
            var _this = this;
            var sheetIndex = ej2_base_1.isNullOrUndefined(args.sheetIndex) ? this.parent.activeSheetIndex : args.sheetIndex;
            var sheet = index_1.getSheet(this.parent, sheetIndex);
            this.parent.setSheetPropertyOnMute(sheet, 'isProtected', true);
            this.parent.setSheetPropertyOnMute(sheet, 'protectSettings', {
                selectCells: args.protectSettings.selectCells, formatCells: args.protectSettings.formatCells,
                formatColumns: args.protectSettings.formatColumns, formatRows: args.protectSettings.formatRows,
                insertLink: args.protectSettings.insertLink, selectUnLockedCells: args.protectSettings.selectUnLockedCells
            });
            if (args.password && args.password.length > 0) {
                index_2.generateHashSaltValue(args.password).then(function (res) {
                    _this.parent.setSheetPropertyOnMute(sheet, 'hashValue', res.hashValue);
                    _this.parent.setSheetPropertyOnMute(sheet, 'saltValue', res.saltValue);
                });
            }
            var isSuspended = this.parent.paintSuspendCount > 0;
            if (isSuspended) {
                this.parent.queuePaintAction('protectSheet', function () {
                    _this.parent.notify(index_3.protectSheetWorkBook, { sheetIndex: sheetIndex, triggerEvent: args.triggerEvent });
                    _this.parent.notify(index_3.updateToggle, { props: 'Protect' });
                });
            }
            else {
                this.parent.notify(index_3.protectSheetWorkBook, {
                    sheetIndex: sheetIndex,
                    triggerEvent: args.triggerEvent
                });
                this.parent.notify(index_3.updateToggle, { props: 'Protect' });
            }
            sheet.columns.forEach(function (column) {
                if (column && ej2_base_1.isUndefined(column.isLocked)) {
                    column.isLocked = true;
                }
            });
        };
        WorkbookProtectSheet.prototype.unprotectsheetHandler = function (args) {
            var _this = this;
            var sheet = this.parent.getActiveSheet();
            Eif (!ej2_base_1.isNullOrUndefined(args.sheet)) {
                sheet = this.parent.sheets[args.sheet];
            }
            this.parent.setSheetPropertyOnMute(sheet, 'isProtected', false);
            var updateProtectSettings = function () {
                sheet.protectSettings.formatCells = sheet.protectSettings.formatColumns = false;
                sheet.protectSettings.formatRows = sheet.protectSettings.selectCells = false;
                _this.parent.notify(index_3.protectSheetWorkBook, sheet.protectSettings);
                _this.parent.notify(index_3.updateToggle, { props: 'Protect' });
            };
            if (this.parent.paintSuspendCount > 0) {
                this.parent.queuePaintAction('unProtectSheet', updateProtectSettings);
            }
            else {
                updateProtectSettings();
            }
        };
        WorkbookProtectSheet.prototype.destroy = function () {
            this.removeEventListener();
            this.parent = null;
        };
        WorkbookProtectSheet.prototype.addEventListener = function () {
            this.parent.on(index_3.protectsheetHandler, this.protectsheetHandler, this);
            this.parent.on(index_4.unprotectsheetHandler, this.unprotectsheetHandler, this);
            this.parent.on(index_3.setLockCells, this.lockCells, this);
        };
        WorkbookProtectSheet.prototype.removeEventListener = function () {
            if (!this.parent.isDestroyed) {
                this.parent.off(index_3.protectsheetHandler, this.protectsheetHandler);
                this.parent.off(index_3.setLockCells, this.lockCells);
                this.parent.off(index_4.unprotectsheetHandler, this.unprotectsheetHandler);
            }
        };
        WorkbookProtectSheet.prototype.lockCells = function (args) {
            var addressInfo = this.parent.getAddressInfo(args.range);
            var indexes = index_5.getSwapRange(addressInfo.indices);
            var sheet = index_1.getSheet(this.parent, addressInfo.sheetIndex);
            var isLocked = args.isLocked ? args.isLocked : false;
            if (indexes[0] === 0 && indexes[2] === sheet.rowCount - 1) {
                for (var i = indexes[1]; i <= indexes[3]; i++) {
                    index_1.setColumn(sheet, i, { isLocked: args.isLocked });
                }
            }
            for (var i = indexes[0]; i <= indexes[2]; i++) {
                for (var j = indexes[1]; j <= indexes[3]; j++) {
                    index_1.setCell(i, j, sheet, { isLocked: isLocked }, true);
                }
            }
            if (args.triggerEvent) {
                this.parent.notify('actionComplete', { action: 'lockCells', eventArgs: args });
            }
        };
        WorkbookProtectSheet.prototype.getModuleName = function () {
            return 'workbookProtectSheet';
        };
        return WorkbookProtectSheet;
    }());
    exports.WorkbookProtectSheet = WorkbookProtectSheet;
});