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