Merge branch 'development'
[KiwiIRC.git] / client / src / views / tabs.js
CommitLineData
50ac472f
D
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);
ab1e3748
D
27
28 this.model.network.on('change:connection_id', function (network, new_val) {
29 this.model.forEach(function(panel) {
30 panel.tab.data('connection_id', new_val);
31 });
32 }, this);
50ac472f
D
33 }
34 },
35
36 render: function () {
37 var that = this;
38
39 this.$el.empty();
1adedc24 40
50ac472f
D
41 if (this.is_network) {
42 // Add the server tab first
43 this.model.server.tab
44 .data('panel', this.model.server)
45 .data('connection_id', this.model.network.get('connection_id'))
46 .appendTo(this.$el);
47 }
48
49 // Go through each panel adding its tab
50 this.model.forEach(function (panel) {
51 // If this is the server panel, ignore as it's already added
52 if (this.is_network && panel == that.model.server)
53 return;
54
55 panel.tab.data('panel', panel);
56
57 if (this.is_network)
58 panel.tab.data('connection_id', this.model.network.get('connection_id'));
59
60 panel.tab.appendTo(that.$el);
61 });
62
63 _kiwi.app.view.doLayout();
64 },
65
66 updateTabTitle: function (panel, new_title) {
67 $('span', panel.tab).text(new_title);
68 },
69
70 panelAdded: function (panel) {
71 // Add a tab to the panel
7b506f74
D
72 panel.tab = $('<li><span></span><div class="activity"></div></li>');
73 panel.tab.find('span').text(panel.get('title') || panel.get('name'));
50ac472f
D
74
75 if (panel.isServer()) {
76 panel.tab.addClass('server');
880239e8
JA
77 panel.tab.addClass('fa');
78 panel.tab.addClass('fa-nonexistant');
50ac472f
D
79 }
80
81 panel.tab.data('panel', panel);
82
83 if (this.is_network)
84 panel.tab.data('connection_id', this.model.network.get('connection_id'));
85
1adedc24 86 this.sortTabs();
50ac472f
D
87
88 panel.bind('change:title', this.updateTabTitle);
89 panel.bind('change:name', this.updateTabTitle);
90
91 _kiwi.app.view.doLayout();
92 },
93 panelRemoved: function (panel) {
c53d433f 94 var connection = _kiwi.app.connections.active_connection;
63acf713 95
50ac472f
D
96 panel.tab.remove();
97 delete panel.tab;
63acf713 98
34027fe0 99 _kiwi.app.panels.trigger('remove', panel);
d1925bdf 100
50ac472f
D
101 _kiwi.app.view.doLayout();
102 },
103
104 panelActive: function (panel, previously_active_panel) {
105 // Remove any existing tabs or part images
106 _kiwi.app.view.$el.find('.panellist .part').remove();
107 _kiwi.app.view.$el.find('.panellist .active').removeClass('active');
108
109 panel.tab.addClass('active');
fa27a34e 110
ad8f537b 111 panel.tab.append('<span class="part fa fa-nonexistant"></span>');
50ac472f
D
112 },
113
114 tabClick: function (e) {
115 var tab = $(e.currentTarget);
116
117 var panel = tab.data('panel');
118 if (!panel) {
119 // A panel wasn't found for this tab... wadda fuck
120 return;
121 }
122
123 panel.view.show();
124 },
125
126 partClick: function (e) {
127 var tab = $(e.currentTarget).parent();
128 var panel = tab.data('panel');
129
130 if (!panel) return;
131
50ac472f 132 // If the nicklist is empty, we haven't joined the channel as yet
ad8f537b 133 // If we part a server, then we need to disconnect from server, close channel tabs,
579fce37 134 // close server tab, then bring client back to homepage
50ac472f
D
135 if (panel.isChannel() && panel.get('members').models.length > 0) {
136 this.model.network.gateway.part(panel.get('name'));
fa27a34e 137
ad8f537b 138 } else if(panel.isServer()) {
fa27a34e 139 if (!this.model.network.get('connected') || confirm(translateText('disconnect_from_server'))) {
09e38d19
R
140 this.model.network.gateway.quit("Leaving");
141 _kiwi.app.connections.remove(this.model.network);
fa27a34e 142 _kiwi.app.startup_applet.view.show();
579fce37 143 }
fa27a34e 144
50ac472f
D
145 } else {
146 panel.close();
147 }
1adedc24
D
148 },
149
150 sortTabs: function() {
151 var that = this,
152 panels = [];
153
154 this.model.forEach(function (panel) {
155 // Ignore the server tab, so all others get added after it
156 if (that.is_network && panel == that.model.server)
157 return;
158
159 panels.push([panel.get('title') || panel.get('name'), panel]);
160 });
161
162 // Sort by the panel name..
163 panels.sort(function(a, b) {
164 if (a[0].toLowerCase() > b[0].toLowerCase()) {
165 return 1;
166 } else if (a[0].toLowerCase() < b[0].toLowerCase()) {
167 return -1;
168 } else {
169 return 0;
170 }
171 });
172
173 // And add them all back in order.
174 _.each(panels, function(panel) {
175 panel[1].tab.appendTo(that.$el);
176 });
50ac472f
D
177 }
178});