Show More
@@ -1,309 +1,310 b'' | |||||
1 | // Copyright (c) IPython Development Team. |
|
1 | // Copyright (c) IPython Development Team. | |
2 | // Distributed under the terms of the Modified BSD License. |
|
2 | // Distributed under the terms of the Modified BSD License. | |
3 |
|
3 | |||
4 | define([ |
|
4 | define([ | |
5 | "widgets/js/widget", |
|
5 | "widgets/js/widget", | |
6 | "jqueryui", |
|
6 | "jqueryui", | |
7 | "components/bootstrap/js/bootstrap.min", |
|
7 | "components/bootstrap/js/bootstrap.min", | |
8 | ], function(widget, $){ |
|
8 | ], function(widget, $){ | |
9 |
|
9 | |||
10 | var IntSliderView = widget.DOMWidgetView.extend({ |
|
10 | var IntSliderView = widget.DOMWidgetView.extend({ | |
11 | render : function(){ |
|
11 | render : function(){ | |
12 | // Called when view is rendered. |
|
12 | // Called when view is rendered. | |
13 | this.$el |
|
13 | this.$el | |
14 | .addClass('widget-hbox-single'); |
|
14 | .addClass('widget-hbox-single'); | |
15 | this.$label = $('<div />') |
|
15 | this.$label = $('<div />') | |
16 | .appendTo(this.$el) |
|
16 | .appendTo(this.$el) | |
17 | .addClass('widget-hlabel') |
|
17 | .addClass('widget-hlabel') | |
18 | .hide(); |
|
18 | .hide(); | |
19 |
|
19 | |||
20 | this.$slider = $('<div />') |
|
20 | this.$slider = $('<div />') | |
21 | .slider({}) |
|
21 | .slider({}) | |
22 | .addClass('slider'); |
|
22 | .addClass('slider'); | |
23 | // Put the slider in a container |
|
23 | // Put the slider in a container | |
24 | this.$slider_container = $('<div />') |
|
24 | this.$slider_container = $('<div />') | |
25 | .addClass('widget-hslider') |
|
25 | .addClass('widget-hslider') | |
26 | .append(this.$slider); |
|
26 | .append(this.$slider); | |
27 | this.$el_to_style = this.$slider_container; // Set default element to style |
|
27 | this.$el_to_style = this.$slider_container; // Set default element to style | |
28 | this.$el.append(this.$slider_container); |
|
28 | this.$el.append(this.$slider_container); | |
29 |
|
29 | |||
30 | this.$readout = $('<div/>') |
|
30 | this.$readout = $('<div/>') | |
31 | .appendTo(this.$el) |
|
31 | .appendTo(this.$el) | |
32 | .addClass('widget-hreadout') |
|
32 | .addClass('widget-hreadout') | |
33 | .hide(); |
|
33 | .hide(); | |
34 |
|
34 | |||
35 | // Set defaults. |
|
35 | // Set defaults. | |
36 | this.update(); |
|
36 | this.update(); | |
37 | }, |
|
37 | }, | |
38 |
|
38 | |||
39 | update : function(options){ |
|
39 | update : function(options){ | |
40 | // Update the contents of this view |
|
40 | // Update the contents of this view | |
41 | // |
|
41 | // | |
42 | // Called when the model is changed. The model may have been |
|
42 | // Called when the model is changed. The model may have been | |
43 | // changed by another view or by a state update from the back-end. |
|
43 | // changed by another view or by a state update from the back-end. | |
44 | if (options === undefined || options.updated_view != this) { |
|
44 | if (options === undefined || options.updated_view != this) { | |
45 | // JQuery slider option keys. These keys happen to have a |
|
45 | // JQuery slider option keys. These keys happen to have a | |
46 | // one-to-one mapping with the corrosponding keys of the model. |
|
46 | // one-to-one mapping with the corrosponding keys of the model. | |
47 | var jquery_slider_keys = ['step', 'max', 'min', 'disabled']; |
|
47 | var jquery_slider_keys = ['step', 'max', 'min', 'disabled']; | |
48 | var that = this; |
|
48 | var that = this; | |
49 | that.$slider.slider({}); |
|
49 | that.$slider.slider({}); | |
50 | _.each(jquery_slider_keys, function(key, i) { |
|
50 | _.each(jquery_slider_keys, function(key, i) { | |
51 | var model_value = that.model.get(key); |
|
51 | var model_value = that.model.get(key); | |
52 | if (model_value !== undefined) { |
|
52 | if (model_value !== undefined) { | |
53 | that.$slider.slider("option", key, model_value); |
|
53 | that.$slider.slider("option", key, model_value); | |
54 | } |
|
54 | } | |
55 | }); |
|
55 | }); | |
56 |
|
56 | |||
57 | // WORKAROUND FOR JQUERY SLIDER BUG. |
|
57 | // WORKAROUND FOR JQUERY SLIDER BUG. | |
58 | // The horizontal position of the slider handle |
|
58 | // The horizontal position of the slider handle | |
59 | // depends on the value of the slider at the time |
|
59 | // depends on the value of the slider at the time | |
60 | // of orientation change. Before applying the new |
|
60 | // of orientation change. Before applying the new | |
61 | // workaround, we set the value to the minimum to |
|
61 | // workaround, we set the value to the minimum to | |
62 | // make sure that the horizontal placement of the |
|
62 | // make sure that the horizontal placement of the | |
63 | // handle in the vertical slider is always |
|
63 | // handle in the vertical slider is always | |
64 | // consistent. |
|
64 | // consistent. | |
65 | var orientation = this.model.get('orientation'); |
|
65 | var orientation = this.model.get('orientation'); | |
66 | var min = this.model.get('min'); |
|
66 | var min = this.model.get('min'); | |
67 | var max = this.model.get('max'); |
|
67 | var max = this.model.get('max'); | |
68 | this.$slider.slider('option', 'value', min); |
|
68 | this.$slider.slider('option', 'value', min); | |
69 | this.$slider.slider('option', 'orientation', orientation); |
|
69 | this.$slider.slider('option', 'orientation', orientation); | |
70 | value = this.model.get('value'); |
|
70 | value = this.model.get('value'); | |
71 | if(value > max) { |
|
71 | if(value > max) { | |
72 | value = max; |
|
72 | value = max; | |
73 | } |
|
73 | } | |
74 | else if(value < min){ |
|
74 | else if(value < min){ | |
75 | value = min; |
|
75 | value = min; | |
76 | } |
|
76 | } | |
77 | this.model.set('value', value, {updated_view: this}); |
|
77 | this.model.set('value', value, {updated_view: this}); | |
78 | this.$slider.slider('option', 'value', value); |
|
78 | this.$slider.slider('option', 'value', value); | |
79 | this.$readout.text(value); |
|
79 | this.$readout.text(value); | |
|
80 | this.touch() | |||
80 |
|
81 | |||
81 | // Use the right CSS classes for vertical & horizontal sliders |
|
82 | // Use the right CSS classes for vertical & horizontal sliders | |
82 | if (orientation=='vertical') { |
|
83 | if (orientation=='vertical') { | |
83 | this.$slider_container |
|
84 | this.$slider_container | |
84 | .removeClass('widget-hslider') |
|
85 | .removeClass('widget-hslider') | |
85 | .addClass('widget-vslider'); |
|
86 | .addClass('widget-vslider'); | |
86 | this.$el |
|
87 | this.$el | |
87 | .removeClass('widget-hbox-single') |
|
88 | .removeClass('widget-hbox-single') | |
88 | .addClass('widget-vbox-single'); |
|
89 | .addClass('widget-vbox-single'); | |
89 | this.$label |
|
90 | this.$label | |
90 | .removeClass('widget-hlabel') |
|
91 | .removeClass('widget-hlabel') | |
91 | .addClass('widget-vlabel'); |
|
92 | .addClass('widget-vlabel'); | |
92 | this.$readout |
|
93 | this.$readout | |
93 | .removeClass('widget-hreadout') |
|
94 | .removeClass('widget-hreadout') | |
94 | .addClass('widget-vreadout'); |
|
95 | .addClass('widget-vreadout'); | |
95 |
|
96 | |||
96 | } else { |
|
97 | } else { | |
97 | this.$slider_container |
|
98 | this.$slider_container | |
98 | .removeClass('widget-vslider') |
|
99 | .removeClass('widget-vslider') | |
99 | .addClass('widget-hslider'); |
|
100 | .addClass('widget-hslider'); | |
100 | this.$el |
|
101 | this.$el | |
101 | .removeClass('widget-vbox-single') |
|
102 | .removeClass('widget-vbox-single') | |
102 | .addClass('widget-hbox-single'); |
|
103 | .addClass('widget-hbox-single'); | |
103 | this.$label |
|
104 | this.$label | |
104 | .removeClass('widget-vlabel') |
|
105 | .removeClass('widget-vlabel') | |
105 | .addClass('widget-hlabel'); |
|
106 | .addClass('widget-hlabel'); | |
106 | this.$readout |
|
107 | this.$readout | |
107 | .removeClass('widget-vreadout') |
|
108 | .removeClass('widget-vreadout') | |
108 | .addClass('widget-hreadout'); |
|
109 | .addClass('widget-hreadout'); | |
109 | } |
|
110 | } | |
110 |
|
111 | |||
111 | var description = this.model.get('description'); |
|
112 | var description = this.model.get('description'); | |
112 | if (description.length === 0) { |
|
113 | if (description.length === 0) { | |
113 | this.$label.hide(); |
|
114 | this.$label.hide(); | |
114 | } else { |
|
115 | } else { | |
115 | this.$label.text(description); |
|
116 | this.$label.text(description); | |
116 | MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$label.get(0)]); |
|
117 | MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$label.get(0)]); | |
117 | this.$label.show(); |
|
118 | this.$label.show(); | |
118 | } |
|
119 | } | |
119 |
|
120 | |||
120 | var readout = this.model.get('readout'); |
|
121 | var readout = this.model.get('readout'); | |
121 | if (readout) { |
|
122 | if (readout) { | |
122 | this.$readout.show(); |
|
123 | this.$readout.show(); | |
123 | } else { |
|
124 | } else { | |
124 | this.$readout.hide(); |
|
125 | this.$readout.hide(); | |
125 | } |
|
126 | } | |
126 | } |
|
127 | } | |
127 | return IntSliderView.__super__.update.apply(this); |
|
128 | return IntSliderView.__super__.update.apply(this); | |
128 | }, |
|
129 | }, | |
129 |
|
130 | |||
130 | events: { |
|
131 | events: { | |
131 | // Dictionary of events and their handlers. |
|
132 | // Dictionary of events and their handlers. | |
132 | "slide" : "handleSliderChange" |
|
133 | "slide" : "handleSliderChange" | |
133 | }, |
|
134 | }, | |
134 |
|
135 | |||
135 | handleSliderChange: function(e, ui) { |
|
136 | handleSliderChange: function(e, ui) { | |
136 | // Called when the slider value is changed. |
|
137 | // Called when the slider value is changed. | |
137 |
|
138 | |||
138 | // Calling model.set will trigger all of the other views of the |
|
139 | // Calling model.set will trigger all of the other views of the | |
139 | // model to update. |
|
140 | // model to update. | |
140 | var actual_value = this._validate_slide_value(ui.value); |
|
141 | var actual_value = this._validate_slide_value(ui.value); | |
141 | this.model.set('value', actual_value, {updated_view: this}); |
|
142 | this.model.set('value', actual_value, {updated_view: this}); | |
142 | this.$readout.text(actual_value); |
|
143 | this.$readout.text(actual_value); | |
143 | this.touch(); |
|
144 | this.touch(); | |
144 | }, |
|
145 | }, | |
145 |
|
146 | |||
146 | _validate_slide_value: function(x) { |
|
147 | _validate_slide_value: function(x) { | |
147 | // Validate the value of the slider before sending it to the back-end |
|
148 | // Validate the value of the slider before sending it to the back-end | |
148 | // and applying it to the other views on the page. |
|
149 | // and applying it to the other views on the page. | |
149 |
|
150 | |||
150 | // Double bit-wise not truncates the decimel (int cast). |
|
151 | // Double bit-wise not truncates the decimel (int cast). | |
151 | return ~~x; |
|
152 | return ~~x; | |
152 | }, |
|
153 | }, | |
153 | }); |
|
154 | }); | |
154 |
|
155 | |||
155 |
|
156 | |||
156 | var IntTextView = widget.DOMWidgetView.extend({ |
|
157 | var IntTextView = widget.DOMWidgetView.extend({ | |
157 | render : function(){ |
|
158 | render : function(){ | |
158 | // Called when view is rendered. |
|
159 | // Called when view is rendered. | |
159 | this.$el |
|
160 | this.$el | |
160 | .addClass('widget-hbox-single'); |
|
161 | .addClass('widget-hbox-single'); | |
161 | this.$label = $('<div />') |
|
162 | this.$label = $('<div />') | |
162 | .appendTo(this.$el) |
|
163 | .appendTo(this.$el) | |
163 | .addClass('widget-hlabel') |
|
164 | .addClass('widget-hlabel') | |
164 | .hide(); |
|
165 | .hide(); | |
165 | this.$textbox = $('<input type="text" />') |
|
166 | this.$textbox = $('<input type="text" />') | |
166 | .addClass('form-control') |
|
167 | .addClass('form-control') | |
167 | .addClass('widget-numeric-text') |
|
168 | .addClass('widget-numeric-text') | |
168 | .appendTo(this.$el); |
|
169 | .appendTo(this.$el); | |
169 | this.$el_to_style = this.$textbox; // Set default element to style |
|
170 | this.$el_to_style = this.$textbox; // Set default element to style | |
170 | this.update(); // Set defaults. |
|
171 | this.update(); // Set defaults. | |
171 | }, |
|
172 | }, | |
172 |
|
173 | |||
173 | update : function(options){ |
|
174 | update : function(options){ | |
174 | // Update the contents of this view |
|
175 | // Update the contents of this view | |
175 | // |
|
176 | // | |
176 | // Called when the model is changed. The model may have been |
|
177 | // Called when the model is changed. The model may have been | |
177 | // changed by another view or by a state update from the back-end. |
|
178 | // changed by another view or by a state update from the back-end. | |
178 | if (options === undefined || options.updated_view != this) { |
|
179 | if (options === undefined || options.updated_view != this) { | |
179 | var value = this.model.get('value'); |
|
180 | var value = this.model.get('value'); | |
180 | if (this._parse_value(this.$textbox.val()) != value) { |
|
181 | if (this._parse_value(this.$textbox.val()) != value) { | |
181 | this.$textbox.val(value); |
|
182 | this.$textbox.val(value); | |
182 | } |
|
183 | } | |
183 |
|
184 | |||
184 | if (this.model.get('disabled')) { |
|
185 | if (this.model.get('disabled')) { | |
185 | this.$textbox.attr('disabled','disabled'); |
|
186 | this.$textbox.attr('disabled','disabled'); | |
186 | } else { |
|
187 | } else { | |
187 | this.$textbox.removeAttr('disabled'); |
|
188 | this.$textbox.removeAttr('disabled'); | |
188 | } |
|
189 | } | |
189 |
|
190 | |||
190 | var description = this.model.get('description'); |
|
191 | var description = this.model.get('description'); | |
191 | if (description.length === 0) { |
|
192 | if (description.length === 0) { | |
192 | this.$label.hide(); |
|
193 | this.$label.hide(); | |
193 | } else { |
|
194 | } else { | |
194 | this.$label.text(description); |
|
195 | this.$label.text(description); | |
195 | MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$label.get(0)]); |
|
196 | MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$label.get(0)]); | |
196 | this.$label.show(); |
|
197 | this.$label.show(); | |
197 | } |
|
198 | } | |
198 | } |
|
199 | } | |
199 | return IntTextView.__super__.update.apply(this); |
|
200 | return IntTextView.__super__.update.apply(this); | |
200 | }, |
|
201 | }, | |
201 |
|
202 | |||
202 | events: { |
|
203 | events: { | |
203 | // Dictionary of events and their handlers. |
|
204 | // Dictionary of events and their handlers. | |
204 | "keyup input" : "handleChanging", |
|
205 | "keyup input" : "handleChanging", | |
205 | "paste input" : "handleChanging", |
|
206 | "paste input" : "handleChanging", | |
206 | "cut input" : "handleChanging", |
|
207 | "cut input" : "handleChanging", | |
207 |
|
208 | |||
208 | // Fires only when control is validated or looses focus. |
|
209 | // Fires only when control is validated or looses focus. | |
209 | "change input" : "handleChanged" |
|
210 | "change input" : "handleChanged" | |
210 | }, |
|
211 | }, | |
211 |
|
212 | |||
212 | handleChanging: function(e) { |
|
213 | handleChanging: function(e) { | |
213 | // Handles and validates user input. |
|
214 | // Handles and validates user input. | |
214 |
|
215 | |||
215 | // Try to parse value as a int. |
|
216 | // Try to parse value as a int. | |
216 | var numericalValue = 0; |
|
217 | var numericalValue = 0; | |
217 | if (e.target.value !== '') { |
|
218 | if (e.target.value !== '') { | |
218 | var trimmed = e.target.value.trim(); |
|
219 | var trimmed = e.target.value.trim(); | |
219 | if (!(['-', '-.', '.', '+.', '+'].indexOf(trimmed) >= 0)) { |
|
220 | if (!(['-', '-.', '.', '+.', '+'].indexOf(trimmed) >= 0)) { | |
220 | numericalValue = this._parse_value(e.target.value); |
|
221 | numericalValue = this._parse_value(e.target.value); | |
221 | } |
|
222 | } | |
222 | } |
|
223 | } | |
223 |
|
224 | |||
224 | // If parse failed, reset value to value stored in model. |
|
225 | // If parse failed, reset value to value stored in model. | |
225 | if (isNaN(numericalValue)) { |
|
226 | if (isNaN(numericalValue)) { | |
226 | e.target.value = this.model.get('value'); |
|
227 | e.target.value = this.model.get('value'); | |
227 | } else if (!isNaN(numericalValue)) { |
|
228 | } else if (!isNaN(numericalValue)) { | |
228 | if (this.model.get('max') !== undefined) { |
|
229 | if (this.model.get('max') !== undefined) { | |
229 | numericalValue = Math.min(this.model.get('max'), numericalValue); |
|
230 | numericalValue = Math.min(this.model.get('max'), numericalValue); | |
230 | } |
|
231 | } | |
231 | if (this.model.get('min') !== undefined) { |
|
232 | if (this.model.get('min') !== undefined) { | |
232 | numericalValue = Math.max(this.model.get('min'), numericalValue); |
|
233 | numericalValue = Math.max(this.model.get('min'), numericalValue); | |
233 | } |
|
234 | } | |
234 |
|
235 | |||
235 | // Apply the value if it has changed. |
|
236 | // Apply the value if it has changed. | |
236 | if (numericalValue != this.model.get('value')) { |
|
237 | if (numericalValue != this.model.get('value')) { | |
237 |
|
238 | |||
238 | // Calling model.set will trigger all of the other views of the |
|
239 | // Calling model.set will trigger all of the other views of the | |
239 | // model to update. |
|
240 | // model to update. | |
240 | this.model.set('value', numericalValue, {updated_view: this}); |
|
241 | this.model.set('value', numericalValue, {updated_view: this}); | |
241 | this.touch(); |
|
242 | this.touch(); | |
242 | } |
|
243 | } | |
243 | } |
|
244 | } | |
244 | }, |
|
245 | }, | |
245 |
|
246 | |||
246 | handleChanged: function(e) { |
|
247 | handleChanged: function(e) { | |
247 | // Applies validated input. |
|
248 | // Applies validated input. | |
248 | if (this.model.get('value') != e.target.value) { |
|
249 | if (this.model.get('value') != e.target.value) { | |
249 | e.target.value = this.model.get('value'); |
|
250 | e.target.value = this.model.get('value'); | |
250 | } |
|
251 | } | |
251 | }, |
|
252 | }, | |
252 |
|
253 | |||
253 | _parse_value: function(value) { |
|
254 | _parse_value: function(value) { | |
254 | // Parse the value stored in a string. |
|
255 | // Parse the value stored in a string. | |
255 | return parseInt(value); |
|
256 | return parseInt(value); | |
256 | }, |
|
257 | }, | |
257 | }); |
|
258 | }); | |
258 |
|
259 | |||
259 |
|
260 | |||
260 | var ProgressView = widget.DOMWidgetView.extend({ |
|
261 | var ProgressView = widget.DOMWidgetView.extend({ | |
261 | render : function(){ |
|
262 | render : function(){ | |
262 | // Called when view is rendered. |
|
263 | // Called when view is rendered. | |
263 | this.$el |
|
264 | this.$el | |
264 | .addClass('widget-hbox-single'); |
|
265 | .addClass('widget-hbox-single'); | |
265 | this.$label = $('<div />') |
|
266 | this.$label = $('<div />') | |
266 | .appendTo(this.$el) |
|
267 | .appendTo(this.$el) | |
267 | .addClass('widget-hlabel') |
|
268 | .addClass('widget-hlabel') | |
268 | .hide(); |
|
269 | .hide(); | |
269 | this.$progress = $('<div />') |
|
270 | this.$progress = $('<div />') | |
270 | .addClass('progress') |
|
271 | .addClass('progress') | |
271 | .addClass('widget-progress') |
|
272 | .addClass('widget-progress') | |
272 | .appendTo(this.$el); |
|
273 | .appendTo(this.$el); | |
273 | this.$el_to_style = this.$progress; // Set default element to style |
|
274 | this.$el_to_style = this.$progress; // Set default element to style | |
274 | this.$bar = $('<div />') |
|
275 | this.$bar = $('<div />') | |
275 | .addClass('progress-bar') |
|
276 | .addClass('progress-bar') | |
276 | .css('width', '50%') |
|
277 | .css('width', '50%') | |
277 | .appendTo(this.$progress); |
|
278 | .appendTo(this.$progress); | |
278 | this.update(); // Set defaults. |
|
279 | this.update(); // Set defaults. | |
279 | }, |
|
280 | }, | |
280 |
|
281 | |||
281 | update : function(){ |
|
282 | update : function(){ | |
282 | // Update the contents of this view |
|
283 | // Update the contents of this view | |
283 | // |
|
284 | // | |
284 | // Called when the model is changed. The model may have been |
|
285 | // Called when the model is changed. The model may have been | |
285 | // changed by another view or by a state update from the back-end. |
|
286 | // changed by another view or by a state update from the back-end. | |
286 | var value = this.model.get('value'); |
|
287 | var value = this.model.get('value'); | |
287 | var max = this.model.get('max'); |
|
288 | var max = this.model.get('max'); | |
288 | var min = this.model.get('min'); |
|
289 | var min = this.model.get('min'); | |
289 | var percent = 100.0 * (value - min) / (max - min); |
|
290 | var percent = 100.0 * (value - min) / (max - min); | |
290 | this.$bar.css('width', percent + '%'); |
|
291 | this.$bar.css('width', percent + '%'); | |
291 |
|
292 | |||
292 | var description = this.model.get('description'); |
|
293 | var description = this.model.get('description'); | |
293 | if (description.length === 0) { |
|
294 | if (description.length === 0) { | |
294 | this.$label.hide(); |
|
295 | this.$label.hide(); | |
295 | } else { |
|
296 | } else { | |
296 | this.$label.text(description); |
|
297 | this.$label.text(description); | |
297 | MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$label.get(0)]); |
|
298 | MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$label.get(0)]); | |
298 | this.$label.show(); |
|
299 | this.$label.show(); | |
299 | } |
|
300 | } | |
300 | return ProgressView.__super__.update.apply(this); |
|
301 | return ProgressView.__super__.update.apply(this); | |
301 | }, |
|
302 | }, | |
302 | }); |
|
303 | }); | |
303 |
|
304 | |||
304 | return { |
|
305 | return { | |
305 | 'IntSliderView': IntSliderView, |
|
306 | 'IntSliderView': IntSliderView, | |
306 | 'IntTextView': IntTextView, |
|
307 | 'IntTextView': IntTextView, | |
307 | 'ProgressView': ProgressView, |
|
308 | 'ProgressView': ProgressView, | |
308 | }; |
|
309 | }; | |
309 | }); |
|
310 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now