UNPKG

4.06 kBJavaScriptView Raw
1/*
2 * Copyright (c) Jupyter Development Team.
3 * Distributed under the terms of the Modified BSD License.
4 */
5/**
6 * Create the command options from the given semantic commands list
7 * and the given default values.
8 *
9 * @param app Jupyter Application
10 * @param semanticCommands Single semantic command or a list of commands
11 * @param defaultValues Default values
12 * @param trans Translation bundle
13 * @returns Command options
14 */
15export 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 // If a command returns a boolean, assume it is the execution success status
53 // So break if it is false.
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//# sourceMappingURL=utils.js.map
\No newline at end of file