UNPKG

4.44 kBJavaScriptView Raw
1/*
2 * Copyright (c) Jupyter Development Team.
3 * Distributed under the terms of the Modified BSD License.
4 */
5import { CommandToolbarButton, LabIcon, Toolbar } from '@jupyterlab/ui-components';
6import { Widget } from '@lumino/widgets';
7import { Signal } from '@lumino/signaling';
8/**
9 * Concrete implementation of IToolbarWidgetRegistry interface
10 */
11export class ToolbarWidgetRegistry {
12 constructor(options) {
13 this._widgets = new Map();
14 this._factoryAdded = new Signal(this);
15 this._defaultFactory = options.defaultFactory;
16 }
17 /**
18 * Default toolbar item factory
19 */
20 get defaultFactory() {
21 return this._defaultFactory;
22 }
23 set defaultFactory(factory) {
24 this._defaultFactory = factory;
25 }
26 /**
27 * A signal emitted when a factory widget has been added.
28 */
29 get factoryAdded() {
30 return this._factoryAdded;
31 }
32 /**
33 * Create a toolbar item widget
34 *
35 * @param widgetFactory The widget factory name that creates the toolbar
36 * @param widget The newly widget containing the toolbar
37 * @param toolbarItem The toolbar item definition
38 * @returns The widget to be inserted in the toolbar.
39 */
40 createWidget(widgetFactory, widget, toolbarItem) {
41 var _a;
42 const factory = (_a = this._widgets.get(widgetFactory)) === null || _a === void 0 ? void 0 : _a.get(toolbarItem.name);
43 return factory
44 ? factory(widget)
45 : this._defaultFactory(widgetFactory, widget, toolbarItem);
46 }
47 /**
48 * Add a new toolbar item factory
49 *
50 * @param widgetFactory The widget factory name that creates the toolbar
51 * @param toolbarItemName The unique toolbar item
52 * @param factory The factory function that receives the widget containing the toolbar and returns the toolbar widget.
53 * @returns The previously defined factory
54 */
55 addFactory(widgetFactory, toolbarItemName, factory) {
56 let namespace = this._widgets.get(widgetFactory);
57 const oldFactory = namespace === null || namespace === void 0 ? void 0 : namespace.get(toolbarItemName);
58 if (!namespace) {
59 namespace = new Map();
60 this._widgets.set(widgetFactory, namespace);
61 }
62 namespace.set(toolbarItemName, factory);
63 this._factoryAdded.emit(toolbarItemName);
64 return oldFactory;
65 }
66 /**
67 * Register a new toolbar item factory
68 *
69 * @param widgetFactory The widget factory name that creates the toolbar
70 * @param toolbarItemName The unique toolbar item
71 * @param factory The factory function that receives the widget containing the toolbar and returns the toolbar widget.
72 * @returns The previously defined factory
73 *
74 * @deprecated since v4 use `addFactory` instead
75 */
76 registerFactory(widgetFactory, toolbarItemName, factory) {
77 return this.addFactory(widgetFactory, toolbarItemName, factory);
78 }
79}
80/**
81 * Create the default toolbar item widget factory
82 *
83 * @param commands Application commands registry
84 * @returns Default factory
85 */
86export function createDefaultFactory(commands) {
87 return (widgetFactory, widget, toolbarItem) => {
88 var _a;
89 switch ((_a = toolbarItem.type) !== null && _a !== void 0 ? _a : 'command') {
90 case 'command': {
91 const { command: tId, args: tArgs, label: tLabel, caption: tCaption, icon: tIcon } = toolbarItem;
92 const id = tId !== null && tId !== void 0 ? tId : '';
93 const args = { toolbar: true, ...tArgs };
94 const icon = tIcon ? LabIcon.resolve({ icon: tIcon }) : undefined;
95 // If there is an icon, undefined label will results in no label
96 // otherwise the label will be set using the setting or the command label
97 const label = (icon !== null && icon !== void 0 ? icon : commands.icon(id, args)) ? tLabel !== null && tLabel !== void 0 ? tLabel : '' : tLabel;
98 return new CommandToolbarButton({
99 commands,
100 id,
101 args,
102 icon,
103 label,
104 caption: tCaption
105 });
106 }
107 case 'spacer':
108 return Toolbar.createSpacerItem();
109 default:
110 return new Widget();
111 }
112 };
113}
114//# sourceMappingURL=registry.js.map
\No newline at end of file