1f0ac187701d1931d396b0c3003cb5337fa6e1e3
[fnpeditor.git] / modules / tabsManager.js
1 rng.modules.tabsManager = function(sandbox) {\r
2 \r
3     var $ = sandbox.$;\r
4     \r
5     var view = $(sandbox.getTemplate('main')());\r
6     var currentSlug;\r
7     var tabContent = {};\r
8     \r
9     function selectTab(slug) {\r
10         var tabBar = view.find('#rng-tabsManager-tabBar');\r
11         \r
12         var prevActive = tabBar.find('li.active');\r
13         var prevSlug;\r
14         if(prevActive.length)\r
15             prevSlug = prevActive.find('a').attr('href').substr(1);\r
16         \r
17         if(prevSlug == slug)\r
18             return;\r
19         if(prevSlug)\r
20             sandbox.publish('leaving', prevSlug);\r
21         \r
22         tabBar.find('li').removeClass('active');\r
23         tabBar.find('a[href=#' + slug + ']').parent().addClass('active');\r
24         \r
25         if(prevSlug)\r
26             tabContent[prevSlug].detach();\r
27         tabContent[slug].appendTo(view.find('#rng-tabsManager-content'));\r
28         currentSlug = slug;\r
29     }\r
30        \r
31     \r
32     view.on('click', '#rng-tabsManager-tabBar li a', function(e) {\r
33         selectTab($(e.target).attr('href').substr(1));\r
34     });\r
35     \r
36     return {\r
37         start: function() {\r
38             sandbox.publish('ready');\r
39         },\r
40         \r
41         getView: function() {\r
42             return view;\r
43         },\r
44         \r
45         addTab: function(title, slug, contentView) {\r
46             tabContent[slug] = contentView;\r
47             view.find('#rng-tabsManager-tabBar').append(sandbox.getTemplate('tabHandle')({title: title, slug: slug}));\r
48             if(_.values(tabContent).length === 1)\r
49                 selectTab(slug);\r
50         },\r
51         getCurrentSlug: function() {\r
52             return currentSlug;\r
53         }\r
54     }\r
55 \r
56 };