Show More
widget_int_range.js
219 lines
| 8.7 KiB
| application/javascript
|
JavascriptLexer
|
r14366 | //---------------------------------------------------------------------------- | ||
// Copyright (C) 2013 The IPython Development Team | ||||
// | ||||
// Distributed under the terms of the BSD License. The full license is in | ||||
// the file COPYING, distributed as part of this software. | ||||
//---------------------------------------------------------------------------- | ||||
//============================================================================ | ||||
// IntRangeWidget | ||||
//============================================================================ | ||||
/** | ||||
* @module IPython | ||||
* @namespace IPython | ||||
**/ | ||||
|
r14537 | define(["notebook/js/widgets/widget"], function(widget_manager){ | ||
|
r14263 | var IntRangeWidgetModel = IPython.WidgetModel.extend({}); | ||
|
r14374 | widget_manager.register_widget_model('IntRangeWidgetModel', IntRangeWidgetModel); | ||
|
r14252 | |||
|
r14568 | var IntSliderView = IPython.DOMWidgetView.extend({ | ||
|
r14252 | |||
|
r14263 | // Called when view is rendered. | ||
render : function(){ | ||||
|
r14268 | this.$el | ||
|
r14295 | .addClass('widget-hbox-single') | ||
|
r14279 | .html(''); | ||
|
r14292 | this.$label = $('<div />') | ||
.appendTo(this.$el) | ||||
|
r14297 | .addClass('widget-hlabel') | ||
|
r14292 | .hide(); | ||
|
r14263 | this.$slider = $('<div />') | ||
|
r14268 | .slider({}) | ||
.addClass('slider'); | ||||
|
r14263 | |||
// Put the slider in a container | ||||
this.$slider_container = $('<div />') | ||||
|
r14296 | .addClass('widget-hslider') | ||
|
r14268 | .append(this.$slider); | ||
|
r14314 | this.$el_to_style = this.$slider_container; // Set default element to style | ||
|
r14263 | this.$el.append(this.$slider_container); | ||
// Set defaults. | ||||
this.update(); | ||||
}, | ||||
|
r14252 | |||
|
r14570 | update : function(options){ | ||
|
r14568 | // 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. | ||||
|
r14570 | if (options === undefined || options.updated_view != this) { | ||
// Slider related keys. | ||||
var _keys = ['step', 'max', 'min', 'disabled']; | ||||
for (var index in _keys) { | ||||
var key = _keys[index]; | ||||
if (this.model.get(key) !== undefined) { | ||||
this.$slider.slider("option", key, this.model.get(key)); | ||||
} | ||||
|
r14263 | } | ||
|
r14292 | |||
|
r14570 | // 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'); | ||||
var value = this.model.get('min'); | ||||
this.$slider.slider('option', 'value', value); | ||||
this.$slider.slider('option', 'orientation', orientation); | ||||
value = this.model.get('value'); | ||||
this.$slider.slider('option', 'value', value); | ||||
|
r14303 | |||
|
r14570 | // 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'); | ||||
|
r14297 | |||
|
r14570 | } 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'); | ||||
} | ||||
|
r14296 | |||
|
r14570 | var description = this.model.get('description'); | ||
if (description.length === 0) { | ||||
this.$label.hide(); | ||||
} else { | ||||
this.$label.html(description); | ||||
this.$label.show(); | ||||
} | ||||
|
r14292 | } | ||
|
r14568 | return IPython.DOMWidgetView.prototype.update.call(this); | ||
|
r14263 | }, | ||
// Handles: User input | ||||
events: { "slide" : "handleSliderChange" }, | ||||
handleSliderChange: function(e, ui) { | ||||
|
r14569 | |||
// Calling model.set will trigger all of the other views of the | ||||
// model to update. | ||||
|
r14570 | this.model.set('value', ~~ui.value, {updated_view: this}); // Double bit-wise not to truncate decimel | ||
|
r14482 | this.touch(); | ||
|
r14263 | }, | ||
}); | ||||
|
r14252 | |||
|
r14374 | widget_manager.register_widget_view('IntSliderView', IntSliderView); | ||
|
r14252 | |||
|
r14568 | var IntTextView = IPython.DOMWidgetView.extend({ | ||
|
r14252 | |||
|
r14263 | // Called when view is rendered. | ||
render : function(){ | ||||
this.$el | ||||
|
r14295 | .addClass('widget-hbox-single') | ||
|
r14279 | .html(''); | ||
|
r14292 | this.$label = $('<div />') | ||
.appendTo(this.$el) | ||||
|
r14297 | .addClass('widget-hlabel') | ||
|
r14292 | .hide(); | ||
|
r14263 | this.$textbox = $('<input type="text" />') | ||
.addClass('input') | ||||
|
r14295 | .addClass('widget-numeric-text') | ||
|
r14263 | .appendTo(this.$el); | ||
|
r14314 | this.$el_to_style = this.$textbox; // Set default element to style | ||
|
r14263 | this.update(); // Set defaults. | ||
}, | ||||
|
r14252 | |||
|
r14570 | update : function(options){ | ||
|
r14568 | // 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. | ||||
|
r14570 | if (options === undefined || options.updated_view != this) { | ||
var value = this.model.get('value'); | ||||
if (parseInt(this.$textbox.val()) != value) { | ||||
this.$textbox.val(value); | ||||
} | ||||
if (this.model.get('disabled')) { | ||||
this.$textbox.attr('disabled','disabled'); | ||||
} else { | ||||
this.$textbox.removeAttr('disabled'); | ||||
} | ||||
|
r14568 | |||
|
r14570 | var description = this.model.get('description'); | ||
if (description.length === 0) { | ||||
this.$label.hide(); | ||||
} else { | ||||
this.$label.html(description); | ||||
this.$label.show(); | ||||
} | ||||
|
r14292 | } | ||
|
r14568 | return IPython.DOMWidgetView.prototype.update.call(this); | ||
|
r14263 | }, | ||
events: {"keyup input" : "handleChanging", | ||||
"paste input" : "handleChanging", | ||||
"cut input" : "handleChanging", | ||||
"change input" : "handleChanged"}, // Fires only when control is validated or looses focus. | ||||
|
r14253 | |||
|
r14263 | // Handles and validates user input. | ||
handleChanging: function(e) { | ||||
|
r14253 | |||
|
r14263 | // Try to parse value as a float. | ||
var numericalValue = 0; | ||||
|
r14466 | if (e.target.value !== '') { | ||
|
r14263 | 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)) { | ||||
|
r14466 | if (this.model.get('max') !== undefined) { | ||
|
r14263 | numericalValue = Math.min(this.model.get('max'), numericalValue); | ||
} | ||||
|
r14466 | if (this.model.get('min') !== undefined) { | ||
|
r14263 | numericalValue = Math.max(this.model.get('min'), numericalValue); | ||
} | ||||
// Apply the value if it has changed. | ||||
if (numericalValue != this.model.get('value')) { | ||||
|
r14569 | |||
// Calling model.set will trigger all of the other views of the | ||||
// model to update. | ||||
|
r14570 | this.model.set('value', numericalValue, {updated_view: this}); | ||
|
r14482 | this.touch(); | ||
|
r14263 | } | ||
} | ||||
}, | ||||
// Applies validated input. | ||||
handleChanged: function(e) { | ||||
// Update the textbox | ||||
if (this.model.get('value') != e.target.value) { | ||||
e.target.value = this.model.get('value'); | ||||
|
r14252 | } | ||
} | ||||
|
r14263 | }); | ||
|
r14252 | |||
|
r14374 | widget_manager.register_widget_view('IntTextView', IntTextView); | ||
|
r14263 | }); | ||