-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathregistry.ts
50 lines (45 loc) · 1.43 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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { DisposableDelegate, IDisposable } from '@lumino/disposable';
import { Widget } from '@lumino/widgets';
import { ITableOfContentsRegistry, TableOfContents } from './tokens';
/**
* Class for registering table of contents generators.
*/
export class TableOfContentsRegistry implements ITableOfContentsRegistry {
/**
* Finds a table of contents model for a widget.
*
* ## Notes
*
* - If unable to find a table of contents model, the method return `undefined`.
*
* @param widget - widget
* @param configuration - Default model configuration
* @returns Table of contents model
*/
getModel(
widget: Widget,
configuration?: TableOfContents.IConfig
): TableOfContents.Model | undefined {
for (const generator of this._generators.values()) {
if (generator.isApplicable(widget)) {
return generator.createNew(widget, configuration);
}
}
}
/**
* Adds a table of contents generator to the registry.
*
* @param generator - table of contents generator
*/
add(generator: TableOfContents.IFactory): IDisposable {
const id = this._idCounter++;
this._generators.set(id, generator);
return new DisposableDelegate(() => {
this._generators.delete(id);
});
}
private _generators = new Map<number, TableOfContents.IFactory>();
private _idCounter = 0;
}