Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50659 views
1
// Copyright (c) IPython Development Team.
2
// Distributed under the terms of the Modified BSD License.
3
4
define([
5
"widgets/js/widget",
6
"jqueryui",
7
"base/js/utils",
8
"bootstrap",
9
], function(widget, $, utils){
10
"use strict";
11
12
var BoxView = widget.DOMWidgetView.extend({
13
initialize: function(){
14
/**
15
* Public constructor
16
*/
17
BoxView.__super__.initialize.apply(this, arguments);
18
this.children_views = new widget.ViewList(this.add_child_model, null, this);
19
this.listenTo(this.model, 'change:children', function(model, value) {
20
this.children_views.update(value);
21
}, this);
22
this.listenTo(this.model, 'change:overflow_x', function(model, value) {
23
this.update_overflow_x();
24
}, this);
25
this.listenTo(this.model, 'change:overflow_y', function(model, value) {
26
this.update_overflow_y();
27
}, this);
28
this.listenTo(this.model, 'change:box_style', function(model, value) {
29
this.update_box_style();
30
}, this);
31
},
32
33
update_attr: function(name, value) {
34
/**
35
* Set a css attr of the widget view.
36
*/
37
this.$box.css(name, value);
38
},
39
40
render: function(){
41
/**
42
* Called when view is rendered.
43
*/
44
this.$box = this.$el;
45
this.$box.addClass('widget-box');
46
this.children_views.update(this.model.get('children'));
47
this.update_overflow_x();
48
this.update_overflow_y();
49
this.update_box_style('');
50
},
51
52
update_overflow_x: function() {
53
/**
54
* Called when the x-axis overflow setting is changed.
55
*/
56
this.$box.css('overflow-x', this.model.get('overflow_x'));
57
},
58
59
update_overflow_y: function() {
60
/**
61
* Called when the y-axis overflow setting is changed.
62
*/
63
this.$box.css('overflow-y', this.model.get('overflow_y'));
64
},
65
66
update_box_style: function(previous_trait_value) {
67
var class_map = {
68
success: ['alert', 'alert-success'],
69
info: ['alert', 'alert-info'],
70
warning: ['alert', 'alert-warning'],
71
danger: ['alert', 'alert-danger']
72
};
73
this.update_mapped_classes(class_map, 'box_style', previous_trait_value, this.$box);
74
},
75
76
add_child_model: function(model) {
77
/**
78
* Called when a model is added to the children list.
79
*/
80
var that = this;
81
var dummy = $('<div/>');
82
that.$box.append(dummy);
83
return this.create_child_view(model).then(function(view) {
84
dummy.replaceWith(view.el);
85
86
// Trigger the displayed event of the child view.
87
that.after_displayed(function() {
88
view.trigger('displayed');
89
});
90
return view;
91
}).catch(utils.reject("Couldn't add child view to box", true));
92
},
93
94
remove: function() {
95
/**
96
* We remove this widget before removing the children as an optimization
97
* we want to remove the entire container from the DOM first before
98
* removing each individual child separately.
99
*/
100
BoxView.__super__.remove.apply(this, arguments);
101
this.children_views.remove();
102
},
103
});
104
105
106
var FlexBoxView = BoxView.extend({
107
render: function(){
108
FlexBoxView.__super__.render.apply(this);
109
this.listenTo(this.model, 'change:orientation', this.update_orientation, this);
110
this.listenTo(this.model, 'change:flex', this._flex_changed, this);
111
this.listenTo(this.model, 'change:pack', this._pack_changed, this);
112
this.listenTo(this.model, 'change:align', this._align_changed, this);
113
this._flex_changed();
114
this._pack_changed();
115
this._align_changed();
116
this.update_orientation();
117
},
118
119
update_orientation: function(){
120
var orientation = this.model.get("orientation");
121
if (orientation == "vertical") {
122
this.$box.removeClass("hbox").addClass("vbox");
123
} else {
124
this.$box.removeClass("vbox").addClass("hbox");
125
}
126
},
127
128
_flex_changed: function(){
129
if (this.model.previous('flex')) {
130
this.$box.removeClass('box-flex' + this.model.previous('flex'));
131
}
132
this.$box.addClass('box-flex' + this.model.get('flex'));
133
},
134
135
_pack_changed: function(){
136
if (this.model.previous('pack')) {
137
this.$box.removeClass(this.model.previous('pack'));
138
}
139
this.$box.addClass(this.model.get('pack'));
140
},
141
142
_align_changed: function(){
143
if (this.model.previous('align')) {
144
this.$box.removeClass('align-' + this.model.previous('align'));
145
}
146
this.$box.addClass('align-' + this.model.get('align'));
147
},
148
});
149
150
return {
151
'BoxView': BoxView,
152
'FlexBoxView': FlexBoxView,
153
};
154
});
155
156