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