##// END OF EJS Templates
Add float range widget
Jonathan Frederic -
Show More
@@ -0,0 +1,1 b''
1 from widget import FloatRangeWidget No newline at end of file
@@ -0,0 +1,2 b''
1 var FloatRangeWidgetModel = IPython.WidgetModel.extend({});
2 IPython.notebook.widget_manager.register_widget_model('FloatRangeWidgetModel', FloatRangeWidgetModel);
@@ -0,0 +1,44 b''
1 var FloatSliderView = IPython.WidgetView.extend({
2
3 // Called when view is rendered.
4 render : function(){
5 this.$el
6 .html('')
7 .addClass(this.model.comm.comm_id);
8 this.$slider = $('<div />')
9 .slider({})
10 .addClass('slider');
11
12 // Put the slider in a container
13 this.$slider_container = $('<div />')
14 .css('padding-top', '4px')
15 .css('padding-bottom', '4px')
16 .append(this.$slider);
17 this.$el.append(this.$slider_container);
18
19 // Set defaults.
20 this.update();
21 },
22
23 // Handles: Backend -> Frontend Sync
24 // Frontent -> Frontend Sync
25 update : function(){
26 // Slider related keys.
27 var _keys = ['value', 'step', 'max', 'min', 'disabled', 'orientation'];
28 for (var index in _keys) {
29 var key = _keys[index];
30 if (this.model.get(key) != undefined) {
31 this.$slider.slider("option", key, this.model.get(key));
32 }
33 }
34 },
35
36 // Handles: User input
37 events: { "slide" : "handleSliderChange" },
38 handleSliderChange: function(e, ui) {
39 this.model.set('value', ui.value);
40 this.model.apply(this);
41 },
42 });
43
44 IPython.notebook.widget_manager.register_widget_view('FloatSliderView', FloatSliderView);
@@ -0,0 +1,71 b''
1
2 var FloatTextView = IPython.WidgetView.extend({
3
4 // Called when view is rendered.
5 render : function(){
6 this.$el
7 .html('')
8 .addClass(this.model.comm.comm_id);
9 this.$textbox = $('<input type="text" />')
10 .addClass('input')
11 .appendTo(this.$el);
12 this.update(); // Set defaults.
13 },
14
15 // Handles: Backend -> Frontend Sync
16 // Frontent -> Frontend Sync
17 update : function(){
18 var value = this.model.get('value');
19 if (!this.changing && parseFloat(this.$textbox.val()) != value) {
20 this.$textbox.val(value);
21 }
22
23 if (this.model.get('disabled')) {
24 this.$textbox.attr('disabled','disabled');
25 } else {
26 this.$textbox.removeAttr('disabled');
27 }
28 },
29
30
31 events: {"keyup input" : "handleChanging",
32 "paste input" : "handleChanging",
33 "cut input" : "handleChanging",
34 "change input" : "handleChanged"}, // Fires only when control is validated or looses focus.
35
36 // Handles and validates user input.
37 handleChanging: function(e) {
38
39 // Try to parse value as a float.
40 var numericalValue = 0.0;
41 if (e.target.value != '') {
42 numericalValue = parseFloat(e.target.value);
43 }
44
45 // If parse failed, reset value to value stored in model.
46 if (isNaN(numericalValue)) {
47 e.target.value = this.model.get('value');
48 } else if (!isNaN(numericalValue)) {
49 numericalValue = Math.min(this.model.get('max'), numericalValue);
50 numericalValue = Math.max(this.model.get('min'), numericalValue);
51
52 // Apply the value if it has changed.
53 if (numericalValue != this.model.get('value')) {
54 this.changing = true;
55 this.model.set('value', numericalValue);
56 this.model.apply(this);
57 this.changing = false;
58 }
59 }
60 },
61
62 // Applies validated input.
63 handleChanged: function(e) {
64 // Update the textbox
65 if (this.model.get('value') != e.target.value) {
66 e.target.value = this.model.get('value');
67 }
68 }
69 });
70
71 IPython.notebook.widget_manager.register_widget_view('FloatTextView', FloatTextView);
@@ -0,0 +1,17 b''
1 import os
2
3 from ..widget import Widget
4 from IPython.utils.traitlets import Unicode, Float, Bool
5 from IPython.utils.javascript import display_all_js
6
7 class FloatRangeWidget(Widget):
8 target_name = Unicode('FloatRangeWidgetModel')
9 default_view_name = Unicode('FloatSliderView')
10 _keys = ['value', 'step', 'max', 'min', 'disabled', 'orientation']
11
12 value = Float(0.0)
13 max = Float(100.0) # Max value
14 min = Float(0.0) # Min value
15 disabled = Bool(False) # Enable or disable user changes
16 step = Float(0.1) # Minimum step that the value can take (ignored by some views)
17 orientation = Unicode(u'horizontal') # Vertical or horizontal (ignored by some views)
General Comments 0
You need to be logged in to leave comments. Login now