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