-
Notifications
You must be signed in to change notification settings - Fork 19
/
backbone-wrapper.tsx
72 lines (64 loc) · 2.07 KB
/
backbone-wrapper.tsx
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
import Immutable from "immutable";
import * as React from "react";
import { WidgetManager } from "../manager/widget-manager";
/**
* Import the styles for jupyter-widgets. This overrides some of the
* styles that jQuery applies to the widgets.
*/
import "@jupyter-widgets/base/css/index.css";
import "@jupyter-widgets/controls/css/labvariables.css";
import "@jupyter-widgets/controls/css/materialcolors.css";
import "@jupyter-widgets/controls/css/phosphor.css";
import "@jupyter-widgets/controls/css/widgets-base.css";
import "@jupyter-widgets/controls/css/widgets.css";
/**
* Bring in the JavaScript and CSS for rendering the
* widgets that require jQuery-UI.
*/
require("jquery-ui/themes/base/all.css");
require("jquery-ui/themes/base/core.css");
require("jquery-ui/themes/base/base.css");
require("jquery-ui/themes/base/theme.css");
interface Props {
model: Immutable.Map<string, any>;
manager?: WidgetManager;
model_id: string;
widgetContainerRef: React.RefObject<HTMLDivElement>;
}
export default class BackboneWrapper extends React.Component<Props> {
created = false;
constructor(props: Props) {
super(props);
this.createWidgetIfNotCreated = this.createWidgetIfNotCreated.bind(this);
}
async componentDidMount() {
return this.createWidgetIfNotCreated();
}
async componentDidUpdate() {
return this.createWidgetIfNotCreated();
}
async createWidgetIfNotCreated() {
const { model, manager, widgetContainerRef, model_id } = this.props;
if (!this.created && manager) {
this.created = true;
try {
const widget = await manager.new_widget_from_state_and_id(
model.toJS(),
model_id
);
const view = await manager.create_view(
widget,
undefined // no options
);
if (widgetContainerRef.current) {
manager.render_view(view, widgetContainerRef.current);
}
} catch (err) {
throw err;
}
}
}
render() {
return <div ref={this.props.widgetContainerRef} />;
}
}