Refactoring
[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').removeClass('nav-tabs');\r
33             }\r
34             return this;\r
35         },\r
36         \r
37         addTab: function(title, slug, content) {\r
38             if(this.contents[slug])\r
39                 return false;\r
40             this.contents[slug] = content;\r
41             \r
42             var text = (typeof title === 'string') ? title : (title.text || '');\r
43             var icon = title.icon || null;\r
44             \r
45             this.nodes.tabBar.append(this.handleTemplate({text: text, icon: icon, slug: slug}));\r
46             if(!this.selectedTab)\r
47                 this.selectTab(slug);\r
48         },\r
49         \r
50         selectTab: function(slug) {\r
51             if(slug !== this.selectedTab && this.contents[slug]) {\r
52                 this.trigger('leaving', this.selectedTab);\r
53                 \r
54                 if(this.selectedTab)\r
55                     this.contents[this.selectedTab].detach();\r
56                 this.nodes.content.append(this.contents[slug]);\r
57                 this.nodes.tabBar.find('.active').removeClass('active');\r
58                 this.nodes.tabBar.find('a[href="#'+slug+'"]').parent().addClass('active');\r
59                 \r
60                 var prevSlug = this.selectedTab;\r
61                 this.selectedTab = slug;\r
62                 this.trigger('tabSelected', {slug: slug, prevSlug: prevSlug});\r
63             }\r
64         },\r
65         \r
66         getAsView: function() {\r
67             return this.$el;\r
68         },\r
69         \r
70         getCurrentSlug: function() {\r
71             return this.selectedTab;\r
72         },\r
73         \r
74         /* Events */\r
75         \r
76         _onTabTitleClicked: function(e) {\r
77             e.preventDefault();\r
78             var target = $(e.target);\r
79             if(target.is('i'))\r
80                 target = target.parent();\r
81             var slug = target.attr('href').substr(1);\r
82             this.selectTab(slug);\r
83         }\r
84     });\r
85 \r
86     \r
87     return {\r
88         View: View\r
89     }\r
90     \r
91 \r
92 });