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
// SelectionContainerWidget
10
//============================================================================
11
12
/**
13
* @module IPython
14
* @namespace IPython
15
**/
16
17
define(["widgets/js/widget"], function(WidgetManager){
18
19
var AccordionView = IPython.DOMWidgetView.extend({
20
render: function(){
21
// Called when view is rendered.
22
var guid = 'accordion' + IPython.utils.uuid();
23
this.$el
24
.attr('id', guid)
25
.addClass('accordion');
26
this.containers = [];
27
this.model_containers = {};
28
this.update_children([], this.model.get('_children'));
29
this.model.on('change:_children', function(model, value, options) {
30
this.update_children(model.previous('_children'), value);
31
}, this);
32
this.model.on('change:selected_index', function(model, value, options) {
33
this.update_selected_index(model.previous('selected_index'), value, options);
34
}, this);
35
this.model.on('change:_titles', function(model, value, options) {
36
this.update_titles(value);
37
}, this);
38
this.model.on('displayed', function() {
39
this.update_titles();
40
}, this);
41
},
42
43
update_titles: function(titles) {
44
// Set tab titles
45
if (!titles) {
46
titles = this.model.get('_titles');
47
}
48
49
var that = this;
50
_.each(titles, function(title, page_index) {
51
var accordian = that.containers[page_index];
52
if (accordian !== undefined) {
53
accordian
54
.find('.accordion-heading')
55
.find('.accordion-toggle')
56
.text(title);
57
}
58
});
59
},
60
61
update_selected_index: function(old_index, new_index, options) {
62
// Only update the selection if the selection wasn't triggered
63
// by the front-end. It must be triggered by the back-end.
64
if (options === undefined || options.updated_view != this) {
65
this.containers[old_index].find('.accordion-body').collapse('hide');
66
if (0 <= new_index && new_index < this.containers.length) {
67
this.containers[new_index].find('.accordion-body').collapse('show');
68
}
69
}
70
},
71
72
update_children: function(old_list, new_list) {
73
// Called when the children list is modified.
74
this.do_diff(old_list,
75
new_list,
76
$.proxy(this.remove_child_model, this),
77
$.proxy(this.add_child_model, this));
78
},
79
80
remove_child_model: function(model) {
81
// Called when a child is removed from children list.
82
var accordion_group = this.model_containers[model.id];
83
this.containers.splice(accordion_group.container_index, 1);
84
delete this.model_containers[model.id];
85
accordion_group.remove();
86
this.delete_child_view(model);
87
},
88
89
add_child_model: function(model) {
90
// Called when a child is added to children list.
91
var view = this.create_child_view(model);
92
var index = this.containers.length;
93
var uuid = IPython.utils.uuid();
94
var accordion_group = $('<div />')
95
.addClass('accordion-group')
96
.appendTo(this.$el);
97
var accordion_heading = $('<div />')
98
.addClass('accordion-heading')
99
.appendTo(accordion_group);
100
var that = this;
101
var accordion_toggle = $('<a />')
102
.addClass('accordion-toggle')
103
.attr('data-toggle', 'collapse')
104
.attr('data-parent', '#' + this.$el.attr('id'))
105
.attr('href', '#' + uuid)
106
.click(function(evt){
107
108
// Calling model.set will trigger all of the other views of the
109
// model to update.
110
that.model.set("selected_index", index, {updated_view: that});
111
that.touch();
112
})
113
.text('Page ' + index)
114
.appendTo(accordion_heading);
115
var accordion_body = $('<div />', {id: uuid})
116
.addClass('accordion-body collapse')
117
.appendTo(accordion_group);
118
var accordion_inner = $('<div />')
119
.addClass('accordion-inner')
120
.appendTo(accordion_body);
121
var container_index = this.containers.push(accordion_group) - 1;
122
accordion_group.container_index = container_index;
123
this.model_containers[model.id] = accordion_group;
124
accordion_inner.append(view.$el);
125
126
this.update();
127
this.update_titles();
128
},
129
});
130
WidgetManager.register_widget_view('AccordionView', AccordionView);
131
132
133
var TabView = IPython.DOMWidgetView.extend({
134
initialize: function() {
135
// Public constructor.
136
this.containers = [];
137
TabView.__super__.initialize.apply(this, arguments);
138
},
139
140
render: function(){
141
// Called when view is rendered.
142
var uuid = 'tabs'+IPython.utils.uuid();
143
var that = this;
144
this.$tabs = $('<div />', {id: uuid})
145
.addClass('nav')
146
.addClass('nav-tabs')
147
.appendTo(this.$el);
148
this.$tab_contents = $('<div />', {id: uuid + 'Content'})
149
.addClass('tab-content')
150
.appendTo(this.$el);
151
this.containers = [];
152
this.update_children([], this.model.get('_children'));
153
this.model.on('change:_children', function(model, value, options) {
154
this.update_children(model.previous('_children'), value);
155
}, this);
156
},
157
158
update_children: function(old_list, new_list) {
159
// Called when the children list is modified.
160
this.do_diff(old_list,
161
new_list,
162
$.proxy(this.remove_child_model, this),
163
$.proxy(this.add_child_model, this));
164
},
165
166
remove_child_model: function(model) {
167
// Called when a child is removed from children list.
168
var view = this.child_views[model.id];
169
this.containers.splice(view.parent_tab.tab_text_index, 1);
170
view.parent_tab.remove();
171
view.parent_container.remove();
172
view.remove();
173
this.delete_child_view(model);
174
},
175
176
add_child_model: function(model) {
177
// Called when a child is added to children list.
178
var view = this.create_child_view(model);
179
var index = this.containers.length;
180
var uuid = IPython.utils.uuid();
181
182
var that = this;
183
var tab = $('<li />')
184
.css('list-style-type', 'none')
185
.appendTo(this.$tabs);
186
view.parent_tab = tab;
187
188
var tab_text = $('<a />')
189
.attr('href', '#' + uuid)
190
.attr('data-toggle', 'tab')
191
.text('Page ' + index)
192
.appendTo(tab)
193
.click(function (e) {
194
195
// Calling model.set will trigger all of the other views of the
196
// model to update.
197
that.model.set("selected_index", index, {updated_view: this});
198
that.touch();
199
that.select_page(index);
200
});
201
tab.tab_text_index = this.containers.push(tab_text) - 1;
202
203
var contents_div = $('<div />', {id: uuid})
204
.addClass('tab-pane')
205
.addClass('fade')
206
.append(view.$el)
207
.appendTo(this.$tab_contents);
208
view.parent_container = contents_div;
209
},
210
211
update: function(options) {
212
// Update the contents of this view
213
//
214
// Called when the model is changed. The model may have been
215
// changed by another view or by a state update from the back-end.
216
if (options === undefined || options.updated_view != this) {
217
// Set tab titles
218
var titles = this.model.get('_titles');
219
var that = this;
220
_.each(titles, function(title, page_index) {
221
var tab_text = that.containers[page_index];
222
if (tab_text !== undefined) {
223
tab_text.text(title);
224
}
225
});
226
227
var selected_index = this.model.get('selected_index');
228
if (0 <= selected_index && selected_index < this.containers.length) {
229
this.select_page(selected_index);
230
}
231
}
232
return TabView.__super__.update.apply(this);
233
},
234
235
select_page: function(index) {
236
// Select a page.
237
this.$tabs.find('li')
238
.removeClass('active');
239
this.containers[index].tab('show');
240
},
241
});
242
WidgetManager.register_widget_view('TabView', TabView);
243
});
244
245