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