Commit | Line | Data |
---|---|---|
51ae0eb8 D |
1 | (function () {\r |
2 | \r | |
3 | var View = Backbone.View.extend({\r | |
4 | events: {\r | |
1a92bda1 | 5 | "click .chan": "chanClick",\r |
3208d521 CC |
6 | "click .channel_name_title": "sortChannelsByNameClick",\r |
7 | "click .users_title": "sortChannelsByUsersClick"\r | |
51ae0eb8 D |
8 | },\r |
9 | \r | |
b3b0ed46 D |
10 | \r |
11 | \r | |
51ae0eb8 | 12 | initialize: function (options) {\r |
0d29c21f | 13 | var text = {\r |
df0aee09 CC |
14 | channel_name: _kiwi.global.i18n.translate('client_applets_chanlist_channelname').fetch(),\r |
15 | users: _kiwi.global.i18n.translate('client_applets_chanlist_users').fetch(),\r | |
247dd7ac | 16 | topic: _kiwi.global.i18n.translate('client_applets_chanlist_topic').fetch()\r |
0d29c21f JA |
17 | };\r |
18 | this.$el = $(_.template($('#tmpl_channel_list').html().trim(), text));\r | |
51ae0eb8 D |
19 | \r |
20 | this.channels = [];\r | |
21 | \r | |
905e5773 CC |
22 | // Sort the table\r |
23 | this.order = '';\r | |
24 | \r | |
51ae0eb8 D |
25 | // Waiting to add the table back into the DOM?\r |
26 | this.waiting = false;\r | |
27 | },\r | |
28 | \r | |
bc2689fe | 29 | render: function () {\r |
51ae0eb8 | 30 | var table = $('table', this.$el),\r |
6aef3648 | 31 | tbody = table.children('tbody:first').detach(),\r |
6b2f04c8 | 32 | that = this,\r |
6b2f04c8 | 33 | i;\r |
51ae0eb8 | 34 | \r |
df0aee09 CC |
35 | // Create the sort icon container and clean previous any previous ones\r |
36 | if($('.applet_chanlist .users_title').find('span.chanlist_sort_users').length == 0) {\r | |
37 | this.$('.users_title').append('<span class="chanlist_sort_users"> </span>');\r | |
38 | } else {\r | |
39 | this.$('.users_title span.chanlist_sort_users').removeClass('icon-sort-up');\r | |
40 | this.$('.users_title span.chanlist_sort_users').removeClass('icon-sort-down');\r | |
41 | }\r | |
42 | if ($('.applet_chanlist .channel_name_title').find('span.chanlist_sort_names').length == 0) {\r | |
43 | this.$('.channel_name_title').append('<span class="chanlist_sort_names"> </span>');\r | |
44 | } else {\r | |
45 | this.$('.channel_name_title span.chanlist_sort_names').removeClass('icon-sort-up');\r | |
46 | this.$('.channel_name_title span.chanlist_sort_names').removeClass('icon-sort-down');\r | |
47 | }\r | |
48 | \r | |
49 | // Push the new sort icon\r | |
905e5773 CC |
50 | switch (this.order) {\r |
51 | case 'user_desc':\r | |
52 | default:\r | |
df0aee09 | 53 | this.$('.users_title span.chanlist_sort_users').addClass('icon-sort-down');\r |
905e5773 CC |
54 | break;\r |
55 | case 'user_asc':\r | |
df0aee09 | 56 | this.$('.users_title span.chanlist_sort_users').addClass('icon-sort-up');\r |
905e5773 CC |
57 | break;\r |
58 | case 'name_asc':\r | |
df0aee09 | 59 | this.$('.channel_name_title span.chanlist_sort_names').addClass('icon-sort-up');\r |
905e5773 CC |
60 | break;\r |
61 | case 'name_desc':\r | |
df0aee09 | 62 | this.$('.channel_name_title span.chanlist_sort_names').addClass('icon-sort-down');\r |
905e5773 CC |
63 | break;\r |
64 | }\r | |
7db891ef | 65 | \r |
b3b0ed46 D |
66 | this.channels = this.sortChannels(this.channels, this.order);\r |
67 | \r | |
68 | // Make sure all the channel DOM nodes are inserted in order\r | |
150391ee | 69 | for (i = 0; i < this.channels.length; i++) {\r |
6b2f04c8 JA |
70 | tbody[0].appendChild(this.channels[i].dom);\r |
71 | }\r | |
b3b0ed46 | 72 | \r |
6aef3648 | 73 | table[0].appendChild(tbody[0]);\r |
1a92bda1 D |
74 | },\r |
75 | \r | |
76 | \r | |
77 | chanClick: function (event) {\r | |
78 | if (event.target) {\r | |
79 | _kiwi.gateway.join(null, $(event.target).data('channel'));\r | |
80 | } else {\r | |
81 | // IE...\r | |
82 | _kiwi.gateway.join(null, $(event.srcElement).data('channel'));\r | |
83 | }\r | |
905e5773 | 84 | },\r |
b3b0ed46 | 85 | \r |
905e5773 CC |
86 | sortChannelsByNameClick: function (event) {\r |
87 | // Revert the sorting to switch between orders\r | |
df0aee09 | 88 | this.order = (this.order == 'name_asc') ? 'name_desc' : 'name_asc';\r |
b3b0ed46 D |
89 | \r |
90 | this.sortChannelsClick();\r | |
905e5773 | 91 | },\r |
b3b0ed46 | 92 | \r |
905e5773 CC |
93 | sortChannelsByUsersClick: function (event) {\r |
94 | // Revert the sorting to switch between orders\r | |
df0aee09 | 95 | this.order = (this.order == 'user_desc' || this.order == '') ? 'user_asc' : 'user_desc';\r |
b3b0ed46 D |
96 | \r |
97 | this.sortChannelsClick();\r | |
905e5773 | 98 | },\r |
905e5773 | 99 | \r |
b3b0ed46 | 100 | sortChannelsClick: function() {\r |
bc2689fe | 101 | this.render();\r |
905e5773 | 102 | },\r |
b3b0ed46 | 103 | \r |
905e5773 | 104 | sortChannels: function (channels, order) {\r |
b3b0ed46 | 105 | var sort_channels = [],\r |
7db891ef | 106 | new_channels = [];\r |
b3b0ed46 D |
107 | \r |
108 | \r | |
905e5773 | 109 | // First we create a light copy of the channels object to do the sorting\r |
b3b0ed46 D |
110 | _.each(channels, function (chan, chan_idx) {\r |
111 | sort_channels.push({'chan_idx': chan_idx, 'num_users': chan.num_users, 'channel': chan.channel});\r | |
905e5773 | 112 | });\r |
b3b0ed46 | 113 | \r |
905e5773 CC |
114 | // Second, we apply the sorting\r |
115 | sort_channels.sort(function (a, b) {\r | |
116 | switch (order) {\r | |
117 | case 'user_asc':\r | |
118 | return a.num_users - b.num_users;\r | |
905e5773 CC |
119 | case 'user_desc':\r |
120 | return b.num_users - a.num_users;\r | |
905e5773 CC |
121 | case 'name_asc':\r |
122 | if (a.channel.toLowerCase() > b.channel.toLowerCase()) return 1;\r | |
123 | if (a.channel.toLowerCase() < b.channel.toLowerCase()) return -1;\r | |
905e5773 CC |
124 | case 'name_desc':\r |
125 | if (a.channel.toLowerCase() < b.channel.toLowerCase()) return 1;\r | |
126 | if (a.channel.toLowerCase() > b.channel.toLowerCase()) return -1;\r | |
905e5773 CC |
127 | default:\r |
128 | return b.num_users - a.num_users;\r | |
905e5773 CC |
129 | }\r |
130 | return 0;\r | |
131 | });\r | |
b3b0ed46 | 132 | \r |
905e5773 | 133 | // Third, we re-shuffle the chanlist according to the sort order\r |
905e5773 | 134 | _.each(sort_channels, function (chan) {\r |
b3b0ed46 | 135 | new_channels.push(channels[chan.chan_idx]);\r |
905e5773 | 136 | });\r |
b3b0ed46 | 137 | \r |
905e5773 | 138 | return new_channels;\r |
51ae0eb8 D |
139 | }\r |
140 | });\r | |
141 | \r | |
142 | \r | |
143 | \r | |
854f9fbe | 144 | var Applet = Backbone.Model.extend({\r |
51ae0eb8 | 145 | initialize: function () {\r |
247dd7ac | 146 | this.set('title', _kiwi.global.i18n.translate('client_applets_chanlist_channellist').fetch());\r |
51ae0eb8 | 147 | this.view = new View();\r |
854f9fbe D |
148 | \r |
149 | this.network = _kiwi.global.components.Network();\r | |
150 | this.network.on('onlist_channel', this.onListChannel, this);\r | |
151 | this.network.on('onlist_start', this.onListStart, this);\r | |
51ae0eb8 D |
152 | },\r |
153 | \r | |
154 | \r | |
854f9fbe D |
155 | // New channels to add to our list\r |
156 | onListChannel: function (event) {\r | |
854f9fbe D |
157 | this.addChannel(event.chans);\r |
158 | },\r | |
159 | \r | |
160 | // A new, fresh channel list starting\r | |
161 | onListStart: function (event) {\r | |
162 | // TODO: clear out our existing list\r | |
163 | },\r | |
164 | \r | |
51ae0eb8 D |
165 | addChannel: function (channels) {\r |
166 | var that = this;\r | |
167 | \r | |
168 | if (!_.isArray(channels)) {\r | |
169 | channels = [channels];\r | |
170 | }\r | |
171 | _.each(channels, function (chan) {\r | |
eb277015 | 172 | var row;\r |
6aef3648 | 173 | row = document.createElement("tr");\r |
73ddddf0 | 174 | row.innerHTML = '<td class="chanlist_name"><a class="chan" data-channel="' + chan.channel + '">' + _.escape(chan.channel) + '</a></td><td class="chanlist_num_users" style="text-align: center;">' + chan.num_users + '</td><td style="padding-left: 2em;" class="chanlist_topic">' + formatIRCMsg(_.escape(chan.topic)) + '</td>';\r |
eb277015 | 175 | chan.dom = row;\r |
51ae0eb8 D |
176 | that.view.channels.push(chan);\r |
177 | });\r | |
178 | \r | |
179 | if (!that.view.waiting) {\r | |
180 | that.view.waiting = true;\r | |
181 | _.defer(function () {\r | |
182 | that.view.render();\r | |
183 | that.view.waiting = false;\r | |
184 | });\r | |
185 | }\r | |
186 | },\r | |
187 | \r | |
188 | \r | |
189 | dispose: function () {\r | |
190 | this.view.channels = null;\r | |
191 | this.view.unbind();\r | |
192 | this.view.$el.html('');\r | |
193 | this.view.remove();\r | |
194 | this.view = null;\r | |
854f9fbe D |
195 | \r |
196 | // Remove any network event bindings\r | |
197 | this.network.off();\r | |
51ae0eb8 D |
198 | }\r |
199 | });\r | |
200 | \r | |
201 | \r | |
854f9fbe D |
202 | \r |
203 | _kiwi.model.Applet.register('kiwi_chanlist', Applet);\r | |
51ae0eb8 | 204 | })(); |