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