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
"jquery",
7
"bootstrap",
8
], function(widget, $){
9
10
var CheckboxView = widget.DOMWidgetView.extend({
11
render : function(){
12
/**
13
* Called when view is rendered.
14
*/
15
this.$el
16
.addClass('widget-hbox widget-checkbox');
17
this.$label = $('<div />')
18
.addClass('widget-label')
19
.appendTo(this.$el)
20
.hide();
21
this.$checkbox = $('<input />')
22
.attr('type', 'checkbox')
23
.appendTo(this.$el)
24
.click($.proxy(this.handle_click, this));
25
26
this.update(); // Set defaults.
27
},
28
29
update_attr: function(name, value) {
30
/**
31
* Set a css attr of the widget view.
32
*/
33
if (name == 'padding' || name == 'margin') {
34
this.$el.css(name, value);
35
} else {
36
this.$checkbox.css(name, value);
37
}
38
},
39
40
handle_click: function() {
41
/**
42
* Handles when the checkbox is clicked.
43
*
44
* Calling model.set will trigger all of the other views of the
45
* model to update.
46
*/
47
var value = this.model.get('value');
48
this.model.set('value', ! value, {updated_view: this});
49
this.touch();
50
},
51
52
update : function(options){
53
/**
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
this.$checkbox.prop('checked', this.model.get('value'));
60
61
if (options === undefined || options.updated_view != this) {
62
this.$checkbox.prop("disabled", this.model.get("disabled"));
63
64
var description = this.model.get("description");
65
if (description.trim().length === 0) {
66
this.$label.hide();
67
} else {
68
this.typeset(this.$label, description);
69
this.$label.show();
70
}
71
}
72
return CheckboxView.__super__.update.apply(this);
73
},
74
75
});
76
77
78
var ToggleButtonView = widget.DOMWidgetView.extend({
79
render : function() {
80
/**
81
* Called when view is rendered.
82
*/
83
var that = this;
84
this.setElement($('<button />')
85
.addClass('btn btn-default')
86
.attr('type', 'button')
87
.on('click', function (e) {
88
e.preventDefault();
89
that.handle_click();
90
}));
91
this.$el.attr("data-toggle", "tooltip");
92
this.model.on('change:button_style', function(model, value) {
93
this.update_button_style();
94
}, this);
95
this.update_button_style('');
96
97
this.update(); // Set defaults.
98
},
99
100
update_button_style: function(previous_trait_value) {
101
var class_map = {
102
primary: ['btn-primary'],
103
success: ['btn-success'],
104
info: ['btn-info'],
105
warning: ['btn-warning'],
106
danger: ['btn-danger']
107
};
108
this.update_mapped_classes(class_map, 'button_style', previous_trait_value);
109
},
110
111
update : function(options){
112
/**
113
* Update the contents of this view
114
*
115
* Called when the model is changed. The model may have been
116
* changed by another view or by a state update from the back-end.
117
*/
118
if (this.model.get('value')) {
119
this.$el.addClass('active');
120
} else {
121
this.$el.removeClass('active');
122
}
123
124
if (options === undefined || options.updated_view != this) {
125
this.$el.prop("disabled", this.model.get("disabled"));
126
this.$el.attr("title", this.model.get("tooltip"));
127
128
var description = this.model.get("description");
129
var icon = this.model.get("icon");
130
if (description.trim().length === 0 && icon.trim().length ===0) {
131
this.$el.html("&nbsp;"); // Preserve button height
132
} else {
133
this.$el.text(description);
134
$('<i class="fa"></i>').prependTo(this.$el).addClass(icon);
135
}
136
}
137
return ToggleButtonView.__super__.update.apply(this);
138
},
139
140
handle_click: function(e) {
141
/**
142
* Handles and validates user input.
143
*
144
* Calling model.set will trigger all of the other views of the
145
* model to update.
146
*/
147
var value = this.model.get('value');
148
this.model.set('value', ! value, {updated_view: this});
149
this.touch();
150
},
151
});
152
153
return {
154
'CheckboxView': CheckboxView,
155
'ToggleButtonView': ToggleButtonView,
156
};
157
});
158
159