@jupyterlab/apputils 0.19.1-alpha.0 → 0.19.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,318 +1,318 @@
1
- "use strict";
2
- // Copyright (c) Jupyter Development Team.
3
- // Distributed under the terms of the Modified BSD License.
4
- Object.defineProperty(exports, "__esModule", { value: true });
5
- const algorithm_1 = require("@phosphor/algorithm");
6
- const properties_1 = require("@phosphor/properties");
7
- const signaling_1 = require("@phosphor/signaling");
8
- const widgets_1 = require("@phosphor/widgets");
9
- /**
10
- * A class that keeps track of widget instances on an Application shell.
11
- *
12
- * #### Notes
13
- * The API surface area of this concrete implementation is substantially larger
14
- * than the instance tracker interface it implements. The interface is intended
15
- * for export by JupyterLab plugins that create widgets and have clients who may
16
- * wish to keep track of newly created widgets. This class, however, can be used
17
- * internally by plugins to restore state as well.
18
- */
19
- class InstanceTracker {
20
- /**
21
- * Create a new instance tracker.
22
- *
23
- * @param options - The instantiation options for an instance tracker.
24
- */
25
- constructor(options) {
26
- this._restore = null;
27
- this._tracker = new widgets_1.FocusTracker();
28
- this._currentChanged = new signaling_1.Signal(this);
29
- this._widgetAdded = new signaling_1.Signal(this);
30
- this._widgetUpdated = new signaling_1.Signal(this);
31
- this._widgets = [];
32
- this._currentWidget = null;
33
- this._isDisposed = false;
34
- this.namespace = options.namespace;
35
- this._tracker.currentChanged.connect(this._onCurrentChanged, this);
36
- }
37
- /**
38
- * A signal emitted when the current widget changes.
39
- */
40
- get currentChanged() {
41
- return this._currentChanged;
42
- }
43
- /**
44
- * A signal emitted when a widget is added.
45
- *
46
- * #### Notes
47
- * This signal will only fire when a widget is added to the tracker. It will
48
- * not fire if a widget is injected into the tracker.
49
- */
50
- get widgetAdded() {
51
- return this._widgetAdded;
52
- }
53
- /**
54
- * A signal emitted when a widget is updated.
55
- */
56
- get widgetUpdated() {
57
- return this._widgetUpdated;
58
- }
59
- /**
60
- * The current widget is the most recently focused or added widget.
61
- *
62
- * #### Notes
63
- * It is the most recently focused widget, or the most recently added
64
- * widget if no widget has taken focus.
65
- */
66
- get currentWidget() {
67
- return this._currentWidget;
68
- }
69
- /**
70
- * The number of widgets held by the tracker.
71
- */
72
- get size() {
73
- return this._tracker.widgets.length;
74
- }
75
- /**
76
- * Add a new widget to the tracker.
77
- *
78
- * @param widget - The widget being added.
79
- */
80
- add(widget) {
81
- if (this._tracker.has(widget)) {
82
- let warning = `${widget.id} already exists in the tracker.`;
83
- console.warn(warning);
84
- return Promise.reject(warning);
85
- }
86
- this._tracker.add(widget);
87
- this._widgets.push(widget);
88
- let injected = Private.injectedProperty.get(widget);
89
- let promise = Promise.resolve(void 0);
90
- if (injected) {
91
- return promise;
92
- }
93
- widget.disposed.connect(this._onWidgetDisposed, this);
94
- // Handle widget state restoration.
95
- if (this._restore) {
96
- let { state } = this._restore;
97
- let widgetName = this._restore.name(widget);
98
- if (widgetName) {
99
- let name = `${this.namespace}:${widgetName}`;
100
- let data = this._restore.args(widget);
101
- Private.nameProperty.set(widget, name);
102
- promise = state.save(name, { data });
103
- }
104
- }
105
- // If there is no focused widget, set this as the current widget.
106
- if (!this._tracker.currentWidget) {
107
- this._currentWidget = widget;
108
- this.onCurrentChanged(widget);
109
- this._currentChanged.emit(widget);
110
- }
111
- // Emit the widget added signal.
112
- this._widgetAdded.emit(widget);
113
- return promise;
114
- }
115
- /**
116
- * Test whether the tracker is disposed.
117
- */
118
- get isDisposed() {
119
- return this._isDisposed;
120
- }
121
- /**
122
- * Dispose of the resources held by the tracker.
123
- */
124
- dispose() {
125
- if (this.isDisposed) {
126
- return;
127
- }
128
- this._isDisposed = true;
129
- signaling_1.Signal.clearData(this);
130
- this._tracker.dispose();
131
- }
132
- /**
133
- * Find the first widget in the tracker that satisfies a filter function.
134
- *
135
- * @param - fn The filter function to call on each widget.
136
- *
137
- * #### Notes
138
- * If no widget is found, the value returned is `undefined`.
139
- */
140
- find(fn) {
141
- return algorithm_1.find(this._tracker.widgets, fn);
142
- }
143
- /**
144
- * Iterate through each widget in the tracker.
145
- *
146
- * @param fn - The function to call on each widget.
147
- */
148
- forEach(fn) {
149
- algorithm_1.each(this._tracker.widgets, widget => {
150
- fn(widget);
151
- });
152
- }
153
- /**
154
- * Filter the widgets in the tracker based on a predicate.
155
- *
156
- * @param fn - The function by which to filter.
157
- */
158
- filter(fn) {
159
- return this._tracker.widgets.filter(fn);
160
- }
161
- /**
162
- * Inject a foreign widget into the instance tracker.
163
- *
164
- * @param widget - The widget to inject into the tracker.
165
- *
166
- * #### Notes
167
- * Any widgets injected into an instance tracker will not have their state
168
- * saved by the tracker. The primary use case for widget injection is for a
169
- * plugin that offers a sub-class of an extant plugin to have its instances
170
- * share the same commands as the parent plugin (since most relevant commands
171
- * will use the `currentWidget` of the parent plugin's instance tracker). In
172
- * this situation, the sub-class plugin may well have its own instance tracker
173
- * for layout and state restoration in addition to injecting its widgets into
174
- * the parent plugin's instance tracker.
175
- */
176
- inject(widget) {
177
- Private.injectedProperty.set(widget, true);
178
- this.add(widget);
179
- }
180
- /**
181
- * Check if this tracker has the specified widget.
182
- *
183
- * @param widget - The widget whose existence is being checked.
184
- */
185
- has(widget) {
186
- return this._tracker.has(widget);
187
- }
188
- /**
189
- * Restore the widgets in this tracker's namespace.
190
- *
191
- * @param options - The configuration options that describe restoration.
192
- *
193
- * @returns A promise that resolves when restoration has completed.
194
- *
195
- * #### Notes
196
- * This function should almost never be invoked by client code. Its primary
197
- * use case is to be invoked by a layout restorer plugin that handles
198
- * multiple instance trackers and, when ready, asks them each to restore their
199
- * respective widgets.
200
- */
201
- restore(options) {
202
- const { command, registry, state, when } = options;
203
- const namespace = this.namespace;
204
- const promises = when
205
- ? [state.fetchNamespace(namespace)].concat(when)
206
- : [state.fetchNamespace(namespace)];
207
- this._restore = options;
208
- return Promise.all(promises).then(([saved]) => {
209
- return Promise.all(saved.map(item => {
210
- const { id, value } = item;
211
- const args = value && value.data;
212
- if (args === undefined) {
213
- return state.remove(item.id);
214
- }
215
- // Execute the command and if it fails, delete the state restore data.
216
- return registry.execute(command, args).catch(() => state.remove(id));
217
- }));
218
- });
219
- }
220
- /**
221
- * Save the restore data for a given widget.
222
- *
223
- * @param widget - The widget being saved.
224
- */
225
- save(widget) {
226
- const injected = Private.injectedProperty.get(widget);
227
- if (!this._restore || !this.has(widget) || injected) {
228
- return;
229
- }
230
- const { state } = this._restore;
231
- const widgetName = this._restore.name(widget);
232
- const oldName = Private.nameProperty.get(widget);
233
- const newName = widgetName ? `${this.namespace}:${widgetName}` : '';
234
- if (oldName && oldName !== newName) {
235
- state.remove(oldName);
236
- }
237
- // Set the name property irrespective of whether the new name is null.
238
- Private.nameProperty.set(widget, newName);
239
- if (newName) {
240
- const data = this._restore.args(widget);
241
- state.save(newName, { data });
242
- }
243
- if (oldName !== newName) {
244
- this._widgetUpdated.emit(widget);
245
- }
246
- }
247
- /**
248
- * Handle the current change event.
249
- *
250
- * #### Notes
251
- * The default implementation is a no-op.
252
- */
253
- onCurrentChanged(value) {
254
- /* no-op */
255
- }
256
- /**
257
- * Handle the current change signal from the internal focus tracker.
258
- */
259
- _onCurrentChanged(sender, args) {
260
- // Bail if the active widget did not change.
261
- if (args.newValue === this._currentWidget) {
262
- return;
263
- }
264
- this._currentWidget = args.newValue;
265
- this.onCurrentChanged(args.newValue);
266
- this._currentChanged.emit(args.newValue);
267
- }
268
- /**
269
- * Clean up after disposed widgets.
270
- */
271
- _onWidgetDisposed(widget) {
272
- const injected = Private.injectedProperty.get(widget);
273
- if (injected) {
274
- return;
275
- }
276
- // Handle widget removal.
277
- algorithm_1.ArrayExt.removeFirstOf(this._widgets, widget);
278
- // Handle the current widget being disposed.
279
- if (widget === this._currentWidget) {
280
- this._currentWidget =
281
- this._tracker.currentWidget ||
282
- this._widgets[this._widgets.length - 1] ||
283
- null;
284
- this._currentChanged.emit(this._currentWidget);
285
- this.onCurrentChanged(this._currentWidget);
286
- }
287
- // If there is no restore data, return.
288
- if (!this._restore) {
289
- return;
290
- }
291
- const { state } = this._restore;
292
- const name = Private.nameProperty.get(widget);
293
- if (name) {
294
- state.remove(name);
295
- }
296
- }
297
- }
298
- exports.InstanceTracker = InstanceTracker;
299
- /*
300
- * A namespace for private data.
301
- */
302
- var Private;
303
- (function (Private) {
304
- /**
305
- * An attached property to indicate whether a widget has been injected.
306
- */
307
- Private.injectedProperty = new properties_1.AttachedProperty({
308
- name: 'injected',
309
- create: () => false
310
- });
311
- /**
312
- * An attached property for a widget's ID in the state database.
313
- */
314
- Private.nameProperty = new properties_1.AttachedProperty({
315
- name: 'name',
316
- create: () => ''
317
- });
318
- })(Private || (Private = {}));
1
+ "use strict";
2
+ // Copyright (c) Jupyter Development Team.
3
+ // Distributed under the terms of the Modified BSD License.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ const algorithm_1 = require("@phosphor/algorithm");
6
+ const properties_1 = require("@phosphor/properties");
7
+ const signaling_1 = require("@phosphor/signaling");
8
+ const widgets_1 = require("@phosphor/widgets");
9
+ /**
10
+ * A class that keeps track of widget instances on an Application shell.
11
+ *
12
+ * #### Notes
13
+ * The API surface area of this concrete implementation is substantially larger
14
+ * than the instance tracker interface it implements. The interface is intended
15
+ * for export by JupyterLab plugins that create widgets and have clients who may
16
+ * wish to keep track of newly created widgets. This class, however, can be used
17
+ * internally by plugins to restore state as well.
18
+ */
19
+ class InstanceTracker {
20
+ /**
21
+ * Create a new instance tracker.
22
+ *
23
+ * @param options - The instantiation options for an instance tracker.
24
+ */
25
+ constructor(options) {
26
+ this._restore = null;
27
+ this._tracker = new widgets_1.FocusTracker();
28
+ this._currentChanged = new signaling_1.Signal(this);
29
+ this._widgetAdded = new signaling_1.Signal(this);
30
+ this._widgetUpdated = new signaling_1.Signal(this);
31
+ this._widgets = [];
32
+ this._currentWidget = null;
33
+ this._isDisposed = false;
34
+ this.namespace = options.namespace;
35
+ this._tracker.currentChanged.connect(this._onCurrentChanged, this);
36
+ }
37
+ /**
38
+ * A signal emitted when the current widget changes.
39
+ */
40
+ get currentChanged() {
41
+ return this._currentChanged;
42
+ }
43
+ /**
44
+ * A signal emitted when a widget is added.
45
+ *
46
+ * #### Notes
47
+ * This signal will only fire when a widget is added to the tracker. It will
48
+ * not fire if a widget is injected into the tracker.
49
+ */
50
+ get widgetAdded() {
51
+ return this._widgetAdded;
52
+ }
53
+ /**
54
+ * A signal emitted when a widget is updated.
55
+ */
56
+ get widgetUpdated() {
57
+ return this._widgetUpdated;
58
+ }
59
+ /**
60
+ * The current widget is the most recently focused or added widget.
61
+ *
62
+ * #### Notes
63
+ * It is the most recently focused widget, or the most recently added
64
+ * widget if no widget has taken focus.
65
+ */
66
+ get currentWidget() {
67
+ return this._currentWidget;
68
+ }
69
+ /**
70
+ * The number of widgets held by the tracker.
71
+ */
72
+ get size() {
73
+ return this._tracker.widgets.length;
74
+ }
75
+ /**
76
+ * Add a new widget to the tracker.
77
+ *
78
+ * @param widget - The widget being added.
79
+ */
80
+ add(widget) {
81
+ if (this._tracker.has(widget)) {
82
+ let warning = `${widget.id} already exists in the tracker.`;
83
+ console.warn(warning);
84
+ return Promise.reject(warning);
85
+ }
86
+ this._tracker.add(widget);
87
+ this._widgets.push(widget);
88
+ let injected = Private.injectedProperty.get(widget);
89
+ let promise = Promise.resolve(void 0);
90
+ if (injected) {
91
+ return promise;
92
+ }
93
+ widget.disposed.connect(this._onWidgetDisposed, this);
94
+ // Handle widget state restoration.
95
+ if (this._restore) {
96
+ let { state } = this._restore;
97
+ let widgetName = this._restore.name(widget);
98
+ if (widgetName) {
99
+ let name = `${this.namespace}:${widgetName}`;
100
+ let data = this._restore.args(widget);
101
+ Private.nameProperty.set(widget, name);
102
+ promise = state.save(name, { data });
103
+ }
104
+ }
105
+ // If there is no focused widget, set this as the current widget.
106
+ if (!this._tracker.currentWidget) {
107
+ this._currentWidget = widget;
108
+ this.onCurrentChanged(widget);
109
+ this._currentChanged.emit(widget);
110
+ }
111
+ // Emit the widget added signal.
112
+ this._widgetAdded.emit(widget);
113
+ return promise;
114
+ }
115
+ /**
116
+ * Test whether the tracker is disposed.
117
+ */
118
+ get isDisposed() {
119
+ return this._isDisposed;
120
+ }
121
+ /**
122
+ * Dispose of the resources held by the tracker.
123
+ */
124
+ dispose() {
125
+ if (this.isDisposed) {
126
+ return;
127
+ }
128
+ this._isDisposed = true;
129
+ signaling_1.Signal.clearData(this);
130
+ this._tracker.dispose();
131
+ }
132
+ /**
133
+ * Find the first widget in the tracker that satisfies a filter function.
134
+ *
135
+ * @param - fn The filter function to call on each widget.
136
+ *
137
+ * #### Notes
138
+ * If no widget is found, the value returned is `undefined`.
139
+ */
140
+ find(fn) {
141
+ return algorithm_1.find(this._tracker.widgets, fn);
142
+ }
143
+ /**
144
+ * Iterate through each widget in the tracker.
145
+ *
146
+ * @param fn - The function to call on each widget.
147
+ */
148
+ forEach(fn) {
149
+ algorithm_1.each(this._tracker.widgets, widget => {
150
+ fn(widget);
151
+ });
152
+ }
153
+ /**
154
+ * Filter the widgets in the tracker based on a predicate.
155
+ *
156
+ * @param fn - The function by which to filter.
157
+ */
158
+ filter(fn) {
159
+ return this._tracker.widgets.filter(fn);
160
+ }
161
+ /**
162
+ * Inject a foreign widget into the instance tracker.
163
+ *
164
+ * @param widget - The widget to inject into the tracker.
165
+ *
166
+ * #### Notes
167
+ * Any widgets injected into an instance tracker will not have their state
168
+ * saved by the tracker. The primary use case for widget injection is for a
169
+ * plugin that offers a sub-class of an extant plugin to have its instances
170
+ * share the same commands as the parent plugin (since most relevant commands
171
+ * will use the `currentWidget` of the parent plugin's instance tracker). In
172
+ * this situation, the sub-class plugin may well have its own instance tracker
173
+ * for layout and state restoration in addition to injecting its widgets into
174
+ * the parent plugin's instance tracker.
175
+ */
176
+ inject(widget) {
177
+ Private.injectedProperty.set(widget, true);
178
+ this.add(widget);
179
+ }
180
+ /**
181
+ * Check if this tracker has the specified widget.
182
+ *
183
+ * @param widget - The widget whose existence is being checked.
184
+ */
185
+ has(widget) {
186
+ return this._tracker.has(widget);
187
+ }
188
+ /**
189
+ * Restore the widgets in this tracker's namespace.
190
+ *
191
+ * @param options - The configuration options that describe restoration.
192
+ *
193
+ * @returns A promise that resolves when restoration has completed.
194
+ *
195
+ * #### Notes
196
+ * This function should almost never be invoked by client code. Its primary
197
+ * use case is to be invoked by a layout restorer plugin that handles
198
+ * multiple instance trackers and, when ready, asks them each to restore their
199
+ * respective widgets.
200
+ */
201
+ restore(options) {
202
+ const { command, registry, state, when } = options;
203
+ const namespace = this.namespace;
204
+ const promises = when
205
+ ? [state.fetchNamespace(namespace)].concat(when)
206
+ : [state.fetchNamespace(namespace)];
207
+ this._restore = options;
208
+ return Promise.all(promises).then(([saved]) => {
209
+ return Promise.all(saved.map(item => {
210
+ const { id, value } = item;
211
+ const args = value && value.data;
212
+ if (args === undefined) {
213
+ return state.remove(item.id);
214
+ }
215
+ // Execute the command and if it fails, delete the state restore data.
216
+ return registry.execute(command, args).catch(() => state.remove(id));
217
+ }));
218
+ });
219
+ }
220
+ /**
221
+ * Save the restore data for a given widget.
222
+ *
223
+ * @param widget - The widget being saved.
224
+ */
225
+ save(widget) {
226
+ const injected = Private.injectedProperty.get(widget);
227
+ if (!this._restore || !this.has(widget) || injected) {
228
+ return;
229
+ }
230
+ const { state } = this._restore;
231
+ const widgetName = this._restore.name(widget);
232
+ const oldName = Private.nameProperty.get(widget);
233
+ const newName = widgetName ? `${this.namespace}:${widgetName}` : '';
234
+ if (oldName && oldName !== newName) {
235
+ state.remove(oldName);
236
+ }
237
+ // Set the name property irrespective of whether the new name is null.
238
+ Private.nameProperty.set(widget, newName);
239
+ if (newName) {
240
+ const data = this._restore.args(widget);
241
+ state.save(newName, { data });
242
+ }
243
+ if (oldName !== newName) {
244
+ this._widgetUpdated.emit(widget);
245
+ }
246
+ }
247
+ /**
248
+ * Handle the current change event.
249
+ *
250
+ * #### Notes
251
+ * The default implementation is a no-op.
252
+ */
253
+ onCurrentChanged(value) {
254
+ /* no-op */
255
+ }
256
+ /**
257
+ * Handle the current change signal from the internal focus tracker.
258
+ */
259
+ _onCurrentChanged(sender, args) {
260
+ // Bail if the active widget did not change.
261
+ if (args.newValue === this._currentWidget) {
262
+ return;
263
+ }
264
+ this._currentWidget = args.newValue;
265
+ this.onCurrentChanged(args.newValue);
266
+ this._currentChanged.emit(args.newValue);
267
+ }
268
+ /**
269
+ * Clean up after disposed widgets.
270
+ */
271
+ _onWidgetDisposed(widget) {
272
+ const injected = Private.injectedProperty.get(widget);
273
+ if (injected) {
274
+ return;
275
+ }
276
+ // Handle widget removal.
277
+ algorithm_1.ArrayExt.removeFirstOf(this._widgets, widget);
278
+ // Handle the current widget being disposed.
279
+ if (widget === this._currentWidget) {
280
+ this._currentWidget =
281
+ this._tracker.currentWidget ||
282
+ this._widgets[this._widgets.length - 1] ||
283
+ null;
284
+ this._currentChanged.emit(this._currentWidget);
285
+ this.onCurrentChanged(this._currentWidget);
286
+ }
287
+ // If there is no restore data, return.
288
+ if (!this._restore) {
289
+ return;
290
+ }
291
+ const { state } = this._restore;
292
+ const name = Private.nameProperty.get(widget);
293
+ if (name) {
294
+ state.remove(name);
295
+ }
296
+ }
297
+ }
298
+ exports.InstanceTracker = InstanceTracker;
299
+ /*
300
+ * A namespace for private data.
301
+ */
302
+ var Private;
303
+ (function (Private) {
304
+ /**
305
+ * An attached property to indicate whether a widget has been injected.
306
+ */
307
+ Private.injectedProperty = new properties_1.AttachedProperty({
308
+ name: 'injected',
309
+ create: () => false
310
+ });
311
+ /**
312
+ * An attached property for a widget's ID in the state database.
313
+ */
314
+ Private.nameProperty = new properties_1.AttachedProperty({
315
+ name: 'name',
316
+ create: () => ''
317
+ });
318
+ })(Private || (Private = {}));