/* * (c) Copyright Ascensio System SIA 2010-2024 * * 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-6 Ernesta Birznieka-Upish * 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 * */ /** * Controller wraps up interaction with desktop app * * Created on 2/16/2018. */ define([ 'core' ], function () { 'use strict'; var webapp = window.DE || window.PE || window.SSE || window.PDFE || window.VE; var features = Object.assign({ version: '{{PRODUCT_VERSION}}', eventloading: true, titlebuttons: true, uithemes: true, btnhome: true, quickprint: true, framesize: { width: window.innerWidth, height: window.innerHeight }, }, webapp.features); var native = window.desktop || window.AscDesktopEditor; !!native && native.execCommand('webapps:features', JSON.stringify(features)); var Desktop = function () { var config = {version:'{{PRODUCT_VERSION}}'}; var titlebuttons; var btnsave_icons = { 'btn-save': 'save', 'btn-save-coauth': 'coauth', 'btn-synch': 'synch' }; let recents = []; var nativevars, helpUrl; if ( !!native ) { native.features = native.features || {}; nativevars = window.RendererProcessVariable; 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 (_.isNumber(obj.skiptoparea)) { if ( $('.asc-window.modal').length && $('.asc-window.modal').position().top < obj.skiptoparea ) $('.asc-window.modal').css('top', obj.skiptoparea); Common.Utils.InternalSettings.set('window-inactive-area-top', obj.skiptoparea); } if ( obj.singlewindow !== undefined ) { // $('#box-document-title .hedset')[obj.singlewindow ? 'hide' : 'show'](); if ( config.isFillFormApp ) { $("#title-doc-name")[obj.singlewindow ? 'hide' : 'show'](); } else { titlebuttons && titlebuttons.home && titlebuttons.home.btn.setVisible(obj.singlewindow); } native.features.singlewindow = obj.singlewindow; Common.NotificationCenter.trigger('desktop:window', {"compositetitle": native.features.singlewindow}); } } else if (/editor:config/.test(cmd)) { if ( param == 'request' ) { var opts = { user: config.user, title: { buttons: [] } }; if ( !!titlebuttons ) { if ( !$.isEmptyObject(titlebuttons) ) { var header = webapp.getController('Viewport').getView('Common.Views.Header'); if (header) { if ( native.getViewportSettings ) { const viewport = native.getViewportSettings(); if ( viewport.widgetType == 'window' && titlebuttons.home ) titlebuttons.home.btn.setVisible(true); } for (var i in titlebuttons) { opts.title.buttons.push(_serializeHeaderButton(i, titlebuttons[i])); } } } 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); } } } } else if (/button:click/.test(cmd)) { var obj = JSON.parse(param); if ( !!obj.action && !!titlebuttons[obj.action] ) { titlebuttons[obj.action].btn.click(); } } else if (/theme:changed/.test(cmd)) { Common.UI.Themes.setTheme(param, "native"); } else if (/^uitheme:added/.test(cmd)) { if ( !nativevars.localthemes ) nativevars.localthemes = []; let json_objs; try { json_objs = JSON.parse(param); } catch (e) { console.warn('local theme is broken'); } if ( json_objs ) { if (json_objs instanceof Array) { nativevars.localthemes = [].concat(nativevars.localthemes, json_objs); Common.UI.Themes.addTheme({themes: json_objs}); } else { nativevars.localthemes.push(json_objs); Common.UI.Themes.addTheme(json_objs); } } } else if (/renderervars:changed/.test(cmd)) { const opts = JSON.parse(param); if ( opts.theme && opts.theme.system ) { window.RendererProcessVariable.theme.system = opts.theme.system; if ( Common.UI.Themes.currentThemeId() == 'theme-system' ) Common.UI.Themes.refreshTheme(true, 'native'); } } 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(); } } else if (/althints:show/.test(cmd)) { if ( /false|hide/.test(param) ) Common.NotificationCenter && Common.NotificationCenter.trigger('hints:clear'); } else if (/file:print/.test(cmd)) { webapp.getController('Main').onPrint(); } else if (/printer:config/.test(cmd)) { var currentPrinter = null; var printers = []; var paramParse; try { paramParse = JSON.parse(param); } catch (e) { console.warn('printers info is broken'); } if(paramParse){ paramParse.printers && (printers = paramParse.printers); paramParse.current_printer && (currentPrinter = paramParse.current_printer); } const ctrl_print = webapp.getController('Print'); if ( ctrl_print ) ctrl_print.setPrintersInfo(currentPrinter, printers); } else if (/file:saveas/.test(cmd)) { webapp.getController('Main').api.asc_DownloadAs(); } else if (/file:save/.test(cmd)) { webapp.getController('Main').api.asc_Save(); } }; window.on_native_message('editor:config', 'request'); if ( !!window.native_message_cmd ) { for ( var c in window.native_message_cmd ) { if (c == 'uitheme:changed') continue; window.on_native_message(c, window.native_message_cmd[c]); } } native.execCommand('webapps:features', JSON.stringify(features)); window.onupdaterecents = function (params) { recents = _parseRecents(params); Common.NotificationCenter.trigger('update:recents', recents); } // hide mask for modal window var style = document.createElement('style'); style.appendChild(document.createTextNode('.modals-mask{opacity:0 !important;}')); document.getElementsByTagName('head')[0].appendChild(style); } var _serializeHeaderButton = function(action, config) { return { action: action, icon: config.icon || undefined, hint: config.btn.options.hint, disabled: config.btn.isDisabled(), visible: config.btn.isVisible(), }; }; var _onTitleButtonDisabled = function (action, e, status) { var _buttons = {}; _buttons[action] = status; native.execCommand('title:button', JSON.stringify({disabled: _buttons})); }; var _onSaveIconChanged = function (e, opts) { native.execCommand('title:button', JSON.stringify({'icon:changed': {'save': btnsave_icons[opts.next]}})); }; var _onModalDialog = function (status) { if ( status == 'open' ) { native.execCommand('title:button', JSON.stringify({disabled: {'all':true}})); } else { var _buttons = {}; for (var i in titlebuttons) { _buttons[i] = titlebuttons[i].btn.isDisabled(); } native.execCommand('title:button', JSON.stringify({'disabled': _buttons})); } }; var _onHintsShow = function (visible, level) { let info = { visible: visible && !(level > 0), }; if ( !!titlebuttons ) { info.hints = {}; !!titlebuttons['print'] && (info.hints['print'] = titlebuttons['print'].btn.btnEl.attr('data-hint-title-lang')); !!titlebuttons['quickprint'] && (info.hints['quickprint'] = titlebuttons['quickprint'].btn.btnEl.attr('data-hint-title-lang')); !!titlebuttons['undo'] && (info.hints['undo'] = titlebuttons['undo'].btn.btnEl.attr('data-hint-title-lang')); !!titlebuttons['redo'] && (info.hints['redo'] = titlebuttons['redo'].btn.btnEl.attr('data-hint-title-lang')); !!titlebuttons['save'] && (info.hints['save'] = titlebuttons['save'].btn.btnEl.attr('data-hint-title-lang')); !!titlebuttons['startover'] && (info.hints['startover'] = titlebuttons['startover'].btn.btnEl.attr('data-hint-title-lang')); } native.execCommand('althints:show', JSON.stringify(info)); } var _onKeyDown = function (e) { if ( Common.UI.HintManager && Common.UI.HintManager.isHintVisible() ) { native.execCommand('althints:keydown', JSON.stringify({code:e.keyCode})); console.log('hint keydown', e.keyCode); } else if ( e.keyCode == 78 /* N */ ) { if (config.canCreateNew && !e.shiftKey && ((Common.Utils.isWindows && e.ctrlKey && !e.metaKey) || (Common.Utils.isMac && e.metaKey && !e.ctrlKey))) { this.process('create:new'); } } } const _onApplySettings = function (menu) { if ( !!titlebuttons.quickprint ) { const var_name = window.SSE ? 'sse-settings-quick-print-button' : window.PE ? 'pe-settings-quick-print-button' : window.PDFE ? 'pdfe-settings-quick-print-button' : window.VE ? 've-settings-quick-print-button' : 'de-settings-quick-print-button'; const is_btn_visible = Common.localStorage.getBool(var_name, false); if ( titlebuttons.quickprint.visible != is_btn_visible ) { titlebuttons.quickprint.visible = is_btn_visible; const obj = { visible: { quickprint: is_btn_visible, } }; native.execCommand('title:button', JSON.stringify(obj)); } } } const _checkHelpAvailable = function () { const me = this; const build_url = function (arg1, arg2, arg3) { const re_ls = /\/$/; return (re_ls.test(arg1) ? arg1 : arg1 + '/') + arg2 + arg3; } fetch(build_url('resources/help/', Common.Locale.getDefaultLanguage(), '/Contents.json')) .then(function (response) { if ( response.ok ) { /* local help avail */ fetch(build_url('resources/help/', Common.Locale.getCurrentLanguage(), '/Contents.json')) .then(function (response){ if ( response.ok ) helpUrl = build_url('resources/help/', Common.Locale.getCurrentLanguage(), ''); }) .catch(function (e) { helpUrl = build_url('resources/help/', Common.Locale.getDefaultLanguage(), ''); }) } }).catch(function (e) { if ( me.helpUrl() ) { fetch(build_url(me.helpUrl(), Common.Locale.getDefaultLanguage(), '/Contents.json')) .then(function (response) { // if ( response.ok ) return response.json(); }) .then(function (text) { /* remote help avail */ fetch(build_url(me.helpUrl(), Common.Locale.getCurrentLanguage(), '/Contents.json')) .then(function (response) { // if ( response.ok ) return response.json(); }) .then(function (t) { helpUrl = build_url(me.helpUrl(), Common.Locale.getCurrentLanguage(), ''); }) .catch(function (e) { helpUrl = build_url(me.helpUrl(), Common.Locale.getDefaultLanguage(), ''); }); }) .catch(function (e){ }) } }); } const _onAppReady = function (opts) { _.extend(config, opts); !!native && native.execCommand('doc:onready', ''); !!native && native.LocalFileRecents(); $('.toolbar').addClass('editor-native-color'); } const _onDocumentReady = function () { if ( config.isEdit ) { function get_locked_message (t) { switch (t) { // case Asc.c_oAscLocalRestrictionType.Nosafe: case Asc.c_oAscLocalRestrictionType.ReadOnly: return Common.Locale.get("tipFileReadOnly",{name:"Common.Translation", default: "Document is read only. You can make changes and save its local copy later."}); default: return Common.Locale.get("tipFileLocked",{name:"Common.Translation", default: "Document is locked for editing. You can make changes and save its local copy later."}); } } const header = webapp.getController('Viewport').getView('Common.Views.Header'); const api = webapp.getController('Main').api; const locktype = api.asc_getLocalRestrictions ? api.asc_getLocalRestrictions() : Asc.c_oAscLocalRestrictionType.None; if ( Asc.c_oAscLocalRestrictionType.None !== locktype ) { features.readonly = true; header.setDocumentReadOnly(true); api.asc_setLocalRestrictions(Asc.c_oAscLocalRestrictionType.None); (new Common.UI.SynchronizeTip({ extCls: 'no-arrow', placement: 'bottom', target: $('.toolbar'), text: get_locked_message(locktype), showLink: false, })).on('closeclick', function () { this.close(); }).show(); native.execCommand('webapps:features', JSON.stringify(features)); api.asc_registerCallback('asc_onDocumentName', function () { if ( features.readonly ) { if ( api.asc_getLocalRestrictions() == Asc.c_oAscLocalRestrictionType.None ) { features.readonly = false; header.setDocumentReadOnly(false); native.execCommand('webapps:features', JSON.stringify(features)); } } }); } } } const _onHidePreloader = function (mode) { features.viewmode = !window.PDFE ? !mode.isEdit : !!mode.isXpsViewer; features.viewmode && (features.btnhome = false); features.crypted = mode.isCrypted; native.execCommand('webapps:features', JSON.stringify(features)); titlebuttons = {}; if ( !features.viewmode ) { var header = webapp.getController('Viewport').getView('Common.Views.Header'); { let viewport; if ( native.getViewportSettings ) { viewport = native.getViewportSettings(); } header.btnHome = (new Common.UI.Button({ cls: 'btn-header', iconCls: 'toolbar__icon icon--inverse btn-home', visible: viewport && viewport.widgetType == 'window', hint: Common.Locale.get('hintBtnHome', {name:"Common.Controllers.Desktop", default: 'Show Main window'}), dataHint:'0', dataHintDirection: 'right', dataHintOffset: '10, -18', dataHintTitle: 'K' })).render($('#box-document-title #slot-btn-dt-home')); titlebuttons['home'] = {btn: header.btnHome}; header.btnHome.on('click', function (e) { native.execCommand('title:button', JSON.stringify({click: "home"})); }); $('#id-box-doc-name').on({ 'dblclick': function (e) { native.execCommand('title:dblclick', JSON.stringify({x: e.originalEvent.screenX, y: e.originalEvent.screenY})) }, 'mousedown': function (e) { native.execCommand('title:mousedown', JSON.stringify({x: e.originalEvent.screenX, y: e.originalEvent.screenY})) }, 'mousemove': function (e) { native.execCommand('title:mousemove', JSON.stringify({x: e.originalEvent.screenX, y: e.originalEvent.screenY})) }, 'mouseup': function (e) { native.execCommand('title:mouseup', JSON.stringify({x: e.originalEvent.screenX, y: e.originalEvent.screenY})) } }); } if (!!header.btnSave) { titlebuttons['save'] = {btn: header.btnSave}; var iconname = /\s?([^\s]+)$/.exec(titlebuttons.save.btn.$icon.attr('class')); !!iconname && iconname.length && (titlebuttons.save.icon = btnsave_icons[iconname]); } if (!!header.btnPrint) titlebuttons['print'] = {btn: header.btnPrint}; if (!!header.btnPrintQuick) { titlebuttons['quickprint'] = { btn: header.btnPrintQuick, visible: header.btnPrintQuick.isVisible(), }; } if (!!header.btnUndo) titlebuttons['undo'] = {btn: header.btnUndo}; if (!!header.btnRedo) titlebuttons['redo'] = {btn: header.btnRedo}; if (!!header.btnStartOver) { titlebuttons['startover'] = {btn: header.btnStartOver}; } if (!!header.btnQuickAccess) titlebuttons['quickaccess'] = {btn: header.btnQuickAccess}; for (var i in titlebuttons) { titlebuttons[i].btn.options.signals = ['disabled']; titlebuttons[i].btn.on('disabled', _onTitleButtonDisabled.bind(this, i)); } 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; } if ( native.features.singlewindow !== undefined ) { if ( config.isFillFormApp ) $("#title-doc-name")[native.features.singlewindow ? 'hide' : 'show'](); // $('#box-document-title .hedset')[native.features.singlewindow ? 'hide' : 'show'](); !!titlebuttons.home && titlebuttons.home.btn.setVisible(native.features.singlewindow); } } const _parseRecents = function (rawarray) { let _files_arr = []; const _is_win = /Win/.test(navigator.platform); const _re_name = !_is_win ? /([^/]+\.[a-zA-Z0-9]{1,})$/ : /([^\\/]+\.[a-zA-Z0-9]{1,})$/; for ( let i in rawarray ) { const _f_ = rawarray[i]; if ( utils.matchFileFormat( _f_.type ) ) { if (_re_name.test(_f_.path)) { _f_.path = $('