Notices into the active panel
[KiwiIRC.git] / client / assets / dev / applet_chanlist.js
CommitLineData
51ae0eb8
D
1(function () {\r
2\r
3 var View = Backbone.View.extend({\r
4 events: {\r
5 },\r
6\r
7\r
8\r
9 initialize: function (options) {\r
10 this.$el = $($('#tmpl_channel_list').html());\r
11\r
12 this.channels = [];\r
13\r
14 // Sort the table by num. users?\r
15 this.ordered = false;\r
16\r
17 // Waiting to add the table back into the DOM?\r
18 this.waiting = false;\r
19 },\r
20\r
21\r
22 render: function () {\r
23 var table = $('table', this.$el),\r
24 tbody = table.children('tbody:first').detach();\r
25 /*tbody.children().each(function (child) {\r
26 var i, chan;\r
27 child = $(child);\r
28 chan = child.children('td:first').text();\r
29 for (i = 0; i < chanList.length; i++) {\r
30 if (chanList[i].channel === chan) {\r
31 chanList[i].html = child.detach();\r
32 break;\r
33 }\r
34 }\r
35 });*/\r
36\r
37 if (this.ordered) {\r
38 this.channels.sort(function (a, b) {\r
39 return b.num_users - a.num_users;\r
40 });\r
41 }\r
42\r
85377d88
D
43 _.each(this.channels, function (chan) {\r
44 tbody.append(chan.html);\r
45 });\r
51ae0eb8
D
46 table.append(tbody);\r
47 }\r
48 });\r
49\r
50\r
51\r
52\r
854f9fbe 53 var Applet = Backbone.Model.extend({\r
51ae0eb8
D
54 initialize: function () {\r
55 this.set('title', 'Channel List');\r
56 this.view = new View();\r
854f9fbe
D
57\r
58 this.network = _kiwi.global.components.Network();\r
59 this.network.on('onlist_channel', this.onListChannel, this);\r
60 this.network.on('onlist_start', this.onListStart, this);\r
51ae0eb8
D
61 },\r
62\r
63\r
854f9fbe
D
64 // New channels to add to our list\r
65 onListChannel: function (event) {\r
66 console.log(event);\r
67 this.addChannel(event.chans);\r
68 },\r
69\r
70 // A new, fresh channel list starting\r
71 onListStart: function (event) {\r
72 // TODO: clear out our existing list\r
73 },\r
74\r
51ae0eb8
D
75 addChannel: function (channels) {\r
76 var that = this;\r
77\r
78 if (!_.isArray(channels)) {\r
79 channels = [channels];\r
80 }\r
81 _.each(channels, function (chan) {\r
82 var html, channel;\r
fb2da56c 83 html = '<tr><td><a class="chan" data-channel="' + chan.channel + '">' + _.escape(chan.channel) + '</a></td><td class="num_users" style="text-align: center;">' + chan.num_users + '</td><td style="padding-left: 2em;">' + formatIRCMsg(_.escape(chan.topic)) + '</td></tr>';\r
51ae0eb8
D
84 chan.html = html;\r
85 that.view.channels.push(chan);\r
86 });\r
87\r
88 if (!that.view.waiting) {\r
89 that.view.waiting = true;\r
90 _.defer(function () {\r
91 that.view.render();\r
92 that.view.waiting = false;\r
93 });\r
94 }\r
95 },\r
96\r
97\r
98 dispose: function () {\r
99 this.view.channels = null;\r
100 this.view.unbind();\r
101 this.view.$el.html('');\r
102 this.view.remove();\r
103 this.view = null;\r
854f9fbe
D
104\r
105 // Remove any network event bindings\r
106 this.network.off();\r
51ae0eb8
D
107 }\r
108 });\r
109\r
110\r
854f9fbe
D
111\r
112 _kiwi.model.Applet.register('kiwi_chanlist', Applet);\r
51ae0eb8 113})();