##// END OF EJS Templates
Fix bug in all children containing views
Jonathan Frederic -
Show More
@@ -1,277 +1,277 b''
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 // ContainerWidget
9 // ContainerWidget
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
18
19 var ContainerView = IPython.DOMWidgetView.extend({
19 var ContainerView = IPython.DOMWidgetView.extend({
20 render: function(){
20 render: function(){
21 // Called when view is rendered.
21 // Called when view is rendered.
22 this.$el
22 this.$el
23 .addClass('widget-container');
23 .addClass('widget-container');
24 this.children={};
24 this.children={};
25 this.update_children([], this.model.get('children'));
25 this.update_children([], this.model.get('_children'));
26 this.model.on('change:children', function(model, value, options) {
26 this.model.on('change:_children', function(model, value, options) {
27 this.update_children(model.previous('children'), value);
27 this.update_children(model.previous('_children'), value);
28 }, this);
28 }, this);
29 this.update()
29 this.update()
30 },
30 },
31
31
32 update_children: function(old_list, new_list) {
32 update_children: function(old_list, new_list) {
33 // Called when the children list changes.
33 // Called when the children list changes.
34 this.do_diff(old_list,
34 this.do_diff(old_list,
35 new_list,
35 new_list,
36 $.proxy(this.remove_child_model, this),
36 $.proxy(this.remove_child_model, this),
37 $.proxy(this.add_child_model, this));
37 $.proxy(this.add_child_model, this));
38 },
38 },
39
39
40 remove_child_model: function(model) {
40 remove_child_model: function(model) {
41 // Called when a model is removed from the children list.
41 // Called when a model is removed from the children list.
42 this.child_views[model.id].remove();
42 this.child_views[model.id].remove();
43 this.delete_child_view(model);
43 this.delete_child_view(model);
44 },
44 },
45
45
46 add_child_model: function(model) {
46 add_child_model: function(model) {
47 // Called when a model is added to the children list.
47 // Called when a model is added to the children list.
48 var view = this.create_child_view(model);
48 var view = this.create_child_view(model);
49 this.$el.append(view.$el);
49 this.$el.append(view.$el);
50 },
50 },
51
51
52 update: function(){
52 update: function(){
53 // Update the contents of this view
53 // Update the contents of this view
54 //
54 //
55 // Called when the model is changed. The model may have been
55 // Called when the model is changed. The model may have been
56 // changed by another view or by a state update from the back-end.
56 // changed by another view or by a state update from the back-end.
57 return ContainerView.__super__.update.apply(this);
57 return ContainerView.__super__.update.apply(this);
58 },
58 },
59 });
59 });
60 widget_manager.register_widget_view('ContainerView', ContainerView);
60 widget_manager.register_widget_view('ContainerView', ContainerView);
61
61
62
62
63 var ModalView = IPython.DOMWidgetView.extend({
63 var ModalView = IPython.DOMWidgetView.extend({
64 render: function(){
64 render: function(){
65 // Called when view is rendered.
65 // Called when view is rendered.
66 var that = this;
66 var that = this;
67 this.children={};
67 this.children={};
68 this.update_children([], this.model.get('children'));
68 this.update_children([], this.model.get('_children'));
69 this.model.on('change:children', function(model, value, options) {
69 this.model.on('change:_children', function(model, value, options) {
70 this.update_children(model.previous('children'), value);
70 this.update_children(model.previous('_children'), value);
71 }, this);
71 }, this);
72
72
73 this.$el
73 this.$el
74 .html('')
74 .html('')
75 .on("remove", function(){
75 .on("remove", function(){
76 that.$window.remove();
76 that.$window.remove();
77 });
77 });
78 this.$window = $('<div />')
78 this.$window = $('<div />')
79 .addClass('modal widget-modal')
79 .addClass('modal widget-modal')
80 .appendTo($('#notebook-container'))
80 .appendTo($('#notebook-container'))
81 .mousedown(function(){
81 .mousedown(function(){
82 that.bring_to_front();
82 that.bring_to_front();
83 });
83 });
84 this.$title_bar = $('<div />')
84 this.$title_bar = $('<div />')
85 .addClass('popover-title')
85 .addClass('popover-title')
86 .appendTo(this.$window)
86 .appendTo(this.$window)
87 .mousedown(function(){
87 .mousedown(function(){
88 that.bring_to_front();
88 that.bring_to_front();
89 });
89 });
90 this.$close = $('<button />')
90 this.$close = $('<button />')
91 .addClass('close icon-remove')
91 .addClass('close icon-remove')
92 .css('margin-left', '5px')
92 .css('margin-left', '5px')
93 .appendTo(this.$title_bar)
93 .appendTo(this.$title_bar)
94 .click(function(){
94 .click(function(){
95 that.hide();
95 that.hide();
96 event.stopPropagation();
96 event.stopPropagation();
97 });
97 });
98 this.$minimize = $('<button />')
98 this.$minimize = $('<button />')
99 .addClass('close icon-arrow-down')
99 .addClass('close icon-arrow-down')
100 .appendTo(this.$title_bar)
100 .appendTo(this.$title_bar)
101 .click(function(){
101 .click(function(){
102 that.popped_out = !that.popped_out;
102 that.popped_out = !that.popped_out;
103 if (!that.popped_out) {
103 if (!that.popped_out) {
104 that.$minimize
104 that.$minimize
105 .removeClass('icon-arrow-down')
105 .removeClass('icon-arrow-down')
106 .addClass('icon-arrow-up');
106 .addClass('icon-arrow-up');
107
107
108 that.$window
108 that.$window
109 .draggable('destroy')
109 .draggable('destroy')
110 .resizable('destroy')
110 .resizable('destroy')
111 .removeClass('widget-modal modal')
111 .removeClass('widget-modal modal')
112 .addClass('docked-widget-modal')
112 .addClass('docked-widget-modal')
113 .detach()
113 .detach()
114 .insertBefore(that.$show_button);
114 .insertBefore(that.$show_button);
115 that.$show_button.hide();
115 that.$show_button.hide();
116 that.$close.hide();
116 that.$close.hide();
117 } else {
117 } else {
118 that.$minimize
118 that.$minimize
119 .addClass('icon-arrow-down')
119 .addClass('icon-arrow-down')
120 .removeClass('icon-arrow-up');
120 .removeClass('icon-arrow-up');
121
121
122 that.$window
122 that.$window
123 .removeClass('docked-widget-modal')
123 .removeClass('docked-widget-modal')
124 .addClass('widget-modal modal')
124 .addClass('widget-modal modal')
125 .detach()
125 .detach()
126 .appendTo($('#notebook-container'))
126 .appendTo($('#notebook-container'))
127 .draggable({handle: '.popover-title', snap: '#notebook, .modal', snapMode: 'both'})
127 .draggable({handle: '.popover-title', snap: '#notebook, .modal', snapMode: 'both'})
128 .resizable()
128 .resizable()
129 .children('.ui-resizable-handle').show();
129 .children('.ui-resizable-handle').show();
130 that.show();
130 that.show();
131 that.$show_button.show();
131 that.$show_button.show();
132 that.$close.show();
132 that.$close.show();
133 }
133 }
134 event.stopPropagation();
134 event.stopPropagation();
135 });
135 });
136 this.$title = $('<div />')
136 this.$title = $('<div />')
137 .addClass('widget-modal-title')
137 .addClass('widget-modal-title')
138 .html('&nbsp;')
138 .html('&nbsp;')
139 .appendTo(this.$title_bar);
139 .appendTo(this.$title_bar);
140 this.$body = $('<div />')
140 this.$body = $('<div />')
141 .addClass('modal-body')
141 .addClass('modal-body')
142 .addClass('widget-modal-body')
142 .addClass('widget-modal-body')
143 .addClass('widget-container')
143 .addClass('widget-container')
144 .appendTo(this.$window);
144 .appendTo(this.$window);
145
145
146 this.$show_button = $('<button />')
146 this.$show_button = $('<button />')
147 .html('&nbsp;')
147 .html('&nbsp;')
148 .addClass('btn btn-info widget-modal-show')
148 .addClass('btn btn-info widget-modal-show')
149 .appendTo(this.$el)
149 .appendTo(this.$el)
150 .click(function(){
150 .click(function(){
151 that.show();
151 that.show();
152 });
152 });
153
153
154 this.$window.draggable({handle: '.popover-title', snap: '#notebook, .modal', snapMode: 'both'});
154 this.$window.draggable({handle: '.popover-title', snap: '#notebook, .modal', snapMode: 'both'});
155 this.$window.resizable();
155 this.$window.resizable();
156 this.$window.on('resize', function(){
156 this.$window.on('resize', function(){
157 that.$body.outerHeight(that.$window.innerHeight() - that.$title_bar.outerHeight());
157 that.$body.outerHeight(that.$window.innerHeight() - that.$title_bar.outerHeight());
158 });
158 });
159
159
160 this.$el_to_style = this.$body;
160 this.$el_to_style = this.$body;
161 this._shown_once = false;
161 this._shown_once = false;
162 this.popped_out = true;
162 this.popped_out = true;
163 },
163 },
164
164
165 hide: function() {
165 hide: function() {
166 // Called when the modal hide button is clicked.
166 // Called when the modal hide button is clicked.
167 this.$window.hide();
167 this.$window.hide();
168 this.$show_button.removeClass('btn-info');
168 this.$show_button.removeClass('btn-info');
169 },
169 },
170
170
171 show: function() {
171 show: function() {
172 // Called when the modal show button is clicked.
172 // Called when the modal show button is clicked.
173 this.$show_button.addClass('btn-info');
173 this.$show_button.addClass('btn-info');
174 this.$window.show();
174 this.$window.show();
175 if (this.popped_out) {
175 if (this.popped_out) {
176 this.$window.css("positon", "absolute");
176 this.$window.css("positon", "absolute");
177 this.$window.css("top", "0px");
177 this.$window.css("top", "0px");
178 this.$window.css("left", Math.max(0, (($('body').outerWidth() - this.$window.outerWidth()) / 2) +
178 this.$window.css("left", Math.max(0, (($('body').outerWidth() - this.$window.outerWidth()) / 2) +
179 $(window).scrollLeft()) + "px");
179 $(window).scrollLeft()) + "px");
180 this.bring_to_front();
180 this.bring_to_front();
181 }
181 }
182 },
182 },
183
183
184 bring_to_front: function() {
184 bring_to_front: function() {
185 // Make the modal top-most, z-ordered about the other modals.
185 // Make the modal top-most, z-ordered about the other modals.
186 var $widget_modals = $(".widget-modal");
186 var $widget_modals = $(".widget-modal");
187 var max_zindex = 0;
187 var max_zindex = 0;
188 $widget_modals.each(function (index, el){
188 $widget_modals.each(function (index, el){
189 max_zindex = Math.max(max_zindex, parseInt($(el).css('z-index')));
189 max_zindex = Math.max(max_zindex, parseInt($(el).css('z-index')));
190 });
190 });
191
191
192 // Start z-index of widget modals at 2000
192 // Start z-index of widget modals at 2000
193 max_zindex = Math.max(max_zindex, 2000);
193 max_zindex = Math.max(max_zindex, 2000);
194
194
195 $widget_modals.each(function (index, el){
195 $widget_modals.each(function (index, el){
196 $el = $(el);
196 $el = $(el);
197 if (max_zindex == parseInt($el.css('z-index'))) {
197 if (max_zindex == parseInt($el.css('z-index'))) {
198 $el.css('z-index', max_zindex - 1);
198 $el.css('z-index', max_zindex - 1);
199 }
199 }
200 });
200 });
201 this.$window.css('z-index', max_zindex);
201 this.$window.css('z-index', max_zindex);
202 },
202 },
203
203
204 update_children: function(old_list, new_list) {
204 update_children: function(old_list, new_list) {
205 // Called when the children list is modified.
205 // Called when the children list is modified.
206 this.do_diff(old_list,
206 this.do_diff(old_list,
207 new_list,
207 new_list,
208 $.proxy(this.remove_child_model, this),
208 $.proxy(this.remove_child_model, this),
209 $.proxy(this.add_child_model, this));
209 $.proxy(this.add_child_model, this));
210 },
210 },
211
211
212 remove_child_model: function(model) {
212 remove_child_model: function(model) {
213 // Called when a child is removed from children list.
213 // Called when a child is removed from children list.
214 this.child_views[model.id].remove();
214 this.child_views[model.id].remove();
215 this.delete_child_view(model);
215 this.delete_child_view(model);
216 },
216 },
217
217
218 add_child_model: function(model) {
218 add_child_model: function(model) {
219 // Called when a child is added to children list.
219 // Called when a child is added to children list.
220 var view = this.create_child_view(model);
220 var view = this.create_child_view(model);
221 this.$body.append(view.$el);
221 this.$body.append(view.$el);
222 },
222 },
223
223
224 update: function(){
224 update: function(){
225 // Update the contents of this view
225 // Update the contents of this view
226 //
226 //
227 // Called when the model is changed. The model may have been
227 // Called when the model is changed. The model may have been
228 // changed by another view or by a state update from the back-end.
228 // changed by another view or by a state update from the back-end.
229 var description = this.model.get('description');
229 var description = this.model.get('description');
230 description = description.replace(/ /g, '&nbsp;', 'm');
230 description = description.replace(/ /g, '&nbsp;', 'm');
231 description = description.replace(/\n/g, '<br>\n', 'm');
231 description = description.replace(/\n/g, '<br>\n', 'm');
232 if (description.length === 0) {
232 if (description.length === 0) {
233 this.$title.html('&nbsp;'); // Preserve title height
233 this.$title.html('&nbsp;'); // Preserve title height
234 } else {
234 } else {
235 this.$title.html(description);
235 this.$title.html(description);
236 }
236 }
237
237
238 var button_text = this.model.get('button_text');
238 var button_text = this.model.get('button_text');
239 button_text = button_text.replace(/ /g, '&nbsp;', 'm');
239 button_text = button_text.replace(/ /g, '&nbsp;', 'm');
240 button_text = button_text.replace(/\n/g, '<br>\n', 'm');
240 button_text = button_text.replace(/\n/g, '<br>\n', 'm');
241 if (button_text.length === 0) {
241 if (button_text.length === 0) {
242 this.$show_button.html('&nbsp;'); // Preserve button height
242 this.$show_button.html('&nbsp;'); // Preserve button height
243 } else {
243 } else {
244 this.$show_button.html(button_text);
244 this.$show_button.html(button_text);
245 }
245 }
246
246
247 if (!this._shown_once) {
247 if (!this._shown_once) {
248 this._shown_once = true;
248 this._shown_once = true;
249 this.show();
249 this.show();
250 }
250 }
251
251
252 return ModalView.__super__.update.apply(this);
252 return ModalView.__super__.update.apply(this);
253 },
253 },
254
254
255 _get_selector_element: function(selector) {
255 _get_selector_element: function(selector) {
256 // Get an element view a 'special' jquery selector. (see widget.js)
256 // Get an element view a 'special' jquery selector. (see widget.js)
257 //
257 //
258 // Since the modal actually isn't within the $el in the DOM, we need to extend
258 // Since the modal actually isn't within the $el in the DOM, we need to extend
259 // the selector logic to allow the user to set css on the modal if need be.
259 // the selector logic to allow the user to set css on the modal if need be.
260 // The convention used is:
260 // The convention used is:
261 // "modal" - select the modal div
261 // "modal" - select the modal div
262 // "modal [selector]" - select element(s) within the modal div.
262 // "modal [selector]" - select element(s) within the modal div.
263 // "[selector]" - select elements within $el
263 // "[selector]" - select elements within $el
264 // "" - select the $el_to_style
264 // "" - select the $el_to_style
265 if (selector.substring(0, 5) == 'modal') {
265 if (selector.substring(0, 5) == 'modal') {
266 if (selector == 'modal') {
266 if (selector == 'modal') {
267 return this.$window;
267 return this.$window;
268 } else {
268 } else {
269 return this.$window.find(selector.substring(6));
269 return this.$window.find(selector.substring(6));
270 }
270 }
271 } else {
271 } else {
272 return ModalView.__super__._get_selector_element.apply(this, [selector]);
272 return ModalView.__super__._get_selector_element.apply(this, [selector]);
273 }
273 }
274 },
274 },
275 });
275 });
276 widget_manager.register_widget_view('ModalView', ModalView);
276 widget_manager.register_widget_view('ModalView', ModalView);
277 });
277 });
@@ -1,244 +1,244 b''
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 // SelectionContainerWidget
9 // SelectionContainerWidget
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
18
19 var AccordionView = IPython.DOMWidgetView.extend({
19 var AccordionView = IPython.DOMWidgetView.extend({
20 render: function(){
20 render: function(){
21 // Called when view is rendered.
21 // Called when view is rendered.
22 var guid = 'accordion' + IPython.utils.uuid();
22 var guid = 'accordion' + IPython.utils.uuid();
23 this.$el
23 this.$el
24 .attr('id', guid)
24 .attr('id', guid)
25 .addClass('accordion');
25 .addClass('accordion');
26 this.containers = [];
26 this.containers = [];
27 this.model_containers = {};
27 this.model_containers = {};
28 this.update_children([], this.model.get('children'));
28 this.update_children([], this.model.get('_children'));
29 this.model.on('change:children', function(model, value, options) {
29 this.model.on('change:_children', function(model, value, options) {
30 this.update_children(model.previous('children'), value);
30 this.update_children(model.previous('_children'), value);
31 }, this);
31 }, this);
32 },
32 },
33
33
34 update: function(options) {
34 update: function(options) {
35 // Update the contents of this view
35 // Update the contents of this view
36 //
36 //
37 // Called when the model is changed. The model may have been
37 // Called when the model is changed. The model may have been
38 // changed by another view or by a state update from the back-end.
38 // changed by another view or by a state update from the back-end.
39 if (options === undefined || options.updated_view != this) {
39 if (options === undefined || options.updated_view != this) {
40 // Set tab titles
40 // Set tab titles
41 var titles = this.model.get('_titles');
41 var titles = this.model.get('_titles');
42 for (var page_index in titles) {
42 for (var page_index in titles) {
43
43
44 var accordian = this.containers[page_index];
44 var accordian = this.containers[page_index];
45 if (accordian !== undefined) {
45 if (accordian !== undefined) {
46 accordian
46 accordian
47 .find('.accordion-heading')
47 .find('.accordion-heading')
48 .find('.accordion-toggle')
48 .find('.accordion-toggle')
49 .html(titles[page_index]);
49 .html(titles[page_index]);
50 }
50 }
51 }
51 }
52
52
53 // Set selected page
53 // Set selected page
54 var selected_index = this.model.get("selected_index");
54 var selected_index = this.model.get("selected_index");
55 if (0 <= selected_index && selected_index < this.containers.length) {
55 if (0 <= selected_index && selected_index < this.containers.length) {
56 for (var index in this.containers) {
56 for (var index in this.containers) {
57 if (index==selected_index) {
57 if (index==selected_index) {
58 this.containers[index].find('.accordion-body').collapse('show');
58 this.containers[index].find('.accordion-body').collapse('show');
59 } else {
59 } else {
60 this.containers[index].find('.accordion-body').collapse('hide');
60 this.containers[index].find('.accordion-body').collapse('hide');
61 }
61 }
62
62
63 }
63 }
64 }
64 }
65 }
65 }
66 return AccordionView.__super__.update.apply(this);
66 return AccordionView.__super__.update.apply(this);
67 },
67 },
68
68
69 update_children: function(old_list, new_list) {
69 update_children: function(old_list, new_list) {
70 // Called when the children list is modified.
70 // Called when the children list is modified.
71 this.do_diff(old_list,
71 this.do_diff(old_list,
72 new_list,
72 new_list,
73 $.proxy(this.remove_child_model, this),
73 $.proxy(this.remove_child_model, this),
74 $.proxy(this.add_child_model, this));
74 $.proxy(this.add_child_model, this));
75 },
75 },
76
76
77 remove_child_model: function(model) {
77 remove_child_model: function(model) {
78 // Called when a child is removed from children list.
78 // Called when a child is removed from children list.
79 var accordion_group = this.model_containers[model.id];
79 var accordion_group = this.model_containers[model.id];
80 this.containers.splice(accordion_group.container_index, 1);
80 this.containers.splice(accordion_group.container_index, 1);
81 delete this.model_containers[model.id];
81 delete this.model_containers[model.id];
82 accordion_group.remove();
82 accordion_group.remove();
83 this.delete_child_view(model);
83 this.delete_child_view(model);
84 },
84 },
85
85
86 add_child_model: function(model) {
86 add_child_model: function(model) {
87 // Called when a child is added to children list.
87 // Called when a child is added to children list.
88 var view = this.create_child_view(model);
88 var view = this.create_child_view(model);
89 var index = this.containers.length;
89 var index = this.containers.length;
90 var uuid = IPython.utils.uuid();
90 var uuid = IPython.utils.uuid();
91 var accordion_group = $('<div />')
91 var accordion_group = $('<div />')
92 .addClass('accordion-group')
92 .addClass('accordion-group')
93 .appendTo(this.$el);
93 .appendTo(this.$el);
94 var accordion_heading = $('<div />')
94 var accordion_heading = $('<div />')
95 .addClass('accordion-heading')
95 .addClass('accordion-heading')
96 .appendTo(accordion_group);
96 .appendTo(accordion_group);
97 var that = this;
97 var that = this;
98 var accordion_toggle = $('<a />')
98 var accordion_toggle = $('<a />')
99 .addClass('accordion-toggle')
99 .addClass('accordion-toggle')
100 .attr('data-toggle', 'collapse')
100 .attr('data-toggle', 'collapse')
101 .attr('data-parent', '#' + this.$el.attr('id'))
101 .attr('data-parent', '#' + this.$el.attr('id'))
102 .attr('href', '#' + uuid)
102 .attr('href', '#' + uuid)
103 .click(function(evt){
103 .click(function(evt){
104
104
105 // Calling model.set will trigger all of the other views of the
105 // Calling model.set will trigger all of the other views of the
106 // model to update.
106 // model to update.
107 that.model.set("selected_index", index, {updated_view: this});
107 that.model.set("selected_index", index, {updated_view: this});
108 that.touch();
108 that.touch();
109 })
109 })
110 .html('Page ' + index)
110 .html('Page ' + index)
111 .appendTo(accordion_heading);
111 .appendTo(accordion_heading);
112 var accordion_body = $('<div />', {id: uuid})
112 var accordion_body = $('<div />', {id: uuid})
113 .addClass('accordion-body collapse')
113 .addClass('accordion-body collapse')
114 .appendTo(accordion_group);
114 .appendTo(accordion_group);
115 var accordion_inner = $('<div />')
115 var accordion_inner = $('<div />')
116 .addClass('accordion-inner')
116 .addClass('accordion-inner')
117 .appendTo(accordion_body);
117 .appendTo(accordion_body);
118 var container_index = this.containers.push(accordion_group) - 1;
118 var container_index = this.containers.push(accordion_group) - 1;
119 accordion_group.container_index = container_index;
119 accordion_group.container_index = container_index;
120 this.model_containers[model.id] = accordion_group;
120 this.model_containers[model.id] = accordion_group;
121 accordion_inner.append(view.$el);
121 accordion_inner.append(view.$el);
122
122
123 this.update();
123 this.update();
124
124
125 // Stupid workaround to close the bootstrap accordion tabs which
125 // Stupid workaround to close the bootstrap accordion tabs which
126 // open by default even though they don't have the `in` class
126 // open by default even though they don't have the `in` class
127 // attached to them. For some reason a delay is required.
127 // attached to them. For some reason a delay is required.
128 // TODO: Better fix.
128 // TODO: Better fix.
129 setTimeout(function(){ that.update(); }, 500);
129 setTimeout(function(){ that.update(); }, 500);
130 },
130 },
131 });
131 });
132 widget_manager.register_widget_view('AccordionView', AccordionView);
132 widget_manager.register_widget_view('AccordionView', AccordionView);
133
133
134
134
135 var TabView = IPython.DOMWidgetView.extend({
135 var TabView = IPython.DOMWidgetView.extend({
136 initialize: function() {
136 initialize: function() {
137 // Public constructor.
137 // Public constructor.
138 this.containers = [];
138 this.containers = [];
139 TabView.__super__.initialize.apply(this, arguments);
139 TabView.__super__.initialize.apply(this, arguments);
140 },
140 },
141
141
142 render: function(){
142 render: function(){
143 // Called when view is rendered.
143 // Called when view is rendered.
144 var uuid = 'tabs'+IPython.utils.uuid();
144 var uuid = 'tabs'+IPython.utils.uuid();
145 var that = this;
145 var that = this;
146 this.$tabs = $('<div />', {id: uuid})
146 this.$tabs = $('<div />', {id: uuid})
147 .addClass('nav')
147 .addClass('nav')
148 .addClass('nav-tabs')
148 .addClass('nav-tabs')
149 .appendTo(this.$el);
149 .appendTo(this.$el);
150 this.$tab_contents = $('<div />', {id: uuid + 'Content'})
150 this.$tab_contents = $('<div />', {id: uuid + 'Content'})
151 .addClass('tab-content')
151 .addClass('tab-content')
152 .appendTo(this.$el);
152 .appendTo(this.$el);
153 this.containers = [];
153 this.containers = [];
154 this.update_children([], this.model.get('children'));
154 this.update_children([], this.model.get('_children'));
155 this.model.on('change:children', function(model, value, options) {
155 this.model.on('change:_children', function(model, value, options) {
156 this.update_children(model.previous('children'), value);
156 this.update_children(model.previous('_children'), value);
157 }, this);
157 }, this);
158 },
158 },
159
159
160 update_children: function(old_list, new_list) {
160 update_children: function(old_list, new_list) {
161 // Called when the children list is modified.
161 // Called when the children list is modified.
162 this.do_diff(old_list,
162 this.do_diff(old_list,
163 new_list,
163 new_list,
164 $.proxy(this.remove_child_model, this),
164 $.proxy(this.remove_child_model, this),
165 $.proxy(this.add_child_model, this));
165 $.proxy(this.add_child_model, this));
166 },
166 },
167
167
168 remove_child_model: function(model) {
168 remove_child_model: function(model) {
169 // Called when a child is removed from children list.
169 // Called when a child is removed from children list.
170 var view = this.child_views[model.id];
170 var view = this.child_views[model.id];
171 this.containers.splice(view.parent_tab.tab_text_index, 1);
171 this.containers.splice(view.parent_tab.tab_text_index, 1);
172 view.parent_tab.remove();
172 view.parent_tab.remove();
173 view.parent_container.remove();
173 view.parent_container.remove();
174 view.remove();
174 view.remove();
175 this.delete_child_view(model);
175 this.delete_child_view(model);
176 },
176 },
177
177
178 add_child_model: function(model) {
178 add_child_model: function(model) {
179 // Called when a child is added to children list.
179 // Called when a child is added to children list.
180 var view = this.create_child_view(model);
180 var view = this.create_child_view(model);
181 var index = this.containers.length;
181 var index = this.containers.length;
182 var uuid = IPython.utils.uuid();
182 var uuid = IPython.utils.uuid();
183
183
184 var that = this;
184 var that = this;
185 var tab = $('<li />')
185 var tab = $('<li />')
186 .css('list-style-type', 'none')
186 .css('list-style-type', 'none')
187 .appendTo(this.$tabs);
187 .appendTo(this.$tabs);
188 view.parent_tab = tab;
188 view.parent_tab = tab;
189
189
190 var tab_text = $('<a />')
190 var tab_text = $('<a />')
191 .attr('href', '#' + uuid)
191 .attr('href', '#' + uuid)
192 .attr('data-toggle', 'tab')
192 .attr('data-toggle', 'tab')
193 .html('Page ' + index)
193 .html('Page ' + index)
194 .appendTo(tab)
194 .appendTo(tab)
195 .click(function (e) {
195 .click(function (e) {
196
196
197 // Calling model.set will trigger all of the other views of the
197 // Calling model.set will trigger all of the other views of the
198 // model to update.
198 // model to update.
199 that.model.set("selected_index", index, {updated_view: this});
199 that.model.set("selected_index", index, {updated_view: this});
200 that.touch();
200 that.touch();
201 that.select_page(index);
201 that.select_page(index);
202 });
202 });
203 tab.tab_text_index = this.containers.push(tab_text) - 1;
203 tab.tab_text_index = this.containers.push(tab_text) - 1;
204
204
205 var contents_div = $('<div />', {id: uuid})
205 var contents_div = $('<div />', {id: uuid})
206 .addClass('tab-pane')
206 .addClass('tab-pane')
207 .addClass('fade')
207 .addClass('fade')
208 .append(view.$el)
208 .append(view.$el)
209 .appendTo(this.$tab_contents);
209 .appendTo(this.$tab_contents);
210 view.parent_container = contents_div;
210 view.parent_container = contents_div;
211 },
211 },
212
212
213 update: function(options) {
213 update: function(options) {
214 // Update the contents of this view
214 // Update the contents of this view
215 //
215 //
216 // Called when the model is changed. The model may have been
216 // Called when the model is changed. The model may have been
217 // changed by another view or by a state update from the back-end.
217 // changed by another view or by a state update from the back-end.
218 if (options === undefined || options.updated_view != this) {
218 if (options === undefined || options.updated_view != this) {
219 // Set tab titles
219 // Set tab titles
220 var titles = this.model.get('_titles');
220 var titles = this.model.get('_titles');
221 for (var page_index in titles) {
221 for (var page_index in titles) {
222 var tab_text = this.containers[page_index];
222 var tab_text = this.containers[page_index];
223 if (tab_text !== undefined) {
223 if (tab_text !== undefined) {
224 tab_text.html(titles[page_index]);
224 tab_text.html(titles[page_index]);
225 }
225 }
226 }
226 }
227
227
228 var selected_index = this.model.get('selected_index');
228 var selected_index = this.model.get('selected_index');
229 if (0 <= selected_index && selected_index < this.containers.length) {
229 if (0 <= selected_index && selected_index < this.containers.length) {
230 this.select_page(selected_index);
230 this.select_page(selected_index);
231 }
231 }
232 }
232 }
233 return TabView.__super__.update.apply(this);
233 return TabView.__super__.update.apply(this);
234 },
234 },
235
235
236 select_page: function(index) {
236 select_page: function(index) {
237 // Select a page.
237 // Select a page.
238 this.$tabs.find('li')
238 this.$tabs.find('li')
239 .removeClass('active');
239 .removeClass('active');
240 this.containers[index].tab('show');
240 this.containers[index].tab('show');
241 },
241 },
242 });
242 });
243 widget_manager.register_widget_view('TabView', TabView);
243 widget_manager.register_widget_view('TabView', TabView);
244 });
244 });
General Comments 0
You need to be logged in to leave comments. Login now