Commit | Line | Data |
---|---|---|
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 | |
72 | panel.tab = $('<li><span>' + (panel.get('title') || panel.get('name')) + '</span><div class="activity"></div></li>'); | |
73 | ||
74 | if (panel.isServer()) { | |
75 | panel.tab.addClass('server'); | |
880239e8 JA |
76 | panel.tab.addClass('fa'); |
77 | panel.tab.addClass('fa-nonexistant'); | |
50ac472f D |
78 | } |
79 | ||
80 | panel.tab.data('panel', panel); | |
81 | ||
82 | if (this.is_network) | |
83 | panel.tab.data('connection_id', this.model.network.get('connection_id')); | |
84 | ||
1adedc24 | 85 | this.sortTabs(); |
50ac472f D |
86 | |
87 | panel.bind('change:title', this.updateTabTitle); | |
88 | panel.bind('change:name', this.updateTabTitle); | |
89 | ||
90 | _kiwi.app.view.doLayout(); | |
91 | }, | |
92 | panelRemoved: function (panel) { | |
c53d433f | 93 | var connection = _kiwi.app.connections.active_connection; |
63acf713 | 94 | |
50ac472f D |
95 | panel.tab.remove(); |
96 | delete panel.tab; | |
63acf713 | 97 | |
34027fe0 | 98 | _kiwi.app.panels.trigger('remove', panel); |
d1925bdf | 99 | |
50ac472f D |
100 | _kiwi.app.view.doLayout(); |
101 | }, | |
102 | ||
103 | panelActive: function (panel, previously_active_panel) { | |
104 | // Remove any existing tabs or part images | |
105 | _kiwi.app.view.$el.find('.panellist .part').remove(); | |
106 | _kiwi.app.view.$el.find('.panellist .active').removeClass('active'); | |
107 | ||
108 | panel.tab.addClass('active'); | |
ad8f537b R |
109 | |
110 | panel.tab.append('<span class="part fa fa-nonexistant"></span>'); | |
50ac472f D |
111 | }, |
112 | ||
113 | tabClick: function (e) { | |
114 | var tab = $(e.currentTarget); | |
115 | ||
116 | var panel = tab.data('panel'); | |
117 | if (!panel) { | |
118 | // A panel wasn't found for this tab... wadda fuck | |
119 | return; | |
120 | } | |
121 | ||
122 | panel.view.show(); | |
123 | }, | |
124 | ||
125 | partClick: function (e) { | |
126 | var tab = $(e.currentTarget).parent(); | |
127 | var panel = tab.data('panel'); | |
128 | ||
129 | if (!panel) return; | |
130 | ||
50ac472f | 131 | // If the nicklist is empty, we haven't joined the channel as yet |
ad8f537b | 132 | // If we part a server, then we need to disconnect from server, close channel tabs, |
579fce37 | 133 | // close server tab, then bring client back to homepage |
50ac472f D |
134 | if (panel.isChannel() && panel.get('members').models.length > 0) { |
135 | this.model.network.gateway.part(panel.get('name')); | |
ad8f537b | 136 | } else if(panel.isServer()) { |
f880f552 | 137 | this.model.network.gateway.quit("Leaving"); |
002797af | 138 | _kiwi.app.connections.remove(this.model.network); |
579fce37 R |
139 | if(_kiwi.app.connections.length < 1) { |
140 | window.location.reload(true); | |
141 | } | |
50ac472f D |
142 | } else { |
143 | panel.close(); | |
144 | } | |
1adedc24 D |
145 | }, |
146 | ||
147 | sortTabs: function() { | |
148 | var that = this, | |
149 | panels = []; | |
150 | ||
151 | this.model.forEach(function (panel) { | |
152 | // Ignore the server tab, so all others get added after it | |
153 | if (that.is_network && panel == that.model.server) | |
154 | return; | |
155 | ||
156 | panels.push([panel.get('title') || panel.get('name'), panel]); | |
157 | }); | |
158 | ||
159 | // Sort by the panel name.. | |
160 | panels.sort(function(a, b) { | |
161 | if (a[0].toLowerCase() > b[0].toLowerCase()) { | |
162 | return 1; | |
163 | } else if (a[0].toLowerCase() < b[0].toLowerCase()) { | |
164 | return -1; | |
165 | } else { | |
166 | return 0; | |
167 | } | |
168 | }); | |
169 | ||
170 | // And add them all back in order. | |
171 | _.each(panels, function(panel) { | |
172 | panel[1].tab.appendTo(that.$el); | |
173 | }); | |
50ac472f D |
174 | } |
175 | }); |