##// END OF EJS Templates
remove .html('');
Jonathan Frederic -
Show More
@@ -1,385 +1,382
1 1 //----------------------------------------------------------------------------
2 2 // Copyright (C) 2013 The IPython Development Team
3 3 //
4 4 // Distributed under the terms of the BSD License. The full license is in
5 5 // the file COPYING, distributed as part of this software.
6 6 //----------------------------------------------------------------------------
7 7
8 8 //============================================================================
9 9 // SelectionWidget
10 10 //============================================================================
11 11
12 12 /**
13 13 * @module IPython
14 14 * @namespace IPython
15 15 **/
16 16
17 17 define(["notebook/js/widgets/widget"], function(widget_manager){
18 18 var SelectionWidgetModel = IPython.WidgetModel.extend({});
19 19 widget_manager.register_widget_model('SelectionWidgetModel', SelectionWidgetModel);
20 20
21 21 var DropdownView = IPython.DOMWidgetView.extend({
22 22
23 23 // Called when view is rendered.
24 24 render : function(){
25 25
26 26 this.$el
27 27 .addClass('widget-hbox-single')
28 28 .html('');
29 29 this.$label = $('<div />')
30 30 .appendTo(this.$el)
31 31 .addClass('widget-hlabel')
32 32 .hide();
33 33 this.$buttongroup = $('<div />')
34 34 .addClass('widget_item')
35 35 .addClass('btn-group')
36 36 .appendTo(this.$el);
37 37 this.$el_to_style = this.$buttongroup; // Set default element to style
38 38 this.$droplabel = $('<button />')
39 39 .addClass('btn')
40 40 .addClass('widget-combo-btn')
41 41 .html('&nbsp;')
42 42 .appendTo(this.$buttongroup);
43 43 this.$dropbutton = $('<button />')
44 44 .addClass('btn')
45 45 .addClass('dropdown-toggle')
46 46 .addClass('widget-combo-carrot-btn')
47 47 .attr('data-toggle', 'dropdown')
48 48 .html('<span class="caret"></span>')
49 49 .appendTo(this.$buttongroup);
50 50 this.$droplist = $('<ul />')
51 51 .addClass('dropdown-menu')
52 52 .appendTo(this.$buttongroup);
53 53
54 54 // Set defaults.
55 55 this.update();
56 56 },
57 57
58 58 update : function(options){
59 59 // Update the contents of this view
60 60 //
61 61 // Called when the model is changed. The model may have been
62 62 // changed by another view or by a state update from the back-end.
63 63
64 64 if (options === undefined || options.updated_view != this) {
65 65 var selected_item_text = this.model.get('value');
66 66 selected_item_text = selected_item_text.replace(/ /g, '&nbsp;');
67 67 selected_item_text = selected_item_text.replace(/\n/g, '<br>\n');
68 68 if (selected_item_text.length === 0) {
69 69 this.$droplabel.html('&nbsp;');
70 70 } else {
71 71 this.$droplabel.html(selected_item_text);
72 72 }
73 73
74 74 var items = this.model.get('values');
75 75 this.$droplist.html('');
76 76 for (var index in items) {
77 77 var that = this;
78 78 var item_button = $('<a href="#"/>')
79 79 .html(items[index])
80 80 .on('click', $.proxy(this.handle_click, this));
81 81 this.$droplist.append($('<li />').append(item_button));
82 82 }
83 83
84 84 if (this.model.get('disabled')) {
85 85 this.$buttongroup.attr('disabled','disabled');
86 86 this.$droplabel.attr('disabled','disabled');
87 87 this.$dropbutton.attr('disabled','disabled');
88 88 this.$droplist.attr('disabled','disabled');
89 89 } else {
90 90 this.$buttongroup.removeAttr('disabled');
91 91 this.$droplabel.removeAttr('disabled');
92 92 this.$dropbutton.removeAttr('disabled');
93 93 this.$droplist.removeAttr('disabled');
94 94 }
95 95
96 96 var description = this.model.get('description');
97 97 if (description.length === 0) {
98 98 this.$label.hide();
99 99 } else {
100 100 this.$label.html(description);
101 101 this.$label.show();
102 102 }
103 103 }
104 104 return IPython.DOMWidgetView.prototype.update.call(this);
105 105 },
106 106
107 107 // Handle when a value is clicked.
108 108 handle_click: function (e) {
109 109
110 110 // Calling model.set will trigger all of the other views of the
111 111 // model to update.
112 112 this.model.set('value', $(e.target).html(), {updated_view: this});
113 113 this.touch();
114 114 },
115 115
116 116 });
117 117
118 118 widget_manager.register_widget_view('DropdownView', DropdownView);
119 119
120 120 var RadioButtonsView = IPython.DOMWidgetView.extend({
121 121
122 122 // Called when view is rendered.
123 123 render : function(){
124 124 this.$el
125 .addClass('widget-hbox')
126 .html('');
125 .addClass('widget-hbox');
127 126 this.$label = $('<div />')
128 127 .appendTo(this.$el)
129 128 .addClass('widget-hlabel')
130 129 .hide();
131 130 this.$container = $('<div />')
132 131 .appendTo(this.$el)
133 132 .addClass('widget-container')
134 133 .addClass('vbox');
135 134 this.$el_to_style = this.$container; // Set default element to style
136 135 this.update();
137 136 },
138 137
139 138 update : function(options){
140 139 // Update the contents of this view
141 140 //
142 141 // Called when the model is changed. The model may have been
143 142 // changed by another view or by a state update from the back-end.
144 143 if (options === undefined || options.updated_view != this) {
145 144 // Add missing items to the DOM.
146 145 var items = this.model.get('values');
147 146 var disabled = this.model.get('disabled');
148 147 for (var index in items) {
149 148 var item_query = ' :input[value="' + items[index] + '"]';
150 149 if (this.$el.find(item_query).length === 0) {
151 150 var $label = $('<label />')
152 151 .addClass('radio')
153 152 .html(items[index])
154 153 .appendTo(this.$container);
155 154
156 155 $('<input />')
157 156 .attr('type', 'radio')
158 157 .addClass(this.model)
159 158 .val(items[index])
160 159 .prependTo($label)
161 160 .on('click', $.proxy(this.handle_click, this));
162 161 }
163 162
164 163 var $item_element = this.$container.find(item_query);
165 164 if (this.model.get('value') == items[index]) {
166 165 $item_element.prop('checked', true);
167 166 } else {
168 167 $item_element.prop('checked', false);
169 168 }
170 169 $item_element.prop('disabled', disabled);
171 170 }
172 171
173 172 // Remove items that no longer exist.
174 173 this.$container.find('input').each(function(i, obj) {
175 174 var value = $(obj).val();
176 175 var found = false;
177 176 for (var index in items) {
178 177 if (items[index] == value) {
179 178 found = true;
180 179 break;
181 180 }
182 181 }
183 182
184 183 if (!found) {
185 184 $(obj).parent().remove();
186 185 }
187 186 });
188 187
189 188 var description = this.model.get('description');
190 189 if (description.length === 0) {
191 190 this.$label.hide();
192 191 } else {
193 192 this.$label.html(description);
194 193 this.$label.show();
195 194 }
196 195 }
197 196 return IPython.DOMWidgetView.prototype.update.call(this);
198 197 },
199 198
200 199 // Handle when a value is clicked.
201 200 handle_click: function (e) {
202 201
203 202 // Calling model.set will trigger all of the other views of the
204 203 // model to update.
205 204 this.model.set('value', $(e.target).val(), {updated_view: this});
206 205 this.touch();
207 206 },
208 207 });
209 208
210 209 widget_manager.register_widget_view('RadioButtonsView', RadioButtonsView);
211 210
212 211
213 212 var ToggleButtonsView = IPython.DOMWidgetView.extend({
214 213
215 214 // Called when view is rendered.
216 215 render : function(){
217 216 this.$el
218 .addClass('widget-hbox-single')
219 .html('');
217 .addClass('widget-hbox-single');
220 218 this.$label = $('<div />')
221 219 .appendTo(this.$el)
222 220 .addClass('widget-hlabel')
223 221 .hide();
224 222 this.$buttongroup = $('<div />')
225 223 .addClass('btn-group')
226 224 .attr('data-toggle', 'buttons-radio')
227 225 .appendTo(this.$el);
228 226 this.$el_to_style = this.$buttongroup; // Set default element to style
229 227 this.update();
230 228 },
231 229
232 230 update : function(options){
233 231 // Update the contents of this view
234 232 //
235 233 // Called when the model is changed. The model may have been
236 234 // changed by another view or by a state update from the back-end.
237 235 if (options === undefined || options.updated_view != this) {
238 236 // Add missing items to the DOM.
239 237 var items = this.model.get('values');
240 238 var disabled = this.model.get('disabled');
241 239 for (var index in items) {
242 240 var item_query = ' :contains("' + items[index] + '")';
243 241 if (this.$buttongroup.find(item_query).length === 0) {
244 242 $('<button />')
245 243 .attr('type', 'button')
246 244 .addClass('btn')
247 245 .html(items[index])
248 246 .appendTo(this.$buttongroup)
249 247 .on('click', $.proxy(this.handle_click, this));
250 248 }
251 249
252 250 var $item_element = this.$buttongroup.find(item_query);
253 251 if (this.model.get('value') == items[index]) {
254 252 $item_element.addClass('active');
255 253 } else {
256 254 $item_element.removeClass('active');
257 255 }
258 256 $item_element.prop('disabled', disabled);
259 257 }
260 258
261 259 // Remove items that no longer exist.
262 260 this.$buttongroup.find('button').each(function(i, obj) {
263 261 var value = $(obj).html();
264 262 var found = false;
265 263 for (var index in items) {
266 264 if (items[index] == value) {
267 265 found = true;
268 266 break;
269 267 }
270 268 }
271 269
272 270 if (!found) {
273 271 $(obj).remove();
274 272 }
275 273 });
276 274
277 275 var description = this.model.get('description');
278 276 if (description.length === 0) {
279 277 this.$label.hide();
280 278 } else {
281 279 this.$label.html(description);
282 280 this.$label.show();
283 281 }
284 282 }
285 283 return IPython.DOMWidgetView.prototype.update.call(this);
286 284 },
287 285
288 286 // Handle when a value is clicked.
289 287 handle_click: function (e) {
290 288
291 289 // Calling model.set will trigger all of the other views of the
292 290 // model to update.
293 291 this.model.set('value', $(e.target).html(), {updated_view: this});
294 292 this.touch();
295 293 },
296 294
297 295 });
298 296
299 297 widget_manager.register_widget_view('ToggleButtonsView', ToggleButtonsView);
300 298
301 299 var ListBoxView = IPython.DOMWidgetView.extend({
302 300
303 301 // Called when view is rendered.
304 302 render : function(){
305 303 this.$el
306 .addClass('widget-hbox')
307 .html('');
304 .addClass('widget-hbox');
308 305 this.$label = $('<div />')
309 306 .appendTo(this.$el)
310 307 .addClass('widget-hlabel')
311 308 .hide();
312 309 this.$listbox = $('<select />')
313 310 .addClass('widget-listbox')
314 311 .attr('size', 6)
315 312 .appendTo(this.$el);
316 313 this.$el_to_style = this.$listbox; // Set default element to style
317 314 this.update();
318 315 },
319 316
320 317 update : function(options){
321 318 // Update the contents of this view
322 319 //
323 320 // Called when the model is changed. The model may have been
324 321 // changed by another view or by a state update from the back-end.
325 322 if (options === undefined || options.updated_view != this) {
326 323 // Add missing items to the DOM.
327 324 var items = this.model.get('values');
328 325 for (var index in items) {
329 326 var item_query = ' :contains("' + items[index] + '")';
330 327 if (this.$listbox.find(item_query).length === 0) {
331 328 $('<option />')
332 329 .html(items[index])
333 330 .attr('value', items[index])
334 331 .appendTo(this.$listbox)
335 332 .on('click', $.proxy(this.handle_click, this));
336 333 }
337 334 }
338 335
339 336 // Select the correct element
340 337 this.$listbox.val(this.model.get('value'));
341 338
342 339 // Disable listbox if needed
343 340 var disabled = this.model.get('disabled');
344 341 this.$listbox.prop('disabled', disabled);
345 342
346 343 // Remove items that no longer exist.
347 344 this.$listbox.find('option').each(function(i, obj) {
348 345 var value = $(obj).html();
349 346 var found = false;
350 347 for (var index in items) {
351 348 if (items[index] == value) {
352 349 found = true;
353 350 break;
354 351 }
355 352 }
356 353
357 354 if (!found) {
358 355 $(obj).remove();
359 356 }
360 357 });
361 358
362 359 var description = this.model.get('description');
363 360 if (description.length === 0) {
364 361 this.$label.hide();
365 362 } else {
366 363 this.$label.html(description);
367 364 this.$label.show();
368 365 }
369 366 }
370 367 return IPython.DOMWidgetView.prototype.update.call(this);
371 368 },
372 369
373 370 // Handle when a value is clicked.
374 371 handle_click: function (e) {
375 372
376 373 // Calling model.set will trigger all of the other views of the
377 374 // model to update.
378 375 this.model.set('value', $(e.target).html(), {updated_view: this});
379 376 this.touch();
380 377 },
381 378
382 379 });
383 380
384 381 widget_manager.register_widget_view('ListBoxView', ListBoxView);
385 382 });
General Comments 0
You need to be logged in to leave comments. Login now