Show More
@@ -226,31 +226,18 b' function(widget_manager, underscore, backbone){' | |||
|
226 | 226 | // triggered on model change |
|
227 | 227 | }, |
|
228 | 228 | |
|
229 | child_view: function(child_model, options) { | |
|
229 | create_child_view: function(child_model, options) { | |
|
230 | 230 | // create and return a child view, given a model and (optionally) a view name |
|
231 | 231 | // if the view name is not given, it defaults to the model's default view attribute |
|
232 | 232 | var child_view = this.model.widget_manager.create_view(child_model, options); |
|
233 | 233 | this.child_views[child_model.id] = child_view; |
|
234 | 234 | return child_view; |
|
235 | 235 | }, |
|
236 | ||
|
237 |
|
|
|
238 | // this function takes an old list and new list of models | |
|
239 | // views in child_views that correspond to deleted ids are deleted | |
|
240 | // views corresponding to added ids are added child_views | |
|
241 | ||
|
242 | // delete old views | |
|
243 | _.each(_.difference(old_list, new_list), function(element, index, list) { | |
|
244 | var view = this.child_views[element.id]; | |
|
245 | delete this.child_views[element.id]; | |
|
246 | view.remove(); | |
|
247 | }, this); | |
|
248 | ||
|
249 | // add new views | |
|
250 | _.each(_.difference(new_list, old_list), function(element, index, list) { | |
|
251 | // this function adds the view to the child_views dictionary | |
|
252 | this.child_view(element); | |
|
253 | }, this); | |
|
236 | ||
|
237 | delete_child_view: function(child_model, options) { | |
|
238 | var view = this.child_views[child_model.id]; | |
|
239 | delete this.child_views[child_model.id]; | |
|
240 | view.remove(); | |
|
254 | 241 | }, |
|
255 | 242 | |
|
256 | 243 | do_diff: function(old_list, new_list, removed_callback, added_callback) { |
@@ -29,11 +29,20 b' define(["notebook/js/widgets/widget"], function(widget_manager) {' | |||
|
29 | 29 | }, |
|
30 | 30 | |
|
31 | 31 | update_children: function(old_list, new_list) { |
|
32 | this.$el.empty(); | |
|
33 | this.update_child_views(old_list, new_list); | |
|
34 | _.each(new_list, function(element, index, list) { | |
|
35 |
|
|
|
36 |
|
|
|
32 | this.do_diff(old_list, | |
|
33 | new_list, | |
|
34 | $.proxy(this.remove_child_model, this), | |
|
35 | $.proxy(this.add_child_model, this)); | |
|
36 | }, | |
|
37 | ||
|
38 | remove_child_model: function(model) { | |
|
39 | this.child_views[model.id].remove(); | |
|
40 | this.delete_child_view(model); | |
|
41 | }, | |
|
42 | ||
|
43 | add_child_model: function(model) { | |
|
44 | var view = this.create_child_view(model); | |
|
45 | this.$el.append(view.$el); | |
|
37 | 46 | }, |
|
38 | 47 | |
|
39 | 48 | update: function(){ |
@@ -188,11 +197,20 b' define(["notebook/js/widgets/widget"], function(widget_manager) {' | |||
|
188 | 197 | }, |
|
189 | 198 | |
|
190 | 199 | update_children: function(old_list, new_list) { |
|
191 | this.$el.empty(); | |
|
192 | this.update_child_views(old_list, new_list); | |
|
193 | _.each(new_list, function(element, index, list) { | |
|
194 |
|
|
|
195 |
|
|
|
200 | this.do_diff(old_list, | |
|
201 | new_list, | |
|
202 | $.proxy(this.remove_child_model, this), | |
|
203 | $.proxy(this.add_child_model, this)); | |
|
204 | }, | |
|
205 | ||
|
206 | remove_child_model: function(model) { | |
|
207 | this.child_views[model.id].remove(); | |
|
208 | this.delete_child_view(model); | |
|
209 | }, | |
|
210 | ||
|
211 | add_child_model: function(model) { | |
|
212 | var view = this.create_child_view(model); | |
|
213 | this.$body.append(view.$el); | |
|
196 | 214 | }, |
|
197 | 215 | |
|
198 | 216 | update: function(){ |
@@ -23,24 +23,13 b' define(["notebook/js/widgets/widget"], function(widget_manager){' | |||
|
23 | 23 | .attr('id', guid) |
|
24 | 24 | .addClass('accordion'); |
|
25 | 25 | this.containers = []; |
|
26 | this.model_containers = {}; | |
|
26 | 27 | this.update_children([], this.model.get('children')); |
|
27 | 28 | this.model.on('change:children', function(model, value, options) { |
|
28 | 29 | this.update_children(model.previous('children'), value); |
|
29 | 30 | }, this); |
|
30 | 31 | }, |
|
31 | 32 | |
|
32 | update_children: function(old_list, new_list) { | |
|
33 | _.each(this.containers, function(element, index, list) { | |
|
34 | element.remove(); | |
|
35 | }, this); | |
|
36 | this.containers = []; | |
|
37 | this.update_child_views(old_list, new_list); | |
|
38 | _.each(new_list, function(element, index, list) { | |
|
39 | this.add_child_view(this.child_views[element]); | |
|
40 | }, this) | |
|
41 | }, | |
|
42 | ||
|
43 | ||
|
44 | 33 | update: function(options) { |
|
45 | 34 | // Update the contents of this view |
|
46 | 35 | // |
@@ -76,9 +65,24 b' define(["notebook/js/widgets/widget"], function(widget_manager){' | |||
|
76 | 65 | } |
|
77 | 66 | return AccordionView.__super__.update.apply(this); |
|
78 | 67 | }, |
|
68 | ||
|
69 | update_children: function(old_list, new_list) { | |
|
70 | this.do_diff(old_list, | |
|
71 | new_list, | |
|
72 | $.proxy(this.remove_child_model, this), | |
|
73 | $.proxy(this.add_child_model, this)); | |
|
74 | }, | |
|
79 | 75 | |
|
80 |
|
|
|
76 | remove_child_model: function(model) { | |
|
77 | var accordion_group = this.model_containers[model.id]; | |
|
78 | this.containers.splice(accordion_group.container_index, 1); | |
|
79 | delete this.model_containers[model.id]; | |
|
80 | accordion_group.remove(); | |
|
81 | this.delete_child_view(model); | |
|
82 | }, | |
|
81 | 83 | |
|
84 | add_child_model: function(model) { | |
|
85 | var view = this.create_child_view(model); | |
|
82 | 86 | var index = this.containers.length; |
|
83 | 87 | var uuid = IPython.utils.uuid(); |
|
84 | 88 | var accordion_group = $('<div />') |
@@ -108,7 +112,9 b' define(["notebook/js/widgets/widget"], function(widget_manager){' | |||
|
108 | 112 | var accordion_inner = $('<div />') |
|
109 | 113 | .addClass('accordion-inner') |
|
110 | 114 | .appendTo(accordion_body); |
|
111 | this.containers.push(accordion_group); | |
|
115 | var container_index = this.containers.push(accordion_group) - 1; | |
|
116 | accordion_group.container_index = container_index; | |
|
117 | this.model_containers[model.id] = accordion_group; | |
|
112 | 118 | accordion_inner.append(view.$el); |
|
113 | 119 | |
|
114 | 120 | this.update(); |
@@ -157,31 +163,25 b' define(["notebook/js/widgets/widget"], function(widget_manager){' | |||
|
157 | 163 | this.add_child_view(this.child_views[element]); |
|
158 | 164 | }, this) |
|
159 | 165 | }, |
|
166 | ||
|
167 | update_children: function(old_list, new_list) { | |
|
168 | this.do_diff(old_list, | |
|
169 | new_list, | |
|
170 | $.proxy(this.remove_child_model, this), | |
|
171 | $.proxy(this.add_child_model, this)); | |
|
172 | }, | |
|
160 | 173 | |
|
161 |
|
|
|
162 | // Update the contents of this view | |
|
163 | // | |
|
164 | // Called when the model is changed. The model may have been | |
|
165 | // changed by another view or by a state update from the back-end. | |
|
166 | if (options === undefined || options.updated_view != this) { | |
|
167 | // Set tab titles | |
|
168 | var titles = this.model.get('_titles'); | |
|
169 | for (var page_index in titles) { | |
|
170 | var tab_text = this.containers[page_index]; | |
|
171 | if (tab_text !== undefined) { | |
|
172 | tab_text.html(titles[page_index]); | |
|
173 | } | |
|
174 | } | |
|
175 | ||
|
176 | var selected_index = this.model.get('selected_index'); | |
|
177 | if (0 <= selected_index && selected_index < this.containers.length) { | |
|
178 | this.select_page(selected_index); | |
|
179 | } | |
|
180 | } | |
|
181 | return TabView.__super__.update.apply(this); | |
|
174 | remove_child_model: function(model) { | |
|
175 | var view = this.child_views[model.id]; | |
|
176 | this.containers.splice(view.parent_tab.tab_text_index, 1); | |
|
177 | view.parent_tab.remove(); | |
|
178 | view.parent_container.remove(); | |
|
179 | view.remove(); | |
|
180 | this.delete_child_view(model); | |
|
182 | 181 | }, |
|
183 | 182 | |
|
184 |
add_child_ |
|
|
183 | add_child_model: function(model) { | |
|
184 | var view = this.create_child_view(model); | |
|
185 | 185 | var index = this.containers.length; |
|
186 | 186 | var uuid = IPython.utils.uuid(); |
|
187 | 187 | |
@@ -189,6 +189,8 b' define(["notebook/js/widgets/widget"], function(widget_manager){' | |||
|
189 | 189 | var tab = $('<li />') |
|
190 | 190 | .css('list-style-type', 'none') |
|
191 | 191 | .appendTo(this.$tabs); |
|
192 | view.parent_tab = tab; | |
|
193 | ||
|
192 | 194 | var tab_text = $('<a />') |
|
193 | 195 | .attr('href', '#' + uuid) |
|
194 | 196 | .attr('data-toggle', 'tab') |
@@ -202,13 +204,37 b' define(["notebook/js/widgets/widget"], function(widget_manager){' | |||
|
202 | 204 | that.touch(); |
|
203 | 205 | that.select_page(index); |
|
204 | 206 | }); |
|
205 | this.containers.push(tab_text); | |
|
207 | tab.tab_text_index = this.containers.push(tab_text) - 1; | |
|
206 | 208 | |
|
207 | 209 | var contents_div = $('<div />', {id: uuid}) |
|
208 | 210 | .addClass('tab-pane') |
|
209 | 211 | .addClass('fade') |
|
210 | 212 | .append(view.$el) |
|
211 | 213 | .appendTo(this.$tab_contents); |
|
214 | view.parent_container = contents_div; | |
|
215 | }, | |
|
216 | ||
|
217 | update: function(options) { | |
|
218 | // Update the contents of this view | |
|
219 | // | |
|
220 | // Called when the model is changed. The model may have been | |
|
221 | // changed by another view or by a state update from the back-end. | |
|
222 | if (options === undefined || options.updated_view != this) { | |
|
223 | // Set tab titles | |
|
224 | var titles = this.model.get('_titles'); | |
|
225 | for (var page_index in titles) { | |
|
226 | var tab_text = this.containers[page_index]; | |
|
227 | if (tab_text !== undefined) { | |
|
228 | tab_text.html(titles[page_index]); | |
|
229 | } | |
|
230 | } | |
|
231 | ||
|
232 | var selected_index = this.model.get('selected_index'); | |
|
233 | if (0 <= selected_index && selected_index < this.containers.length) { | |
|
234 | this.select_page(selected_index); | |
|
235 | } | |
|
236 | } | |
|
237 | return TabView.__super__.update.apply(this); | |
|
212 | 238 | }, |
|
213 | 239 | |
|
214 | 240 | select_page: function(index) { |
@@ -27,7 +27,7 b' class AccordionWidget(DOMWidget):' | |||
|
27 | 27 | _titles = Dict(help="Titles of the pages", sync=True) |
|
28 | 28 | selected_index = Int(0, sync=True) |
|
29 | 29 | |
|
30 | children = List(Instance(DOMWidget)) | |
|
30 | children = List(Instance(DOMWidget), sync=True) | |
|
31 | 31 | |
|
32 | 32 | # Public methods |
|
33 | 33 | def set_title(self, index, title): |
General Comments 0
You need to be logged in to leave comments.
Login now