This repository has been archived by the owner on Nov 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathbootstrapper.ts
181 lines (161 loc) · 4.73 KB
/
bootstrapper.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
175
176
177
178
179
180
181
/*-----------------------------------------------------------------------------
| Copyright (c) 2014-2015, S. Chris Colbert
|
| Distributed under the terms of the BSD 3-Clause License.
|
| The full license is in the file LICENSE, distributed with this software.
|----------------------------------------------------------------------------*/
module phosphor.shell {
import Container = di.Container;
import IContainer = di.IContainer;
import createBoxSizing = utility.createBoxSizing;
import Widget = widgets.Widget;
/**
* A class which manages bootstrapping an application.
*
* An application will typically define its own Bootstrapper subclass
* which overrides the necessary methods to customize the application.
*/
export
class Bootstrapper {
/**
* Construct a new bootstrapper.
*/
constructor() { }
/**
* Get the dependency injection container for the application.
*
* This is created by the `createContainer` method.
*/
get container(): IContainer {
return this._container;
}
/**
* Get the plugin list for the application.
*
* This is created by the `createPluginList` method.
*/
get pluginList(): IPluginList {
return this._pluginList;
}
/**
* Get the top-level shell view for the application.
*
* This is created by the `createShell` method.
*/
get shell(): IShellView {
return this._shell;
}
/**
* Run the bootstrapper.
*
* This invokes the various bootstrap methods in the proper order
* and updates the internal state of the bootstrapper.
*
* This method should not be reimplemented.
*/
run(): void {
this._container = this.createContainer();
this.configureContainer();
this._shell = this.createShell();
this.configureShell();
this._pluginList = this.createPluginList();
this.configurePlugins().then(() => {
this.finalize();
}).catch(ex => {
console.error('plugin initialization failed', ex);
});
}
/**
* Create the dependency injection container for the application.
*
* This can be reimplemented by subclasses as needed.
*
* The default implementation creates an instance of `Container`.
*/
protected createContainer(): IContainer {
return new Container();
}
/**
* Create the application plugin list.
*
* This can be reimplmented by subclasses as needed.
*
* The default implementation resolves an `IPluginList`.
*/
protected createPluginList(): IPluginList {
return this.container.resolve(IPluginList);
}
/**
* Create the application shell widget.
*
* This can be reimplemented by subclasses as needed.
*
* The default implementation resolves an `IShellView`.
*/
protected createShell(): IShellView {
return this.container.resolve(IShellView);
}
/**
* Configure the application dependency injection container.
*
* This can be reimplemented by subclasses as needed.
*/
protected configureContainer(): void {
var container = this.container;
if (!container.isRegistered(IContainer)) {
container.registerInstance(IContainer, container);
}
if (!container.isRegistered(IPluginList)) {
container.registerType(IPluginList, PluginList);
}
if (!container.isRegistered(IShellView)) {
container.registerType(IShellView, ShellView);
}
}
/**
* Configure the application plugins.
*
* Subclasses should reimplement this method to add the application
* plugins to the plugin list. This should return a promise which
* resolves once all plugins are initialized.
*
* The default implementation returns an empty resolved promise.
*/
protected configurePlugins(): Promise<void> {
return Promise.resolve<void>();
}
/**
* Configure the application shell widget.
*
* This can be reimplemented by subclasses as needed.
*/
protected configureShell(): void { }
/**
* Finalize the bootstrapping process.
*
* This is called after all plugins are resolved and intialized.
*
* It is the last method called in the bootstrapping process.
*
* This can be reimplemented by subclasses as needed.
*
* The default implementation attaches the shell widget to the DOM
* using the "main" element or `document.body`, and adds a window
* resize event handler which refits the shell on window resize.
*/
protected finalize(): void {
var shell = this.shell;
var elem = document.getElementById('main') || document.body;
var box = createBoxSizing(elem);
var fit = () => shell.fit(void 0, void 0, box);
window.addEventListener('resize', fit);
shell.attach(elem);
shell.show();
fit();
}
private _shell: IShellView = null;
private _container: IContainer = null;
private _pluginList: IPluginList = null;
}
} // module phosphor.shell