-
Notifications
You must be signed in to change notification settings - Fork 947
/
Copy pathwidget_box.ts
189 lines (161 loc) · 4.67 KB
/
widget_box.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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
DOMWidgetView,
unpack_models,
ViewList,
JupyterLuminoPanelWidget,
reject,
WidgetModel,
WidgetView
} from '@jupyter-widgets/base';
import { CoreDOMWidgetModel } from './widget_core';
import { ArrayExt } from '@lumino/algorithm';
import { MessageLoop } from '@lumino/messaging';
import { Widget } from '@lumino/widgets';
import $ from 'jquery';
export class BoxModel extends CoreDOMWidgetModel {
defaults(): Backbone.ObjectHash {
return {
...super.defaults(),
_view_name: 'BoxView',
_model_name: 'BoxModel',
children: [],
box_style: ''
};
}
static serializers = {
...CoreDOMWidgetModel.serializers,
children: { deserialize: unpack_models }
};
}
export class HBoxModel extends BoxModel {
defaults(): Backbone.ObjectHash {
return {
...super.defaults(),
_view_name: 'HBoxView',
_model_name: 'HBoxModel'
};
}
}
export class VBoxModel extends BoxModel {
defaults(): Backbone.ObjectHash {
return {
...super.defaults(),
_view_name: 'VBoxView',
_model_name: 'VBoxModel'
};
}
}
export class BoxView extends DOMWidgetView {
_createElement(tagName: string): HTMLElement {
this.pWidget = new JupyterLuminoPanelWidget({ view: this });
return this.pWidget.node;
}
_setElement(el: HTMLElement): void {
if (this.el || el !== this.pWidget.node) {
// Boxes don't allow setting the element beyond the initial creation.
throw new Error('Cannot reset the DOM element.');
}
this.el = this.pWidget.node;
this.$el = $(this.pWidget.node);
}
initialize(parameters: WidgetView.IInitializeParameters): void {
super.initialize(parameters);
this.children_views = new ViewList(this.add_child_model, null, this);
this.listenTo(this.model, 'change:children', this.update_children);
this.listenTo(this.model, 'change:box_style', this.update_box_style);
this.pWidget.addClass('jupyter-widgets');
this.pWidget.addClass('widget-container');
this.pWidget.addClass('widget-box');
}
render(): void {
super.render();
this.update_children();
this.set_box_style();
}
update_children(): void {
this.children_views
?.update(this.model.get('children'))
.then((views: DOMWidgetView[]) => {
// Notify all children that their sizes may have changed.
views.forEach(view => {
MessageLoop.postMessage(
view.pWidget,
Widget.ResizeMessage.UnknownSize
);
});
});
}
update_box_style(): void {
this.update_mapped_classes(BoxView.class_map, 'box_style');
}
set_box_style(): void {
this.set_mapped_classes(BoxView.class_map, 'box_style');
}
add_child_model(model: WidgetModel): Promise<DOMWidgetView> {
// we insert a dummy element so the order is preserved when we add
// the rendered content later.
const dummy = new Widget();
this.pWidget.addWidget(dummy);
return this.create_child_view(model)
.then((view: DOMWidgetView) => {
// replace the dummy widget with the new one.
const i = ArrayExt.firstIndexOf(this.pWidget.widgets, dummy);
this.pWidget.insertWidget(i, view.pWidget);
dummy.dispose();
return view;
})
.catch(reject('Could not add child view to box', true));
}
remove(): void {
this.children_views = null;
super.remove();
}
children_views: ViewList<DOMWidgetView> | null;
pWidget: JupyterLuminoPanelWidget;
static class_map = {
success: ['alert', 'alert-success'],
info: ['alert', 'alert-info'],
warning: ['alert', 'alert-warning'],
danger: ['alert', 'alert-danger']
};
}
export class HBoxView extends BoxView {
/**
* Public constructor
*/
initialize(parameters: WidgetView.IInitializeParameters): void {
super.initialize(parameters);
this.pWidget.addClass('widget-hbox');
}
}
export class VBoxView extends BoxView {
/**
* Public constructor
*/
initialize(parameters: WidgetView.IInitializeParameters): void {
super.initialize(parameters);
this.pWidget.addClass('widget-vbox');
}
}
export class GridBoxView extends BoxView {
/**
* Public constructor
*/
initialize(parameters: WidgetView.IInitializeParameters): void {
super.initialize(parameters);
this.pWidget.addClass('widget-gridbox');
// display needn't be set to flex and grid
this.pWidget.removeClass('widget-box');
}
}
export class GridBoxModel extends BoxModel {
defaults(): Backbone.ObjectHash {
return {
...super.defaults(),
_view_name: 'GridBoxView',
_model_name: 'GridBoxModel'
};
}
}