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
// IntWidget
10
//============================================================================
11
12
/**
13
* @module IPython
14
* @namespace IPython
15
**/
16
17
define(["widgets/js/widget"], function(WidgetManager){
18
19
var IntSliderView = 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
29
this.$slider = $('<div />')
30
.slider({})
31
.addClass('slider');
32
// Put the slider in a container
33
this.$slider_container = $('<div />')
34
.addClass('widget-hslider')
35
.append(this.$slider)
36
this.$el_to_style = this.$slider_container; // Set default element to style
37
this.$el.append(this.$slider_container);
38
39
this.$readout = $('<div/>')
40
.appendTo(this.$el)
41
.addClass('widget-hreadout')
42
.hide();
43
44
// Set defaults.
45
this.update();
46
},
47
48
update : function(options){
49
// Update the contents of this view
50
//
51
// Called when the model is changed. The model may have been
52
// changed by another view or by a state update from the back-end.
53
if (options === undefined || options.updated_view != this) {
54
// JQuery slider option keys. These keys happen to have a
55
// one-to-one mapping with the corrosponding keys of the model.
56
var jquery_slider_keys = ['step', 'max', 'min', 'disabled'];
57
var that = this;
58
_.each(jquery_slider_keys, function(key, i) {
59
var model_value = that.model.get(key);
60
if (model_value !== undefined) {
61
that.$slider.slider("option", key, model_value);
62
}
63
});
64
65
// WORKAROUND FOR JQUERY SLIDER BUG.
66
// The horizontal position of the slider handle
67
// depends on the value of the slider at the time
68
// of orientation change. Before applying the new
69
// workaround, we set the value to the minimum to
70
// make sure that the horizontal placement of the
71
// handle in the vertical slider is always
72
// consistent.
73
var orientation = this.model.get('orientation');
74
var value = this.model.get('min');
75
this.$slider.slider('option', 'value', value);
76
this.$slider.slider('option', 'orientation', orientation);
77
value = this.model.get('value');
78
this.$slider.slider('option', 'value', value);
79
this.$readout.text(value);
80
81
// Use the right CSS classes for vertical & horizontal sliders
82
if (orientation=='vertical') {
83
this.$slider_container
84
.removeClass('widget-hslider')
85
.addClass('widget-vslider');
86
this.$el
87
.removeClass('widget-hbox-single')
88
.addClass('widget-vbox-single');
89
this.$label
90
.removeClass('widget-hlabel')
91
.addClass('widget-vlabel');
92
this.$readout
93
.removeClass('widget-hreadout')
94
.addClass('widget-vreadout');
95
96
} else {
97
this.$slider_container
98
.removeClass('widget-vslider')
99
.addClass('widget-hslider');
100
this.$el
101
.removeClass('widget-vbox-single')
102
.addClass('widget-hbox-single');
103
this.$label
104
.removeClass('widget-vlabel')
105
.addClass('widget-hlabel');
106
this.$readout
107
.removeClass('widget-vreadout')
108
.addClass('widget-hreadout');
109
}
110
111
var description = this.model.get('description');
112
if (description.length === 0) {
113
this.$label.hide();
114
} else {
115
this.$label.text(description);
116
this.$label.show();
117
}
118
119
var readout = this.model.get('readout');
120
if (readout) {
121
this.$readout.show();
122
} else {
123
this.$readout.hide();
124
}
125
}
126
return IntSliderView.__super__.update.apply(this);
127
},
128
129
events: {
130
// Dictionary of events and their handlers.
131
"slide" : "handleSliderChange"
132
},
133
134
handleSliderChange: function(e, ui) {
135
// Called when the slider value is changed.
136
137
// Calling model.set will trigger all of the other views of the
138
// model to update.
139
var actual_value = this._validate_slide_value(ui.value);
140
this.model.set('value', actual_value, {updated_view: this});
141
this.$readout.text(actual_value);
142
this.touch();
143
},
144
145
_validate_slide_value: function(x) {
146
// Validate the value of the slider before sending it to the back-end
147
// and applying it to the other views on the page.
148
149
// Double bit-wise not truncates the decimel (int cast).
150
return ~~x;
151
},
152
});
153
WidgetManager.register_widget_view('IntSliderView', IntSliderView);
154
155
156
var IntTextView = IPython.DOMWidgetView.extend({
157
render : function(){
158
// Called when view is rendered.
159
this.$el
160
.addClass('widget-hbox-single');
161
this.$label = $('<div />')
162
.appendTo(this.$el)
163
.addClass('widget-hlabel')
164
.hide();
165
this.$textbox = $('<input type="text" />')
166
.addClass('input')
167
.addClass('widget-numeric-text')
168
.appendTo(this.$el);
169
this.$el_to_style = this.$textbox; // Set default element to style
170
this.update(); // Set defaults.
171
},
172
173
update : function(options){
174
// Update the contents of this view
175
//
176
// Called when the model is changed. The model may have been
177
// changed by another view or by a state update from the back-end.
178
if (options === undefined || options.updated_view != this) {
179
var value = this.model.get('value');
180
if (this._parse_value(this.$textbox.val()) != value) {
181
this.$textbox.val(value);
182
}
183
184
if (this.model.get('disabled')) {
185
this.$textbox.attr('disabled','disabled');
186
} else {
187
this.$textbox.removeAttr('disabled');
188
}
189
190
var description = this.model.get('description');
191
if (description.length === 0) {
192
this.$label.hide();
193
} else {
194
this.$label.text(description);
195
this.$label.show();
196
}
197
}
198
return IntTextView.__super__.update.apply(this);
199
},
200
201
events: {
202
// Dictionary of events and their handlers.
203
"keyup input" : "handleChanging",
204
"paste input" : "handleChanging",
205
"cut input" : "handleChanging",
206
207
// Fires only when control is validated or looses focus.
208
"change input" : "handleChanged"
209
},
210
211
handleChanging: function(e) {
212
// Handles and validates user input.
213
214
// Try to parse value as a int.
215
var numericalValue = 0;
216
if (e.target.value !== '') {
217
var trimmed = e.target.value.trim();
218
if (!(['-', '-.', '.', '+.', '+'].indexOf(trimmed) >= 0)) {
219
numericalValue = this._parse_value(e.target.value);
220
}
221
}
222
223
// If parse failed, reset value to value stored in model.
224
if (isNaN(numericalValue)) {
225
e.target.value = this.model.get('value');
226
} else if (!isNaN(numericalValue)) {
227
if (this.model.get('max') !== undefined) {
228
numericalValue = Math.min(this.model.get('max'), numericalValue);
229
}
230
if (this.model.get('min') !== undefined) {
231
numericalValue = Math.max(this.model.get('min'), numericalValue);
232
}
233
234
// Apply the value if it has changed.
235
if (numericalValue != this.model.get('value')) {
236
237
// Calling model.set will trigger all of the other views of the
238
// model to update.
239
this.model.set('value', numericalValue, {updated_view: this});
240
this.touch();
241
}
242
}
243
},
244
245
handleChanged: function(e) {
246
// Applies validated input.
247
if (this.model.get('value') != e.target.value) {
248
e.target.value = this.model.get('value');
249
}
250
},
251
252
_parse_value: function(value) {
253
// Parse the value stored in a string.
254
return parseInt(value);
255
},
256
});
257
WidgetManager.register_widget_view('IntTextView', IntTextView);
258
259
260
var ProgressView = IPython.DOMWidgetView.extend({
261
render : function(){
262
// Called when view is rendered.
263
this.$el
264
.addClass('widget-hbox-single');
265
this.$label = $('<div />')
266
.appendTo(this.$el)
267
.addClass('widget-hlabel')
268
.hide();
269
this.$progress = $('<div />')
270
.addClass('progress')
271
.addClass('widget-progress')
272
.appendTo(this.$el);
273
this.$el_to_style = this.$progress; // Set default element to style
274
this.$bar = $('<div />')
275
.addClass('bar')
276
.css('width', '50%')
277
.appendTo(this.$progress);
278
this.update(); // Set defaults.
279
},
280
281
update : function(){
282
// Update the contents of this view
283
//
284
// Called when the model is changed. The model may have been
285
// changed by another view or by a state update from the back-end.
286
var value = this.model.get('value');
287
var max = this.model.get('max');
288
var min = this.model.get('min');
289
var percent = 100.0 * (value - min) / (max - min);
290
this.$bar.css('width', percent + '%');
291
292
var description = this.model.get('description');
293
if (description.length === 0) {
294
this.$label.hide();
295
} else {
296
this.$label.text(description);
297
this.$label.show();
298
}
299
return ProgressView.__super__.update.apply(this);
300
},
301
});
302
WidgetManager.register_widget_view('ProgressView', ProgressView);
303
304
305
// Return the slider and text views so they can be inheritted to create the
306
// float versions.
307
return [IntSliderView, IntTextView];
308
});
309
310