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