Show More
@@ -1,260 +1,260 b'' | |||
|
1 | 1 | define(["notebook/js/widget"], function(){ |
|
2 | 2 | var SelectionWidgetModel = IPython.WidgetModel.extend({}); |
|
3 | 3 | IPython.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 |
|
|
|
19 |
|
|
|
20 |
|
|
|
18 | .addClass('widget_item') | |
|
19 | .addClass('btn-group') | |
|
20 | .appendTo(this.$el); | |
|
21 | 21 | this.$el_to_style = this.$buttongroup; // Set default element to style |
|
22 | 22 | this.$droplabel = $('<button />') |
|
23 |
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
|
|
|
23 | .addClass('btn') | |
|
24 | .addClass('widget-combo-btn') | |
|
25 | .html(' ') | |
|
26 | .appendTo(this.$buttongroup); | |
|
27 | 27 | this.$dropbutton = $('<button />') |
|
28 |
|
|
|
29 |
|
|
|
30 |
|
|
|
31 |
|
|
|
32 |
|
|
|
33 |
|
|
|
28 | .addClass('btn') | |
|
29 | .addClass('dropdown-toggle') | |
|
30 | .addClass('widget-combo-carrot-btn') | |
|
31 | .attr('data-toggle', 'dropdown') | |
|
32 | .html('<span class="caret"></span>') | |
|
33 | .appendTo(this.$buttongroup); | |
|
34 | 34 | this.$droplist = $('<ul />') |
|
35 |
|
|
|
36 |
|
|
|
35 | .addClass('dropdown-menu') | |
|
36 | .appendTo(this.$buttongroup); | |
|
37 | 37 | |
|
38 | 38 | // Set defaults. |
|
39 | 39 | this.update(); |
|
40 | 40 | }, |
|
41 | 41 | |
|
42 | 42 | // Handles: Backend -> Frontend Sync |
|
43 | 43 | // Frontent -> Frontend Sync |
|
44 | 44 | update : function(){ |
|
45 | 45 | |
|
46 | 46 | var selected_item_text = this.model.get('value'); |
|
47 | 47 | selected_item_text.replace(' ', ''); |
|
48 | 48 | if (selected_item_text == '') { |
|
49 | 49 | this.$droplabel.html(' '); |
|
50 | 50 | } else { |
|
51 | 51 | this.$droplabel.html(this.model.get('value')); |
|
52 | 52 | } |
|
53 | 53 | |
|
54 | 54 | var items = this.model.get('values'); |
|
55 | 55 | this.$droplist.html(''); |
|
56 | 56 | for (var index in items) { |
|
57 | 57 | var that = this; |
|
58 | 58 | var item_button = $('<a href="#"/>') |
|
59 | 59 | .html(items[index]) |
|
60 | 60 | .on('click', function(e){ |
|
61 | 61 | that.model.set('value', $(e.target).html(), this); |
|
62 | 62 | that.model.update_other_views(that); |
|
63 | 63 | }) |
|
64 | 64 | |
|
65 | 65 | this.$droplist.append($('<li />').append(item_button)) |
|
66 | 66 | } |
|
67 | 67 | |
|
68 | 68 | if (this.model.get('disabled')) { |
|
69 | 69 | this.$buttongroup.attr('disabled','disabled'); |
|
70 | 70 | this.$droplabel.attr('disabled','disabled'); |
|
71 | 71 | this.$dropbutton.attr('disabled','disabled'); |
|
72 | 72 | this.$droplist.attr('disabled','disabled'); |
|
73 | 73 | } else { |
|
74 | 74 | this.$buttongroup.removeAttr('disabled'); |
|
75 | 75 | this.$droplabel.removeAttr('disabled'); |
|
76 | 76 | this.$dropbutton.removeAttr('disabled'); |
|
77 | 77 | this.$droplist.removeAttr('disabled'); |
|
78 | 78 | } |
|
79 | 79 | |
|
80 | 80 | var description = this.model.get('description'); |
|
81 | 81 | if (description.length == 0) { |
|
82 | 82 | this.$label.hide(); |
|
83 | 83 | } else { |
|
84 | 84 | this.$label.html(description); |
|
85 | 85 | this.$label.show(); |
|
86 | 86 | } |
|
87 | 87 | return IPython.WidgetView.prototype.update.call(this); |
|
88 | 88 | }, |
|
89 | 89 | |
|
90 | 90 | }); |
|
91 | 91 | |
|
92 | 92 | IPython.widget_manager.register_widget_view('DropdownView', DropdownView); |
|
93 | 93 | |
|
94 | 94 | var RadioButtonsView = IPython.WidgetView.extend({ |
|
95 | 95 | |
|
96 | 96 | // Called when view is rendered. |
|
97 | 97 | render : function(){ |
|
98 | 98 | this.$el |
|
99 | 99 | .addClass('widget-hbox') |
|
100 | 100 | .html(''); |
|
101 | 101 | this.$label = $('<div />') |
|
102 | 102 | .appendTo(this.$el) |
|
103 | 103 | .addClass('widget-hlabel') |
|
104 | 104 | .hide(); |
|
105 | 105 | this.$container = $('<div />') |
|
106 | 106 | .appendTo(this.$el) |
|
107 | 107 | .addClass('widget-container') |
|
108 | 108 | .addClass('vbox'); |
|
109 | 109 | this.$el_to_style = this.$container; // Set default element to style |
|
110 | 110 | this.update(); |
|
111 | 111 | }, |
|
112 | 112 | |
|
113 | 113 | // Handles: Backend -> Frontend Sync |
|
114 | 114 | // Frontent -> Frontend Sync |
|
115 | 115 | update : function(){ |
|
116 | 116 | |
|
117 | 117 | // Add missing items to the DOM. |
|
118 | 118 | var items = this.model.get('values'); |
|
119 | 119 | var disabled = this.model.get('disabled'); |
|
120 | 120 | for (var index in items) { |
|
121 | 121 | var item_query = ' :input[value="' + items[index] + '"]'; |
|
122 | 122 | if (this.$el.find(item_query).length == 0) { |
|
123 | 123 | var $label = $('<label />') |
|
124 | 124 | .addClass('radio') |
|
125 | 125 | .html(items[index]) |
|
126 | 126 | .appendTo(this.$container); |
|
127 | 127 | |
|
128 | 128 | var that = this; |
|
129 | 129 | $('<input />') |
|
130 | 130 | .attr('type', 'radio') |
|
131 | 131 | .addClass(this.model) |
|
132 | 132 | .val(items[index]) |
|
133 | 133 | .prependTo($label) |
|
134 | 134 | .on('click', function(e){ |
|
135 | 135 | that.model.set('value', $(e.target).val(), this); |
|
136 | 136 | that.model.update_other_views(that); |
|
137 | 137 | }); |
|
138 | 138 | } |
|
139 | 139 | |
|
140 | 140 | var $item_element = this.$container.find(item_query); |
|
141 | 141 | if (this.model.get('value') == items[index]) { |
|
142 | 142 | $item_element.prop('checked', true); |
|
143 | 143 | } else { |
|
144 | 144 | $item_element.prop('checked', false); |
|
145 | 145 | } |
|
146 | 146 | $item_element.prop('disabled', disabled); |
|
147 | 147 | } |
|
148 | 148 | |
|
149 | 149 | // Remove items that no longer exist. |
|
150 | 150 | this.$container.find('input').each(function(i, obj) { |
|
151 | 151 | var value = $(obj).val(); |
|
152 | 152 | var found = false; |
|
153 | 153 | for (var index in items) { |
|
154 | 154 | if (items[index] == value) { |
|
155 | 155 | found = true; |
|
156 | 156 | break; |
|
157 | 157 | } |
|
158 | 158 | } |
|
159 | 159 | |
|
160 | 160 | if (!found) { |
|
161 | 161 | $(obj).parent().remove(); |
|
162 | 162 | } |
|
163 | 163 | }); |
|
164 | 164 | |
|
165 | 165 | var description = this.model.get('description'); |
|
166 | 166 | if (description.length == 0) { |
|
167 | 167 | this.$label.hide(); |
|
168 | 168 | } else { |
|
169 | 169 | this.$label.html(description); |
|
170 | 170 | this.$label.show(); |
|
171 | 171 | } |
|
172 | 172 | return IPython.WidgetView.prototype.update.call(this); |
|
173 | 173 | }, |
|
174 | 174 | |
|
175 | 175 | }); |
|
176 | 176 | |
|
177 | 177 | IPython.widget_manager.register_widget_view('RadioButtonsView', RadioButtonsView); |
|
178 | 178 | |
|
179 | 179 | |
|
180 | 180 | var ToggleButtonsView = IPython.WidgetView.extend({ |
|
181 | 181 | |
|
182 | 182 | // Called when view is rendered. |
|
183 | 183 | render : function(){ |
|
184 | 184 | this.$el |
|
185 | 185 | .addClass('widget-hbox-single') |
|
186 | 186 | .html(''); |
|
187 | 187 | this.$label = $('<div />') |
|
188 | 188 | .appendTo(this.$el) |
|
189 | 189 | .addClass('widget-hlabel') |
|
190 | 190 | .hide(); |
|
191 | 191 | this.$buttongroup = $('<div />') |
|
192 | 192 | .addClass('btn-group') |
|
193 | 193 | .attr('data-toggle', 'buttons-radio') |
|
194 | 194 | .appendTo(this.$el); |
|
195 | 195 | this.$el_to_style = this.$buttongroup; // Set default element to style |
|
196 | 196 | this.update(); |
|
197 | 197 | }, |
|
198 | 198 | |
|
199 | 199 | // Handles: Backend -> Frontend Sync |
|
200 | 200 | // Frontent -> Frontend Sync |
|
201 | 201 | update : function(){ |
|
202 | 202 | |
|
203 | 203 | // Add missing items to the DOM. |
|
204 | 204 | var items = this.model.get('values'); |
|
205 | 205 | var disabled = this.model.get('disabled'); |
|
206 | 206 | for (var index in items) { |
|
207 | 207 | var item_query = ' :contains("' + items[index] + '")'; |
|
208 | 208 | if (this.$buttongroup.find(item_query).length == 0) { |
|
209 | 209 | |
|
210 | 210 | var that = this; |
|
211 | 211 | $('<button />') |
|
212 | 212 | .attr('type', 'button') |
|
213 | 213 | .addClass('btn') |
|
214 | 214 | .html(items[index]) |
|
215 | 215 | .appendTo(this.$buttongroup) |
|
216 | 216 | .on('click', function(e){ |
|
217 | 217 | that.model.set('value', $(e.target).html(), this); |
|
218 | 218 | that.model.update_other_views(that); |
|
219 | 219 | }); |
|
220 | 220 | } |
|
221 | 221 | |
|
222 | 222 | var $item_element = this.$buttongroup.find(item_query); |
|
223 | 223 | if (this.model.get('value') == items[index]) { |
|
224 | 224 | $item_element.addClass('active'); |
|
225 | 225 | } else { |
|
226 | 226 | $item_element.removeClass('active'); |
|
227 | 227 | } |
|
228 | 228 | $item_element.prop('disabled', disabled); |
|
229 | 229 | } |
|
230 | 230 | |
|
231 | 231 | // Remove items that no longer exist. |
|
232 | 232 | this.$buttongroup.find('button').each(function(i, obj) { |
|
233 | 233 | var value = $(obj).html(); |
|
234 | 234 | var found = false; |
|
235 | 235 | for (var index in items) { |
|
236 | 236 | if (items[index] == value) { |
|
237 | 237 | found = true; |
|
238 | 238 | break; |
|
239 | 239 | } |
|
240 | 240 | } |
|
241 | 241 | |
|
242 | 242 | if (!found) { |
|
243 | 243 | $(obj).remove(); |
|
244 | 244 | } |
|
245 | 245 | }); |
|
246 | 246 | |
|
247 | 247 | var description = this.model.get('description'); |
|
248 | 248 | if (description.length == 0) { |
|
249 | 249 | this.$label.hide(); |
|
250 | 250 | } else { |
|
251 | 251 | this.$label.html(description); |
|
252 | 252 | this.$label.show(); |
|
253 | 253 | } |
|
254 | 254 | return IPython.WidgetView.prototype.update.call(this); |
|
255 | 255 | }, |
|
256 | 256 | |
|
257 | 257 | }); |
|
258 | 258 | |
|
259 | 259 | IPython.widget_manager.register_widget_view('ToggleButtonsView', ToggleButtonsView); |
|
260 | 260 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now