Show More
@@ -226,7 +226,7 b' function(widget_manager, underscore, backbone){' | |||||
226 | // triggered on model change |
|
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 | // create and return a child view, given a model and (optionally) a view name |
|
230 | // create and return a child view, given a model and (optionally) a view name | |
231 | // if the view name is not given, it defaults to the model's default view attribute |
|
231 | // if the view name is not given, it defaults to the model's default view attribute | |
232 | var child_view = this.model.widget_manager.create_view(child_model, options); |
|
232 | var child_view = this.model.widget_manager.create_view(child_model, options); | |
@@ -234,23 +234,10 b' function(widget_manager, underscore, backbone){' | |||||
234 | return child_view; |
|
234 | return child_view; | |
235 | }, |
|
235 | }, | |
236 |
|
236 | |||
237 |
|
|
237 | delete_child_view: function(child_model, options) { | |
238 | // this function takes an old list and new list of models |
|
238 | var view = this.child_views[child_model.id]; | |
239 | // views in child_views that correspond to deleted ids are deleted |
|
239 | delete this.child_views[child_model.id]; | |
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 |
|
|
240 | 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); |
|
|||
254 | }, |
|
241 | }, | |
255 |
|
242 | |||
256 | do_diff: function(old_list, new_list, removed_callback, added_callback) { |
|
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 | update_children: function(old_list, new_list) { |
|
31 | update_children: function(old_list, new_list) { | |
32 | this.$el.empty(); |
|
32 | this.do_diff(old_list, | |
33 | this.update_child_views(old_list, new_list); |
|
33 | new_list, | |
34 | _.each(new_list, function(element, index, list) { |
|
34 | $.proxy(this.remove_child_model, this), | |
35 |
|
|
35 | $.proxy(this.add_child_model, this)); | |
36 |
|
|
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 | update: function(){ |
|
48 | update: function(){ | |
@@ -188,11 +197,20 b' define(["notebook/js/widgets/widget"], function(widget_manager) {' | |||||
188 | }, |
|
197 | }, | |
189 |
|
198 | |||
190 | update_children: function(old_list, new_list) { |
|
199 | update_children: function(old_list, new_list) { | |
191 | this.$el.empty(); |
|
200 | this.do_diff(old_list, | |
192 | this.update_child_views(old_list, new_list); |
|
201 | new_list, | |
193 | _.each(new_list, function(element, index, list) { |
|
202 | $.proxy(this.remove_child_model, this), | |
194 |
|
|
203 | $.proxy(this.add_child_model, this)); | |
195 |
|
|
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 | update: function(){ |
|
216 | update: function(){ |
@@ -23,24 +23,13 b' define(["notebook/js/widgets/widget"], function(widget_manager){' | |||||
23 | .attr('id', guid) |
|
23 | .attr('id', guid) | |
24 | .addClass('accordion'); |
|
24 | .addClass('accordion'); | |
25 | this.containers = []; |
|
25 | this.containers = []; | |
|
26 | this.model_containers = {}; | |||
26 | this.update_children([], this.model.get('children')); |
|
27 | this.update_children([], this.model.get('children')); | |
27 | this.model.on('change:children', function(model, value, options) { |
|
28 | this.model.on('change:children', function(model, value, options) { | |
28 | this.update_children(model.previous('children'), value); |
|
29 | this.update_children(model.previous('children'), value); | |
29 | }, this); |
|
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 | update: function(options) { |
|
33 | update: function(options) { | |
45 | // Update the contents of this view |
|
34 | // Update the contents of this view | |
46 | // |
|
35 | // | |
@@ -77,8 +66,23 b' define(["notebook/js/widgets/widget"], function(widget_manager){' | |||||
77 | return AccordionView.__super__.update.apply(this); |
|
66 | return AccordionView.__super__.update.apply(this); | |
78 | }, |
|
67 | }, | |
79 |
|
68 | |||
80 |
|
|
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 | }, | |||
|
75 | ||||
|
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 | var index = this.containers.length; |
|
86 | var index = this.containers.length; | |
83 | var uuid = IPython.utils.uuid(); |
|
87 | var uuid = IPython.utils.uuid(); | |
84 | var accordion_group = $('<div />') |
|
88 | var accordion_group = $('<div />') | |
@@ -108,7 +112,9 b' define(["notebook/js/widgets/widget"], function(widget_manager){' | |||||
108 | var accordion_inner = $('<div />') |
|
112 | var accordion_inner = $('<div />') | |
109 | .addClass('accordion-inner') |
|
113 | .addClass('accordion-inner') | |
110 | .appendTo(accordion_body); |
|
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 | accordion_inner.append(view.$el); |
|
118 | accordion_inner.append(view.$el); | |
113 |
|
119 | |||
114 | this.update(); |
|
120 | this.update(); | |
@@ -158,30 +164,24 b' define(["notebook/js/widgets/widget"], function(widget_manager){' | |||||
158 | }, this) |
|
164 | }, this) | |
159 | }, |
|
165 | }, | |
160 |
|
166 | |||
161 |
update: function(o |
|
167 | update_children: function(old_list, new_list) { | |
162 | // Update the contents of this view |
|
168 | this.do_diff(old_list, | |
163 |
|
|
169 | new_list, | |
164 | // Called when the model is changed. The model may have been |
|
170 | $.proxy(this.remove_child_model, this), | |
165 | // changed by another view or by a state update from the back-end. |
|
171 | $.proxy(this.add_child_model, this)); | |
166 | if (options === undefined || options.updated_view != this) { |
|
172 | }, | |
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 |
|
173 | |||
176 | var selected_index = this.model.get('selected_index'); |
|
174 | remove_child_model: function(model) { | |
177 | if (0 <= selected_index && selected_index < this.containers.length) { |
|
175 | var view = this.child_views[model.id]; | |
178 | this.select_page(selected_index); |
|
176 | this.containers.splice(view.parent_tab.tab_text_index, 1); | |
179 | } |
|
177 | view.parent_tab.remove(); | |
180 | } |
|
178 | view.parent_container.remove(); | |
181 | return TabView.__super__.update.apply(this); |
|
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 | var index = this.containers.length; |
|
185 | var index = this.containers.length; | |
186 | var uuid = IPython.utils.uuid(); |
|
186 | var uuid = IPython.utils.uuid(); | |
187 |
|
187 | |||
@@ -189,6 +189,8 b' define(["notebook/js/widgets/widget"], function(widget_manager){' | |||||
189 | var tab = $('<li />') |
|
189 | var tab = $('<li />') | |
190 | .css('list-style-type', 'none') |
|
190 | .css('list-style-type', 'none') | |
191 | .appendTo(this.$tabs); |
|
191 | .appendTo(this.$tabs); | |
|
192 | view.parent_tab = tab; | |||
|
193 | ||||
192 | var tab_text = $('<a />') |
|
194 | var tab_text = $('<a />') | |
193 | .attr('href', '#' + uuid) |
|
195 | .attr('href', '#' + uuid) | |
194 | .attr('data-toggle', 'tab') |
|
196 | .attr('data-toggle', 'tab') | |
@@ -202,13 +204,37 b' define(["notebook/js/widgets/widget"], function(widget_manager){' | |||||
202 | that.touch(); |
|
204 | that.touch(); | |
203 | that.select_page(index); |
|
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 | var contents_div = $('<div />', {id: uuid}) |
|
209 | var contents_div = $('<div />', {id: uuid}) | |
208 | .addClass('tab-pane') |
|
210 | .addClass('tab-pane') | |
209 | .addClass('fade') |
|
211 | .addClass('fade') | |
210 | .append(view.$el) |
|
212 | .append(view.$el) | |
211 | .appendTo(this.$tab_contents); |
|
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 | select_page: function(index) { |
|
240 | select_page: function(index) { |
@@ -27,7 +27,7 b' class AccordionWidget(DOMWidget):' | |||||
27 | _titles = Dict(help="Titles of the pages", sync=True) |
|
27 | _titles = Dict(help="Titles of the pages", sync=True) | |
28 | selected_index = Int(0, sync=True) |
|
28 | selected_index = Int(0, sync=True) | |
29 |
|
29 | |||
30 | children = List(Instance(DOMWidget)) |
|
30 | children = List(Instance(DOMWidget), sync=True) | |
31 |
|
31 | |||
32 | # Public methods |
|
32 | # Public methods | |
33 | def set_title(self, index, title): |
|
33 | def set_title(self, index, title): |
General Comments 0
You need to be logged in to leave comments.
Login now