##// END OF EJS Templates
Change accordion to use a children attribute
Jason Grout -
Show More
@@ -1,180 +1,184 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 // MultiContainerWidget
9 // MultiContainerWidget
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/base"], function(widget_manager){
17 define(["notebook/js/widgets/base"], function(widget_manager){
18 var MulticontainerModel = IPython.WidgetModel.extend({});
18 var MulticontainerModel = IPython.WidgetModel.extend({});
19 widget_manager.register_widget_model('MulticontainerWidgetModel', MulticontainerModel);
19 widget_manager.register_widget_model('MulticontainerWidgetModel', MulticontainerModel);
20
20
21 var AccordionView = IPython.WidgetView.extend({
21 var AccordionView = IPython.WidgetView.extend({
22
22
23 render: function(){
23 render: function(){
24 var guid = 'accordion' + IPython.utils.uuid();
24 var guid = 'accordion' + IPython.utils.uuid();
25 this.$el
25 this.$el
26 .attr('id', guid)
26 .attr('id', guid)
27 .addClass('accordion');
27 .addClass('accordion');
28 this.containers = [];
28 this.containers = [];
29 for (var i in children) {
30 this.add_child_view(this.child_view(children[i]))
31 }
32
29 },
33 },
30 update: function() {
34 update: function() {
31 // Set tab titles
35 // Set tab titles
32 var titles = this.model.get('_titles');
36 var titles = this.model.get('_titles');
33 for (var page_index in titles) {
37 for (var page_index in titles) {
34
38
35 var accordian = this.containers[page_index];
39 var accordian = this.containers[page_index];
36 if (accordian !== undefined) {
40 if (accordian !== undefined) {
37 accordian
41 accordian
38 .find('.accordion-heading')
42 .find('.accordion-heading')
39 .find('.accordion-toggle')
43 .find('.accordion-toggle')
40 .html(titles[page_index]);
44 .html(titles[page_index]);
41 }
45 }
42 }
46 }
43
47
44 // Set selected page
48 // Set selected page
45 var selected_index = this.model.get("selected_index");
49 var selected_index = this.model.get("selected_index");
46 if (0 <= selected_index && selected_index < this.containers.length) {
50 if (0 <= selected_index && selected_index < this.containers.length) {
47 for (var index in this.containers) {
51 for (var index in this.containers) {
48 if (index==selected_index) {
52 if (index==selected_index) {
49 this.containers[index].find('.accordion-body').collapse('show');
53 this.containers[index].find('.accordion-body').collapse('show');
50 } else {
54 } else {
51 this.containers[index].find('.accordion-body').collapse('hide');
55 this.containers[index].find('.accordion-body').collapse('hide');
52 }
56 }
53
57
54 }
58 }
55 }
59 }
56
60
57 return IPython.WidgetView.prototype.update.call(this);
61 return IPython.WidgetView.prototype.update.call(this);
58 },
62 },
59
63
60 add_child_view: function(attr, view) {
64 add_child_view: function(view) {
61
65
62 var index = this.containers.length;
66 var index = this.containers.length;
63 var uuid = IPython.utils.uuid();
67 var uuid = IPython.utils.uuid();
64 var accordion_group = $('<div />')
68 var accordion_group = $('<div />')
65 .addClass('accordion-group')
69 .addClass('accordion-group')
66 .appendTo(this.$el);
70 .appendTo(this.$el);
67 var accordion_heading = $('<div />')
71 var accordion_heading = $('<div />')
68 .addClass('accordion-heading')
72 .addClass('accordion-heading')
69 .appendTo(accordion_group);
73 .appendTo(accordion_group);
70 var that = this;
74 var that = this;
71 var accordion_toggle = $('<a />')
75 var accordion_toggle = $('<a />')
72 .addClass('accordion-toggle')
76 .addClass('accordion-toggle')
73 .attr('data-toggle', 'collapse')
77 .attr('data-toggle', 'collapse')
74 .attr('data-parent', '#' + this.$el.attr('id'))
78 .attr('data-parent', '#' + this.$el.attr('id'))
75 .attr('href', '#' + uuid)
79 .attr('href', '#' + uuid)
76 .click(function(evt){
80 .click(function(evt){
77 that.model.set("selected_index", index);
81 that.model.set("selected_index", index);
78 that.touch();
82 that.touch();
79 })
83 })
80 .html('Page ' + index)
84 .html('Page ' + index)
81 .appendTo(accordion_heading);
85 .appendTo(accordion_heading);
82 var accordion_body = $('<div />', {id: uuid})
86 var accordion_body = $('<div />', {id: uuid})
83 .addClass('accordion-body collapse')
87 .addClass('accordion-body collapse')
84 .appendTo(accordion_group);
88 .appendTo(accordion_group);
85 var accordion_inner = $('<div />')
89 var accordion_inner = $('<div />')
86 .addClass('accordion-inner')
90 .addClass('accordion-inner')
87 .appendTo(accordion_body);
91 .appendTo(accordion_body);
88 this.containers.push(accordion_group);
92 this.containers.push(accordion_group);
89 accordion_inner.append(view.$el);
93 accordion_inner.append(view.$el);
90
94
91 this.update();
95 this.update();
92
96
93 // Stupid workaround to close the bootstrap accordion tabs which
97 // Stupid workaround to close the bootstrap accordion tabs which
94 // open by default even though they don't have the `in` class
98 // open by default even though they don't have the `in` class
95 // attached to them. For some reason a delay is required.
99 // attached to them. For some reason a delay is required.
96 // TODO: Better fix.
100 // TODO: Better fix.
97 setTimeout(function(){ that.update(); }, 500);
101 setTimeout(function(){ that.update(); }, 500);
98 },
102 },
99 });
103 });
100
104
101 widget_manager.register_widget_view('AccordionView', AccordionView);
105 widget_manager.register_widget_view('AccordionView', AccordionView);
102
106
103 var TabView = IPython.WidgetView.extend({
107 var TabView = IPython.WidgetView.extend({
104
108
105 initialize: function() {
109 initialize: function() {
106 this.containers = [];
110 this.containers = [];
107 IPython.WidgetView.prototype.initialize.apply(this, arguments);
111 IPython.WidgetView.prototype.initialize.apply(this, arguments);
108 },
112 },
109
113
110 render: function(){
114 render: function(){
111 var uuid = 'tabs'+IPython.utils.uuid();
115 var uuid = 'tabs'+IPython.utils.uuid();
112 var that = this;
116 var that = this;
113 this.$tabs = $('<div />', {id: uuid})
117 this.$tabs = $('<div />', {id: uuid})
114 .addClass('nav')
118 .addClass('nav')
115 .addClass('nav-tabs')
119 .addClass('nav-tabs')
116 .appendTo(this.$el);
120 .appendTo(this.$el);
117 this.$tab_contents = $('<div />', {id: uuid + 'Content'})
121 this.$tab_contents = $('<div />', {id: uuid + 'Content'})
118 .addClass('tab-content')
122 .addClass('tab-content')
119 .appendTo(this.$el);
123 .appendTo(this.$el);
120 var children = this.model.get('children');
124 var children = this.model.get('children');
121 for (var i in children) {
125 for (var i in children) {
122 this.add_child_view(this.child_view(children[i]))
126 this.add_child_view(this.child_view(children[i]))
123 }
127 }
124 this.update();
128 this.update();
125 },
129 },
126
130
127 update: function() {
131 update: function() {
128 // Set tab titles
132 // Set tab titles
129 var titles = this.model.get('_titles');
133 var titles = this.model.get('_titles');
130 for (var page_index in titles) {
134 for (var page_index in titles) {
131 var tab_text = this.containers[page_index];
135 var tab_text = this.containers[page_index];
132 if (tab_text !== undefined) {
136 if (tab_text !== undefined) {
133 tab_text.html(titles[page_index]);
137 tab_text.html(titles[page_index]);
134 }
138 }
135 }
139 }
136
140
137 var selected_index = this.model.get('selected_index');
141 var selected_index = this.model.get('selected_index');
138 if (0 <= selected_index && selected_index < this.containers.length) {
142 if (0 <= selected_index && selected_index < this.containers.length) {
139 this.select_page(selected_index);
143 this.select_page(selected_index);
140 }
144 }
141
145
142 return IPython.WidgetView.prototype.update.call(this);
146 return IPython.WidgetView.prototype.update.call(this);
143 },
147 },
144
148
145 add_child_view: function(view) {
149 add_child_view: function(view) {
146 var index = this.containers.length;
150 var index = this.containers.length;
147 var uuid = IPython.utils.uuid();
151 var uuid = IPython.utils.uuid();
148
152
149 var that = this;
153 var that = this;
150 var tab = $('<li />')
154 var tab = $('<li />')
151 .css('list-style-type', 'none')
155 .css('list-style-type', 'none')
152 .appendTo(this.$tabs);
156 .appendTo(this.$tabs);
153 var tab_text = $('<a />')
157 var tab_text = $('<a />')
154 .attr('href', '#' + uuid)
158 .attr('href', '#' + uuid)
155 .attr('data-toggle', 'tab')
159 .attr('data-toggle', 'tab')
156 .html('Page ' + index)
160 .html('Page ' + index)
157 .appendTo(tab)
161 .appendTo(tab)
158 .click(function (e) {
162 .click(function (e) {
159 that.model.set("selected_index", index);
163 that.model.set("selected_index", index);
160 that.touch();
164 that.touch();
161 that.select_page(index);
165 that.select_page(index);
162 });
166 });
163 this.containers.push(tab_text);
167 this.containers.push(tab_text);
164
168
165 var contents_div = $('<div />', {id: uuid})
169 var contents_div = $('<div />', {id: uuid})
166 .addClass('tab-pane')
170 .addClass('tab-pane')
167 .addClass('fade')
171 .addClass('fade')
168 .append(view.$el)
172 .append(view.$el)
169 .appendTo(this.$tab_contents);
173 .appendTo(this.$tab_contents);
170 },
174 },
171
175
172 select_page: function(index) {
176 select_page: function(index) {
173 this.$tabs.find('li')
177 this.$tabs.find('li')
174 .removeClass('active');
178 .removeClass('active');
175 this.containers[index].tab('show');
179 this.containers[index].tab('show');
176 },
180 },
177 });
181 });
178
182
179 widget_manager.register_widget_view('TabView', TabView);
183 widget_manager.register_widget_view('TabView', TabView);
180 });
184 });
General Comments 0
You need to be logged in to leave comments. Login now