Client: view.js split up into multiple files
[KiwiIRC.git] / client / assets / src / views / tabs.js
1 // Model for this = _kiwi.model.PanelList
2 _kiwi.view.Tabs = Backbone.View.extend({
3 tagName: 'ul',
4 className: 'panellist',
5
6 events: {
7 'click li': 'tabClick',
8 'click li .part': 'partClick'
9 },
10
11 initialize: function () {
12 this.model.on("add", this.panelAdded, this);
13 this.model.on("remove", this.panelRemoved, this);
14 this.model.on("reset", this.render, this);
15
16 this.model.on('active', this.panelActive, this);
17
18 // Network tabs start with a server, so determine what we are now
19 this.is_network = false;
20
21 if (this.model.network) {
22 this.is_network = true;
23
24 this.model.network.on('change:name', function (network, new_val) {
25 $('span', this.model.server.tab).text(new_val);
26 }, this);
27 }
28 },
29
30 render: function () {
31 var that = this;
32
33 this.$el.empty();
34
35 if (this.is_network) {
36 // Add the server tab first
37 this.model.server.tab
38 .data('panel', this.model.server)
39 .data('connection_id', this.model.network.get('connection_id'))
40 .appendTo(this.$el);
41 }
42
43 // Go through each panel adding its tab
44 this.model.forEach(function (panel) {
45 // If this is the server panel, ignore as it's already added
46 if (this.is_network && panel == that.model.server)
47 return;
48
49 panel.tab.data('panel', panel);
50
51 if (this.is_network)
52 panel.tab.data('connection_id', this.model.network.get('connection_id'));
53
54 panel.tab.appendTo(that.$el);
55 });
56
57 _kiwi.app.view.doLayout();
58 },
59
60 updateTabTitle: function (panel, new_title) {
61 $('span', panel.tab).text(new_title);
62 },
63
64 panelAdded: function (panel) {
65 // Add a tab to the panel
66 panel.tab = $('<li><span>' + (panel.get('title') || panel.get('name')) + '</span><div class="activity"></div></li>');
67
68 if (panel.isServer()) {
69 panel.tab.addClass('server');
70 panel.tab.addClass('icon-nonexistant');
71 }
72
73 panel.tab.data('panel', panel);
74
75 if (this.is_network)
76 panel.tab.data('connection_id', this.model.network.get('connection_id'));
77
78 panel.tab.appendTo(this.$el);
79
80 panel.bind('change:title', this.updateTabTitle);
81 panel.bind('change:name', this.updateTabTitle);
82
83 _kiwi.app.view.doLayout();
84 },
85 panelRemoved: function (panel) {
86 panel.tab.remove();
87 delete panel.tab;
88
89 _kiwi.app.view.doLayout();
90 },
91
92 panelActive: function (panel, previously_active_panel) {
93 // Remove any existing tabs or part images
94 _kiwi.app.view.$el.find('.panellist .part').remove();
95 _kiwi.app.view.$el.find('.panellist .active').removeClass('active');
96
97 panel.tab.addClass('active');
98
99 // Only show the part image on non-server tabs
100 if (!panel.isServer()) {
101 panel.tab.append('<span class="part icon-nonexistant"></span>');
102 }
103 },
104
105 tabClick: function (e) {
106 var tab = $(e.currentTarget);
107
108 var panel = tab.data('panel');
109 if (!panel) {
110 // A panel wasn't found for this tab... wadda fuck
111 return;
112 }
113
114 panel.view.show();
115 },
116
117 partClick: function (e) {
118 var tab = $(e.currentTarget).parent();
119 var panel = tab.data('panel');
120
121 if (!panel) return;
122
123 // Only need to part if it's a channel
124 // If the nicklist is empty, we haven't joined the channel as yet
125 if (panel.isChannel() && panel.get('members').models.length > 0) {
126 this.model.network.gateway.part(panel.get('name'));
127 } else {
128 panel.close();
129 }
130 }
131 });