web-apps/apps/spreadsheeteditor/main/app/controller/WBProtection.js

366 lines
16 KiB
JavaScript
Raw Normal View History

/*
*
* (c) Copyright Ascensio System SIA 2010-2021
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation. In accordance with
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
* that Ascensio System SIA expressly excludes the warranty of non-infringement
* of any third-party rights.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA at 20A-12 Ernesta Birznieka-Upisha
* street, Riga, Latvia, EU, LV-1050.
*
* The interactive user interfaces in modified source and object code versions
* of the Program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU AGPL version 3.
*
* Pursuant to Section 7(b) of the License you must retain the original Product
* logo when distributing the program. Pursuant to Section 7(e) we decline to
* grant you any rights under trademark law for use of our trademarks.
*
* All the Product's GUI elements, including illustrations and icon sets, as
* well as technical writing content are licensed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
*
*/
/**
* WBProtection.js
*
* Created by Julia Radzhabova on 21.06.2021
* Copyright (c) 2021Ascensio System SIA. All rights reserved.
*
*/
define([
'core',
'common/main/lib/view/Protection',
2021-06-21 21:43:55 +00:00
'spreadsheeteditor/main/app/view/WBProtection',
2021-06-22 21:46:43 +00:00
'spreadsheeteditor/main/app/view/ProtectDialog',
'spreadsheeteditor/main/app/view/ProtectRangesDlg'
], function () {
'use strict';
SSE.Controllers.WBProtection = Backbone.Controller.extend(_.extend({
models : [],
collections : [
],
views : [
'WBProtection'
],
sdkViewName : '#id_main',
initialize: function () {
this.addListeners({
'WBProtection': {
'protect:workbook': _.bind(this.onWorkbookClick, this),
'protect:sheet': _.bind(this.onSheetClick, this),
'protect:ranges': _.bind(this.onRangesClick, this),
'protect:lock-options': _.bind(this.onLockOptionClick, this)
}
});
},
onLaunch: function () {
this._state = {};
this.wsLockOptions = ['SelectLockedCells', 'SelectUnlockedCells', 'FormatCells', 'FormatColumns', 'FormatRows', 'InsertColumns', 'InsertRows', 'InsertHyperlinks', 'DeleteColumns',
'DeleteRows', 'Sort', 'AutoFilter', 'PivotTables', 'Objects', 'Scenarios'];
SSE.enumLock && this.wsLockOptions.forEach(function(item){
SSE.enumLock[item] = item;
});
Common.NotificationCenter.on('app:ready', this.onAppReady.bind(this));
2021-08-04 21:37:48 +00:00
Common.NotificationCenter.on('protect:sheet', _.bind(this.onSheetClick, this));
},
setConfig: function (data, api) {
this.setApi(api);
if (data) {
this.sdkViewName = data['sdkviewname'] || this.sdkViewName;
}
},
setApi: function (api) {
if (api) {
this.api = api;
2021-06-22 09:16:38 +00:00
this.api.asc_registerCallback('asc_onChangeProtectWorkbook',_.bind(this.onChangeProtectWorkbook, this));
2021-06-22 21:46:43 +00:00
this.api.asc_registerCallback('asc_onChangeProtectWorksheet',_.bind(this.onChangeProtectSheet, this));
2021-08-11 22:42:14 +00:00
this.api.asc_registerCallback('asc_onActiveSheetChanged', _.bind(this.onActiveSheetChanged, this));
this.api.asc_registerCallback('asc_onSelectionChanged', _.bind(this.onApiSelectionChanged, this));
}
},
setMode: function(mode) {
this.appConfig = mode;
this.appConfig.isEdit && (this.view = this.createView('WBProtection', {
mode: mode
}));
return this;
},
createToolbarPanel: function() {
if (this.view)
return this.view.getPanel();
},
getView: function(name) {
return !name && this.view ?
this.view : Backbone.Controller.prototype.getView.call(this, name);
},
onWorkbookClick: function(state) {
2021-11-09 15:11:50 +00:00
this.view.btnProtectWB.toggle(!state, true);
2021-06-21 21:23:45 +00:00
if (state) {
var me = this,
2021-06-22 09:16:38 +00:00
btn,
2021-06-21 21:43:55 +00:00
win = new SSE.Views.ProtectDialog({
2021-06-22 11:56:07 +00:00
type: 'workbook',
2021-06-22 09:16:38 +00:00
handler: function(result, value) {
btn = result;
2021-06-21 21:23:45 +00:00
if (result == 'ok') {
2021-06-22 13:07:10 +00:00
var props = me.api.asc_getProtectedWorkbook();
2021-11-09 13:22:06 +00:00
props.asc_setLockStructure(value, _.bind(me.onSetProtectedWorkbook, me));
2021-06-21 21:23:45 +00:00
}
Common.NotificationCenter.trigger('edit:complete');
2021-06-21 20:53:28 +00:00
}
2021-06-22 09:16:38 +00:00
}).on('close', function() {
if (btn!=='ok')
me.view.btnProtectWB.toggle(false, true);
2021-06-21 21:23:45 +00:00
});
2021-06-21 20:53:28 +00:00
2021-06-21 21:23:45 +00:00
win.show();
} else {
var me = this,
2021-06-22 09:16:38 +00:00
btn,
props = me.api.asc_getProtectedWorkbook();
if (props.asc_isPassword()) {
var win = new Common.Views.OpenDialog({
title: me.view.txtWBUnlockTitle,
closable: true,
type: Common.Utils.importTextType.DRM,
txtOpenFile: me.view.txtWBUnlockDescription,
validatePwd: false,
handler: function (result, value) {
btn = result;
if (result == 'ok') {
if (me.api) {
2021-11-09 14:33:20 +00:00
props.asc_setLockStructure(value && value.drmOptions ? value.drmOptions.asc_getPassword() : undefined, _.bind(me.onSetProtectedWorkbook, me));
2021-06-22 09:16:38 +00:00
}
Common.NotificationCenter.trigger('edit:complete');
2021-06-21 21:23:45 +00:00
}
}
2021-06-22 09:16:38 +00:00
}).on('close', function() {
if (btn!=='ok')
me.view.btnProtectWB.toggle(true, true);
});
win.show();
} else {
2021-11-09 13:22:06 +00:00
props.asc_setLockStructure(undefined, _.bind(me.onSetProtectedWorkbook, me));
2021-06-22 09:16:38 +00:00
}
2021-06-21 21:23:45 +00:00
}
},
2021-11-09 13:22:06 +00:00
onSetProtectedWorkbook: function(props) {
this.api.asc_setProtectedWorkbook(props);
},
onSheetClick: function(state) {
2021-11-09 15:11:50 +00:00
this.view.btnProtectSheet.toggle(!state, true);
2021-06-21 21:23:45 +00:00
if (state) {
2021-06-22 11:56:07 +00:00
var me = this,
btn,
2021-06-22 13:07:10 +00:00
props = me.api.asc_getProtectedSheet(),
2021-06-22 11:56:07 +00:00
win = new SSE.Views.ProtectDialog({
type: 'sheet',
2021-06-22 13:07:10 +00:00
props: props,
2021-06-22 11:56:07 +00:00
handler: function(result, value, props) {
btn = result;
if (result == 'ok') {
props.asc_setSheet(true);
value && props.asc_setPassword(value);
me.api.asc_setProtectedSheet(props);
}
Common.NotificationCenter.trigger('edit:complete');
}
}).on('close', function() {
if (btn!=='ok')
me.view.btnProtectSheet.toggle(false, true);
});
2021-06-21 21:23:45 +00:00
2021-06-22 11:56:07 +00:00
win.show();
2021-06-21 21:23:45 +00:00
} else {
var me = this,
2021-06-22 11:56:07 +00:00
btn,
props = me.api.asc_getProtectedSheet();
if (props.asc_isPassword()) {
var win = new Common.Views.OpenDialog({
2021-06-21 21:23:45 +00:00
title: me.view.txtSheetUnlockTitle,
closable: true,
type: Common.Utils.importTextType.DRM,
txtOpenFile: me.view.txtSheetUnlockDescription,
validatePwd: false,
handler: function (result, value) {
2021-06-22 11:56:07 +00:00
btn = result;
2021-06-21 21:23:45 +00:00
if (result == 'ok') {
if (me.api) {
2021-06-22 11:56:07 +00:00
props.asc_setSheet(false, value);
me.api.asc_setProtectedSheet(props);
2021-06-21 21:23:45 +00:00
}
Common.NotificationCenter.trigger('edit:complete');
}
}
2021-06-22 11:56:07 +00:00
}).on('close', function() {
if (btn!=='ok')
me.view.btnProtectSheet.toggle(true, true);
2021-06-21 21:23:45 +00:00
});
2021-06-22 11:56:07 +00:00
win.show();
} else {
props.asc_setSheet(false);
me.api.asc_setProtectedSheet(props);
}
2021-06-21 21:23:45 +00:00
}
},
onRangesClick: function() {
2021-06-22 21:46:43 +00:00
var me = this,
props = me.api.asc_getProtectedRanges(),
win = new SSE.Views.ProtectRangesDlg({
api: me.api,
props: props,
handler: function(result, settings) {
if (result=='protect-sheet') {
2021-06-23 22:01:25 +00:00
me.api.asc_setProtectedRanges(settings.arr, settings.deletedArr);
2021-06-22 21:46:43 +00:00
me.onSheetClick(true);
} else if (result == 'ok') {
me.api.asc_setProtectedRanges(settings.arr, settings.deletedArr);
}
Common.NotificationCenter.trigger('edit:complete');
}
});
win.show();
},
onLockOptionClick: function(type, value) {
switch (type) {
case 0: // cell
this.api.asc_setCellLocked(value=='checked');
break;
case 1: // shape
2021-08-04 17:54:21 +00:00
var props = new Asc.asc_CImgProperty();
props.asc_putProtectionLocked(value=='checked');
this.api.asc_setGraphicObjectProps(props);
break;
case 2: // text
2021-08-04 17:54:21 +00:00
var props = new Asc.asc_CImgProperty();
props.asc_putProtectionLockText(value=='checked');
this.api.asc_setGraphicObjectProps(props);
break;
case 3: // formula
2021-08-04 19:32:07 +00:00
this.api.asc_setCellHiddenFormulas(value=='checked');
break;
}
Common.NotificationCenter.trigger('edit:complete', this);
},
onAppReady: function (config) {
if (!this.view) return;
2021-06-21 20:53:28 +00:00
var me = this;
(new Promise(function (resolve) {
resolve();
})).then(function () {
me.view.btnProtectWB.toggle(me.api.asc_isProtectedWorkbook(), true);
2021-07-19 13:56:46 +00:00
var props = me.getWSProps();
me.view.btnProtectSheet.toggle(props.wsLock, true); //current sheet
Common.Utils.lockControls(SSE.enumLock['Objects'], props.wsProps['Objects'], { array: [me.view.chLockedText, me.view.chLockedShape]});
2021-07-19 20:49:51 +00:00
Common.Utils.lockControls(SSE.enumLock.wsLock, props.wsLock, { array: [me.view.btnAllowRanges]});
2021-06-21 20:53:28 +00:00
});
},
2021-06-22 09:16:38 +00:00
onChangeProtectWorkbook: function() {
this.view && this.view.btnProtectWB.toggle(this.api.asc_isProtectedWorkbook(), true);
2021-06-22 09:16:38 +00:00
},
onChangeProtectSheet: function() {
2021-08-11 22:42:14 +00:00
var props = this.getWSProps(true);
2021-09-23 16:13:19 +00:00
if (this.view && props) {
this.view.btnProtectSheet.toggle(props.wsLock, true); //current sheet
Common.Utils.lockControls(SSE.enumLock['Objects'], props.wsProps['Objects'], { array: [this.view.chLockedText, this.view.chLockedShape]});
Common.Utils.lockControls(SSE.enumLock.wsLock, props.wsLock, { array: [this.view.btnAllowRanges]});
}
Common.NotificationCenter.trigger('protect:wslock', props);
},
2021-08-11 22:42:14 +00:00
onActiveSheetChanged: function() {
this.onChangeProtectSheet(); //current sheet
},
2021-08-11 22:42:14 +00:00
getWSProps: function(update) {
if (!this.appConfig || !this.appConfig.isEdit && !this.appConfig.isRestrictedEdit) return;
2021-08-11 22:42:14 +00:00
if (update || !this._state.protection) {
var wsProtected = !!this.api.asc_isProtectedSheet();
var arr = [];
if (wsProtected) {
arr = [];
var props = this.api.asc_getProtectedSheet();
props && this.wsLockOptions.forEach(function(item){
arr[item] = props['asc_get' + item] ? props['asc_get' + item]() : false;
});
} else {
this.wsLockOptions.forEach(function(item){
arr[item] = false;
});
}
this._state.protection = {wsLock: wsProtected, wsProps: arr};
}
2021-06-22 12:04:10 +00:00
2021-08-11 22:42:14 +00:00
return this._state.protection;
},
onApiSelectionChanged: function(info) {
if (!this.view) return;
if ($('.asc-window.enable-key-events:visible').length>0) return;
var selectionType = info.asc_getSelectionType();
var need_disable = (selectionType === Asc.c_oAscSelectionType.RangeCells || selectionType === Asc.c_oAscSelectionType.RangeCol ||
selectionType === Asc.c_oAscSelectionType.RangeRow || selectionType === Asc.c_oAscSelectionType.RangeMax);
Common.Utils.lockControls(SSE.enumLock.selRange, need_disable, { array: [this.view.chLockedText, this.view.chLockedShape]});
var xfs = info.asc_getXfs();
this.view.chLockedCell.setValue(!!xfs.asc_getLocked(), true);
this.view.chHiddenFormula.setValue(!!xfs.asc_getHidden(), true);
2021-08-04 17:54:21 +00:00
if (selectionType === Asc.c_oAscSelectionType.RangeSlicer || selectionType === Asc.c_oAscSelectionType.RangeImage ||
selectionType === Asc.c_oAscSelectionType.RangeShape || selectionType === Asc.c_oAscSelectionType.RangeShapeText ||
selectionType === Asc.c_oAscSelectionType.RangeChart || selectionType === Asc.c_oAscSelectionType.RangeChartText) {
var selectedObjects = this.api.asc_getGraphicObjectProps();
for (var i = 0; i < selectedObjects.length; i++) {
if (selectedObjects[i].asc_getObjectType() == Asc.c_oAscTypeSelectElement.Image) {
var elValue = selectedObjects[i].asc_getObjectValue();
var locktext = elValue.asc_getProtectionLockText(),
lock = elValue.asc_getProtectionLocked();
this.view.chLockedText.setValue(locktext!==undefined ? !!locktext : 'indeterminate', true);
this.view.chLockedShape.setValue(lock!==undefined ? !!lock : 'indeterminate', true);
Common.Utils.lockControls(SSE.enumLock.wsLockText, locktext===null, { array: [this.view.chLockedText]});
Common.Utils.lockControls(SSE.enumLock.wsLockShape, lock===null, { array: [this.view.chLockedShape]});
break;
}
}
}
}
}, SSE.Controllers.WBProtection || {}));
});