Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50659 views
1
//----------------------------------------------------------------------------
2
// Copyright (C) 2013 The IPython Development Team
3
//
4
// Distributed under the terms of the BSD License. The full license is in
5
// the file COPYING, distributed as part of this software.
6
//----------------------------------------------------------------------------
7
8
//============================================================================
9
// ContainerWidget
10
//============================================================================
11
12
/**
13
* @module IPython
14
* @namespace IPython
15
**/
16
17
define(["widgets/js/widget"], function(WidgetManager) {
18
19
var ContainerView = IPython.DOMWidgetView.extend({
20
render: function(){
21
// Called when view is rendered.
22
this.$el.addClass('widget-container')
23
.addClass('vbox');
24
this.children={};
25
this.update_children([], this.model.get('_children'));
26
this.model.on('change:_children', function(model, value, options) {
27
this.update_children(model.previous('_children'), value);
28
}, this);
29
this.update();
30
},
31
32
update_children: function(old_list, new_list) {
33
// Called when the children list changes.
34
this.do_diff(old_list,
35
new_list,
36
$.proxy(this.remove_child_model, this),
37
$.proxy(this.add_child_model, this));
38
},
39
40
remove_child_model: function(model) {
41
// Called when a model is removed from the children list.
42
this.child_views[model.id].remove();
43
this.delete_child_view(model);
44
},
45
46
add_child_model: function(model) {
47
// Called when a model is added to the children list.
48
var view = this.create_child_view(model);
49
this.$el.append(view.$el);
50
},
51
52
update: function(){
53
// Update the contents of this view
54
//
55
// Called when the model is changed. The model may have been
56
// changed by another view or by a state update from the back-end.
57
return ContainerView.__super__.update.apply(this);
58
},
59
});
60
61
WidgetManager.register_widget_view('ContainerView', ContainerView);
62
63
var PopupView = IPython.DOMWidgetView.extend({
64
render: function(){
65
// Called when view is rendered.
66
var that = this;
67
this.children={};
68
69
this.$el.on("remove", function(){
70
that.$window.remove();
71
});
72
this.$window = $('<div />')
73
.addClass('modal widget-modal')
74
.appendTo($('#notebook-container'))
75
.mousedown(function(){
76
that.bring_to_front();
77
});
78
79
// Set the elements array since the this.$window element is not child
80
// of this.$el and the parent widget manager or other widgets may
81
// need to know about all of the top-level widgets. The IPython
82
// widget manager uses this to register the elements with the
83
// keyboard manager.
84
this.additional_elements = [this.$window]
85
86
this.$title_bar = $('<div />')
87
.addClass('popover-title')
88
.appendTo(this.$window)
89
.mousedown(function(){
90
that.bring_to_front();
91
});
92
this.$close = $('<button />')
93
.addClass('close icon-remove')
94
.css('margin-left', '5px')
95
.appendTo(this.$title_bar)
96
.click(function(){
97
that.hide();
98
event.stopPropagation();
99
});
100
this.$minimize = $('<button />')
101
.addClass('close icon-arrow-down')
102
.appendTo(this.$title_bar)
103
.click(function(){
104
that.popped_out = !that.popped_out;
105
if (!that.popped_out) {
106
that.$minimize
107
.removeClass('icon-arrow-down')
108
.addClass('icon-arrow-up');
109
110
that.$window
111
.draggable('destroy')
112
.resizable('destroy')
113
.removeClass('widget-modal modal')
114
.addClass('docked-widget-modal')
115
.detach()
116
.insertBefore(that.$show_button);
117
that.$show_button.hide();
118
that.$close.hide();
119
} else {
120
that.$minimize
121
.addClass('icon-arrow-down')
122
.removeClass('icon-arrow-up');
123
124
that.$window
125
.removeClass('docked-widget-modal')
126
.addClass('widget-modal modal')
127
.detach()
128
.appendTo($('#notebook-container'))
129
.draggable({handle: '.popover-title', snap: '#notebook, .modal', snapMode: 'both'})
130
.resizable()
131
.children('.ui-resizable-handle').show();
132
that.show();
133
that.$show_button.show();
134
that.$close.show();
135
}
136
event.stopPropagation();
137
});
138
this.$title = $('<div />')
139
.addClass('widget-modal-title')
140
.html("&nbsp;")
141
.appendTo(this.$title_bar);
142
this.$body = $('<div />')
143
.addClass('modal-body')
144
.addClass('widget-modal-body')
145
.addClass('widget-container')
146
.addClass('vbox')
147
.appendTo(this.$window);
148
149
this.$show_button = $('<button />')
150
.html("&nbsp;")
151
.addClass('btn btn-info widget-modal-show')
152
.appendTo(this.$el)
153
.click(function(){
154
that.show();
155
});
156
157
this.$window.draggable({handle: '.popover-title', snap: '#notebook, .modal', snapMode: 'both'});
158
this.$window.resizable();
159
this.$window.on('resize', function(){
160
that.$body.outerHeight(that.$window.innerHeight() - that.$title_bar.outerHeight());
161
});
162
163
this.$el_to_style = this.$body;
164
this._shown_once = false;
165
this.popped_out = true;
166
167
this.update_children([], this.model.get('_children'));
168
this.model.on('change:_children', function(model, value, options) {
169
this.update_children(model.previous('_children'), value);
170
}, this);
171
this.update();
172
},
173
174
hide: function() {
175
// Called when the modal hide button is clicked.
176
this.$window.hide();
177
this.$show_button.removeClass('btn-info');
178
},
179
180
show: function() {
181
// Called when the modal show button is clicked.
182
this.$show_button.addClass('btn-info');
183
this.$window.show();
184
if (this.popped_out) {
185
this.$window.css("positon", "absolute");
186
this.$window.css("top", "0px");
187
this.$window.css("left", Math.max(0, (($('body').outerWidth() - this.$window.outerWidth()) / 2) +
188
$(window).scrollLeft()) + "px");
189
this.bring_to_front();
190
}
191
},
192
193
bring_to_front: function() {
194
// Make the modal top-most, z-ordered about the other modals.
195
var $widget_modals = $(".widget-modal");
196
var max_zindex = 0;
197
$widget_modals.each(function (index, el){
198
max_zindex = Math.max(max_zindex, parseInt($(el).css('z-index')));
199
});
200
201
// Start z-index of widget modals at 2000
202
max_zindex = Math.max(max_zindex, 2000);
203
204
$widget_modals.each(function (index, el){
205
$el = $(el);
206
if (max_zindex == parseInt($el.css('z-index'))) {
207
$el.css('z-index', max_zindex - 1);
208
}
209
});
210
this.$window.css('z-index', max_zindex);
211
},
212
213
update_children: function(old_list, new_list) {
214
// Called when the children list is modified.
215
this.do_diff(old_list,
216
new_list,
217
$.proxy(this.remove_child_model, this),
218
$.proxy(this.add_child_model, this));
219
},
220
221
remove_child_model: function(model) {
222
// Called when a child is removed from children list.
223
this.child_views[model.id].remove();
224
this.delete_child_view(model);
225
},
226
227
add_child_model: function(model) {
228
// Called when a child is added to children list.
229
var view = this.create_child_view(model);
230
this.$body.append(view.$el);
231
},
232
233
update: function(){
234
// Update the contents of this view
235
//
236
// Called when the model is changed. The model may have been
237
// changed by another view or by a state update from the back-end.
238
var description = this.model.get('description');
239
if (description.trim().length === 0) {
240
this.$title.html("&nbsp;"); // Preserve title height
241
} else {
242
this.$title.text(description);
243
}
244
245
var button_text = this.model.get('button_text');
246
if (button_text.trim().length === 0) {
247
this.$show_button.html("&nbsp;"); // Preserve button height
248
} else {
249
this.$show_button.text(button_text);
250
}
251
252
if (!this._shown_once) {
253
this._shown_once = true;
254
this.show();
255
}
256
257
return PopupView.__super__.update.apply(this);
258
},
259
260
_get_selector_element: function(selector) {
261
// Get an element view a 'special' jquery selector. (see widget.js)
262
//
263
// Since the modal actually isn't within the $el in the DOM, we need to extend
264
// the selector logic to allow the user to set css on the modal if need be.
265
// The convention used is:
266
// "modal" - select the modal div
267
// "modal [selector]" - select element(s) within the modal div.
268
// "[selector]" - select elements within $el
269
// "" - select the $el_to_style
270
if (selector.substring(0, 5) == 'modal') {
271
if (selector == 'modal') {
272
return this.$window;
273
} else {
274
return this.$window.find(selector.substring(6));
275
}
276
} else {
277
return PopupView.__super__._get_selector_element.apply(this, [selector]);
278
}
279
},
280
});
281
WidgetManager.register_widget_view('PopupView', PopupView);
282
});
283
284