Debian Codegraph @

jupyter-widgets-controls/lib/widget_box.js

raw
1 // Copyright (c) Jupyter Development Team.
2 // Distributed under the terms of the Modified BSD License.
3 import { DOMWidgetView, unpack_models, ViewList, JupyterLuminoPanelWidget, reject, } from '@jupyter-widgets/base';
4 import { CoreDOMWidgetModel } from './widget_core';
5 import { ArrayExt } from '@lumino/algorithm';
6 import { MessageLoop } from '@lumino/messaging';
7 import { Widget } from '@lumino/widgets';
8 import $ from 'jquery';
9 export class BoxModel extends CoreDOMWidgetModel {
10 defaults() {
11 return Object.assign(Object.assign({}, super.defaults()), { _view_name: 'BoxView', _model_name: 'BoxModel', children: [], box_style: '' });
12 }
13 }
14 BoxModel.serializers = Object.assign(Object.assign({}, CoreDOMWidgetModel.serializers), { children: { deserialize: unpack_models } });
15 export class HBoxModel extends BoxModel {
16 defaults() {
17 return Object.assign(Object.assign({}, super.defaults()), { _view_name: 'HBoxView', _model_name: 'HBoxModel' });
18 }
19 }
20 export class VBoxModel extends BoxModel {
21 defaults() {
22 return Object.assign(Object.assign({}, super.defaults()), { _view_name: 'VBoxView', _model_name: 'VBoxModel' });
23 }
24 }
25 export class BoxView extends DOMWidgetView {
26 _createElement(tagName) {
27 this.luminoWidget = new JupyterLuminoPanelWidget({ view: this });
28 return this.luminoWidget.node;
29 }
30 _setElement(el) {
31 if (this.el || el !== this.luminoWidget.node) {
32 // Boxes don't allow setting the element beyond the initial creation.
33 throw new Error('Cannot reset the DOM element.');
34 }
35 this.el = this.luminoWidget.node;
36 this.$el = $(this.luminoWidget.node);
37 }
38 initialize(parameters) {
39 super.initialize(parameters);
40 this.children_views = new ViewList(this.add_child_model, null, this);
41 this.listenTo(this.model, 'change:children', this.update_children);
42 this.listenTo(this.model, 'change:box_style', this.update_box_style);
43 this.luminoWidget.addClass('jupyter-widgets');
44 this.luminoWidget.addClass('widget-container');
45 this.luminoWidget.addClass('widget-box');
46 }
47 render() {
48 super.render();
49 this.update_children();
50 this.set_box_style();
51 }
52 update_children() {
53 var _a;
54 (_a = this.children_views) === null || _a === void 0 ? void 0 : _a.update(this.model.get('children')).then((views) => {
55 // Notify all children that their sizes may have changed.
56 views.forEach((view) => {
57 MessageLoop.postMessage(view.luminoWidget, Widget.ResizeMessage.UnknownSize);
58 });
59 });
60 }
61 update_box_style() {
62 this.update_mapped_classes(BoxView.class_map, 'box_style');
63 }
64 set_box_style() {
65 this.set_mapped_classes(BoxView.class_map, 'box_style');
66 }
67 add_child_model(model) {
68 // we insert a dummy element so the order is preserved when we add
69 // the rendered content later.
70 const dummy = new Widget();
71 this.luminoWidget.addWidget(dummy);
72 return this.create_child_view(model)
73 .then((view) => {
74 // replace the dummy widget with the new one.
75 const i = ArrayExt.firstIndexOf(this.luminoWidget.widgets, dummy);
76 this.luminoWidget.insertWidget(i, view.luminoWidget);
77 dummy.dispose();
78 return view;
79 })
80 .catch(reject('Could not add child view to box', true));
81 }
82 remove() {
83 this.children_views = null;
84 super.remove();
85 }
86 }
87 BoxView.class_map = {
88 success: ['alert', 'alert-success'],
89 info: ['alert', 'alert-info'],
90 warning: ['alert', 'alert-warning'],
91 danger: ['alert', 'alert-danger'],
92 };
93 export class HBoxView extends BoxView {
94 /**
95 * Public constructor
96 */
97 initialize(parameters) {
98 super.initialize(parameters);
99 this.luminoWidget.addClass('widget-hbox');
100 }
101 }
102 export class VBoxView extends BoxView {
103 /**
104 * Public constructor
105 */
106 initialize(parameters) {
107 super.initialize(parameters);
108 this.luminoWidget.addClass('widget-vbox');
109 }
110 }
111 export class GridBoxView extends BoxView {
112 /**
113 * Public constructor
114 */
115 initialize(parameters) {
116 super.initialize(parameters);
117 this.luminoWidget.addClass('widget-gridbox');
118 // display needn't be set to flex and grid
119 this.luminoWidget.removeClass('widget-box');
120 }
121 }
122 export class GridBoxModel extends BoxModel {
123 defaults() {
124 return Object.assign(Object.assign({}, super.defaults()), { _view_name: 'GridBoxView', _model_name: 'GridBoxModel' });
125 }
126 }
127 //# sourceMappingURL=widget_box.js.map