2016-04-01 13:17:09 +00:00
|
|
|
/*
|
|
|
|
*
|
2019-01-17 13:05:03 +00:00
|
|
|
* (c) Copyright Ascensio System SIA 2010-2019
|
2016-04-01 13:17:09 +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
|
|
|
|
*
|
2019-01-17 13:00:34 +00:00
|
|
|
* You can contact Ascensio System SIA at 20A-12 Ernesta Birznieka-Upisha
|
|
|
|
* street, Riga, Latvia, EU, LV-1050.
|
2016-04-01 13:17:09 +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
|
|
|
|
*
|
|
|
|
*/
|
2016-03-11 00:48:53 +00:00
|
|
|
/**
|
|
|
|
* FormulaDialog.js
|
|
|
|
*
|
|
|
|
* Formula Dialog Controller
|
|
|
|
*
|
|
|
|
* Created by Alexey.Musinov on 14/04/2014
|
2018-03-01 12:16:38 +00:00
|
|
|
* Copyright (c) 2018 Ascensio System SIA. All rights reserved.
|
2016-03-11 00:48:53 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
define([
|
|
|
|
'core',
|
|
|
|
'spreadsheeteditor/main/app/collection/FormulaGroups',
|
2019-06-20 09:51:27 +00:00
|
|
|
'spreadsheeteditor/main/app/view/FormulaDialog',
|
|
|
|
'spreadsheeteditor/main/app/view/FormulaTab'
|
2016-03-11 00:48:53 +00:00
|
|
|
], function () {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
SSE.Controllers = SSE.Controllers || {};
|
|
|
|
|
2019-06-20 09:51:27 +00:00
|
|
|
SSE.Controllers.FormulaDialog = Backbone.Controller.extend(_.extend({
|
2016-03-11 00:48:53 +00:00
|
|
|
models: [],
|
|
|
|
views: [
|
2019-06-20 09:51:27 +00:00
|
|
|
'FormulaDialog',
|
|
|
|
'FormulaTab'
|
2016-03-11 00:48:53 +00:00
|
|
|
],
|
|
|
|
collections: [
|
|
|
|
'FormulaGroups'
|
|
|
|
],
|
|
|
|
|
|
|
|
initialize: function () {
|
|
|
|
var me = this;
|
2019-03-04 13:02:57 +00:00
|
|
|
me.langJson = {};
|
|
|
|
me.langDescJson = {};
|
|
|
|
|
2016-03-11 00:48:53 +00:00
|
|
|
this.addListeners({
|
|
|
|
'FileMenu': {
|
|
|
|
'settings:apply': function() {
|
2019-07-19 12:31:04 +00:00
|
|
|
if (!me.mode || !me.mode.isEdit) return;
|
|
|
|
|
2016-03-11 00:48:53 +00:00
|
|
|
me.needUpdateFormula = true;
|
2019-02-25 13:14:41 +00:00
|
|
|
|
|
|
|
var lang = Common.localStorage.getItem("sse-settings-func-locale");
|
|
|
|
Common.Utils.InternalSettings.set("sse-settings-func-locale", lang);
|
|
|
|
|
|
|
|
me.formulasGroups.reset();
|
|
|
|
me.reloadTranslations(lang);
|
2016-03-11 00:48:53 +00:00
|
|
|
}
|
2019-06-20 09:51:27 +00:00
|
|
|
},
|
|
|
|
'FormulaTab': {
|
|
|
|
'function:apply': this.applyFunction
|
2019-06-24 12:57:28 +00:00
|
|
|
},
|
|
|
|
'Toolbar': {
|
2019-08-12 11:48:20 +00:00
|
|
|
'function:apply': this.applyFunction,
|
|
|
|
'tab:active': this.onTabActive
|
2016-03-11 00:48:53 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-06-25 07:39:09 +00:00
|
|
|
applyFunction: function(func, autocomplete, group) {
|
2019-06-20 09:51:27 +00:00
|
|
|
if (func) {
|
2019-06-24 12:57:28 +00:00
|
|
|
if (func.origin === 'more') {
|
2019-06-25 07:39:09 +00:00
|
|
|
this.showDialog(group);
|
2019-06-24 12:57:28 +00:00
|
|
|
} else {
|
|
|
|
this.api.asc_insertFormula(func.name, Asc.c_oAscPopUpSelectorType.Func, !!autocomplete);
|
|
|
|
!autocomplete && this.updateLast10Formulas(func.origin);
|
|
|
|
}
|
2019-06-20 09:51:27 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
setConfig: function(config) {
|
|
|
|
this.toolbar = config.toolbar;
|
|
|
|
this.formulaTab = this.createView('FormulaTab', {
|
|
|
|
toolbar: this.toolbar.toolbar,
|
|
|
|
formulasGroups: this.formulasGroups
|
|
|
|
});
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2016-03-11 00:48:53 +00:00
|
|
|
setApi: function (api) {
|
|
|
|
this.api = api;
|
|
|
|
|
2017-02-07 11:12:41 +00:00
|
|
|
if (this.formulasGroups && this.api) {
|
2019-05-27 13:19:48 +00:00
|
|
|
Common.Utils.InternalSettings.set("sse-settings-func-last", Common.localStorage.getItem("sse-settings-func-last"));
|
|
|
|
|
2019-08-12 11:48:20 +00:00
|
|
|
this.reloadTranslations(Common.localStorage.getItem("sse-settings-func-locale") || this.appOptions.lang, true);
|
2016-03-11 00:48:53 +00:00
|
|
|
|
2017-02-07 11:12:41 +00:00
|
|
|
var me = this;
|
2016-03-11 00:48:53 +00:00
|
|
|
|
2017-02-07 11:12:41 +00:00
|
|
|
this.formulas = new SSE.Views.FormulaDialog({
|
|
|
|
api : this.api,
|
2016-03-11 00:48:53 +00:00
|
|
|
toolclose : 'hide',
|
2017-02-07 11:12:41 +00:00
|
|
|
formulasGroups : this.formulasGroups,
|
2019-06-20 09:51:27 +00:00
|
|
|
handler : _.bind(this.applyFunction, this)
|
2016-03-11 00:48:53 +00:00
|
|
|
});
|
2017-02-07 11:12:41 +00:00
|
|
|
this.formulas.on({
|
2016-03-11 00:48:53 +00:00
|
|
|
'hide': function () {
|
2019-02-25 13:14:41 +00:00
|
|
|
me.api.asc_enableKeyEvents(true);
|
2016-03-11 00:48:53 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-02-07 11:12:41 +00:00
|
|
|
|
2019-06-24 12:57:28 +00:00
|
|
|
this.formulaTab && this.formulaTab.setApi(this.api);
|
|
|
|
|
2017-02-07 11:12:41 +00:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
setMode: function(mode) {
|
2019-07-19 12:31:04 +00:00
|
|
|
this.mode = mode;
|
2017-02-07 11:12:41 +00:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
onLaunch: function () {
|
|
|
|
this.formulasGroups = this.getApplication().getCollection('FormulaGroups');
|
2019-02-25 13:14:41 +00:00
|
|
|
|
2019-06-20 09:51:27 +00:00
|
|
|
var descriptions = ['Financial', 'Logical', 'TextAndData', 'DateAndTime', 'LookupAndReference', 'Mathematic', 'Cube', 'Database', 'Engineering', 'Information',
|
|
|
|
'Statistical', 'Last10'];
|
|
|
|
|
2019-02-25 13:14:41 +00:00
|
|
|
Common.Gateway.on('init', this.loadConfig.bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
loadConfig: function(data) {
|
|
|
|
this.appOptions = {};
|
|
|
|
this.appOptions.lang = data.config.lang;
|
|
|
|
},
|
|
|
|
|
2019-08-12 11:48:20 +00:00
|
|
|
reloadTranslations: function (lang, suppressEvent) {
|
2019-02-25 13:14:41 +00:00
|
|
|
var me = this;
|
|
|
|
lang = (lang || 'en').split(/[\-_]/)[0].toLowerCase();
|
|
|
|
|
2019-03-04 13:02:57 +00:00
|
|
|
Common.Utils.InternalSettings.set("sse-settings-func-locale", lang);
|
|
|
|
if (me.langJson[lang]) {
|
|
|
|
me.api.asc_setLocalization(me.langJson[lang]);
|
|
|
|
Common.NotificationCenter.trigger('formula:settings', this);
|
|
|
|
} else if (lang == 'en') {
|
|
|
|
me.api.asc_setLocalization(undefined);
|
|
|
|
Common.NotificationCenter.trigger('formula:settings', this);
|
|
|
|
} else {
|
2019-02-25 13:14:41 +00:00
|
|
|
Common.Utils.loadConfig('resources/formula-lang/' + lang + '.json',
|
|
|
|
function (config) {
|
2019-03-04 13:02:57 +00:00
|
|
|
if ( config != 'error' ) {
|
|
|
|
me.langJson[lang] = config;
|
2019-02-25 13:14:41 +00:00
|
|
|
me.api.asc_setLocalization(config);
|
2019-03-04 13:02:57 +00:00
|
|
|
Common.NotificationCenter.trigger('formula:settings', this);
|
|
|
|
}
|
2019-02-25 13:14:41 +00:00
|
|
|
});
|
2019-03-04 13:02:57 +00:00
|
|
|
}
|
2019-02-25 13:14:41 +00:00
|
|
|
|
2019-03-04 13:02:57 +00:00
|
|
|
if (me.langDescJson[lang])
|
2019-08-12 11:48:20 +00:00
|
|
|
me.loadingFormulas(me.langDescJson[lang], suppressEvent);
|
2019-03-04 13:02:57 +00:00
|
|
|
else {
|
|
|
|
Common.Utils.loadConfig('resources/formula-lang/' + lang + '_desc.json',
|
|
|
|
function (config) {
|
|
|
|
if ( config != 'error' ) {
|
|
|
|
me.langDescJson[lang] = config;
|
2019-08-12 11:48:20 +00:00
|
|
|
me.loadingFormulas(config, suppressEvent);
|
2019-03-04 13:02:57 +00:00
|
|
|
} else {
|
|
|
|
Common.Utils.loadConfig('resources/formula-lang/en_desc.json',
|
|
|
|
function (config) {
|
|
|
|
me.langDescJson[lang] = (config != 'error') ? config : null;
|
2019-08-12 11:48:20 +00:00
|
|
|
me.loadingFormulas(me.langDescJson[lang], suppressEvent);
|
2019-03-04 13:02:57 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
getDescription: function(lang) {
|
|
|
|
if (!lang) return '';
|
|
|
|
lang = lang.toLowerCase() ;
|
|
|
|
|
|
|
|
if (this.langDescJson[lang])
|
|
|
|
return this.langDescJson[lang];
|
|
|
|
return null;
|
2016-03-11 00:48:53 +00:00
|
|
|
},
|
|
|
|
|
2019-06-25 07:39:09 +00:00
|
|
|
showDialog: function (group) {
|
2016-03-11 00:48:53 +00:00
|
|
|
if (this.formulas) {
|
2019-02-25 13:14:41 +00:00
|
|
|
if ( this.needUpdateFormula ) {
|
|
|
|
this.needUpdateFormula = false;
|
|
|
|
|
2019-03-04 13:02:57 +00:00
|
|
|
if (this.formulas.$window) {
|
|
|
|
this.formulas.fillFormulasGroups();
|
|
|
|
}
|
2019-02-25 13:14:41 +00:00
|
|
|
}
|
2019-06-25 07:39:09 +00:00
|
|
|
this.formulas.show(group);
|
2016-03-11 00:48:53 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
hideDialog: function () {
|
|
|
|
if (this.formulas && this.formulas.isVisible()) {
|
|
|
|
this.formulas.hide();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-05-27 13:19:48 +00:00
|
|
|
updateLast10Formulas: function(formula) {
|
|
|
|
var arr = Common.Utils.InternalSettings.get("sse-settings-func-last") || 'SUM;AVERAGE;IF;HYPERLINK;COUNT;MAX;SIN;SUMIF;PMT;STDEV';
|
|
|
|
arr = arr.split(';');
|
|
|
|
var idx = _.indexOf(arr, formula);
|
|
|
|
arr.splice((idx<0) ? arr.length-1 : idx, 1);
|
|
|
|
arr.unshift(formula);
|
|
|
|
var val = arr.join(';');
|
|
|
|
Common.localStorage.setItem("sse-settings-func-last", val);
|
|
|
|
Common.Utils.InternalSettings.set("sse-settings-func-last", val);
|
|
|
|
|
|
|
|
if (this.formulasGroups) {
|
|
|
|
var group = this.formulasGroups.findWhere({name : 'Last10'});
|
|
|
|
group && group.set('functions', this.loadingLast10Formulas(this.getDescription(Common.Utils.InternalSettings.get("sse-settings-func-locale"))));
|
2019-06-24 12:57:28 +00:00
|
|
|
this.formulaTab && this.formulaTab.updateRecent();
|
2019-05-27 13:19:48 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
loadingLast10Formulas: function(descrarr) {
|
|
|
|
var arr = (Common.Utils.InternalSettings.get("sse-settings-func-last") || 'SUM;AVERAGE;IF;HYPERLINK;COUNT;MAX;SIN;SUMIF;PMT;STDEV').split(';'),
|
|
|
|
separator = this.api.asc_getFunctionArgumentSeparator(),
|
|
|
|
functions = [];
|
|
|
|
for (var j = 0; j < arr.length; j++) {
|
|
|
|
var funcname = arr[j];
|
|
|
|
functions.push(new SSE.Models.FormulaModel({
|
|
|
|
index : j,
|
|
|
|
group : 'Last10',
|
|
|
|
name : this.api.asc_getFormulaLocaleName(funcname),
|
|
|
|
origin: funcname,
|
|
|
|
args : ((descrarr && descrarr[funcname]) ? descrarr[funcname].a : '').replace(/[,;]/g, separator),
|
|
|
|
desc : (descrarr && descrarr[funcname]) ? descrarr[funcname].d : ''
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
return functions;
|
|
|
|
},
|
|
|
|
|
2019-08-12 11:48:20 +00:00
|
|
|
loadingFormulas: function (descrarr, suppressEvent) {
|
2016-03-11 00:48:53 +00:00
|
|
|
var i = 0, j = 0,
|
|
|
|
ascGroupName,
|
|
|
|
ascFunctions,
|
|
|
|
functions,
|
|
|
|
store = this.formulasGroups,
|
|
|
|
formulaGroup = null,
|
|
|
|
index = 0,
|
|
|
|
funcInd = 0,
|
|
|
|
info = null,
|
|
|
|
allFunctions = [],
|
2019-03-05 07:05:28 +00:00
|
|
|
allFunctionsGroup = null,
|
2019-05-27 13:19:48 +00:00
|
|
|
last10FunctionsGroup = null,
|
2019-03-05 07:05:28 +00:00
|
|
|
separator = this.api.asc_getFunctionArgumentSeparator();
|
2016-03-11 00:48:53 +00:00
|
|
|
|
|
|
|
if (store) {
|
2019-05-27 13:19:48 +00:00
|
|
|
ascGroupName = 'Last10';
|
|
|
|
last10FunctionsGroup = new SSE.Models.FormulaGroup ({
|
|
|
|
name : ascGroupName,
|
|
|
|
index : index,
|
2019-06-20 09:51:27 +00:00
|
|
|
store : store,
|
|
|
|
caption : this['sCategory' + ascGroupName] || ascGroupName
|
2019-05-27 13:19:48 +00:00
|
|
|
});
|
|
|
|
if (last10FunctionsGroup) {
|
|
|
|
last10FunctionsGroup.set('functions', this.loadingLast10Formulas(descrarr));
|
|
|
|
store.push(last10FunctionsGroup);
|
|
|
|
index += 1;
|
|
|
|
}
|
|
|
|
|
2019-06-20 09:51:27 +00:00
|
|
|
ascGroupName = 'All';
|
2016-03-11 00:48:53 +00:00
|
|
|
allFunctionsGroup = new SSE.Models.FormulaGroup ({
|
2019-06-20 09:51:27 +00:00
|
|
|
name : ascGroupName,
|
2016-03-11 00:48:53 +00:00
|
|
|
index : index,
|
2019-06-20 09:51:27 +00:00
|
|
|
store : store,
|
|
|
|
caption : this['sCategory' + ascGroupName] || ascGroupName
|
2016-03-11 00:48:53 +00:00
|
|
|
});
|
|
|
|
if (allFunctionsGroup) {
|
|
|
|
store.push(allFunctionsGroup);
|
2019-05-27 13:19:48 +00:00
|
|
|
index += 1;
|
|
|
|
}
|
2016-03-11 00:48:53 +00:00
|
|
|
|
2019-05-27 13:19:48 +00:00
|
|
|
if (allFunctionsGroup) {
|
2016-03-11 00:48:53 +00:00
|
|
|
info = this.api.asc_getFormulasInfo();
|
|
|
|
|
|
|
|
for (i = 0; i < info.length; i += 1) {
|
|
|
|
ascGroupName = info[i].asc_getGroupName();
|
|
|
|
ascFunctions = info[i].asc_getFormulasArray();
|
|
|
|
|
|
|
|
formulaGroup = new SSE.Models.FormulaGroup({
|
|
|
|
name : ascGroupName,
|
|
|
|
index : index,
|
2019-06-20 09:51:27 +00:00
|
|
|
store : store,
|
|
|
|
caption : this['sCategory' + ascGroupName] || ascGroupName
|
2016-03-11 00:48:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
index += 1;
|
|
|
|
|
|
|
|
functions = [];
|
|
|
|
|
|
|
|
for (j = 0; j < ascFunctions.length; j += 1) {
|
2016-04-28 15:31:14 +00:00
|
|
|
var funcname = ascFunctions[j].asc_getName();
|
2016-03-11 00:48:53 +00:00
|
|
|
var func = new SSE.Models.FormulaModel({
|
|
|
|
index : funcInd,
|
|
|
|
group : ascGroupName,
|
2016-04-28 14:37:33 +00:00
|
|
|
name : ascFunctions[j].asc_getLocaleName(),
|
2019-05-27 13:19:48 +00:00
|
|
|
origin: funcname,
|
2019-05-17 08:40:40 +00:00
|
|
|
args : ((descrarr && descrarr[funcname]) ? descrarr[funcname].a : '').replace(/[,;]/g, separator),
|
2019-02-25 13:14:41 +00:00
|
|
|
desc : (descrarr && descrarr[funcname]) ? descrarr[funcname].d : ''
|
2016-03-11 00:48:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
funcInd += 1;
|
|
|
|
|
|
|
|
functions.push(func);
|
|
|
|
allFunctions.push(func);
|
|
|
|
}
|
|
|
|
|
|
|
|
formulaGroup.set('functions', functions);
|
|
|
|
store.push(formulaGroup);
|
|
|
|
}
|
|
|
|
|
|
|
|
allFunctionsGroup.set('functions',
|
|
|
|
_.sortBy(allFunctions, function (model) {return model.get('name'); }));
|
|
|
|
}
|
|
|
|
}
|
2019-08-12 11:48:20 +00:00
|
|
|
!suppressEvent && this.formulaTab && this.formulaTab.fillFunctions();
|
2019-06-20 09:51:27 +00:00
|
|
|
},
|
2019-08-12 11:48:20 +00:00
|
|
|
|
|
|
|
onTabActive: function (tab) {
|
|
|
|
if ( tab == 'formula' && !this._formulasInited && this.formulaTab) {
|
|
|
|
this.formulaTab.fillFunctions();
|
|
|
|
this._formulasInited = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-06-20 09:51:27 +00:00
|
|
|
sCategoryAll: 'All',
|
|
|
|
sCategoryLast10: '10 last used',
|
|
|
|
sCategoryLogical: 'Logical',
|
|
|
|
sCategoryCube: 'Cube',
|
|
|
|
sCategoryDatabase: 'Database',
|
|
|
|
sCategoryDateAndTime: 'Date and time',
|
|
|
|
sCategoryEngineering: 'Engineering',
|
|
|
|
sCategoryFinancial: 'Financial',
|
|
|
|
sCategoryInformation: 'Information',
|
|
|
|
sCategoryLookupAndReference: 'Lookup and reference',
|
|
|
|
sCategoryMathematic: 'Math and trigonometry',
|
|
|
|
sCategoryStatistical: 'Statistical',
|
|
|
|
sCategoryTextAndData: 'Text and data'
|
|
|
|
|
|
|
|
}, SSE.Controllers.FormulaDialog || {}));
|
2016-03-11 00:48:53 +00:00
|
|
|
});
|