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