##// END OF EJS Templates
Added TabView to multicontainer
Jonathan Frederic -
Show More
@@ -1,61 +1,126 b''
1 require(["notebook/js/widget"], function(){
1 require(["notebook/js/widget"], function(){
2 var MulticontainerModel = IPython.WidgetModel.extend({});
2 var MulticontainerModel = IPython.WidgetModel.extend({});
3 IPython.notebook.widget_manager.register_widget_model('MulticontainerWidgetModel', MulticontainerModel);
3 IPython.notebook.widget_manager.register_widget_model('MulticontainerWidgetModel', MulticontainerModel);
4
4
5 var AccordionView = IPython.WidgetView.extend({
5 var AccordionView = IPython.WidgetView.extend({
6
6
7 render: function(){
7 render: function(){
8 this.$el = $('<div />', {id: IPython.utils.uuid()})
8 this.$el = $('<div />', {id: IPython.utils.uuid()})
9 .addClass('accordion');
9 .addClass('accordion');
10 this.containers = [];
10 this.containers = [];
11 },
11 },
12
12
13 update: function() {
13 update: function() {
14 // Set tab titles
14 // Set tab titles
15 var titles = this.model.get('_titles');
15 var titles = this.model.get('_titles');
16 for (var page_index in titles) {
16 for (var page_index in titles) {
17
17
18 var accordian = this.containers[page_index]
18 var accordian = this.containers[page_index]
19 if (accordian != undefined) {
19 if (accordian != undefined) {
20 accordian
20 accordian
21 .find('.accordion-heading')
21 .find('.accordion-heading')
22 .find('.accordion-toggle')
22 .find('.accordion-toggle')
23 .html(titles[page_index]);
23 .html(titles[page_index]);
24 }
24 }
25 }
25 }
26
26
27 return IPython.WidgetView.prototype.update.call(this);
27 return IPython.WidgetView.prototype.update.call(this);
28 },
28 },
29
29
30 display_child: function(view) {
30 display_child: function(view) {
31
31
32 var index = this.containers.length;
32 var index = this.containers.length;
33 var uuid = IPython.utils.uuid();
33 var uuid = IPython.utils.uuid();
34 var accordion_group = $('<div />')
34 var accordion_group = $('<div />')
35 .addClass('accordion-group')
35 .addClass('accordion-group')
36 .appendTo(this.$el);
36 .appendTo(this.$el);
37 var accordion_heading = $('<div />')
37 var accordion_heading = $('<div />')
38 .addClass('accordion-heading')
38 .addClass('accordion-heading')
39 .appendTo(accordion_group);
39 .appendTo(accordion_group);
40 var accordion_toggle = $('<a />')
40 var accordion_toggle = $('<a />')
41 .addClass('accordion-toggle')
41 .addClass('accordion-toggle')
42 .attr('data-toggle', 'collapse')
42 .attr('data-toggle', 'collapse')
43 .attr('data-parent', '#' + this.$el.attr('id'))
43 .attr('data-parent', '#' + this.$el.attr('id'))
44 .attr('href', '#' + uuid)
44 .attr('href', '#' + uuid)
45 .html('Page ' + index)
45 .html('Page ' + index)
46 .appendTo(accordion_heading);
46 .appendTo(accordion_heading);
47 var accordion_body = $('<div />', {id: uuid})
47 var accordion_body = $('<div />', {id: uuid})
48 .addClass('accordion-body collapse')
48 .addClass('accordion-body collapse')
49 .appendTo(accordion_group);
49 .appendTo(accordion_group);
50 var accordion_inner = $('<div />')
50 var accordion_inner = $('<div />')
51 .addClass('accordion-inner')
51 .addClass('accordion-inner')
52 .appendTo(accordion_body);
52 .appendTo(accordion_body);
53 this.containers.push(accordion_group);
53 this.containers.push(accordion_group);
54
54
55 accordion_inner.append(view.$el);
55 accordion_inner.append(view.$el);
56 this.update();
56 this.update();
57 },
57 },
58 });
58 });
59
59
60 IPython.notebook.widget_manager.register_widget_view('AccordionView', AccordionView);
60 IPython.notebook.widget_manager.register_widget_view('AccordionView', AccordionView);
61 }); No newline at end of file
61
62 var TabView = IPython.WidgetView.extend({
63
64 render: function(){
65 this.$el = $('<div />');
66 var uuid = IPython.utils.uuid();
67 this.$tabs = $('<div />', {id: uuid})
68 .addClass('nav')
69 .addClass('nav-tabs')
70 .appendTo(this.$el);
71 this.$tab_contents = $('<div />', {id: uuid + 'Content'})
72 .addClass('tab-content')
73 .appendTo(this.$el);
74
75 this.containers = [];
76 },
77
78 update: function() {
79 // Set tab titles
80 var titles = this.model.get('_titles');
81 for (var page_index in titles) {
82 var tab_text = this.containers[page_index]
83 if (tab_text != undefined) {
84 tab_text.html(titles[page_index]);
85 }
86 }
87
88 return IPython.WidgetView.prototype.update.call(this);
89 },
90
91 display_child: function(view) {
92
93 var index = this.containers.length;
94 var uuid = IPython.utils.uuid();
95
96 var that = this;
97 var tab = $('<li />')
98 .css('list-style-type', 'none')
99 .appendTo(this.$tabs);
100 var tab_text = $('<a />')
101 .attr('href', '#' + uuid)
102 .attr('data-toggle', 'tab')
103 .html('Page ' + index)
104 .appendTo(tab)
105 .click(function (e) {
106 that.$tabs.find('li')
107 .removeClass('active');
108 });
109 this.containers.push(tab_text);
110
111 var contents_div = $('<div />', {id: uuid})
112 .addClass('tab-pane')
113 .addClass('fade')
114 .append(view.$el)
115 .appendTo(this.$tab_contents);
116
117 if (index==0) {
118 tab_text.tab('show');
119 }
120 this.update();
121 },
122 });
123
124 IPython.notebook.widget_manager.register_widget_view('TabView', TabView);
125
126 });
@@ -1,55 +1,55 b''
1 """MulticontainerWidget class.
1 """MulticontainerWidget class.
2
2
3 Represents a multipage container that can be used to group other widgets into
3 Represents a multipage container that can be used to group other widgets into
4 pages.
4 pages.
5 """
5 """
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7 # Copyright (c) 2013, the IPython Development Team.
7 # Copyright (c) 2013, the IPython Development Team.
8 #
8 #
9 # Distributed under the terms of the Modified BSD License.
9 # Distributed under the terms of the Modified BSD License.
10 #
10 #
11 # The full license is in the file COPYING.txt, distributed with this software.
11 # The full license is in the file COPYING.txt, distributed with this software.
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13
13
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15 # Imports
15 # Imports
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17 from widget import Widget
17 from widget import Widget
18 from IPython.utils.traitlets import Unicode, Dict
18 from IPython.utils.traitlets import Unicode, Dict
19
19
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21 # Classes
21 # Classes
22 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
23 class MulticontainerWidget(Widget):
23 class MulticontainerWidget(Widget):
24 target_name = Unicode('MulticontainerWidgetModel')
24 target_name = Unicode('MulticontainerWidgetModel')
25 default_view_name = Unicode('AccordionView')
25 default_view_name = Unicode('TabView')
26
26
27 # Keys
27 # Keys
28 _keys = ['_titles']
28 _keys = ['_titles']
29 _titles = Dict(help="Titles of the pages")
29 _titles = Dict(help="Titles of the pages")
30
30
31 # Public methods
31 # Public methods
32 def set_title(self, index, title):
32 def set_title(self, index, title):
33 """Sets the title of a container pages
33 """Sets the title of a container pages
34
34
35 Parameters
35 Parameters
36 ----------
36 ----------
37 index : int
37 index : int
38 Index of the container page
38 Index of the container page
39 title : unicode
39 title : unicode
40 New title"""
40 New title"""
41 self._titles[index] = title
41 self._titles[index] = title
42 self.send_state('_titles')
42 self.send_state('_titles')
43
43
44
44
45 def get_title(self, index):
45 def get_title(self, index):
46 """Gets the title of a container pages
46 """Gets the title of a container pages
47
47
48 Parameters
48 Parameters
49 ----------
49 ----------
50 index : int
50 index : int
51 Index of the container page"""
51 Index of the container page"""
52 if index in self._titles:
52 if index in self._titles:
53 return self._titles[index]
53 return self._titles[index]
54 else:
54 else:
55 return None
55 return None
General Comments 0
You need to be logged in to leave comments. Login now