Add server + client versions to CTCP VERSION reply
[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);
27 }
63acf713
PS
28
29 this.panel_access = new Array();
50ac472f
D
30 },
31
32 render: function () {
33 var that = this;
34
35 this.$el.empty();
36
37 if (this.is_network) {
38 // Add the server tab first
39 this.model.server.tab
40 .data('panel', this.model.server)
41 .data('connection_id', this.model.network.get('connection_id'))
42 .appendTo(this.$el);
43 }
44
45 // Go through each panel adding its tab
46 this.model.forEach(function (panel) {
47 // If this is the server panel, ignore as it's already added
48 if (this.is_network && panel == that.model.server)
49 return;
50
51 panel.tab.data('panel', panel);
52
53 if (this.is_network)
54 panel.tab.data('connection_id', this.model.network.get('connection_id'));
55
56 panel.tab.appendTo(that.$el);
57 });
58
59 _kiwi.app.view.doLayout();
60 },
61
62 updateTabTitle: function (panel, new_title) {
63 $('span', panel.tab).text(new_title);
64 },
65
66 panelAdded: function (panel) {
67 // Add a tab to the panel
68 panel.tab = $('<li><span>' + (panel.get('title') || panel.get('name')) + '</span><div class="activity"></div></li>');
69
70 if (panel.isServer()) {
71 panel.tab.addClass('server');
72 panel.tab.addClass('icon-nonexistant');
73 }
74
75 panel.tab.data('panel', panel);
76
77 if (this.is_network)
78 panel.tab.data('connection_id', this.model.network.get('connection_id'));
79
80 panel.tab.appendTo(this.$el);
81
82 panel.bind('change:title', this.updateTabTitle);
83 panel.bind('change:name', this.updateTabTitle);
84
63acf713 85 //Adding a panel
63acf713
PS
86 this.panel_access.unshift(panel.cid);
87
50ac472f
D
88 _kiwi.app.view.doLayout();
89 },
90 panelRemoved: function (panel) {
c53d433f 91 var connection = _kiwi.app.connections.active_connection;
63acf713 92
50ac472f 93 panel.tab.remove();
63acf713 94
63acf713
PS
95 // If closing the active panel, switch to the last-accessed panel
96 if (this.panel_access[0] === _kiwi.app.panels().active.cid) {
97 this.panel_access.shift();
98
c53d433f
PS
99 //Get the last-accessed panel model now that we removed the closed one
100 var model = connection.panels.getByCid(this.panel_access[0]);
101
102 if (model) {
103 model.view.show();
104 }
63acf713
PS
105 }
106
50ac472f 107 delete panel.tab;
63acf713 108
50ac472f
D
109 _kiwi.app.view.doLayout();
110 },
111
112 panelActive: function (panel, previously_active_panel) {
c53d433f
PS
113 var panel_index = this.panel_access.indexOf(panel.cid);
114
50ac472f
D
115 // Remove any existing tabs or part images
116 _kiwi.app.view.$el.find('.panellist .part').remove();
117 _kiwi.app.view.$el.find('.panellist .active').removeClass('active');
118
119 panel.tab.addClass('active');
120
121 // Only show the part image on non-server tabs
122 if (!panel.isServer()) {
123 panel.tab.append('<span class="part icon-nonexistant"></span>');
124 }
1797120d 125
1797120d 126 if (panel_index > -1) {
63acf713 127 this.panel_access.splice(panel_index, 1);
1797120d
PS
128 }
129
130 //Make this panel the most recently accessed
63acf713 131 this.panel_access.unshift(panel.cid);
50ac472f
D
132 },
133
134 tabClick: function (e) {
135 var tab = $(e.currentTarget);
136
137 var panel = tab.data('panel');
138 if (!panel) {
139 // A panel wasn't found for this tab... wadda fuck
140 return;
141 }
142
143 panel.view.show();
144 },
145
146 partClick: function (e) {
147 var tab = $(e.currentTarget).parent();
148 var panel = tab.data('panel');
149
150 if (!panel) return;
151
152 // Only need to part if it's a channel
153 // If the nicklist is empty, we haven't joined the channel as yet
154 if (panel.isChannel() && panel.get('members').models.length > 0) {
155 this.model.network.gateway.part(panel.get('name'));
156 } else {
157 panel.close();
158 }
159 }
160});