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