Skip to content

Commit

Permalink
Merge pull request #2930 from ONLYOFFICE/fix/custom-function
Browse files Browse the repository at this point in the history
Fix/custom function
  • Loading branch information
JuliaRadzhabova authored Apr 12, 2024
2 parents 6778ceb + 8ffe79d commit a0118d7
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 42 deletions.
7 changes: 6 additions & 1 deletion apps/spreadsheeteditor/main/app/controller/DocumentHolder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3349,7 +3349,12 @@ define([
switch (type) {
case Asc.c_oAscPopUpSelectorType.Func:
iconCls = 'menu__icon btn-function';
hint = (funcdesc && funcdesc[origname]) ? funcdesc[origname].d : '';
if (funcdesc && funcdesc[origname])
hint = funcdesc[origname].d;
else {
var custom = me.api.asc_getCustomFunctionInfo(origname);
hint = custom ? custom.asc_getDescription() || '' : '';
}
break;
case Asc.c_oAscPopUpSelectorType.Table:
iconCls = 'menu__icon btn-menu-table';
Expand Down
102 changes: 91 additions & 11 deletions apps/spreadsheeteditor/main/app/controller/FormulaDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ define([
this.onApiSheetChanged();
}
this.api.asc_registerCallback('asc_onSendFunctionWizardInfo', _.bind(this.onSendFunctionWizardInfo, this));
this.api.asc_registerCallback('asc_onAddCustomFunction', _.bind(this.onAddCustomFunction, this));

return this;
},
Expand Down Expand Up @@ -189,17 +190,17 @@ define([
var me = this;
Common.Utils.InternalSettings.set("sse-settings-func-locale", lang);
if (me.langJson[lang]) {
me.api.asc_setLocalization(me.langJson[lang]);
me.api.asc_setLocalization(me.langJson[lang], lang);
Common.NotificationCenter.trigger('formula:settings', this);
} else if (lang == 'en') {
me.api.asc_setLocalization(undefined);
me.api.asc_setLocalization(undefined, lang);
Common.NotificationCenter.trigger('formula:settings', this);
} else {
Common.Utils.loadConfig('resources/formula-lang/' + lang + '.json',
function (config) {
if ( config != 'error' ) {
me.langJson[lang] = config;
me.api.asc_setLocalization(config);
me.api.asc_setLocalization(config, lang);
Common.NotificationCenter.trigger('formula:settings', this);
}
});
Expand Down Expand Up @@ -257,11 +258,12 @@ define([
var name = props.asc_getName(),
origin = this.api.asc_getFormulaNameByLocale(name),
descrarr = this.getDescription(Common.Utils.InternalSettings.get("sse-settings-func-locale")),
custom = this.api.asc_getCustomFunctionInfo(origin),
funcprops = {
name: name,
origin: origin,
args: ((descrarr && descrarr[origin]) ? descrarr[origin].a : '').replace(/[,;]/g, this.api.asc_getFunctionArgumentSeparator()),
desc: (descrarr && descrarr[origin]) ? descrarr[origin].d : ''
desc: (descrarr && descrarr[origin]) ? descrarr[origin].d : custom ? custom.asc_getDescription() || '' : ''
};

(new SSE.Views.FormulaWizard({
Expand Down Expand Up @@ -306,16 +308,24 @@ define([
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 = [];
functions = [],
allFunctionsGroup = this.formulasGroups ? this.formulasGroups.findWhere({name : 'All'}) : null,
allFunctions = allFunctionsGroup ? allFunctionsGroup.get('functions') : null;

for (var j = 0; j < arr.length; j++) {
var funcname = arr[j];
var funcname = arr[j],
custom = descrarr && descrarr[funcname] ? null : this.api.asc_getCustomFunctionInfo(funcname),
desc = (descrarr && descrarr[funcname]) ? descrarr[funcname].d : custom ? custom.asc_getDescription() || '' : '';

if (!desc && allFunctions && !_.find(allFunctions, function(item){ return item.get('origin')===funcname; }))
continue;
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 : ''
desc : desc
}));
}
return functions;
Expand Down Expand Up @@ -345,7 +355,6 @@ define([
caption : this['sCategory' + ascGroupName] || ascGroupName
});
if (last10FunctionsGroup) {
last10FunctionsGroup.set('functions', this.loadingLast10Formulas(descrarr));
store.push(last10FunctionsGroup);
index += 1;
}
Expand Down Expand Up @@ -381,14 +390,15 @@ define([
functions = [];

for (j = 0; j < ascFunctions.length; j += 1) {
var funcname = ascFunctions[j].asc_getName();
var funcname = ascFunctions[j].asc_getName(),
custom = descrarr && descrarr[funcname] ? null : this.api.asc_getCustomFunctionInfo(funcname);
var func = new SSE.Models.FormulaModel({
index : funcInd,
group : ascGroupName,
name : ascFunctions[j].asc_getLocaleName(),
origin: funcname,
args : ((descrarr && descrarr[funcname]) ? descrarr[funcname].a : '').replace(/[,;]/g, separator),
desc : (descrarr && descrarr[funcname]) ? descrarr[funcname].d : ''
desc : (descrarr && descrarr[funcname]) ? descrarr[funcname].d : custom ? custom.asc_getDescription() || '' : ''
});

funcInd += 1;
Expand All @@ -403,11 +413,80 @@ define([

allFunctionsGroup.set('functions',
_.sortBy(allFunctions, function (model) {return model.get('name'); }));

last10FunctionsGroup && last10FunctionsGroup.set('functions', this.loadingLast10Formulas(descrarr));
}
}
(!suppressEvent || this._formulasInited) && this.formulaTab && this.formulaTab.fillFunctions();
},

onAddCustomFunction: function() {
this.needUpdateFormula = true;

var i = 0, j = 0,
customGroupName = 'Custom',
ascFunctions,
functions,
store = this.formulasGroups,
funcInd = 0,
info = null,
allFunctions = [],
allFunctionsGroup = null,
customFunctionsGroup = null;

if (store) {
allFunctionsGroup = this.formulasGroups.findWhere({name : 'All'});
if (allFunctionsGroup) {
allFunctions = allFunctionsGroup.get('functions');
for (i = 0; i < allFunctions.length; i++) {
if (allFunctions[i].get('group')===customGroupName) {
allFunctions.splice(i, 1);
i--;
}
}
}

customFunctionsGroup = this.formulasGroups.findWhere({name : customGroupName});
if (!customFunctionsGroup) {
customFunctionsGroup = new SSE.Models.FormulaGroup ({
name : customGroupName,
index : store.length,
store : store,
caption : this['sCategory' + customGroupName] || customGroupName
});
store.push(customFunctionsGroup);
}
info = this.api.asc_getFormulasInfo();
for (i = 0; i < info.length; i += 1) {
functions = [];
if (info[i].asc_getGroupName()===customGroupName) {
ascFunctions = info[i].asc_getFormulasArray();
for (j = 0; j < ascFunctions.length; j += 1) {
var funcname = ascFunctions[j].asc_getName(),
custom = this.api.asc_getCustomFunctionInfo(funcname);
var func = new SSE.Models.FormulaModel({
index : funcInd,
group : customGroupName,
name : ascFunctions[j].asc_getLocaleName(),
origin: funcname,
args : '',
desc : custom ? custom.asc_getDescription() || '' : ''
});

funcInd += 1;

functions.push(func);
allFunctions.push(func);
}
customFunctionsGroup.set('functions', _.sortBy(functions, function (model) {return model.get('name'); }));
allFunctionsGroup.set('functions', _.sortBy(allFunctions, function (model) {return model.get('name'); }));
break;
}
}
this.formulaTab && this.formulaTab.updateCustom();
}
},

onTabActive: function (tab) {
if ( tab == 'formula' && !this._formulasInited && this.formulaTab) {
this.formulaTab.fillFunctions();
Expand Down Expand Up @@ -492,7 +571,8 @@ define([
sCategoryLookupAndReference: 'Lookup and reference',
sCategoryMathematic: 'Math and trigonometry',
sCategoryStatistical: 'Statistical',
sCategoryTextAndData: 'Text and data'
sCategoryTextAndData: 'Text and data',
sCategoryCustom: 'Custom'

}, SSE.Controllers.FormulaDialog || {}));
});
92 changes: 66 additions & 26 deletions apps/spreadsheeteditor/main/app/view/FormulaTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,22 +542,42 @@ define([
formulaDialog = SSE.getController('FormulaDialog'),
group = me.formulasGroups.findWhere({name : name});

if (group) {
var functions = group.get('functions');
var functions = group ? group.get('functions') : [];
functions && functions.forEach(function(item) {
arr.push(new Common.UI.MenuItem({
caption: item.get('name'),
value: item.get('origin')
}));
});
if (arr.length) {
var mnu = new Common.UI.MenuItem({
caption : formulaDialog['sCategory' + name] || name,
var btn = this.btnMore,
mnu;
if (btn.menu && btn.menu.rendered) {
for (var i = 0; i < btn.menu.items.length; i++) {
if (btn.menu.items[i].options.value===name) {
mnu = btn.menu.items[i];
break;
}
}
}
if (mnu) {
var menu = mnu.menu._innerMenu;
if (menu) {
menu.removeAll();
arr.forEach(function(item){
menu.addItem(item);
});
}
mnu.setVisible(arr.length>0);
} else {
mnu = new Common.UI.MenuItem({
caption: formulaDialog['sCategory' + name] || name,
value: name,
visible: arr.length>0,
menu: new Common.UI.Menu({
menuAlign: 'tl-tr',
items: [
{template: _.template('<div id="id-toolbar-formula-menu-'+ name +'" style="display: flex;" class="open"></div>')},
{ caption: '--' },
{template: _.template('<div id="id-toolbar-formula-menu-' + name + '" style="display: flex;" class="open"></div>')},
{caption: '--'},
{
caption: me.txtAdditional,
value: 'more',
Expand All @@ -572,35 +592,33 @@ define([
mnu.menu.on('show:after', function (menu, e) {
var internalMenu = menu._innerMenu;
internalMenu.scroller.update({alwaysVisibleY: true});
_.delay(function() {
_.delay(function () {
menu._innerMenu && menu._innerMenu.items[0].cmpEl.find('> a').focus();
}, 10);
}).on('keydown:before', function(menu, e) {
if (e.keyCode == Common.UI.Keys.LEFT || e.keyCode == Common.UI.Keys.ESC) {
var $parent = menu.cmpEl.parent();
if ($parent.hasClass('dropdown-submenu') && $parent.hasClass('over')) { // close submenu
$parent.removeClass('over');
$parent.find('> a').focus();
}
}).on('keydown:before', function (menu, e) {
if (e.keyCode == Common.UI.Keys.LEFT || e.keyCode == Common.UI.Keys.ESC) {
var $parent = menu.cmpEl.parent();
if ($parent.hasClass('dropdown-submenu') && $parent.hasClass('over')) { // close submenu
$parent.removeClass('over');
$parent.find('> a').focus();
}
}
});

// internal menu
var menu = new Common.UI.Menu({
maxHeight: 300,
cls: 'internal-menu',
items: arr,
outerMenu: {menu: mnu.menu, index: 0}
outerMenu: {menu: mnu.menu, index: 0}
});
menu.on('item:click', function (menu, item, e) {
me.fireEvent('function:apply', [{name: item.caption, origin: item.value}, false, name]);
});
mnu.menu._innerMenu = menu;
mnu.menu.setInnerMenu([{menu: menu, index: 0}]);

return mnu;
}
}
return mnu;
},

fillFunctions: function () {
Expand All @@ -620,16 +638,21 @@ define([

// more button
var me = this,
morearr = [];
['Cube', 'Database', 'Engineering', 'Information', 'Statistical'].forEach(function(name) {
var mnu = me.setMenuItemMenu(name);
mnu && morearr.push(mnu);
btn = this.btnMore,
morearr = [],
visiblecount = 0;

btn.menu && btn.menu.rendered && btn.menu.removeAll();

['Cube', 'Database', 'Engineering', 'Information', 'Statistical', 'Custom'].forEach(function(name) {
var mnu = me.setMenuItemMenu(name);
if (mnu) {
morearr.push(mnu);
mnu.visible && (visiblecount++);
}
});
var btn = this.btnMore;
if (morearr.length) {
if (btn.menu && btn.menu.rendered) {
btn.menu.removeAll();
morearr.forEach(function(item){
btn.menu.addItem(item);
});
Expand All @@ -651,14 +674,31 @@ define([
menu.cmpEl.attr({tabindex: "-1"});
});
}
Common.Utils.lockControls(Common.enumLock.noSubitems, morearr.length<1, {array: [btn]});
Common.Utils.lockControls(Common.enumLock.noSubitems, visiblecount<1, {array: [btn]});
}
},

updateRecent: function() {
this.formulasGroups && this.setButtonMenu(this.btnRecent, 'Last10');
},

updateCustom: function() {
var btn = this.btnMore,
mnu = this.formulasGroups ? this.setMenuItemMenu('Custom') : null;
if (mnu) {
var hasvisible = false;
if (btn.menu && btn.menu.rendered) {
for (var i = 0; i < btn.menu.items.length; i++) {
if (btn.menu.items[i].visible) {
hasvisible = true;
break;
}
}
}
Common.Utils.lockControls(Common.enumLock.noSubitems, !hasvisible, {array: [btn]});
}
},

setApi: function (api) {
this.api = api;
},
Expand Down
5 changes: 3 additions & 2 deletions apps/spreadsheeteditor/main/app/view/FormulaWizard.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,10 @@ define([
getArgumentName: function(argcount) {
var name = '',
namesLen = this.argsNames.length;
if ((!this.repeatedArg || this.repeatedArg.length<1) && argcount<namesLen && this.argsNames[argcount]!=='...') // no repeated args
if ((!this.repeatedArg || this.repeatedArg.length<1) && argcount<namesLen && this.argsNames[argcount]!=='...') { // no repeated args
name = this.argsNames[argcount];
else if (this.repeatedArg && this.repeatedArg.length>0 && this.argsNames[namesLen-1]==='...') {
(name==='') && (name = this.textArgument + (this.maxArgCount>1 ? (' ' + (argcount+1)) : ''));
} else if (this.repeatedArg && this.repeatedArg.length>0 && this.argsNames[namesLen-1]==='...') {
var repeatedLen = this.repeatedArg.length;
var req = namesLen-1 - repeatedLen; // required/no-repeated
if (argcount<req) // get required args as is
Expand Down
1 change: 1 addition & 0 deletions apps/spreadsheeteditor/main/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,7 @@
"SSE.Controllers.FormulaDialog.sCategoryMathematic": "Math & Trig",
"SSE.Controllers.FormulaDialog.sCategoryStatistical": "Statistical",
"SSE.Controllers.FormulaDialog.sCategoryTextAndData": "Text & Data",
"SSE.Controllers.FormulaDialog.sCategoryCustom": "Custom",
"SSE.Controllers.LeftMenu.newDocumentTitle": "Unnamed spreadsheet",
"SSE.Controllers.LeftMenu.textByColumns": "By columns",
"SSE.Controllers.LeftMenu.textByRows": "By rows",
Expand Down
Loading

0 comments on commit a0118d7

Please sign in to comment.