Commit | Line | Data |
---|---|---|
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 | |
0935901c | 10 | this.$el = $($('#tmpl_channel_list').html().trim());\r |
51ae0eb8 D |
11 | \r |
12 | this.channels = [];\r | |
13 | \r | |
14 | // Sort the table by num. users?\r | |
926fc1b2 | 15 | this.ordered = true;\r |
51ae0eb8 D |
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 | |
6aef3648 | 24 | tbody = table.children('tbody:first').detach(),\r |
6b2f04c8 JA |
25 | that = this,\r |
26 | channels_length = this.channels.length,\r | |
27 | i;\r | |
28 | \r | |
29 | tbody.children().each(function (idx, child) {\r | |
30 | if (that.channels[idx].channel === $(child.querySelector('.chan')).data('channel')) {\r | |
31 | that.channels[idx].dom = tbody[0].removeChild(child);\r | |
32 | }\r | |
33 | });\r | |
51ae0eb8 D |
34 | \r |
35 | if (this.ordered) {\r | |
36 | this.channels.sort(function (a, b) {\r | |
37 | return b.num_users - a.num_users;\r | |
38 | });\r | |
39 | }\r | |
40 | \r | |
6b2f04c8 JA |
41 | for (i = 0; i < channels_length; i++) {\r |
42 | tbody[0].appendChild(this.channels[i].dom);\r | |
43 | }\r | |
6aef3648 | 44 | table[0].appendChild(tbody[0]);\r |
51ae0eb8 D |
45 | }\r |
46 | });\r | |
47 | \r | |
48 | \r | |
49 | \r | |
50 | \r | |
854f9fbe | 51 | var Applet = Backbone.Model.extend({\r |
51ae0eb8 D |
52 | initialize: function () {\r |
53 | this.set('title', 'Channel List');\r | |
54 | this.view = new View();\r | |
854f9fbe D |
55 | \r |
56 | this.network = _kiwi.global.components.Network();\r | |
57 | this.network.on('onlist_channel', this.onListChannel, this);\r | |
58 | this.network.on('onlist_start', this.onListStart, this);\r | |
51ae0eb8 D |
59 | },\r |
60 | \r | |
61 | \r | |
854f9fbe D |
62 | // New channels to add to our list\r |
63 | onListChannel: function (event) {\r | |
854f9fbe D |
64 | this.addChannel(event.chans);\r |
65 | },\r | |
66 | \r | |
67 | // A new, fresh channel list starting\r | |
68 | onListStart: function (event) {\r | |
69 | // TODO: clear out our existing list\r | |
70 | },\r | |
71 | \r | |
51ae0eb8 D |
72 | addChannel: function (channels) {\r |
73 | var that = this;\r | |
74 | \r | |
75 | if (!_.isArray(channels)) {\r | |
76 | channels = [channels];\r | |
77 | }\r | |
78 | _.each(channels, function (chan) {\r | |
eb277015 | 79 | var row;\r |
6aef3648 | 80 | row = document.createElement("tr");\r |
eb277015 JA |
81 | row.innerHTML = '<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>';\r |
82 | chan.dom = row;\r | |
51ae0eb8 D |
83 | that.view.channels.push(chan);\r |
84 | });\r | |
85 | \r | |
86 | if (!that.view.waiting) {\r | |
87 | that.view.waiting = true;\r | |
88 | _.defer(function () {\r | |
89 | that.view.render();\r | |
90 | that.view.waiting = false;\r | |
91 | });\r | |
92 | }\r | |
93 | },\r | |
94 | \r | |
95 | \r | |
96 | dispose: function () {\r | |
97 | this.view.channels = null;\r | |
98 | this.view.unbind();\r | |
99 | this.view.$el.html('');\r | |
100 | this.view.remove();\r | |
101 | this.view = null;\r | |
854f9fbe D |
102 | \r |
103 | // Remove any network event bindings\r | |
104 | this.network.off();\r | |
51ae0eb8 D |
105 | }\r |
106 | });\r | |
107 | \r | |
108 | \r | |
854f9fbe D |
109 | \r |
110 | _kiwi.model.Applet.register('kiwi_chanlist', Applet);\r | |
51ae0eb8 | 111 | })(); |