Show More
@@ -1,107 +1,109 | |||
|
1 | 1 | |
|
2 | 2 | require(["notebook/js/widget"], function(){ |
|
3 | 3 | |
|
4 | 4 | var BoolWidgetModel = IPython.WidgetModel.extend({}); |
|
5 | 5 | IPython.notebook.widget_manager.register_widget_model('BoolWidgetModel', BoolWidgetModel); |
|
6 | 6 | |
|
7 | 7 | var CheckboxView = IPython.WidgetView.extend({ |
|
8 | 8 | |
|
9 | 9 | // Called when view is rendered. |
|
10 | 10 | render : function(){ |
|
11 | 11 | this.$el = $('<div />') |
|
12 | 12 | .addClass('widget-hbox-single'); |
|
13 | 13 | this.$label = $('<div />') |
|
14 | 14 | .addClass('widget-hlabel') |
|
15 | 15 | .appendTo(this.$el) |
|
16 | 16 | .hide(); |
|
17 | 17 | var that = this; |
|
18 | 18 | this.$checkbox = $('<input />') |
|
19 | 19 | .attr('type', 'checkbox') |
|
20 | 20 | .click(function(el) { |
|
21 | 21 | that.user_invoked_update = true; |
|
22 | 22 | that.model.set('value', that.$checkbox.prop('checked')); |
|
23 | 23 | that.model.update_other_views(that); |
|
24 | 24 | that.user_invoked_update = false; |
|
25 | 25 | }) |
|
26 | 26 | .appendTo(this.$el); |
|
27 | 27 | |
|
28 | this.$el_to_style = this.$checkbox; // Set default element to style | |
|
28 | 29 | this.update(); // Set defaults. |
|
29 | 30 | }, |
|
30 | 31 | |
|
31 | 32 | // Handles: Backend -> Frontend Sync |
|
32 | 33 | // Frontent -> Frontend Sync |
|
33 | 34 | update : function(){ |
|
34 | 35 | if (!this.user_invoked_update) { |
|
35 | 36 | this.$checkbox.prop('checked', this.model.get('value')); |
|
36 | 37 | |
|
37 | 38 | var disabled = this.model.get('disabled'); |
|
38 | 39 | this.$checkbox.prop('disabled', disabled); |
|
39 | 40 | |
|
40 | 41 | var description = this.model.get('description'); |
|
41 | 42 | if (description.length == 0) { |
|
42 | 43 | this.$label.hide(); |
|
43 | 44 | } else { |
|
44 | 45 | this.$label.html(description); |
|
45 | 46 | this.$label.show(); |
|
46 | 47 | } |
|
47 | 48 | } |
|
48 | 49 | return IPython.WidgetView.prototype.update.call(this); |
|
49 | 50 | }, |
|
50 | 51 | |
|
51 | 52 | }); |
|
52 | 53 | |
|
53 | 54 | IPython.notebook.widget_manager.register_widget_view('CheckboxView', CheckboxView); |
|
54 | 55 | |
|
55 | 56 | var ToggleButtonView = IPython.WidgetView.extend({ |
|
56 | 57 | |
|
57 | 58 | // Called when view is rendered. |
|
58 | 59 | render : function(){ |
|
59 | 60 | this.$el |
|
60 | 61 | .html(''); |
|
61 | 62 | |
|
62 | 63 | this.$button = $('<button />') |
|
63 | 64 | .addClass('btn') |
|
64 | 65 | .attr('type', 'button') |
|
65 | 66 | .attr('data-toggle', 'button') |
|
66 | 67 | .appendTo(this.$el); |
|
68 | this.$el_to_style = this.$button; // Set default element to style | |
|
67 | 69 | |
|
68 | 70 | this.update(); // Set defaults. |
|
69 | 71 | }, |
|
70 | 72 | |
|
71 | 73 | // Handles: Backend -> Frontend Sync |
|
72 | 74 | // Frontent -> Frontend Sync |
|
73 | 75 | update : function(){ |
|
74 | 76 | if (!this.user_invoked_update) { |
|
75 | 77 | if (this.model.get('value')) { |
|
76 | 78 | this.$button.addClass('active'); |
|
77 | 79 | } else { |
|
78 | 80 | this.$button.removeClass('active'); |
|
79 | 81 | } |
|
80 | 82 | |
|
81 | 83 | var disabled = this.model.get('disabled'); |
|
82 | 84 | this.$button.prop('disabled', disabled); |
|
83 | 85 | |
|
84 | 86 | var description = this.model.get('description'); |
|
85 | 87 | if (description.length == 0) { |
|
86 | 88 | this.$button.html(' '); // Preserve button height |
|
87 | 89 | } else { |
|
88 | 90 | this.$button.html(description); |
|
89 | 91 | } |
|
90 | 92 | } |
|
91 | 93 | return IPython.WidgetView.prototype.update.call(this); |
|
92 | 94 | }, |
|
93 | 95 | |
|
94 | 96 | events: {"click button" : "handleClick"}, |
|
95 | 97 | |
|
96 | 98 | // Handles and validates user input. |
|
97 | 99 | handleClick: function(e) { |
|
98 | 100 | this.user_invoked_update = true; |
|
99 | 101 | this.model.set('value', ! $(e.target).hasClass('active')); |
|
100 | 102 | this.model.update_other_views(this); |
|
101 | 103 | this.user_invoked_update = false; |
|
102 | 104 | }, |
|
103 | 105 | }); |
|
104 | 106 | |
|
105 | 107 | IPython.notebook.widget_manager.register_widget_view('ToggleButtonView', ToggleButtonView); |
|
106 | 108 | |
|
107 | 109 | }); |
@@ -1,236 +1,239 | |||
|
1 | 1 | require(["notebook/js/widget"], function(){ |
|
2 | 2 | var FloatRangeWidgetModel = IPython.WidgetModel.extend({}); |
|
3 | 3 | IPython.notebook.widget_manager.register_widget_model('FloatRangeWidgetModel', FloatRangeWidgetModel); |
|
4 | 4 | |
|
5 | 5 | var FloatSliderView = IPython.WidgetView.extend({ |
|
6 | 6 | |
|
7 | 7 | // Called when view is rendered. |
|
8 | 8 | render : function(){ |
|
9 | 9 | this.$el |
|
10 | 10 | .addClass('widget-hbox-single') |
|
11 | 11 | .html(''); |
|
12 | 12 | this.$label = $('<div />') |
|
13 | 13 | .appendTo(this.$el) |
|
14 | 14 | .addClass('widget-hlabel') |
|
15 | 15 | .hide(); |
|
16 | 16 | this.$slider = $('<div />') |
|
17 | 17 | .slider({}) |
|
18 | 18 | .addClass('slider'); |
|
19 | 19 | |
|
20 | 20 | // Put the slider in a container |
|
21 | 21 | this.$slider_container = $('<div />') |
|
22 | 22 | .addClass('widget-hslider') |
|
23 | 23 | .append(this.$slider); |
|
24 | this.$el_to_style = this.$slider_container; // Set default element to style | |
|
24 | 25 | this.$el.append(this.$slider_container); |
|
25 | 26 | |
|
26 | 27 | // Set defaults. |
|
27 | 28 | this.update(); |
|
28 | 29 | }, |
|
29 | 30 | |
|
30 | 31 | // Handles: Backend -> Frontend Sync |
|
31 | 32 | // Frontent -> Frontend Sync |
|
32 | 33 | update : function(){ |
|
33 | 34 | // Slider related keys. |
|
34 | 35 | var _keys = ['step', 'max', 'min', 'disabled']; |
|
35 | 36 | for (var index in _keys) { |
|
36 | 37 | var key = _keys[index]; |
|
37 | 38 | if (this.model.get(key) != undefined) { |
|
38 | 39 | this.$slider.slider("option", key, this.model.get(key)); |
|
39 | 40 | } |
|
40 | 41 | } |
|
41 | 42 | |
|
42 | 43 | // WORKAROUND FOR JQUERY SLIDER BUG. |
|
43 | 44 | // The horizontal position of the slider handle |
|
44 | 45 | // depends on the value of the slider at the time |
|
45 | 46 | // of orientation change. Before applying the new |
|
46 | 47 | // workaround, we set the value to the minimum to |
|
47 | 48 | // make sure that the horizontal placement of the |
|
48 | 49 | // handle in the vertical slider is always |
|
49 | 50 | // consistent. |
|
50 | 51 | var orientation = this.model.get('orientation'); |
|
51 | 52 | var value = this.model.get('min'); |
|
52 | 53 | this.$slider.slider('option', 'value', value); |
|
53 | 54 | this.$slider.slider('option', 'orientation', orientation); |
|
54 | 55 | var value = this.model.get('value'); |
|
55 | 56 | this.$slider.slider('option', 'value', value); |
|
56 | 57 | |
|
57 | 58 | // Use the right CSS classes for vertical & horizontal sliders |
|
58 | 59 | if (orientation=='vertical') { |
|
59 | 60 | this.$slider_container |
|
60 | 61 | .removeClass('widget-hslider') |
|
61 | 62 | .addClass('widget-vslider'); |
|
62 | 63 | this.$el |
|
63 | 64 | .removeClass('widget-hbox-single') |
|
64 | 65 | .addClass('widget-vbox-single'); |
|
65 | 66 | this.$label |
|
66 | 67 | .removeClass('widget-hlabel') |
|
67 | 68 | .addClass('widget-vlabel'); |
|
68 | 69 | |
|
69 | 70 | } else { |
|
70 | 71 | this.$slider_container |
|
71 | 72 | .removeClass('widget-vslider') |
|
72 | 73 | .addClass('widget-hslider'); |
|
73 | 74 | this.$el |
|
74 | 75 | .removeClass('widget-vbox-single') |
|
75 | 76 | .addClass('widget-hbox-single'); |
|
76 | 77 | this.$label |
|
77 | 78 | .removeClass('widget-vlabel') |
|
78 | 79 | .addClass('widget-hlabel'); |
|
79 | 80 | } |
|
80 | 81 | |
|
81 | 82 | var description = this.model.get('description'); |
|
82 | 83 | if (description.length == 0) { |
|
83 | 84 | this.$label.hide(); |
|
84 | 85 | } else { |
|
85 | 86 | this.$label.html(description); |
|
86 | 87 | this.$label.show(); |
|
87 | 88 | } |
|
88 | 89 | return IPython.WidgetView.prototype.update.call(this); |
|
89 | 90 | }, |
|
90 | 91 | |
|
91 | 92 | // Handles: User input |
|
92 | 93 | events: { "slide" : "handleSliderChange" }, |
|
93 | 94 | handleSliderChange: function(e, ui) { |
|
94 | 95 | this.model.set('value', ui.value); |
|
95 | 96 | this.model.update_other_views(this); |
|
96 | 97 | }, |
|
97 | 98 | }); |
|
98 | 99 | |
|
99 | 100 | IPython.notebook.widget_manager.register_widget_view('FloatSliderView', FloatSliderView); |
|
100 | 101 | |
|
101 | 102 | |
|
102 | 103 | var FloatTextView = IPython.WidgetView.extend({ |
|
103 | 104 | |
|
104 | 105 | // Called when view is rendered. |
|
105 | 106 | render : function(){ |
|
106 | 107 | this.$el |
|
107 | 108 | .addClass('widget-hbox-single') |
|
108 | 109 | .html(''); |
|
109 | 110 | this.$label = $('<div />') |
|
110 | 111 | .appendTo(this.$el) |
|
111 | 112 | .addClass('widget-hlabel') |
|
112 | 113 | .hide(); |
|
113 | 114 | this.$textbox = $('<input type="text" />') |
|
114 | 115 | .addClass('input') |
|
115 | 116 | .addClass('widget-numeric-text') |
|
116 | 117 | .appendTo(this.$el); |
|
118 | this.$el_to_style = this.$textbox; // Set default element to style | |
|
117 | 119 | this.update(); // Set defaults. |
|
118 | 120 | }, |
|
119 | 121 | |
|
120 | 122 | // Handles: Backend -> Frontend Sync |
|
121 | 123 | // Frontent -> Frontend Sync |
|
122 | 124 | update : function(){ |
|
123 | 125 | var value = this.model.get('value'); |
|
124 | 126 | if (!this.changing && parseFloat(this.$textbox.val()) != value) { |
|
125 | 127 | this.$textbox.val(value); |
|
126 | 128 | } |
|
127 | 129 | |
|
128 | 130 | if (this.model.get('disabled')) { |
|
129 | 131 | this.$textbox.attr('disabled','disabled'); |
|
130 | 132 | } else { |
|
131 | 133 | this.$textbox.removeAttr('disabled'); |
|
132 | 134 | } |
|
133 | 135 | |
|
134 | 136 | var description = this.model.get('description'); |
|
135 | 137 | if (description.length == 0) { |
|
136 | 138 | this.$label.hide(); |
|
137 | 139 | } else { |
|
138 | 140 | this.$label.html(description); |
|
139 | 141 | this.$label.show(); |
|
140 | 142 | } |
|
141 | 143 | return IPython.WidgetView.prototype.update.call(this); |
|
142 | 144 | }, |
|
143 | 145 | |
|
144 | 146 | |
|
145 | 147 | events: {"keyup input" : "handleChanging", |
|
146 | 148 | "paste input" : "handleChanging", |
|
147 | 149 | "cut input" : "handleChanging", |
|
148 | 150 | "change input" : "handleChanged"}, // Fires only when control is validated or looses focus. |
|
149 | 151 | |
|
150 | 152 | // Handles and validates user input. |
|
151 | 153 | handleChanging: function(e) { |
|
152 | 154 | |
|
153 | 155 | // Try to parse value as a float. |
|
154 | 156 | var numericalValue = 0.0; |
|
155 | 157 | if (e.target.value != '') { |
|
156 | 158 | numericalValue = parseFloat(e.target.value); |
|
157 | 159 | } |
|
158 | 160 | |
|
159 | 161 | // If parse failed, reset value to value stored in model. |
|
160 | 162 | if (isNaN(numericalValue)) { |
|
161 | 163 | e.target.value = this.model.get('value'); |
|
162 | 164 | } else if (!isNaN(numericalValue)) { |
|
163 | 165 | if (this.model.get('max') != undefined) { |
|
164 | 166 | numericalValue = Math.min(this.model.get('max'), numericalValue); |
|
165 | 167 | } |
|
166 | 168 | if (this.model.get('min') != undefined) { |
|
167 | 169 | numericalValue = Math.max(this.model.get('min'), numericalValue); |
|
168 | 170 | } |
|
169 | 171 | |
|
170 | 172 | // Apply the value if it has changed. |
|
171 | 173 | if (numericalValue != this.model.get('value')) { |
|
172 | 174 | this.changing = true; |
|
173 | 175 | this.model.set('value', numericalValue); |
|
174 | 176 | this.model.update_other_views(this); |
|
175 | 177 | this.changing = false; |
|
176 | 178 | } |
|
177 | 179 | } |
|
178 | 180 | }, |
|
179 | 181 | |
|
180 | 182 | // Applies validated input. |
|
181 | 183 | handleChanged: function(e) { |
|
182 | 184 | // Update the textbox |
|
183 | 185 | if (this.model.get('value') != e.target.value) { |
|
184 | 186 | e.target.value = this.model.get('value'); |
|
185 | 187 | } |
|
186 | 188 | } |
|
187 | 189 | }); |
|
188 | 190 | |
|
189 | 191 | IPython.notebook.widget_manager.register_widget_view('FloatTextView', FloatTextView); |
|
190 | 192 | |
|
191 | 193 | |
|
192 | 194 | var ProgressView = IPython.WidgetView.extend({ |
|
193 | 195 | |
|
194 | 196 | // Called when view is rendered. |
|
195 | 197 | render : function(){ |
|
196 | 198 | this.$el |
|
197 | 199 | .addClass('widget-hbox-single') |
|
198 | 200 | .html(''); |
|
199 | 201 | this.$label = $('<div />') |
|
200 | 202 | .appendTo(this.$el) |
|
201 | 203 | .addClass('widget-hlabel') |
|
202 | 204 | .hide(); |
|
203 | 205 | this.$progress = $('<div />') |
|
204 | 206 | .addClass('progress') |
|
205 | 207 | .addClass('widget-progress') |
|
206 | 208 | .appendTo(this.$el); |
|
209 | this.$el_to_style = this.$progress; // Set default element to style | |
|
207 | 210 | this.$bar = $('<div />') |
|
208 | 211 | .addClass('bar') |
|
209 | 212 | .css('width', '50%') |
|
210 | 213 | .appendTo(this.$progress); |
|
211 | 214 | this.update(); // Set defaults. |
|
212 | 215 | }, |
|
213 | 216 | |
|
214 | 217 | // Handles: Backend -> Frontend Sync |
|
215 | 218 | // Frontent -> Frontend Sync |
|
216 | 219 | update : function(){ |
|
217 | 220 | var value = this.model.get('value'); |
|
218 | 221 | var max = this.model.get('max'); |
|
219 | 222 | var min = this.model.get('min'); |
|
220 | 223 | var percent = 100.0 * (value - min) / (max - min); |
|
221 | 224 | this.$bar.css('width', percent + '%'); |
|
222 | 225 | |
|
223 | 226 | var description = this.model.get('description'); |
|
224 | 227 | if (description.length == 0) { |
|
225 | 228 | this.$label.hide(); |
|
226 | 229 | } else { |
|
227 | 230 | this.$label.html(description); |
|
228 | 231 | this.$label.show(); |
|
229 | 232 | } |
|
230 | 233 | return IPython.WidgetView.prototype.update.call(this); |
|
231 | 234 | }, |
|
232 | 235 | |
|
233 | 236 | }); |
|
234 | 237 | |
|
235 | 238 | IPython.notebook.widget_manager.register_widget_view('ProgressView', ProgressView); |
|
236 | 239 | }); |
@@ -1,189 +1,191 | |||
|
1 | 1 | require(["notebook/js/widget"], function(){ |
|
2 | 2 | var IntRangeWidgetModel = IPython.WidgetModel.extend({}); |
|
3 | 3 | IPython.notebook.widget_manager.register_widget_model('IntRangeWidgetModel', IntRangeWidgetModel); |
|
4 | 4 | |
|
5 | 5 | var IntSliderView = IPython.WidgetView.extend({ |
|
6 | 6 | |
|
7 | 7 | // Called when view is rendered. |
|
8 | 8 | render : function(){ |
|
9 | 9 | this.$el |
|
10 | 10 | .addClass('widget-hbox-single') |
|
11 | 11 | .html(''); |
|
12 | 12 | this.$label = $('<div />') |
|
13 | 13 | .appendTo(this.$el) |
|
14 | 14 | .addClass('widget-hlabel') |
|
15 | 15 | .hide(); |
|
16 | 16 | this.$slider = $('<div />') |
|
17 | 17 | .slider({}) |
|
18 | 18 | .addClass('slider'); |
|
19 | 19 | |
|
20 | 20 | // Put the slider in a container |
|
21 | 21 | this.$slider_container = $('<div />') |
|
22 | 22 | .addClass('widget-hslider') |
|
23 | 23 | .append(this.$slider); |
|
24 | this.$el_to_style = this.$slider_container; // Set default element to style | |
|
24 | 25 | this.$el.append(this.$slider_container); |
|
25 | 26 | |
|
26 | 27 | // Set defaults. |
|
27 | 28 | this.update(); |
|
28 | 29 | }, |
|
29 | 30 | |
|
30 | 31 | // Handles: Backend -> Frontend Sync |
|
31 | 32 | // Frontent -> Frontend Sync |
|
32 | 33 | update : function(){ |
|
33 | 34 | // Slider related keys. |
|
34 | 35 | var _keys = ['step', 'max', 'min', 'disabled']; |
|
35 | 36 | for (var index in _keys) { |
|
36 | 37 | var key = _keys[index]; |
|
37 | 38 | if (this.model.get(key) != undefined) { |
|
38 | 39 | this.$slider.slider("option", key, this.model.get(key)); |
|
39 | 40 | } |
|
40 | 41 | } |
|
41 | 42 | |
|
42 | 43 | // WORKAROUND FOR JQUERY SLIDER BUG. |
|
43 | 44 | // The horizontal position of the slider handle |
|
44 | 45 | // depends on the value of the slider at the time |
|
45 | 46 | // of orientation change. Before applying the new |
|
46 | 47 | // workaround, we set the value to the minimum to |
|
47 | 48 | // make sure that the horizontal placement of the |
|
48 | 49 | // handle in the vertical slider is always |
|
49 | 50 | // consistent. |
|
50 | 51 | var orientation = this.model.get('orientation'); |
|
51 | 52 | var value = this.model.get('min'); |
|
52 | 53 | this.$slider.slider('option', 'value', value); |
|
53 | 54 | this.$slider.slider('option', 'orientation', orientation); |
|
54 | 55 | var value = this.model.get('value'); |
|
55 | 56 | this.$slider.slider('option', 'value', value); |
|
56 | 57 | |
|
57 | 58 | // Use the right CSS classes for vertical & horizontal sliders |
|
58 | 59 | if (orientation=='vertical') { |
|
59 | 60 | this.$slider_container |
|
60 | 61 | .removeClass('widget-hslider') |
|
61 | 62 | .addClass('widget-vslider'); |
|
62 | 63 | this.$el |
|
63 | 64 | .removeClass('widget-hbox-single') |
|
64 | 65 | .addClass('widget-vbox-single'); |
|
65 | 66 | this.$label |
|
66 | 67 | .removeClass('widget-hlabel') |
|
67 | 68 | .addClass('widget-vlabel'); |
|
68 | 69 | |
|
69 | 70 | } else { |
|
70 | 71 | this.$slider_container |
|
71 | 72 | .removeClass('widget-vslider') |
|
72 | 73 | .addClass('widget-hslider'); |
|
73 | 74 | this.$el |
|
74 | 75 | .removeClass('widget-vbox-single') |
|
75 | 76 | .addClass('widget-hbox-single'); |
|
76 | 77 | this.$label |
|
77 | 78 | .removeClass('widget-vlabel') |
|
78 | 79 | .addClass('widget-hlabel'); |
|
79 | 80 | } |
|
80 | 81 | |
|
81 | 82 | var description = this.model.get('description'); |
|
82 | 83 | if (description.length == 0) { |
|
83 | 84 | this.$label.hide(); |
|
84 | 85 | } else { |
|
85 | 86 | this.$label.html(description); |
|
86 | 87 | this.$label.show(); |
|
87 | 88 | } |
|
88 | 89 | return IPython.WidgetView.prototype.update.call(this); |
|
89 | 90 | }, |
|
90 | 91 | |
|
91 | 92 | // Handles: User input |
|
92 | 93 | events: { "slide" : "handleSliderChange" }, |
|
93 | 94 | handleSliderChange: function(e, ui) { |
|
94 | 95 | this.model.set('value', ~~ui.value); // Double bit-wise not to truncate decimel |
|
95 | 96 | this.model.update_other_views(this); |
|
96 | 97 | }, |
|
97 | 98 | }); |
|
98 | 99 | |
|
99 | 100 | IPython.notebook.widget_manager.register_widget_view('IntSliderView', IntSliderView); |
|
100 | 101 | |
|
101 | 102 | var IntTextView = IPython.WidgetView.extend({ |
|
102 | 103 | |
|
103 | 104 | // Called when view is rendered. |
|
104 | 105 | render : function(){ |
|
105 | 106 | this.$el |
|
106 | 107 | .addClass('widget-hbox-single') |
|
107 | 108 | .html(''); |
|
108 | 109 | this.$label = $('<div />') |
|
109 | 110 | .appendTo(this.$el) |
|
110 | 111 | .addClass('widget-hlabel') |
|
111 | 112 | .hide(); |
|
112 | 113 | this.$textbox = $('<input type="text" />') |
|
113 | 114 | .addClass('input') |
|
114 | 115 | .addClass('widget-numeric-text') |
|
115 | 116 | .appendTo(this.$el); |
|
117 | this.$el_to_style = this.$textbox; // Set default element to style | |
|
116 | 118 | this.update(); // Set defaults. |
|
117 | 119 | }, |
|
118 | 120 | |
|
119 | 121 | // Handles: Backend -> Frontend Sync |
|
120 | 122 | // Frontent -> Frontend Sync |
|
121 | 123 | update : function(){ |
|
122 | 124 | var value = this.model.get('value'); |
|
123 | 125 | if (!this.changing && parseInt(this.$textbox.val()) != value) { |
|
124 | 126 | this.$textbox.val(value); |
|
125 | 127 | } |
|
126 | 128 | |
|
127 | 129 | if (this.model.get('disabled')) { |
|
128 | 130 | this.$textbox.attr('disabled','disabled'); |
|
129 | 131 | } else { |
|
130 | 132 | this.$textbox.removeAttr('disabled'); |
|
131 | 133 | } |
|
132 | 134 | |
|
133 | 135 | var description = this.model.get('description'); |
|
134 | 136 | if (description.length == 0) { |
|
135 | 137 | this.$label.hide(); |
|
136 | 138 | } else { |
|
137 | 139 | this.$label.html(description); |
|
138 | 140 | this.$label.show(); |
|
139 | 141 | } |
|
140 | 142 | return IPython.WidgetView.prototype.update.call(this); |
|
141 | 143 | }, |
|
142 | 144 | |
|
143 | 145 | |
|
144 | 146 | events: {"keyup input" : "handleChanging", |
|
145 | 147 | "paste input" : "handleChanging", |
|
146 | 148 | "cut input" : "handleChanging", |
|
147 | 149 | "change input" : "handleChanged"}, // Fires only when control is validated or looses focus. |
|
148 | 150 | |
|
149 | 151 | // Handles and validates user input. |
|
150 | 152 | handleChanging: function(e) { |
|
151 | 153 | |
|
152 | 154 | // Try to parse value as a float. |
|
153 | 155 | var numericalValue = 0; |
|
154 | 156 | if (e.target.value != '') { |
|
155 | 157 | numericalValue = parseInt(e.target.value); |
|
156 | 158 | } |
|
157 | 159 | |
|
158 | 160 | // If parse failed, reset value to value stored in model. |
|
159 | 161 | if (isNaN(numericalValue)) { |
|
160 | 162 | e.target.value = this.model.get('value'); |
|
161 | 163 | } else if (!isNaN(numericalValue)) { |
|
162 | 164 | if (this.model.get('max') != undefined) { |
|
163 | 165 | numericalValue = Math.min(this.model.get('max'), numericalValue); |
|
164 | 166 | } |
|
165 | 167 | if (this.model.get('min') != undefined) { |
|
166 | 168 | numericalValue = Math.max(this.model.get('min'), numericalValue); |
|
167 | 169 | } |
|
168 | 170 | |
|
169 | 171 | // Apply the value if it has changed. |
|
170 | 172 | if (numericalValue != this.model.get('value')) { |
|
171 | 173 | this.changing = true; |
|
172 | 174 | this.model.set('value', numericalValue); |
|
173 | 175 | this.model.update_other_views(this); |
|
174 | 176 | this.changing = false; |
|
175 | 177 | } |
|
176 | 178 | } |
|
177 | 179 | }, |
|
178 | 180 | |
|
179 | 181 | // Applies validated input. |
|
180 | 182 | handleChanged: function(e) { |
|
181 | 183 | // Update the textbox |
|
182 | 184 | if (this.model.get('value') != e.target.value) { |
|
183 | 185 | e.target.value = this.model.get('value'); |
|
184 | 186 | } |
|
185 | 187 | } |
|
186 | 188 | }); |
|
187 | 189 | |
|
188 | 190 | IPython.notebook.widget_manager.register_widget_view('IntTextView', IntTextView); |
|
189 | 191 | }); |
@@ -1,248 +1,251 | |||
|
1 | 1 | require(["notebook/js/widget"], function(){ |
|
2 | 2 | var SelectionWidgetModel = IPython.WidgetModel.extend({}); |
|
3 | 3 | IPython.notebook.widget_manager.register_widget_model('SelectionWidgetModel', SelectionWidgetModel); |
|
4 | 4 | |
|
5 | 5 | var DropdownView = IPython.WidgetView.extend({ |
|
6 | 6 | |
|
7 | 7 | // Called when view is rendered. |
|
8 | 8 | render : function(){ |
|
9 | 9 | |
|
10 | 10 | this.$el |
|
11 | 11 | .addClass('widget-hbox-single') |
|
12 | 12 | .html(''); |
|
13 | 13 | this.$label = $('<div />') |
|
14 | 14 | .appendTo(this.$el) |
|
15 | 15 | .addClass('widget-hlabel') |
|
16 | 16 | .hide(); |
|
17 | 17 | this.$buttongroup = $('<div />') |
|
18 | 18 | .addClass('widget_item') |
|
19 | 19 | .addClass('btn-group') |
|
20 | 20 | .appendTo(this.$el); |
|
21 | this.$el_to_style = this.$buttongroup; // Set default element to style | |
|
21 | 22 | this.$droplabel = $('<button />') |
|
22 | 23 | .addClass('btn') |
|
23 | 24 | .addClass('widget-combo-btn') |
|
24 | 25 | .appendTo(this.$buttongroup); |
|
25 | 26 | this.$dropbutton = $('<button />') |
|
26 | 27 | .addClass('btn') |
|
27 | 28 | .addClass('dropdown-toggle') |
|
28 | 29 | .attr('data-toggle', 'dropdown') |
|
29 | 30 | .html('<span class="caret"></span>') |
|
30 | 31 | .appendTo(this.$buttongroup); |
|
31 | 32 | this.$droplist = $('<ul />') |
|
32 | 33 | .addClass('dropdown-menu') |
|
33 | 34 | .appendTo(this.$buttongroup); |
|
34 | 35 | |
|
35 | 36 | // Set defaults. |
|
36 | 37 | this.update(); |
|
37 | 38 | }, |
|
38 | 39 | |
|
39 | 40 | // Handles: Backend -> Frontend Sync |
|
40 | 41 | // Frontent -> Frontend Sync |
|
41 | 42 | update : function(){ |
|
42 | 43 | this.$droplabel.html(this.model.get('value')); |
|
43 | 44 | |
|
44 | 45 | var items = this.model.get('values'); |
|
45 | 46 | this.$droplist.html(''); |
|
46 | 47 | for (var index in items) { |
|
47 | 48 | var that = this; |
|
48 | 49 | var item_button = $('<a href="#"/>') |
|
49 | 50 | .html(items[index]) |
|
50 | 51 | .on('click', function(e){ |
|
51 | 52 | that.model.set('value', $(e.target).html(), this); |
|
52 | 53 | that.model.update_other_views(that); |
|
53 | 54 | }) |
|
54 | 55 | |
|
55 | 56 | this.$droplist.append($('<li />').append(item_button)) |
|
56 | 57 | } |
|
57 | 58 | |
|
58 | 59 | if (this.model.get('disabled')) { |
|
59 | 60 | this.$buttongroup.attr('disabled','disabled'); |
|
60 | 61 | this.$droplabel.attr('disabled','disabled'); |
|
61 | 62 | this.$dropbutton.attr('disabled','disabled'); |
|
62 | 63 | this.$droplist.attr('disabled','disabled'); |
|
63 | 64 | } else { |
|
64 | 65 | this.$buttongroup.removeAttr('disabled'); |
|
65 | 66 | this.$droplabel.removeAttr('disabled'); |
|
66 | 67 | this.$dropbutton.removeAttr('disabled'); |
|
67 | 68 | this.$droplist.removeAttr('disabled'); |
|
68 | 69 | } |
|
69 | 70 | |
|
70 | 71 | var description = this.model.get('description'); |
|
71 | 72 | if (description.length == 0) { |
|
72 | 73 | this.$label.hide(); |
|
73 | 74 | } else { |
|
74 | 75 | this.$label.html(description); |
|
75 | 76 | this.$label.show(); |
|
76 | 77 | } |
|
77 | 78 | return IPython.WidgetView.prototype.update.call(this); |
|
78 | 79 | }, |
|
79 | 80 | |
|
80 | 81 | }); |
|
81 | 82 | |
|
82 | 83 | IPython.notebook.widget_manager.register_widget_view('DropdownView', DropdownView); |
|
83 | 84 | |
|
84 | 85 | var RadioButtonsView = IPython.WidgetView.extend({ |
|
85 | 86 | |
|
86 | 87 | // Called when view is rendered. |
|
87 | 88 | render : function(){ |
|
88 | 89 | this.$el |
|
89 | 90 | .addClass('widget-hbox') |
|
90 | 91 | .html(''); |
|
91 | 92 | this.$label = $('<div />') |
|
92 | 93 | .appendTo(this.$el) |
|
93 | 94 | .addClass('widget-hlabel') |
|
94 | 95 | .hide(); |
|
95 | 96 | this.$container = $('<div />') |
|
96 | 97 | .appendTo(this.$el) |
|
97 | 98 | .addClass('widget-container') |
|
98 | 99 | .addClass('vbox'); |
|
100 | this.$el_to_style = this.$container; // Set default element to style | |
|
99 | 101 | this.update(); |
|
100 | 102 | }, |
|
101 | 103 | |
|
102 | 104 | // Handles: Backend -> Frontend Sync |
|
103 | 105 | // Frontent -> Frontend Sync |
|
104 | 106 | update : function(){ |
|
105 | 107 | |
|
106 | 108 | // Add missing items to the DOM. |
|
107 | 109 | var items = this.model.get('values'); |
|
108 | 110 | var disabled = this.model.get('disabled'); |
|
109 | 111 | for (var index in items) { |
|
110 | 112 | var item_query = ' :input[value="' + items[index] + '"]'; |
|
111 | 113 | if (this.$el.find(item_query).length == 0) { |
|
112 | 114 | var $label = $('<label />') |
|
113 | 115 | .addClass('radio') |
|
114 | 116 | .html(items[index]) |
|
115 | 117 | .appendTo(this.$container); |
|
116 | 118 | |
|
117 | 119 | var that = this; |
|
118 | 120 | $('<input />') |
|
119 | 121 | .attr('type', 'radio') |
|
120 | 122 | .addClass(this.model) |
|
121 | 123 | .val(items[index]) |
|
122 | 124 | .prependTo($label) |
|
123 | 125 | .on('click', function(e){ |
|
124 | 126 | that.model.set('value', $(e.target).val(), this); |
|
125 | 127 | that.model.update_other_views(that); |
|
126 | 128 | }); |
|
127 | 129 | } |
|
128 | 130 | |
|
129 | 131 | var $item_element = this.$container.find(item_query); |
|
130 | 132 | if (this.model.get('value') == items[index]) { |
|
131 | 133 | $item_element.prop('checked', true); |
|
132 | 134 | } else { |
|
133 | 135 | $item_element.prop('checked', false); |
|
134 | 136 | } |
|
135 | 137 | $item_element.prop('disabled', disabled); |
|
136 | 138 | } |
|
137 | 139 | |
|
138 | 140 | // Remove items that no longer exist. |
|
139 | 141 | this.$container.find('input').each(function(i, obj) { |
|
140 | 142 | var value = $(obj).val(); |
|
141 | 143 | var found = false; |
|
142 | 144 | for (var index in items) { |
|
143 | 145 | if (items[index] == value) { |
|
144 | 146 | found = true; |
|
145 | 147 | break; |
|
146 | 148 | } |
|
147 | 149 | } |
|
148 | 150 | |
|
149 | 151 | if (!found) { |
|
150 | 152 | $(obj).parent().remove(); |
|
151 | 153 | } |
|
152 | 154 | }); |
|
153 | 155 | |
|
154 | 156 | var description = this.model.get('description'); |
|
155 | 157 | if (description.length == 0) { |
|
156 | 158 | this.$label.hide(); |
|
157 | 159 | } else { |
|
158 | 160 | this.$label.html(description); |
|
159 | 161 | this.$label.show(); |
|
160 | 162 | } |
|
161 | 163 | return IPython.WidgetView.prototype.update.call(this); |
|
162 | 164 | }, |
|
163 | 165 | |
|
164 | 166 | }); |
|
165 | 167 | |
|
166 | 168 | IPython.notebook.widget_manager.register_widget_view('RadioButtonsView', RadioButtonsView); |
|
167 | 169 | |
|
168 | 170 | |
|
169 | 171 | var ToggleButtonsView = IPython.WidgetView.extend({ |
|
170 | 172 | |
|
171 | 173 | // Called when view is rendered. |
|
172 | 174 | render : function(){ |
|
173 | 175 | this.$el |
|
174 | 176 | .addClass('widget-hbox-single') |
|
175 | 177 | .html(''); |
|
176 | 178 | this.$label = $('<div />') |
|
177 | 179 | .appendTo(this.$el) |
|
178 | 180 | .addClass('widget-hlabel') |
|
179 | 181 | .hide(); |
|
180 | 182 | this.$buttongroup = $('<div />') |
|
181 | 183 | .addClass('btn-group') |
|
182 | 184 | .attr('data-toggle', 'buttons-radio') |
|
183 | 185 | .appendTo(this.$el); |
|
186 | this.$el_to_style = this.$buttongroup; // Set default element to style | |
|
184 | 187 | this.update(); |
|
185 | 188 | }, |
|
186 | 189 | |
|
187 | 190 | // Handles: Backend -> Frontend Sync |
|
188 | 191 | // Frontent -> Frontend Sync |
|
189 | 192 | update : function(){ |
|
190 | 193 | |
|
191 | 194 | // Add missing items to the DOM. |
|
192 | 195 | var items = this.model.get('values'); |
|
193 | 196 | var disabled = this.model.get('disabled'); |
|
194 | 197 | for (var index in items) { |
|
195 | 198 | var item_query = ' :contains("' + items[index] + '")'; |
|
196 | 199 | if (this.$buttongroup.find(item_query).length == 0) { |
|
197 | 200 | |
|
198 | 201 | var that = this; |
|
199 | 202 | $('<button />') |
|
200 | 203 | .attr('type', 'button') |
|
201 | 204 | .addClass('btn') |
|
202 | 205 | .html(items[index]) |
|
203 | 206 | .appendTo(this.$buttongroup) |
|
204 | 207 | .on('click', function(e){ |
|
205 | 208 | that.model.set('value', $(e.target).html(), this); |
|
206 | 209 | that.model.update_other_views(that); |
|
207 | 210 | }); |
|
208 | 211 | } |
|
209 | 212 | |
|
210 | 213 | var $item_element = this.$buttongroup.find(item_query); |
|
211 | 214 | if (this.model.get('value') == items[index]) { |
|
212 | 215 | $item_element.addClass('active'); |
|
213 | 216 | } else { |
|
214 | 217 | $item_element.removeClass('active'); |
|
215 | 218 | } |
|
216 | 219 | $item_element.prop('disabled', disabled); |
|
217 | 220 | } |
|
218 | 221 | |
|
219 | 222 | // Remove items that no longer exist. |
|
220 | 223 | this.$buttongroup.find('button').each(function(i, obj) { |
|
221 | 224 | var value = $(obj).html(); |
|
222 | 225 | var found = false; |
|
223 | 226 | for (var index in items) { |
|
224 | 227 | if (items[index] == value) { |
|
225 | 228 | found = true; |
|
226 | 229 | break; |
|
227 | 230 | } |
|
228 | 231 | } |
|
229 | 232 | |
|
230 | 233 | if (!found) { |
|
231 | 234 | $(obj).remove(); |
|
232 | 235 | } |
|
233 | 236 | }); |
|
234 | 237 | |
|
235 | 238 | var description = this.model.get('description'); |
|
236 | 239 | if (description.length == 0) { |
|
237 | 240 | this.$label.hide(); |
|
238 | 241 | } else { |
|
239 | 242 | this.$label.html(description); |
|
240 | 243 | this.$label.show(); |
|
241 | 244 | } |
|
242 | 245 | return IPython.WidgetView.prototype.update.call(this); |
|
243 | 246 | }, |
|
244 | 247 | |
|
245 | 248 | }); |
|
246 | 249 | |
|
247 | 250 | IPython.notebook.widget_manager.register_widget_view('ToggleButtonsView', ToggleButtonsView); |
|
248 | 251 | }); |
@@ -1,129 +1,131 | |||
|
1 | 1 | require(["notebook/js/widget"], function(){ |
|
2 | 2 | var StringWidgetModel = IPython.WidgetModel.extend({}); |
|
3 | 3 | IPython.notebook.widget_manager.register_widget_model('StringWidgetModel', StringWidgetModel); |
|
4 | 4 | |
|
5 | 5 | var LabelView = IPython.WidgetView.extend({ |
|
6 | 6 | |
|
7 | 7 | // Called when view is rendered. |
|
8 | 8 | render : function(){ |
|
9 | 9 | this.$el = $('<div />'); |
|
10 | 10 | this.update(); // Set defaults. |
|
11 | 11 | }, |
|
12 | 12 | |
|
13 | 13 | // Handles: Backend -> Frontend Sync |
|
14 | 14 | // Frontent -> Frontend Sync |
|
15 | 15 | update : function(){ |
|
16 | 16 | this.$el.html(this.model.get('value')); |
|
17 | 17 | return IPython.WidgetView.prototype.update.call(this); |
|
18 | 18 | }, |
|
19 | 19 | |
|
20 | 20 | }); |
|
21 | 21 | |
|
22 | 22 | IPython.notebook.widget_manager.register_widget_view('LabelView', LabelView); |
|
23 | 23 | |
|
24 | 24 | var TextAreaView = IPython.WidgetView.extend({ |
|
25 | 25 | |
|
26 | 26 | // Called when view is rendered. |
|
27 | 27 | render : function(){ |
|
28 | 28 | this.$el |
|
29 | 29 | .addClass('widget-hbox') |
|
30 | 30 | .html(''); |
|
31 | 31 | this.$label = $('<div />') |
|
32 | 32 | .appendTo(this.$el) |
|
33 | 33 | .addClass('widget-hlabel') |
|
34 | 34 | .hide(); |
|
35 | 35 | this.$textbox = $('<textarea />') |
|
36 | 36 | .attr('rows', 5) |
|
37 | 37 | .addClass('widget-text') |
|
38 | 38 | .appendTo(this.$el); |
|
39 | this.$el_to_style = this.$textbox; // Set default element to style | |
|
39 | 40 | this.update(); // Set defaults. |
|
40 | 41 | }, |
|
41 | 42 | |
|
42 | 43 | // Handles: Backend -> Frontend Sync |
|
43 | 44 | // Frontent -> Frontend Sync |
|
44 | 45 | update : function(){ |
|
45 | 46 | if (!this.user_invoked_update) { |
|
46 | 47 | this.$textbox.val(this.model.get('value')); |
|
47 | 48 | } |
|
48 | 49 | |
|
49 | 50 | var disabled = this.model.get('disabled'); |
|
50 | 51 | this.$textbox.prop('disabled', disabled); |
|
51 | 52 | |
|
52 | 53 | var description = this.model.get('description'); |
|
53 | 54 | if (description.length == 0) { |
|
54 | 55 | this.$label.hide(); |
|
55 | 56 | } else { |
|
56 | 57 | this.$label.html(description); |
|
57 | 58 | this.$label.show(); |
|
58 | 59 | } |
|
59 | 60 | return IPython.WidgetView.prototype.update.call(this); |
|
60 | 61 | }, |
|
61 | 62 | |
|
62 | 63 | events: {"keyup textarea" : "handleChanging", |
|
63 | 64 | "paste textarea" : "handleChanging", |
|
64 | 65 | "cut textarea" : "handleChanging"}, |
|
65 | 66 | |
|
66 | 67 | // Handles and validates user input. |
|
67 | 68 | handleChanging: function(e) { |
|
68 | 69 | this.user_invoked_update = true; |
|
69 | 70 | this.model.set('value', e.target.value); |
|
70 | 71 | this.model.update_other_views(this); |
|
71 | 72 | this.user_invoked_update = false; |
|
72 | 73 | }, |
|
73 | 74 | }); |
|
74 | 75 | |
|
75 | 76 | IPython.notebook.widget_manager.register_widget_view('TextAreaView', TextAreaView); |
|
76 | 77 | |
|
77 | 78 | var TextBoxView = IPython.WidgetView.extend({ |
|
78 | 79 | |
|
79 | 80 | // Called when view is rendered. |
|
80 | 81 | render : function(){ |
|
81 | 82 | this.$el |
|
82 | 83 | .addClass('widget-hbox-single') |
|
83 | 84 | .html(''); |
|
84 | 85 | this.$label = $('<div />') |
|
85 | 86 | .addClass('widget-hlabel') |
|
86 | 87 | .appendTo(this.$el) |
|
87 | 88 | .hide(); |
|
88 | 89 | this.$textbox = $('<input type="text" />') |
|
89 | 90 | .addClass('input') |
|
90 | 91 | .addClass('widget-text') |
|
91 | 92 | .appendTo(this.$el); |
|
93 | this.$el_to_style = this.$textbox; // Set default element to style | |
|
92 | 94 | this.update(); // Set defaults. |
|
93 | 95 | }, |
|
94 | 96 | |
|
95 | 97 | // Handles: Backend -> Frontend Sync |
|
96 | 98 | // Frontent -> Frontend Sync |
|
97 | 99 | update : function(){ |
|
98 | 100 | if (!this.user_invoked_update) { |
|
99 | 101 | this.$textbox.val(this.model.get('value')); |
|
100 | 102 | } |
|
101 | 103 | |
|
102 | 104 | var disabled = this.model.get('disabled'); |
|
103 | 105 | this.$textbox.prop('disabled', disabled); |
|
104 | 106 | |
|
105 | 107 | var description = this.model.get('description'); |
|
106 | 108 | if (description.length == 0) { |
|
107 | 109 | this.$label.hide(); |
|
108 | 110 | } else { |
|
109 | 111 | this.$label.html(description); |
|
110 | 112 | this.$label.show(); |
|
111 | 113 | } |
|
112 | 114 | return IPython.WidgetView.prototype.update.call(this); |
|
113 | 115 | }, |
|
114 | 116 | |
|
115 | 117 | events: {"keyup input" : "handleChanging", |
|
116 | 118 | "paste input" : "handleChanging", |
|
117 | 119 | "cut input" : "handleChanging"}, |
|
118 | 120 | |
|
119 | 121 | // Handles and validates user input. |
|
120 | 122 | handleChanging: function(e) { |
|
121 | 123 | this.user_invoked_update = true; |
|
122 | 124 | this.model.set('value', e.target.value); |
|
123 | 125 | this.model.update_other_views(this); |
|
124 | 126 | this.user_invoked_update = false; |
|
125 | 127 | }, |
|
126 | 128 | }); |
|
127 | 129 | |
|
128 | 130 | IPython.notebook.widget_manager.register_widget_view('TextBoxView', TextBoxView); |
|
129 | 131 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now