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
// SelectionWidget
10
//============================================================================
11
12
/**
13
* @module IPython
14
* @namespace IPython
15
**/
16
17
define(["widgets/js/widget"], function(WidgetManager){
18
19
var DropdownView = IPython.DOMWidgetView.extend({
20
render : function(){
21
// Called when view is rendered.
22
this.$el
23
.addClass('widget-hbox-single');
24
this.$label = $('<div />')
25
.appendTo(this.$el)
26
.addClass('widget-hlabel')
27
.hide();
28
this.$buttongroup = $('<div />')
29
.addClass('widget_item')
30
.addClass('btn-group')
31
.appendTo(this.$el);
32
this.$el_to_style = this.$buttongroup; // Set default element to style
33
this.$droplabel = $('<button />')
34
.addClass('btn')
35
.addClass('widget-combo-btn')
36
.html("&nbsp;")
37
.appendTo(this.$buttongroup);
38
this.$dropbutton = $('<button />')
39
.addClass('btn')
40
.addClass('dropdown-toggle')
41
.addClass('widget-combo-carrot-btn')
42
.attr('data-toggle', 'dropdown')
43
.append($('<span />').addClass("caret"))
44
.appendTo(this.$buttongroup);
45
this.$droplist = $('<ul />')
46
.addClass('dropdown-menu')
47
.appendTo(this.$buttongroup);
48
49
// Set defaults.
50
this.update();
51
},
52
53
update : function(options){
54
// Update the contents of this view
55
//
56
// Called when the model is changed. The model may have been
57
// changed by another view or by a state update from the back-end.
58
59
if (options === undefined || options.updated_view != this) {
60
var selected_item_text = this.model.get('value_name');
61
if (selected_item_text.trim().length === 0) {
62
this.$droplabel.html("&nbsp;");
63
} else {
64
this.$droplabel.text(selected_item_text);
65
}
66
67
var items = this.model.get('value_names');
68
var $replace_droplist = $('<ul />')
69
.addClass('dropdown-menu');
70
var that = this;
71
_.each(items, function(item, i) {
72
var item_button = $('<a href="#"/>')
73
.text(item)
74
.on('click', $.proxy(that.handle_click, that));
75
$replace_droplist.append($('<li />').append(item_button));
76
});
77
78
this.$droplist.replaceWith($replace_droplist);
79
this.$droplist.remove();
80
this.$droplist = $replace_droplist;
81
82
if (this.model.get('disabled')) {
83
this.$buttongroup.attr('disabled','disabled');
84
this.$droplabel.attr('disabled','disabled');
85
this.$dropbutton.attr('disabled','disabled');
86
this.$droplist.attr('disabled','disabled');
87
} else {
88
this.$buttongroup.removeAttr('disabled');
89
this.$droplabel.removeAttr('disabled');
90
this.$dropbutton.removeAttr('disabled');
91
this.$droplist.removeAttr('disabled');
92
}
93
94
var description = this.model.get('description');
95
if (description.length === 0) {
96
this.$label.hide();
97
} else {
98
this.$label.text(description);
99
this.$label.show();
100
}
101
}
102
return DropdownView.__super__.update.apply(this);
103
},
104
105
handle_click: function (e) {
106
// Handle when a value is clicked.
107
108
// Calling model.set will trigger all of the other views of the
109
// model to update.
110
this.model.set('value_name', $(e.target).text(), {updated_view: this});
111
this.touch();
112
},
113
114
});
115
WidgetManager.register_widget_view('DropdownView', DropdownView);
116
117
118
var RadioButtonsView = IPython.DOMWidgetView.extend({
119
render : function(){
120
// Called when view is rendered.
121
this.$el
122
.addClass('widget-hbox');
123
this.$label = $('<div />')
124
.appendTo(this.$el)
125
.addClass('widget-hlabel')
126
.hide();
127
this.$container = $('<div />')
128
.appendTo(this.$el)
129
.addClass('widget-radio-box');
130
this.$el_to_style = this.$container; // Set default element to style
131
this.update();
132
},
133
134
update : function(options){
135
// Update the contents of this view
136
//
137
// Called when the model is changed. The model may have been
138
// changed by another view or by a state update from the back-end.
139
if (options === undefined || options.updated_view != this) {
140
// Add missing items to the DOM.
141
var items = this.model.get('value_names');
142
var disabled = this.model.get('disabled');
143
var that = this;
144
_.each(items, function(item, index) {
145
var item_query = ' :input[value="' + item + '"]';
146
if (that.$el.find(item_query).length === 0) {
147
var $label = $('<label />')
148
.addClass('radio')
149
.text(item)
150
.appendTo(that.$container);
151
152
$('<input />')
153
.attr('type', 'radio')
154
.addClass(that.model)
155
.val(item)
156
.prependTo($label)
157
.on('click', $.proxy(that.handle_click, that));
158
}
159
160
var $item_element = that.$container.find(item_query);
161
if (that.model.get('value_name') == item) {
162
$item_element.prop('checked', true);
163
} else {
164
$item_element.prop('checked', false);
165
}
166
$item_element.prop('disabled', disabled);
167
});
168
169
// Remove items that no longer exist.
170
this.$container.find('input').each(function(i, obj) {
171
var value = $(obj).val();
172
var found = false;
173
_.each(items, function(item, index) {
174
if (item == value) {
175
found = true;
176
return false;
177
}
178
});
179
180
if (!found) {
181
$(obj).parent().remove();
182
}
183
});
184
185
var description = this.model.get('description');
186
if (description.length === 0) {
187
this.$label.hide();
188
} else {
189
this.$label.text(description);
190
this.$label.show();
191
}
192
}
193
return RadioButtonsView.__super__.update.apply(this);
194
},
195
196
handle_click: function (e) {
197
// Handle when a value is clicked.
198
199
// Calling model.set will trigger all of the other views of the
200
// model to update.
201
this.model.set('value_name', $(e.target).val(), {updated_view: this});
202
this.touch();
203
},
204
});
205
WidgetManager.register_widget_view('RadioButtonsView', RadioButtonsView);
206
207
208
var ToggleButtonsView = IPython.DOMWidgetView.extend({
209
render : function(){
210
// Called when view is rendered.
211
this.$el
212
.addClass('widget-hbox-single');
213
this.$label = $('<div />')
214
.appendTo(this.$el)
215
.addClass('widget-hlabel')
216
.hide();
217
this.$buttongroup = $('<div />')
218
.addClass('btn-group')
219
.attr('data-toggle', 'buttons-radio')
220
.appendTo(this.$el);
221
this.$el_to_style = this.$buttongroup; // Set default element to style
222
this.update();
223
},
224
225
update : function(options){
226
// Update the contents of this view
227
//
228
// Called when the model is changed. The model may have been
229
// changed by another view or by a state update from the back-end.
230
if (options === undefined || options.updated_view != this) {
231
// Add missing items to the DOM.
232
var items = this.model.get('value_names');
233
var disabled = this.model.get('disabled');
234
var that = this;
235
var item_html;
236
_.each(items, function(item, index) {
237
if (item.trim().length == 0) {
238
item_html = "&nbsp;";
239
} else {
240
item_html = IPython.utils.escape_html(item);
241
}
242
var item_query = '[data-value="' + item + '"]';
243
var $item_element = that.$buttongroup.find(item_query);
244
if (!$item_element.length) {
245
$item_element = $('<button/>')
246
.attr('type', 'button')
247
.addClass('btn')
248
.html(item_html)
249
.appendTo(that.$buttongroup)
250
.attr('data-value', item)
251
.on('click', $.proxy(that.handle_click, that));
252
}
253
if (that.model.get('value_name') == item) {
254
$item_element.addClass('active');
255
} else {
256
$item_element.removeClass('active');
257
}
258
$item_element.prop('disabled', disabled);
259
});
260
261
// Remove items that no longer exist.
262
this.$buttongroup.find('button').each(function(i, obj) {
263
var value = $(obj).data('value');
264
var found = false;
265
_.each(items, function(item, index) {
266
if (item == value) {
267
found = true;
268
return false;
269
}
270
});
271
272
if (!found) {
273
$(obj).remove();
274
}
275
});
276
277
var description = this.model.get('description');
278
if (description.length === 0) {
279
this.$label.hide();
280
} else {
281
this.$label.text(description);
282
this.$label.show();
283
}
284
}
285
return ToggleButtonsView.__super__.update.apply(this);
286
},
287
288
handle_click: function (e) {
289
// Handle when a value is clicked.
290
291
// Calling model.set will trigger all of the other views of the
292
// model to update.
293
this.model.set('value_name', $(e.target).data('value'), {updated_view: this});
294
this.touch();
295
},
296
});
297
WidgetManager.register_widget_view('ToggleButtonsView', ToggleButtonsView);
298
299
300
var SelectView = IPython.DOMWidgetView.extend({
301
render : function(){
302
// Called when view is rendered.
303
this.$el
304
.addClass('widget-hbox');
305
this.$label = $('<div />')
306
.appendTo(this.$el)
307
.addClass('widget-hlabel')
308
.hide();
309
this.$listbox = $('<select />')
310
.addClass('widget-listbox')
311
.attr('size', 6)
312
.appendTo(this.$el);
313
this.$el_to_style = this.$listbox; // Set default element to style
314
this.update();
315
},
316
317
update : function(options){
318
// Update the contents of this view
319
//
320
// Called when the model is changed. The model may have been
321
// changed by another view or by a state update from the back-end.
322
if (options === undefined || options.updated_view != this) {
323
// Add missing items to the DOM.
324
var items = this.model.get('value_names');
325
var that = this;
326
_.each(items, function(item, index) {
327
var item_query = ' :contains("' + item + '")';
328
if (that.$listbox.find(item_query).length === 0) {
329
$('<option />')
330
.text(item)
331
.attr('value_name', item)
332
.appendTo(that.$listbox)
333
.on('click', $.proxy(that.handle_click, that));
334
}
335
});
336
337
// Select the correct element
338
this.$listbox.val(this.model.get('value_name'));
339
340
// Disable listbox if needed
341
var disabled = this.model.get('disabled');
342
this.$listbox.prop('disabled', disabled);
343
344
// Remove items that no longer exist.
345
this.$listbox.find('option').each(function(i, obj) {
346
var value = $(obj).text();
347
var found = false;
348
_.each(items, function(item, index) {
349
if (item == value) {
350
found = true;
351
return false;
352
}
353
});
354
355
if (!found) {
356
$(obj).remove();
357
}
358
});
359
360
var description = this.model.get('description');
361
if (description.length === 0) {
362
this.$label.hide();
363
} else {
364
this.$label.text(description);
365
this.$label.show();
366
}
367
}
368
return SelectView.__super__.update.apply(this);
369
},
370
371
handle_click: function (e) {
372
// Handle when a value is clicked.
373
374
// Calling model.set will trigger all of the other views of the
375
// model to update.
376
this.model.set('value_name', $(e.target).text(), {updated_view: this});
377
this.touch();
378
},
379
});
380
WidgetManager.register_widget_view('SelectView', SelectView);
381
});
382
383