dcd3e96d0a82b835332b06b0ab8436f18efde256
[fnpeditor.git] / views / tabs / tabs.js
1 define([\r
2 'libs/text!./templates/main.html',\r
3 'libs/text!./templates/handle.html',\r
4 'libs/underscore-min',\r
5 'libs/backbone-min',\r
6 ], function(mainTemplate, handleTemplate, _, Backbone) {\r
7     'use strict';\r
8     \r
9     var View = Backbone.View.extend({\r
10         className: 'rng-view-tabs',\r
11         \r
12         events: {\r
13             'click ul a, i': '_onTabTitleClicked'\r
14         },\r
15         \r
16         initialize: function(options) {\r
17             this.options = options || {};\r
18             this.template = _.template(mainTemplate),\r
19             this.handleTemplate = _.template(handleTemplate);\r
20             this.contents = {};\r
21             this.selectedTab = null;\r
22         },\r
23         \r
24         render: function() {\r
25             this.$el.html(this.template());\r
26             this.nodes = {\r
27                 tabBar: this.$('.rng-view-tabs-tabBar'),\r
28                 content: this.$('.rng-view-tabs-content')\r
29             }\r
30             \r
31             if(this.options.stacked) {\r
32                 this.nodes.tabBar.addClass('nav-stacked nav-pills');\r
33             }\r
34         },\r
35         \r
36         addTab: function(title, slug, content) {\r
37             if(this.contents[slug])\r
38                 return false;\r
39             this.contents[slug] = content;\r
40             \r
41             var text = (typeof title === 'string') ? title : (title.text || '');\r
42             var icon = title.icon || null;\r
43             \r
44             this.nodes.tabBar.append(this.handleTemplate({text: text, icon: icon, slug: slug}));\r
45             if(!this.selectedTab)\r
46                 this.selectTab(slug);\r
47         },\r
48         \r
49         selectTab: function(slug) {\r
50             if(slug !== this.selectedTab && this.contents[slug]) {\r
51                 this.trigger('leaving', this.selectedTab);\r
52                 \r
53                 if(this.selectedTab)\r
54                     this.contents[this.selectedTab].detach();\r
55                 this.nodes.content.append(this.contents[slug]);\r
56                 this.nodes.tabBar.find('.active').removeClass('active');\r
57                 this.nodes.tabBar.find('a[href="#'+slug+'"]').parent().addClass('active');\r
58                 \r
59                 this.selectedTab = slug;\r
60                 this.trigger('tabSelected', slug);\r
61             }\r
62         },\r
63         \r
64         /* Events */\r
65         \r
66         _onTabTitleClicked: function(e) {\r
67             e.preventDefault();\r
68             var target = $(e.target);\r
69             if(target.is('i'))\r
70                 target = target.parent();\r
71             var slug = target.attr('href').substr(1);\r
72             this.selectTab(slug);\r
73         }\r
74     });\r
75 \r
76     \r
77     return {\r
78         View: View\r
79     }\r
80     \r
81 \r
82 });