1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | export function createSemanticCommand(app, semanticCommands, defaultValues, trans) {
|
16 | const { commands, shell } = app;
|
17 | const commandList = Array.isArray(semanticCommands)
|
18 | ? semanticCommands
|
19 | : [semanticCommands];
|
20 | return {
|
21 | label: concatenateTexts('label'),
|
22 | caption: concatenateTexts('caption'),
|
23 | isEnabled: () => {
|
24 | var _a;
|
25 | const isEnabled = reduceAttribute('isEnabled');
|
26 | return ((isEnabled.length > 0 &&
|
27 | !isEnabled.some(enabled => enabled === false)) ||
|
28 | ((_a = defaultValues.isEnabled) !== null && _a !== void 0 ? _a : false));
|
29 | },
|
30 | isToggled: () => {
|
31 | var _a;
|
32 | const isToggled = reduceAttribute('isToggled');
|
33 | return (isToggled.some(enabled => enabled === true) ||
|
34 | ((_a = defaultValues.isToggled) !== null && _a !== void 0 ? _a : false));
|
35 | },
|
36 | isVisible: () => {
|
37 | var _a;
|
38 | const isVisible = reduceAttribute('isVisible');
|
39 | return ((isVisible.length > 0 &&
|
40 | !isVisible.some(visible => visible === false)) ||
|
41 | ((_a = defaultValues.isVisible) !== null && _a !== void 0 ? _a : true));
|
42 | },
|
43 | execute: async () => {
|
44 | const widget = shell.currentWidget;
|
45 | const commandIds = commandList.map(cmd => widget !== null ? cmd.getActiveCommandId(widget) : null);
|
46 | const toExecute = commandIds.filter(commandId => commandId !== null && commands.isEnabled(commandId));
|
47 | let result = null;
|
48 | if (toExecute.length > 0) {
|
49 | for (const commandId of toExecute) {
|
50 | result = await commands.execute(commandId);
|
51 | if (typeof result === 'boolean' && result === false) {
|
52 |
|
53 |
|
54 | break;
|
55 | }
|
56 | }
|
57 | }
|
58 | else if (defaultValues.execute) {
|
59 | result = await commands.execute(defaultValues.execute);
|
60 | }
|
61 | return result;
|
62 | }
|
63 | };
|
64 | function reduceAttribute(attribute) {
|
65 | const widget = shell.currentWidget;
|
66 | const commandIds = commandList.map(cmd => widget !== null ? cmd.getActiveCommandId(widget) : null);
|
67 | const attributes = commandIds
|
68 | .filter(commandId => commandId !== null)
|
69 | .map(commandId => commands[attribute](commandId));
|
70 | return attributes;
|
71 | }
|
72 | function concatenateTexts(attribute) {
|
73 | return () => {
|
74 | var _a;
|
75 | const texts = reduceAttribute(attribute).map((text, textIndex) => attribute == 'caption' && textIndex > 0
|
76 | ? text.toLocaleLowerCase()
|
77 | : text);
|
78 | switch (texts.length) {
|
79 | case 0:
|
80 | return (_a = defaultValues[attribute]) !== null && _a !== void 0 ? _a : '';
|
81 | case 1:
|
82 | return texts[0];
|
83 | default: {
|
84 | const hasEllipsis = texts.some(l => /…$/.test(l));
|
85 | const main = texts
|
86 | .slice(undefined, -1)
|
87 | .map(l => l.replace(/…$/, ''))
|
88 | .join(', ');
|
89 | const end = texts.slice(-1)[0].replace(/…$/, '') + (hasEllipsis ? '…' : '');
|
90 | return trans.__('%1 and %2', main, end);
|
91 | }
|
92 | }
|
93 | };
|
94 | }
|
95 | }
|
96 |
|
\ | No newline at end of file |