-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathregistry.ts
174 lines (163 loc) · 5.05 KB
/
registry.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* Copyright (c) Jupyter Development Team.
* Distributed under the terms of the Modified BSD License.
*/
import {
CommandToolbarButton,
LabIcon,
Toolbar
} from '@jupyterlab/ui-components';
import { CommandRegistry } from '@lumino/commands';
import { Widget } from '@lumino/widgets';
import { IToolbarWidgetRegistry, ToolbarRegistry } from '../tokens';
import { ISignal, Signal } from '@lumino/signaling';
/**
* Concrete implementation of IToolbarWidgetRegistry interface
*/
export class ToolbarWidgetRegistry implements IToolbarWidgetRegistry {
constructor(options: ToolbarRegistry.IOptions) {
this._defaultFactory = options.defaultFactory;
}
/**
* Default toolbar item factory
*/
get defaultFactory(): (
widgetFactory: string,
widget: Widget,
toolbarItem: ToolbarRegistry.IWidget
) => Widget {
return this._defaultFactory;
}
set defaultFactory(
factory: (
widgetFactory: string,
widget: Widget,
toolbarItem: ToolbarRegistry.IWidget
) => Widget
) {
this._defaultFactory = factory;
}
/**
* A signal emitted when a factory widget has been added.
*/
get factoryAdded(): ISignal<this, string> {
return this._factoryAdded;
}
/**
* Create a toolbar item widget
*
* @param widgetFactory The widget factory name that creates the toolbar
* @param widget The newly widget containing the toolbar
* @param toolbarItem The toolbar item definition
* @returns The widget to be inserted in the toolbar.
*/
createWidget(
widgetFactory: string,
widget: Widget,
toolbarItem: ToolbarRegistry.IWidget
): Widget {
const factory = this._widgets.get(widgetFactory)?.get(toolbarItem.name);
return factory
? factory(widget)
: this._defaultFactory(widgetFactory, widget, toolbarItem);
}
/**
* Add a new toolbar item factory
*
* @param widgetFactory The widget factory name that creates the toolbar
* @param toolbarItemName The unique toolbar item
* @param factory The factory function that receives the widget containing the toolbar and returns the toolbar widget.
* @returns The previously defined factory
*/
addFactory<T extends Widget = Widget>(
widgetFactory: string,
toolbarItemName: string,
factory: (main: T) => Widget
): ((main: T) => Widget) | undefined {
let namespace = this._widgets.get(widgetFactory);
const oldFactory = namespace?.get(toolbarItemName);
if (!namespace) {
namespace = new Map<string, (main: Widget) => Widget>();
this._widgets.set(widgetFactory, namespace);
}
namespace.set(toolbarItemName, factory);
this._factoryAdded.emit(toolbarItemName);
return oldFactory;
}
/**
* Register a new toolbar item factory
*
* @param widgetFactory The widget factory name that creates the toolbar
* @param toolbarItemName The unique toolbar item
* @param factory The factory function that receives the widget containing the toolbar and returns the toolbar widget.
* @returns The previously defined factory
*
* @deprecated since v4 use `addFactory` instead
*/
registerFactory<T extends Widget = Widget>(
widgetFactory: string,
toolbarItemName: string,
factory: (main: T) => Widget
): ((main: T) => Widget) | undefined {
return this.addFactory(widgetFactory, toolbarItemName, factory);
}
protected _defaultFactory: (
widgetFactory: string,
widget: Widget,
toolbarItem: ToolbarRegistry.IWidget
) => Widget;
protected _widgets: Map<string, Map<string, (main: Widget) => Widget>> =
new Map<string, Map<string, (main: Widget) => Widget>>();
protected _factoryAdded = new Signal<this, string>(this);
}
/**
* Create the default toolbar item widget factory
*
* @param commands Application commands registry
* @returns Default factory
*/
export function createDefaultFactory(
commands: CommandRegistry
): (
widgetFactory: string,
widget: Widget,
toolbarItem: ToolbarRegistry.IWidget
) => Widget {
return (
widgetFactory: string,
widget: Widget,
toolbarItem: ToolbarRegistry.IWidget
) => {
switch (toolbarItem.type ?? 'command') {
case 'command': {
const {
command: tId,
args: tArgs,
label: tLabel,
caption: tCaption,
icon: tIcon
} = toolbarItem;
const id = tId ?? '';
const args = { toolbar: true, ...tArgs };
const icon = tIcon ? LabIcon.resolve({ icon: tIcon }) : undefined;
const toolbar = (widget as any).toolbar as Toolbar;
// If there is an icon, undefined label will results in no label
// otherwise the label will be set using the setting or the command label
const label = icon ?? commands.icon(id, args) ? tLabel ?? '' : tLabel;
return new CommandToolbarButton({
commands,
id,
args,
icon,
label,
caption: tCaption as string,
noFocusOnClick: toolbar?.noFocusOnClick ?? false
});
}
case 'spacer':
return Toolbar.createSpacerItem();
default:
return new Widget();
}
};
}