widget_int.js
366 lines
| 14.1 KiB
| application/javascript
|
JavascriptLexer
Jonathan Frederic
|
r17198 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
Jonathan Frederic
|
r14366 | |||
Jonathan Frederic
|
r17198 | define([ | ||
"widgets/js/widget", | ||||
Jonathan Frederic
|
r17216 | "jqueryui", | ||
MinRK
|
r17312 | "bootstrap", | ||
Jonathan Frederic
|
r17216 | ], function(widget, $){ | ||
Jonathan Frederic
|
r17198 | |||
var IntSliderView = widget.DOMWidgetView.extend({ | ||||
Jonathan Frederic
|
r14672 | render : function(){ | ||
// Called when view is rendered. | ||||
this.$el | ||||
.addClass('widget-hbox-single'); | ||||
this.$label = $('<div />') | ||||
.appendTo(this.$el) | ||||
.addClass('widget-hlabel') | ||||
.hide(); | ||||
Brian E. Granger
|
r14982 | |||
Jonathan Frederic
|
r14672 | this.$slider = $('<div />') | ||
.slider({}) | ||||
.addClass('slider'); | ||||
// Put the slider in a container | ||||
this.$slider_container = $('<div />') | ||||
.addClass('widget-hslider') | ||||
Jonathan Frederic
|
r16817 | .append(this.$slider); | ||
Jonathan Frederic
|
r14672 | this.$el.append(this.$slider_container); | ||
Brian E. Granger
|
r14982 | this.$readout = $('<div/>') | ||
.appendTo(this.$el) | ||||
Brian E. Granger
|
r14983 | .addClass('widget-hreadout') | ||
Brian E. Granger
|
r14982 | .hide(); | ||
Jonathan Frederic
|
r17723 | |||
this.model.on('change:slider_color', function(sender, value) { | ||||
this.$slider.find('a').css('background', value); | ||||
}, this); | ||||
Brian E. Granger
|
r14982 | |||
Jonathan Frederic
|
r14672 | // Set defaults. | ||
this.update(); | ||||
}, | ||||
Jonathan Frederic
|
r17723 | |||
update_attr: function(name, value) { | ||||
// Set a css attr of the widget view. | ||||
if (name == 'color') { | ||||
this.$readout.css(name, value); | ||||
} else if (name.substring(0, 4) == 'font') { | ||||
this.$readout.css(name, value); | ||||
} else if (name.substring(0, 6) == 'border') { | ||||
this.$slider.find('a').css(name, value); | ||||
this.$slider_container.css(name, value); | ||||
} else if (name == 'width' || name == 'height' || name == 'background') { | ||||
this.$slider_container.css(name, value); | ||||
} else { | ||||
this.$slider.css(name, value); | ||||
} | ||||
}, | ||||
Jonathan Frederic
|
r14672 | |||
update : function(options){ | ||||
// Update the contents of this view | ||||
// | ||||
// Called when the model is changed. The model may have been | ||||
// changed by another view or by a state update from the back-end. | ||||
if (options === undefined || options.updated_view != this) { | ||||
// JQuery slider option keys. These keys happen to have a | ||||
// one-to-one mapping with the corrosponding keys of the model. | ||||
Gordon Ball
|
r17703 | var jquery_slider_keys = ['step', 'max', 'min', 'disabled']; | ||
Jonathan Frederic
|
r14686 | var that = this; | ||
Jonathan Frederic
|
r17175 | that.$slider.slider({}); | ||
Jonathan Frederic
|
r14672 | _.each(jquery_slider_keys, function(key, i) { | ||
Jonathan Frederic
|
r14686 | var model_value = that.model.get(key); | ||
Jonathan Frederic
|
r14672 | if (model_value !== undefined) { | ||
Jonathan Frederic
|
r14686 | that.$slider.slider("option", key, model_value); | ||
Jonathan Frederic
|
r14672 | } | ||
}); | ||||
Gordon Ball
|
r17703 | var range_value = this.model.get("_range"); | ||
if (range_value !== undefined) { | ||||
this.$slider.slider("option", "range", range_value); | ||||
} | ||||
Jonathan Frederic
|
r14672 | |||
// WORKAROUND FOR JQUERY SLIDER BUG. | ||||
// The horizontal position of the slider handle | ||||
// depends on the value of the slider at the time | ||||
// of orientation change. Before applying the new | ||||
// workaround, we set the value to the minimum to | ||||
// make sure that the horizontal placement of the | ||||
// handle in the vertical slider is always | ||||
// consistent. | ||||
var orientation = this.model.get('orientation'); | ||||
Tarun Gaba
|
r17274 | var min = this.model.get('min'); | ||
var max = this.model.get('max'); | ||||
Gordon Ball
|
r17703 | if (this.model.get('_range')) { | ||
Gordon Ball
|
r17684 | this.$slider.slider('option', 'values', [min, min]); | ||
Gordon Ball
|
r17059 | } else { | ||
Gordon Ball
|
r17684 | this.$slider.slider('option', 'value', min); | ||
Gordon Ball
|
r17059 | } | ||
Jonathan Frederic
|
r14672 | this.$slider.slider('option', 'orientation', orientation); | ||
Tarun Gaba
|
r17657 | var value = this.model.get('value'); | ||
Gordon Ball
|
r17703 | if (this.model.get('_range')) { | ||
Gordon Ball
|
r17702 | // values for the range case are validated python-side in | ||
// _Bounded{Int,Float}RangeWidget._validate | ||||
Gordon Ball
|
r17059 | this.$slider.slider('option', 'values', value); | ||
this.$readout.text(value.join("-")); | ||||
} else { | ||||
Gordon Ball
|
r17702 | if(value > max) { | ||
value = max; | ||||
} | ||||
else if(value < min){ | ||||
value = min; | ||||
} | ||||
Gordon Ball
|
r17059 | this.$slider.slider('option', 'value', value); | ||
this.$readout.text(value); | ||||
} | ||||
Tarun Gaba
|
r17654 | if(this.model.get('value')!=value) { | ||
this.model.set('value', value, {updated_view: this}); | ||||
this.touch(); | ||||
} | ||||
Jonathan Frederic
|
r14672 | |||
// Use the right CSS classes for vertical & horizontal sliders | ||||
if (orientation=='vertical') { | ||||
this.$slider_container | ||||
.removeClass('widget-hslider') | ||||
.addClass('widget-vslider'); | ||||
this.$el | ||||
.removeClass('widget-hbox-single') | ||||
.addClass('widget-vbox-single'); | ||||
this.$label | ||||
.removeClass('widget-hlabel') | ||||
.addClass('widget-vlabel'); | ||||
Brian E. Granger
|
r14983 | this.$readout | ||
.removeClass('widget-hreadout') | ||||
.addClass('widget-vreadout'); | ||||
Jonathan Frederic
|
r14672 | |||
} else { | ||||
this.$slider_container | ||||
.removeClass('widget-vslider') | ||||
.addClass('widget-hslider'); | ||||
this.$el | ||||
.removeClass('widget-vbox-single') | ||||
.addClass('widget-hbox-single'); | ||||
this.$label | ||||
.removeClass('widget-vlabel') | ||||
.addClass('widget-hlabel'); | ||||
Brian E. Granger
|
r14983 | this.$readout | ||
.removeClass('widget-vreadout') | ||||
.addClass('widget-hreadout'); | ||||
Jonathan Frederic
|
r14672 | } | ||
var description = this.model.get('description'); | ||||
if (description.length === 0) { | ||||
this.$label.hide(); | ||||
} else { | ||||
this.$label.text(description); | ||||
Jonathan Frederic
|
r16817 | MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$label.get(0)]); | ||
Jonathan Frederic
|
r14672 | this.$label.show(); | ||
} | ||||
Brian E. Granger
|
r14982 | |||
var readout = this.model.get('readout'); | ||||
if (readout) { | ||||
this.$readout.show(); | ||||
} else { | ||||
this.$readout.hide(); | ||||
} | ||||
Jonathan Frederic
|
r14672 | } | ||
return IntSliderView.__super__.update.apply(this); | ||||
}, | ||||
events: { | ||||
// Dictionary of events and their handlers. | ||||
"slide" : "handleSliderChange" | ||||
}, | ||||
handleSliderChange: function(e, ui) { | ||||
// Called when the slider value is changed. | ||||
// Calling model.set will trigger all of the other views of the | ||||
// model to update. | ||||
Gordon Ball
|
r17703 | if (this.model.get("_range")) { | ||
Gordon Ball
|
r17059 | var actual_value = ui.values.map(this._validate_slide_value); | ||
this.$readout.text(actual_value.join("-")); | ||||
} else { | ||||
var actual_value = this._validate_slide_value(ui.value); | ||||
this.$readout.text(actual_value); | ||||
} | ||||
Brian E. Granger
|
r14982 | this.model.set('value', actual_value, {updated_view: this}); | ||
Jonathan Frederic
|
r14672 | this.touch(); | ||
}, | ||||
Jonathan Frederic
|
r14694 | |||
_validate_slide_value: function(x) { | ||||
// Validate the value of the slider before sending it to the back-end | ||||
// and applying it to the other views on the page. | ||||
// Double bit-wise not truncates the decimel (int cast). | ||||
return ~~x; | ||||
}, | ||||
Jonathan Frederic
|
r14672 | }); | ||
Jonathan Frederic
|
r17198 | var IntTextView = widget.DOMWidgetView.extend({ | ||
Jonathan Frederic
|
r14672 | render : function(){ | ||
// Called when view is rendered. | ||||
this.$el | ||||
.addClass('widget-hbox-single'); | ||||
this.$label = $('<div />') | ||||
.appendTo(this.$el) | ||||
.addClass('widget-hlabel') | ||||
.hide(); | ||||
this.$textbox = $('<input type="text" />') | ||||
Jonathan Frederic
|
r16952 | .addClass('form-control') | ||
Jonathan Frederic
|
r14672 | .addClass('widget-numeric-text') | ||
.appendTo(this.$el); | ||||
this.update(); // Set defaults. | ||||
}, | ||||
update : function(options){ | ||||
// Update the contents of this view | ||||
// | ||||
// Called when the model is changed. The model may have been | ||||
// changed by another view or by a state update from the back-end. | ||||
if (options === undefined || options.updated_view != this) { | ||||
var value = this.model.get('value'); | ||||
Jonathan Frederic
|
r14694 | if (this._parse_value(this.$textbox.val()) != value) { | ||
Jonathan Frederic
|
r14672 | this.$textbox.val(value); | ||
} | ||||
if (this.model.get('disabled')) { | ||||
this.$textbox.attr('disabled','disabled'); | ||||
} else { | ||||
this.$textbox.removeAttr('disabled'); | ||||
} | ||||
var description = this.model.get('description'); | ||||
if (description.length === 0) { | ||||
this.$label.hide(); | ||||
} else { | ||||
this.$label.text(description); | ||||
Jonathan Frederic
|
r16817 | MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$label.get(0)]); | ||
Jonathan Frederic
|
r14672 | this.$label.show(); | ||
} | ||||
} | ||||
return IntTextView.__super__.update.apply(this); | ||||
}, | ||||
Jonathan Frederic
|
r17723 | update_attr: function(name, value) { | ||
// Set a css attr of the widget view. | ||||
this.$textbox.css(name, value); | ||||
}, | ||||
Jonathan Frederic
|
r14672 | events: { | ||
// Dictionary of events and their handlers. | ||||
"keyup input" : "handleChanging", | ||||
"paste input" : "handleChanging", | ||||
"cut input" : "handleChanging", | ||||
// Fires only when control is validated or looses focus. | ||||
"change input" : "handleChanged" | ||||
}, | ||||
handleChanging: function(e) { | ||||
// Handles and validates user input. | ||||
Jonathan Frederic
|
r14694 | // Try to parse value as a int. | ||
Jonathan Frederic
|
r14672 | var numericalValue = 0; | ||
if (e.target.value !== '') { | ||||
jon
|
r16019 | var trimmed = e.target.value.trim(); | ||
if (!(['-', '-.', '.', '+.', '+'].indexOf(trimmed) >= 0)) { | ||||
numericalValue = this._parse_value(e.target.value); | ||||
} | ||||
Jonathan Frederic
|
r14672 | } | ||
// If parse failed, reset value to value stored in model. | ||||
if (isNaN(numericalValue)) { | ||||
e.target.value = this.model.get('value'); | ||||
} else if (!isNaN(numericalValue)) { | ||||
if (this.model.get('max') !== undefined) { | ||||
numericalValue = Math.min(this.model.get('max'), numericalValue); | ||||
} | ||||
if (this.model.get('min') !== undefined) { | ||||
numericalValue = Math.max(this.model.get('min'), numericalValue); | ||||
} | ||||
// Apply the value if it has changed. | ||||
if (numericalValue != this.model.get('value')) { | ||||
// Calling model.set will trigger all of the other views of the | ||||
// model to update. | ||||
this.model.set('value', numericalValue, {updated_view: this}); | ||||
this.touch(); | ||||
} | ||||
} | ||||
}, | ||||
MinRK
|
r14792 | handleChanged: function(e) { | ||
Jonathan Frederic
|
r14672 | // Applies validated input. | ||
if (this.model.get('value') != e.target.value) { | ||||
e.target.value = this.model.get('value'); | ||||
} | ||||
Jonathan Frederic
|
r14694 | }, | ||
_parse_value: function(value) { | ||||
// Parse the value stored in a string. | ||||
return parseInt(value); | ||||
}, | ||||
Jonathan Frederic
|
r14672 | }); | ||
Jonathan Frederic
|
r14694 | |||
Jonathan Frederic
|
r17198 | var ProgressView = widget.DOMWidgetView.extend({ | ||
Jonathan Frederic
|
r14694 | render : function(){ | ||
// Called when view is rendered. | ||||
this.$el | ||||
.addClass('widget-hbox-single'); | ||||
this.$label = $('<div />') | ||||
.appendTo(this.$el) | ||||
.addClass('widget-hlabel') | ||||
.hide(); | ||||
this.$progress = $('<div />') | ||||
.addClass('progress') | ||||
.addClass('widget-progress') | ||||
.appendTo(this.$el); | ||||
this.$bar = $('<div />') | ||||
Jonathan Frederic
|
r16913 | .addClass('progress-bar') | ||
Jonathan Frederic
|
r14694 | .css('width', '50%') | ||
.appendTo(this.$progress); | ||||
this.update(); // Set defaults. | ||||
}, | ||||
update : function(){ | ||||
// Update the contents of this view | ||||
// | ||||
// Called when the model is changed. The model may have been | ||||
// changed by another view or by a state update from the back-end. | ||||
var value = this.model.get('value'); | ||||
var max = this.model.get('max'); | ||||
var min = this.model.get('min'); | ||||
var percent = 100.0 * (value - min) / (max - min); | ||||
this.$bar.css('width', percent + '%'); | ||||
var description = this.model.get('description'); | ||||
if (description.length === 0) { | ||||
this.$label.hide(); | ||||
} else { | ||||
this.$label.text(description); | ||||
Jonathan Frederic
|
r16817 | MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$label.get(0)]); | ||
Jonathan Frederic
|
r14694 | this.$label.show(); | ||
} | ||||
return ProgressView.__super__.update.apply(this); | ||||
}, | ||||
Jonathan Frederic
|
r17723 | |||
update_attr: function(name, value) { | ||||
// Set a css attr of the widget view. | ||||
if (name.substring(0, 6) == 'border' || name == 'width' || name == 'height' || name == 'background') { | ||||
this.$progress.css(name, value); | ||||
} else if (name == 'color') { | ||||
this.$bar.css('background', value); | ||||
} else { | ||||
this.$bar.css(name, value); | ||||
} | ||||
}, | ||||
Jonathan Frederic
|
r14694 | }); | ||
Jonathan Frederic
|
r17198 | return { | ||
'IntSliderView': IntSliderView, | ||||
'IntTextView': IntTextView, | ||||
'ProgressView': ProgressView, | ||||
}; | ||||
Jonathan Frederic
|
r14672 | }); | ||