-
Notifications
You must be signed in to change notification settings - Fork 947
/
Copy pathhtmlmanager.ts
196 lines (178 loc) · 5.68 KB
/
htmlmanager.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { maxSatisfying } from 'semver';
import * as base from '@jupyter-widgets/base';
import * as outputWidgets from './output';
import { ManagerBase } from '@jupyter-widgets/base-manager';
import { MessageLoop } from '@lumino/messaging';
import * as LuminoWidget from '@lumino/widgets';
import {
RenderMimeRegistry,
standardRendererFactories,
} from '@jupyterlab/rendermime';
import { WidgetRenderer, WIDGET_MIMETYPE } from './output_renderers';
import { WidgetModel, WidgetView, DOMWidgetView } from '@jupyter-widgets/base';
export class HTMLManager extends ManagerBase {
constructor(options?: {
loader?: (moduleName: string, moduleVersion: string) => Promise<any>;
}) {
super();
this.loader = options?.loader;
this.renderMime = new RenderMimeRegistry({
initialFactories: standardRendererFactories,
});
this.renderMime.addFactory(
{
safe: false,
mimeTypes: [WIDGET_MIMETYPE],
createRenderer: (options) => new WidgetRenderer(options, this),
},
0
);
this._viewList = new Set<DOMWidgetView>();
window.addEventListener('resize', () => {
this._viewList.forEach((view) => {
MessageLoop.postMessage(
view.luminoWidget || view.pWidget,
LuminoWidget.Widget.ResizeMessage.UnknownSize
);
});
});
}
/**
* Display the specified view. Element where the view is displayed
* is specified in the `options.el` argument.
*/
async display_view(
view: Promise<DOMWidgetView> | DOMWidgetView,
el: HTMLElement
): Promise<void> {
let v: DOMWidgetView;
try {
v = await view;
} catch (error) {
const msg = `Could not create a view for ${view}`;
console.error(msg);
const ModelCls = base.createErrorWidgetModel(error, msg);
const errorModel = new ModelCls();
v = new base.ErrorWidgetView({
model: errorModel,
});
v.render();
}
LuminoWidget.Widget.attach(v.luminoWidget || v.pWidget, el);
this._viewList.add(v);
v.once('remove', () => {
this._viewList.delete(v);
});
}
/**
* Placeholder implementation for _get_comm_info.
*/
_get_comm_info(): Promise<{}> {
return Promise.resolve({});
}
/**
* Placeholder implementation for _create_comm.
*/
_create_comm(
comm_target_name: string,
model_id: string,
data?: any,
metadata?: any,
buffers?: ArrayBuffer[] | ArrayBufferView[]
): Promise<any> {
return Promise.resolve({
on_close: () => {
return;
},
on_msg: () => {
return;
},
close: () => {
return;
},
});
}
/**
* Load a class and return a promise to the loaded object.
*/
protected loadClass(
className: string,
moduleName: string,
moduleVersion: string
): Promise<typeof WidgetModel | typeof WidgetView> {
return new Promise((resolve, reject) => {
if (
moduleName === '@jupyter-widgets/base' ||
moduleName === '@jupyter-widgets/controls'
) {
moduleVersion = `^${moduleVersion}`;
}
if (moduleName === '@jupyter-widgets/base') {
const best = maxSatisfying(['1.2.0', '2.0.0'], moduleVersion);
if (best === '1.2.0') {
// ipywidgets 7 model
resolve(require('@jupyter-widgets/base7'));
} else {
// ipywidgets 8 model
resolve(require('@jupyter-widgets/base'));
}
} else if (moduleName === '@jupyter-widgets/controls') {
const best = maxSatisfying(['1.5.0', '2.0.0'], moduleVersion);
if (best === '1.5.0') {
// ipywidgets 7 controls JS and CSS
require('@jupyter-widgets/controls7/css/widgets-base.css');
// If lab variables are not found, we set them (we don't want to reset the variables if they are already defined)
if (
getComputedStyle(document.documentElement).getPropertyValue(
'--jp-layout-color0'
) === ''
) {
require('@jupyter-widgets/controls7/css/labvariables.css');
}
resolve(require('@jupyter-widgets/controls7'));
} else {
// ipywidgets 8 controls JS and CSS
require('@jupyter-widgets/controls/css/widgets-base.css');
// If lab variables are not found, we set them (we don't want to reset the variables if they are already defined)
if (
getComputedStyle(document.documentElement).getPropertyValue(
'--jp-layout-color0'
) === ''
) {
require('@jupyter-widgets/controls/css/labvariables.css');
}
resolve(require('@jupyter-widgets/controls'));
}
} else if (moduleName === '@jupyter-widgets/output') {
resolve(outputWidgets);
} else if (this.loader !== undefined) {
resolve(this.loader(moduleName, moduleVersion));
} else {
reject(`Could not load module ${moduleName}@${moduleVersion}`);
}
}).then((module) => {
if ((module as any)[className]) {
return (module as any)[className];
} else {
return Promise.reject(
`Class ${className} not found in module ${moduleName}@${moduleVersion}`
);
}
});
}
/**
* Renderers for contents of the output widgets
*
* Defines how outputs in the output widget should be rendered.
*/
renderMime: RenderMimeRegistry;
/**
* A loader for a given module name and module version, and returns a promise to a module
*/
loader:
| ((moduleName: string, moduleVersion: string) => Promise<any>)
| undefined;
private _viewList: Set<DOMWidgetView>;
}