3 'libs/text!./templates/main.html',
4 'libs/text!./templates/handle.html',
7 ], function($, mainTemplate, handleTemplate, _, Backbone) {
10 var View = Backbone.View.extend({
11 className: 'rng-view-tabs',
14 'click ul a, i': '_onTabTitleClicked'
17 initialize: function(options) {
18 this.options = options || {};
19 this.template = _.template(mainTemplate);
20 this.handleTemplate = _.template(handleTemplate);
22 this.selectedTab = null;
26 this.$el.html(this.template());
28 tabBar: this.$('.rng-view-tabs-tabBar'),
29 content: this.$('.rng-view-tabs-content')
32 if(this.options.stacked) {
33 this.nodes.tabBar.addClass('nav-stacked nav-pills').removeClass('nav-tabs');
35 if(this.options.position === 'right') {
36 this.$el.addClass('tabs-right');
37 this.nodes.content.addClass('tab-content');
42 addTab: function(title, slug, content) {
43 if(this.contents[slug]) {
44 this.contents[slug].detach();
46 this.contents[slug] = content;
48 var text = (typeof title === 'string') ? title : (title.text || '');
49 var icon = title.icon || null;
51 if(!this.tabExists(slug)) {
52 this.nodes.tabBar.append(this.handleTemplate({text: text, icon: icon, slug: slug}));
54 if(!this.selectedTab) {
59 selectTab: function(slug) {
60 if(slug !== this.selectedTab && this.contents[slug]) {
61 this.trigger('leaving', this.selectedTab);
63 if(this.selectedTab) {
64 var toDetach = this.contents[this.selectedTab];
70 this.nodes.content.append(this.contents[slug]);
71 if(this.contents[slug].onShow) {
72 this.contents[slug].onShow();
74 this.nodes.tabBar.find('.active').removeClass('active');
75 this.nodes.tabBar.find('a[href="#'+slug+'"]').parent().addClass('active');
77 var prevSlug = this.selectedTab;
78 this.selectedTab = slug;
79 this.trigger('tabSelected', {slug: slug, prevSlug: prevSlug});
83 getAsView: function() {
87 getCurrentSlug: function() {
88 return this.selectedTab;
91 tabExists: function(slug) {
92 return this.nodes.tabBar.find('a[href="#'+ slug + '"]').length > 0;
97 _onTabTitleClicked: function(e) {
99 var target = $(e.target);
101 target = target.parent();
103 var slug = target.attr('href').substr(1);
104 this.selectTab(slug);