From 325667e0724ba5a0bd4b6a5ad1f9da0dfddc5741 Mon Sep 17 00:00:00 2001 From: Maxim Kadushkin Date: Mon, 25 Feb 2019 16:14:41 +0300 Subject: [PATCH] [SSE] asynchronous load translation for formulas --- .../main/app/controller/FormulaDialog.js | 69 ++++++---- .../main/app/controller/LeftMenu.js | 5 - .../main/app/controller/Main.js | 15 +-- .../main/app/view/FormulaLang.js | 123 ------------------ 4 files changed, 47 insertions(+), 165 deletions(-) delete mode 100644 apps/spreadsheeteditor/main/app/view/FormulaLang.js diff --git a/apps/spreadsheeteditor/main/app/controller/FormulaDialog.js b/apps/spreadsheeteditor/main/app/controller/FormulaDialog.js index 941b410ae..7eea41c52 100644 --- a/apps/spreadsheeteditor/main/app/controller/FormulaDialog.js +++ b/apps/spreadsheeteditor/main/app/controller/FormulaDialog.js @@ -64,6 +64,12 @@ define([ 'FileMenu': { 'settings:apply': function() { me.needUpdateFormula = true; + + var lang = Common.localStorage.getItem("sse-settings-func-locale"); + Common.Utils.InternalSettings.set("sse-settings-func-locale", lang); + + me.formulasGroups.reset(); + me.reloadTranslations(lang); } } }); @@ -73,7 +79,8 @@ define([ this.api = api; if (this.formulasGroups && this.api) { - this.loadingFormulas(); + this.reloadTranslations( + Common.localStorage.getItem("sse-settings-func-locale") || this.appOptions.lang ); var me = this; @@ -82,7 +89,7 @@ define([ toolclose : 'hide', formulasGroups : this.formulasGroups, handler : function (func) { - if (func && me.api) { + if (func) { me.api.asc_insertFormula(func, Asc.c_oAscPopUpSelectorType.Func); } } @@ -90,9 +97,7 @@ define([ this.formulas.on({ 'hide': function () { - if (me.api) { - me.api.asc_enableKeyEvents(true); - } + me.api.asc_enableKeyEvents(true); } }); } @@ -101,19 +106,48 @@ define([ }, setMode: function(mode) { - this.mode = mode; return this; }, onLaunch: function () { this.formulasGroups = this.getApplication().getCollection('FormulaGroups'); + + Common.Gateway.on('init', this.loadConfig.bind(this)); + }, + + loadConfig: function(data) { + this.appOptions = {}; + this.appOptions.lang = data.config.lang; + }, + + reloadTranslations: function (lang) { + var me = this; + lang = (lang || 'en').split(/[\-_]/)[0].toLowerCase(); + + if ( lang != 'en' ) { + Common.Utils.InternalSettings.set("sse-settings-func-locale", lang); + Common.Utils.loadConfig('resources/formula-lang/' + lang + '.json', + function (config) { + if ( config != 'error' ) + me.api.asc_setLocalization(config); + }); + } else me.api.asc_setLocalization(undefined); + + Common.Utils.loadConfig('resources/formula-lang/' + lang + '_desc.json', + function (config) { + if ( config != 'error' ) + me.loadingFormulas(config); + }); }, showDialog: function () { if (this.formulas) { - if (this.needUpdateFormula) - this.updateFormulas(); - this.needUpdateFormula = false; + if ( this.needUpdateFormula ) { + this.needUpdateFormula = false; + + this.formulas.fillFormulasGroups(); + this.formulas.fillFunctions('All'); + } this.formulas.show(); } }, @@ -123,7 +157,7 @@ define([ } }, - loadingFormulas: function () { + loadingFormulas: function (descrarr) { var i = 0, j = 0, ascGroupName, ascFunctions, @@ -137,8 +171,6 @@ define([ allFunctionsGroup = null; if (store) { - var value = SSE.Views.FormulaLang.getDescription(Common.Utils.InternalSettings.get("sse-settings-func-locale")); - allFunctionsGroup = new SSE.Models.FormulaGroup ({ name : 'All', index : index, @@ -172,8 +204,8 @@ define([ index : funcInd, group : ascGroupName, name : ascFunctions[j].asc_getLocaleName(), - args : (value && value[funcname]) ? value[funcname].a : '', - desc : (value && value[funcname]) ? value[funcname].d : '' + args : (descrarr && descrarr[funcname]) ? descrarr[funcname].a : '', + desc : (descrarr && descrarr[funcname]) ? descrarr[funcname].d : '' }); funcInd += 1; @@ -190,15 +222,6 @@ define([ _.sortBy(allFunctions, function (model) {return model.get('name'); })); } } - }, - - updateFormulas: function () { - this.formulasGroups.reset(); - this.loadingFormulas(); - if (this.formulas.$window) { - this.formulas.fillFormulasGroups(); - this.formulas.fillFunctions('All'); - } } }); }); diff --git a/apps/spreadsheeteditor/main/app/controller/LeftMenu.js b/apps/spreadsheeteditor/main/app/controller/LeftMenu.js index 124df1cd1..70b5c0d4d 100644 --- a/apps/spreadsheeteditor/main/app/controller/LeftMenu.js +++ b/apps/spreadsheeteditor/main/app/controller/LeftMenu.js @@ -375,11 +375,6 @@ define([ this.api.asc_setAutoSaveGap(value); } - value = Common.localStorage.getItem("sse-settings-func-locale"); - Common.Utils.InternalSettings.set("sse-settings-func-locale", value); - if (value) value = SSE.Views.FormulaLang.get(value); - if (value!==null) this.api.asc_setLocalization(value); - value = Common.localStorage.getItem("sse-settings-reg-settings"); if (value!==null) this.api.asc_setLocale(parseInt(value)); diff --git a/apps/spreadsheeteditor/main/app/controller/Main.js b/apps/spreadsheeteditor/main/app/controller/Main.js index 4e70b20de..09db86c08 100644 --- a/apps/spreadsheeteditor/main/app/controller/Main.js +++ b/apps/spreadsheeteditor/main/app/controller/Main.js @@ -54,8 +54,7 @@ define([ 'spreadsheeteditor/main/app/collection/ShapeGroups', 'spreadsheeteditor/main/app/collection/TableTemplates', 'spreadsheeteditor/main/app/collection/EquationGroups', - 'spreadsheeteditor/main/app/controller/FormulaDialog', - 'spreadsheeteditor/main/app/view/FormulaLang' + 'spreadsheeteditor/main/app/controller/FormulaDialog' ], function () { 'use strict'; @@ -318,18 +317,6 @@ define([ this.api.asc_setLocale((this.editorConfig.lang) ? parseInt(Common.util.LanguageInfo.getLocalLanguageCode(this.editorConfig.lang)) : 0x0409); } - value = Common.localStorage.getItem("sse-settings-func-locale"); - if (value===null) { - var lang = ((this.editorConfig.lang) ? this.editorConfig.lang : 'en').split(/[\-\_]/)[0].toLowerCase(); - Common.Utils.InternalSettings.set("sse-settings-func-locale", lang); - if (lang !== 'en') - value = SSE.Views.FormulaLang.get(lang); - } else { - Common.Utils.InternalSettings.set("sse-settings-func-locale", value); - value = SSE.Views.FormulaLang.get(value); - } - if (value) this.api.asc_setLocalization(value); - value = Common.localStorage.getBool("sse-settings-r1c1"); Common.Utils.InternalSettings.set("sse-settings-r1c1", value); this.api.asc_setR1C1Mode(value); diff --git a/apps/spreadsheeteditor/main/app/view/FormulaLang.js b/apps/spreadsheeteditor/main/app/view/FormulaLang.js deleted file mode 100644 index b3a5915c6..000000000 --- a/apps/spreadsheeteditor/main/app/view/FormulaLang.js +++ /dev/null @@ -1,123 +0,0 @@ -/* - * - * (c) Copyright Ascensio System SIA 2010-2019 - * - * 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 - * -*/ -define([ -], function () { - 'use strict'; - - SSE.Views = SSE.Views || {}; - - SSE.Views.FormulaLang = new(function() { - var langJson = {}, - langDescJson = {}; - - var _createXMLHTTPObject = function() { - var xmlhttp; - try { - xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); - } - catch (e) { - try { - xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); - } - catch (E) { - xmlhttp = false; - } - } - if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { - xmlhttp = new XMLHttpRequest(); - } - return xmlhttp; - }; - - var _get = function(lang) { - if (!lang) return ''; - lang = lang.toLowerCase() ; - - if (langJson[lang]) - return langJson[lang]; - else if (lang == 'en') - return undefined; - else { - try { - var xhrObj = _createXMLHTTPObject(); - if (xhrObj && lang) { - xhrObj.open('GET', 'resources/formula-lang/' + lang + '.json', false); - xhrObj.send(''); - langJson[lang] = eval("(" + xhrObj.responseText + ")"); - return langJson[lang]; - } - } - catch (e) { - } - } - - return null; - }; - - - var _getDescription = function(lang) { - if (!lang) return ''; - lang = lang.toLowerCase() ; - - if (langDescJson[lang]) - return langDescJson[lang]; - else { - try { - var xhrObj = _createXMLHTTPObject(); - if (xhrObj && lang) { - xhrObj.open('GET', 'resources/formula-lang/' + lang + '_desc.json', false); - xhrObj.send(''); - if (xhrObj.status==200 || - (xhrObj.status==0 && !!xhrObj.responseURL && xhrObj.responseURL.startsWith('file://'))) - langDescJson[lang] = eval("(" + xhrObj.responseText + ")"); - else { - xhrObj.open('GET', 'resources/formula-lang/en_desc.json', false); - xhrObj.send(''); - langDescJson[lang] = eval("(" + xhrObj.responseText + ")"); - } - return langDescJson[lang]; - } - } - catch (e) { - } - } - - return null; - }; - - return { - get: _get, - getDescription: _getDescription - }; - })(); -});