web-apps/apps/common/main/lib/controller/Desktop.js

282 lines
12 KiB
JavaScript
Raw Normal View History

2018-02-21 23:23:32 +00:00
/*
*
* (c) Copyright Ascensio System SIA 2010-2019
2018-02-21 23:23:32 +00:00
*
* 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.
2018-02-21 23:23:32 +00:00
*
* 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
*
*/
/**
* Controller wraps up interaction with desktop app
*
* Created by Maxim.Kadushkin on 2/16/2018.
*/
define([
'core'
], function () {
'use strict';
2020-03-05 10:09:53 +00:00
var native = window.AscDesktopEditor;
!!native && native.execCommand('webapps:features', JSON.stringify({
version: '{{PRODUCT_VERSION}}',
eventloading: true,
titlebuttons: true
}));
2018-02-21 23:23:32 +00:00
var Desktop = function () {
2019-10-07 12:47:56 +00:00
var config = {version:'{{PRODUCT_VERSION}}'};
2020-03-05 10:09:53 +00:00
var webapp = window.DE || window.PE || window.SSE;
2019-10-07 12:47:56 +00:00
var titlebuttons;
var btnsave_icons = {
'btn-save': 'save',
'btn-save-coauth': 'coauth',
'btn-synch': 'synch' };
2018-02-21 23:23:32 +00:00
2020-03-05 10:09:53 +00:00
if ( !!native ) {
2019-05-13 11:45:17 +00:00
window.on_native_message = function (cmd, param) {
if (/^style:change/.test(cmd)) {
var obj = JSON.parse(param);
if ( obj.element == 'toolbar' ) {
if ( obj.action == 'off' && obj.style == 'native-color' ) {
$('.toolbar').removeClass('editor-native-color');
}
} else
if ( obj.element == 'body' ) {
if ( obj.action == 'merge' ) {
var style = document.createElement('style');
style.innerHTML = obj.style;
document.body.appendChild(style);
}
}
} else
if (/window:features/.test(cmd)) {
var obj = JSON.parse(param);
if ( obj.canUndock == 'true' ) {
if ( !config.canUndock ) {
config.canUndock = true;
if ( !_.isEmpty(config) )
Common.NotificationCenter.trigger('app:config', {canUndock:true});
}
}
2020-03-15 15:13:29 +00:00
if (_.isNumber(obj.skiptoparea)) {
2020-04-03 13:26:40 +00:00
if ( $('.asc-window.modal').length && $('.asc-window.modal').position().top < obj.skiptoparea )
2020-04-02 14:17:35 +00:00
$('.asc-window.modal').css('top', obj.skiptoparea);
2020-03-15 15:13:29 +00:00
Common.Utils.InternalSettings.set('window-inactive-area-top', obj.skiptoparea);
}
2019-05-13 11:45:17 +00:00
} else
if (/window:status/.test(cmd)) {
var obj = JSON.parse(param);
if ( obj.action == 'undocking' ) {
Common.NotificationCenter.trigger('undock:status', {status:obj.status=='undocked'?'undocked':'docked'});
}
} else
if (/editor:config/.test(cmd)) {
2019-10-07 12:47:56 +00:00
if ( param == 'request' ) {
if ( !!titlebuttons ) {
var opts = {
user: config.user,
title: { buttons: [] }
};
var header = webapp.getController('Viewport').getView('Common.Views.Header');
if ( header ) {
for (var i in titlebuttons) {
opts.title.buttons.push(_serializeHeaderButton(i, titlebuttons[i]));
}
}
2019-10-07 12:47:56 +00:00
2020-03-05 10:09:53 +00:00
native.execCommand('editor:config', JSON.stringify(opts));
} else
if ( !config.callback_editorconfig ) {
config.callback_editorconfig = function() {
setTimeout(function(){window.on_native_message(cmd, param);},0);
2019-10-07 12:47:56 +00:00
}
}
}
} else
if (/button:click/.test(cmd)) {
var obj = JSON.parse(param);
if ( !!obj.action ) {
2019-11-05 12:17:48 +00:00
titlebuttons[obj.action].btn.click();
2019-10-07 12:47:56 +00:00
}
2020-02-07 13:15:41 +00:00
} else
if (/element:show/.test(cmd)) {
var _mr = /title:(?:(true|show)|(false|hide))/.exec(param);
if ( _mr ) {
if (!!_mr[1]) $('#app-title').show();
else if (!!_mr[2]) $('#app-title').hide();
}
2019-05-13 11:45:17 +00:00
}
};
if ( !!window.native_message_cmd ) {
for ( var c in window.native_message_cmd ) {
window.on_native_message(c, window.native_message_cmd[c]);
}
}
2020-03-05 10:09:53 +00:00
native.execCommand('webapps:features', JSON.stringify({version: config.version, eventloading:true, titlebuttons:true}));
2020-03-17 15:06:09 +00:00
// hide mask for modal window
var style = document.createElement('style');
style.appendChild(document.createTextNode('.modals-mask{opacity:0;}'));
document.getElementsByTagName('head')[0].appendChild(style);
2019-05-13 11:45:17 +00:00
}
2019-10-07 12:47:56 +00:00
var _serializeHeaderButton = function(action, config) {
return {
action: action,
icon: config.icon || undefined,
2019-10-07 12:47:56 +00:00
hint: config.btn.options.hint,
2020-03-13 14:46:10 +00:00
disabled: config.btn.isDisabled()
2019-10-07 12:47:56 +00:00
};
};
2019-11-05 12:17:48 +00:00
var _onTitleButtonDisabled = function (action, e, status) {
var _buttons = {};
_buttons[action] = status;
2020-03-05 10:09:53 +00:00
native.execCommand('title:button', JSON.stringify({disabled: _buttons}));
};
var _onSaveIconChanged = function (e, opts) {
2020-03-05 10:09:53 +00:00
native.execCommand('title:button', JSON.stringify({'icon:changed': {'save': btnsave_icons[opts.next]}}));
};
var _onModalDialog = function (status) {
if ( status == 'open' ) {
2020-03-05 10:09:53 +00:00
native.execCommand('title:button', JSON.stringify({disabled: {'all':true}}));
} else {
var _buttons = {};
for (var i in titlebuttons) {
2020-03-13 14:46:10 +00:00
_buttons[i] = titlebuttons[i].btn.isDisabled();
}
2020-03-05 10:09:53 +00:00
native.execCommand('title:button', JSON.stringify({'disabled': _buttons}));
}
2019-10-07 12:47:56 +00:00
};
2018-02-21 23:23:32 +00:00
return {
init: function (opts) {
_.extend(config, opts);
if ( config.isDesktopApp ) {
2018-04-27 13:32:39 +00:00
Common.NotificationCenter.on('app:ready', function (opts) {
_.extend(config, opts);
2020-03-05 10:09:53 +00:00
!!native && native.execCommand('doc:onready', '');
$('.toolbar').addClass('editor-native-color');
2018-02-21 23:23:32 +00:00
});
2019-05-13 11:45:17 +00:00
Common.NotificationCenter.on('action:undocking', function (opts) {
2020-03-05 10:09:53 +00:00
native.execCommand('editor:event', JSON.stringify({action:'undocking', state: opts == 'dock' ? 'dock' : 'undock'}));
2019-05-13 11:45:17 +00:00
});
2019-05-13 11:45:17 +00:00
Common.NotificationCenter.on('app:face', function (mode) {
if ( config.canUndock ) {
Common.NotificationCenter.trigger('app:config', {canUndock: true});
}
2019-10-07 12:47:56 +00:00
var header = webapp.getController('Viewport').getView('Common.Views.Header');
2020-02-25 15:23:38 +00:00
titlebuttons = {};
if ( !!header.btnSave ) {
2020-03-13 14:46:10 +00:00
titlebuttons['save'] = {btn: header.btnSave};
2020-02-25 15:23:38 +00:00
var iconname = /\s?([^\s]+)$/.exec(titlebuttons.save.btn.$icon.attr('class'));
!!iconname && iconname.length && (titlebuttons.save.icon = btnsave_icons[iconname]);
}
if ( !!header.btnPrint )
2020-03-13 14:46:10 +00:00
titlebuttons['print'] = {btn: header.btnPrint};
2020-02-25 15:23:38 +00:00
if ( !!header.btnUndo )
2020-03-13 14:46:10 +00:00
titlebuttons['undo'] = {btn: header.btnUndo};
2020-02-25 15:23:38 +00:00
if ( !!header.btnRedo )
2020-03-13 14:46:10 +00:00
titlebuttons['redo'] = {btn: header.btnRedo};
2019-10-07 12:47:56 +00:00
2019-11-05 12:17:48 +00:00
for (var i in titlebuttons) {
titlebuttons[i].btn.options.signals = ['disabled'];
titlebuttons[i].btn.on('disabled', _onTitleButtonDisabled.bind(this, i));
2019-10-07 12:47:56 +00:00
}
2020-02-26 13:14:04 +00:00
if (!!titlebuttons.save) {
titlebuttons.save.btn.options.signals.push('icon:changed');
titlebuttons.save.btn.on('icon:changed', _onSaveIconChanged.bind(this));
}
if ( !!config.callback_editorconfig ) {
config.callback_editorconfig();
delete config.callback_editorconfig;
}
2019-05-13 11:45:17 +00:00
});
Common.NotificationCenter.on({
'modal:show': _onModalDialog.bind(this, 'open'),
'modal:close': _onModalDialog.bind(this, 'close')
});
2018-02-21 23:23:32 +00:00
}
},
process: function (opts) {
2020-03-05 10:09:53 +00:00
if ( config.isDesktopApp && !!native ) {
2018-04-27 13:32:39 +00:00
if ( opts == 'goback' ) {
2020-03-05 10:09:53 +00:00
native.execCommand('go:folder',
2018-02-21 23:23:32 +00:00
config.isOffline ? 'offline' : config.customization.goback.url);
return true;
2018-04-27 13:32:39 +00:00
} else
if ( opts == 'preloader:hide' ) {
2020-03-05 10:09:53 +00:00
native.execCommand('editor:onready', '');
2018-04-27 13:32:39 +00:00
return true;
} else
if ( opts == 'create:new' ) {
if (config.createUrl == 'desktop://create.new') {
2020-03-05 10:09:53 +00:00
native.LocalFileCreate(!!window.SSE ? 2 : !!window.PE ? 1 : 0);
return true;
}
2018-02-21 23:23:32 +00:00
}
}
2018-04-27 13:32:39 +00:00
return false;
},
requestClose: function () {
2020-03-05 10:09:53 +00:00
if ( config.isDesktopApp && !!native ) {
native.execCommand('editor:event', JSON.stringify({action:'close', url: config.customization.goback.url}));
}
2018-02-21 23:23:32 +00:00
}
};
};
Common.Controllers.Desktop = new Desktop();
2020-02-07 13:15:41 +00:00
});