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