int_range.js
191 lines
| 7.2 KiB
| application/javascript
|
JavascriptLexer
Jonathan Frederic
|
r14342 | define(["notebook/js/widget"], function(){ | ||
Jonathan Frederic
|
r14263 | var IntRangeWidgetModel = IPython.WidgetModel.extend({}); | ||
Jonathan Frederic
|
r14342 | IPython.widget_manager.register_widget_model('IntRangeWidgetModel', IntRangeWidgetModel); | ||
Jonathan Frederic
|
r14252 | |||
Jonathan Frederic
|
r14263 | var IntSliderView = IPython.WidgetView.extend({ | ||
Jonathan Frederic
|
r14252 | |||
Jonathan Frederic
|
r14263 | // Called when view is rendered. | ||
render : function(){ | ||||
Jonathan Frederic
|
r14268 | this.$el | ||
Jonathan Frederic
|
r14295 | .addClass('widget-hbox-single') | ||
Jonathan Frederic
|
r14279 | .html(''); | ||
Jonathan Frederic
|
r14292 | this.$label = $('<div />') | ||
.appendTo(this.$el) | ||||
Jonathan Frederic
|
r14297 | .addClass('widget-hlabel') | ||
Jonathan Frederic
|
r14292 | .hide(); | ||
Jonathan Frederic
|
r14263 | this.$slider = $('<div />') | ||
Jonathan Frederic
|
r14268 | .slider({}) | ||
.addClass('slider'); | ||||
Jonathan Frederic
|
r14263 | |||
// Put the slider in a container | ||||
this.$slider_container = $('<div />') | ||||
Jonathan Frederic
|
r14296 | .addClass('widget-hslider') | ||
Jonathan Frederic
|
r14268 | .append(this.$slider); | ||
Jonathan Frederic
|
r14314 | this.$el_to_style = this.$slider_container; // Set default element to style | ||
Jonathan Frederic
|
r14263 | this.$el.append(this.$slider_container); | ||
// Set defaults. | ||||
this.update(); | ||||
}, | ||||
Jonathan Frederic
|
r14252 | |||
Jonathan Frederic
|
r14263 | // Handles: Backend -> Frontend Sync | ||
// Frontent -> Frontend Sync | ||||
update : function(){ | ||||
// Slider related keys. | ||||
Jonathan Frederic
|
r14303 | var _keys = ['step', 'max', 'min', 'disabled']; | ||
Jonathan Frederic
|
r14263 | for (var index in _keys) { | ||
var key = _keys[index]; | ||||
if (this.model.get(key) != undefined) { | ||||
this.$slider.slider("option", key, this.model.get(key)); | ||||
} | ||||
Jonathan Frederic
|
r14252 | } | ||
Jonathan Frederic
|
r14292 | |||
Jonathan Frederic
|
r14303 | // 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. | ||||
Jonathan Frederic
|
r14296 | var orientation = this.model.get('orientation'); | ||
Jonathan Frederic
|
r14303 | var value = this.model.get('min'); | ||
this.$slider.slider('option', 'value', value); | ||||
this.$slider.slider('option', 'orientation', orientation); | ||||
var value = this.model.get('value'); | ||||
this.$slider.slider('option', 'value', value); | ||||
// Use the right CSS classes for vertical & horizontal sliders | ||||
Jonathan Frederic
|
r14296 | if (orientation=='vertical') { | ||
this.$slider_container | ||||
.removeClass('widget-hslider') | ||||
.addClass('widget-vslider'); | ||||
this.$el | ||||
.removeClass('widget-hbox-single') | ||||
Jonathan Frederic
|
r14297 | .addClass('widget-vbox-single'); | ||
this.$label | ||||
.removeClass('widget-hlabel') | ||||
.addClass('widget-vlabel'); | ||||
Jonathan Frederic
|
r14296 | } else { | ||
this.$slider_container | ||||
.removeClass('widget-vslider') | ||||
.addClass('widget-hslider'); | ||||
this.$el | ||||
Jonathan Frederic
|
r14297 | .removeClass('widget-vbox-single') | ||
Jonathan Frederic
|
r14296 | .addClass('widget-hbox-single'); | ||
Jonathan Frederic
|
r14297 | this.$label | ||
.removeClass('widget-vlabel') | ||||
.addClass('widget-hlabel'); | ||||
Jonathan Frederic
|
r14296 | } | ||
Jonathan Frederic
|
r14292 | var description = this.model.get('description'); | ||
if (description.length == 0) { | ||||
this.$label.hide(); | ||||
} else { | ||||
this.$label.html(description); | ||||
this.$label.show(); | ||||
} | ||||
Jonathan Frederic
|
r14279 | return IPython.WidgetView.prototype.update.call(this); | ||
Jonathan Frederic
|
r14263 | }, | ||
// Handles: User input | ||||
events: { "slide" : "handleSliderChange" }, | ||||
handleSliderChange: function(e, ui) { | ||||
this.model.set('value', ~~ui.value); // Double bit-wise not to truncate decimel | ||||
Jonathan Frederic
|
r14278 | this.model.update_other_views(this); | ||
Jonathan Frederic
|
r14263 | }, | ||
}); | ||||
Jonathan Frederic
|
r14252 | |||
Jonathan Frederic
|
r14342 | IPython.widget_manager.register_widget_view('IntSliderView', IntSliderView); | ||
Jonathan Frederic
|
r14252 | |||
Jonathan Frederic
|
r14263 | var IntTextView = IPython.WidgetView.extend({ | ||
Jonathan Frederic
|
r14252 | |||
Jonathan Frederic
|
r14263 | // Called when view is rendered. | ||
render : function(){ | ||||
this.$el | ||||
Jonathan Frederic
|
r14295 | .addClass('widget-hbox-single') | ||
Jonathan Frederic
|
r14279 | .html(''); | ||
Jonathan Frederic
|
r14292 | this.$label = $('<div />') | ||
.appendTo(this.$el) | ||||
Jonathan Frederic
|
r14297 | .addClass('widget-hlabel') | ||
Jonathan Frederic
|
r14292 | .hide(); | ||
Jonathan Frederic
|
r14263 | this.$textbox = $('<input type="text" />') | ||
.addClass('input') | ||||
Jonathan Frederic
|
r14295 | .addClass('widget-numeric-text') | ||
Jonathan Frederic
|
r14263 | .appendTo(this.$el); | ||
Jonathan Frederic
|
r14314 | this.$el_to_style = this.$textbox; // Set default element to style | ||
Jonathan Frederic
|
r14263 | this.update(); // Set defaults. | ||
}, | ||||
Jonathan Frederic
|
r14252 | |||
Jonathan Frederic
|
r14263 | // Handles: Backend -> Frontend Sync | ||
// Frontent -> Frontend Sync | ||||
update : function(){ | ||||
var value = this.model.get('value'); | ||||
if (!this.changing && parseInt(this.$textbox.val()) != value) { | ||||
this.$textbox.val(value); | ||||
} | ||||
if (this.model.get('disabled')) { | ||||
this.$textbox.attr('disabled','disabled'); | ||||
} else { | ||||
this.$textbox.removeAttr('disabled'); | ||||
} | ||||
Jonathan Frederic
|
r14292 | |||
var description = this.model.get('description'); | ||||
if (description.length == 0) { | ||||
this.$label.hide(); | ||||
} else { | ||||
this.$label.html(description); | ||||
this.$label.show(); | ||||
} | ||||
Jonathan Frederic
|
r14279 | return IPython.WidgetView.prototype.update.call(this); | ||
Jonathan Frederic
|
r14263 | }, | ||
events: {"keyup input" : "handleChanging", | ||||
"paste input" : "handleChanging", | ||||
"cut input" : "handleChanging", | ||||
"change input" : "handleChanged"}, // Fires only when control is validated or looses focus. | ||||
Jonathan Frederic
|
r14253 | |||
Jonathan Frederic
|
r14263 | // Handles and validates user input. | ||
handleChanging: function(e) { | ||||
Jonathan Frederic
|
r14253 | |||
Jonathan Frederic
|
r14263 | // Try to parse value as a float. | ||
var numericalValue = 0; | ||||
if (e.target.value != '') { | ||||
numericalValue = parseInt(e.target.value); | ||||
} | ||||
// 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')) { | ||||
this.changing = true; | ||||
this.model.set('value', numericalValue); | ||||
Jonathan Frederic
|
r14278 | this.model.update_other_views(this); | ||
Jonathan Frederic
|
r14263 | this.changing = false; | ||
} | ||||
} | ||||
}, | ||||
// Applies validated input. | ||||
handleChanged: function(e) { | ||||
// Update the textbox | ||||
if (this.model.get('value') != e.target.value) { | ||||
e.target.value = this.model.get('value'); | ||||
Jonathan Frederic
|
r14252 | } | ||
} | ||||
Jonathan Frederic
|
r14263 | }); | ||
Jonathan Frederic
|
r14252 | |||
Jonathan Frederic
|
r14342 | IPython.widget_manager.register_widget_view('IntTextView', IntTextView); | ||
Jonathan Frederic
|
r14263 | }); | ||