Added a /slap command
[KiwiIRC.git] / client_backbone / dev / applet_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());
11
12 this.channels = [];
13
14 // Sort the table by num. users?
15 this.ordered = false;
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 /*tbody.children().each(function (child) {
26 var i, chan;
27 child = $(child);
28 chan = child.children('td:first').text();
29 for (i = 0; i < chanList.length; i++) {
30 if (chanList[i].channel === chan) {
31 chanList[i].html = child.detach();
32 break;
33 }
34 }
35 });*/
36
37 if (this.ordered) {
38 this.channels.sort(function (a, b) {
39 return b.num_users - a.num_users;
40 });
41 }
42
43 _.each(this.channels, function (chan) {
44 tbody.append(chan.html);
45 });
46 table.append(tbody);
47 }
48 });
49
50
51
52
53 kiwi.applets.Chanlist = Backbone.Model.extend({
54 initialize: function () {
55 this.set('title', 'Channel List');
56 this.view = new View();
57 },
58
59
60 addChannel: function (channels) {
61 var that = this;
62
63 if (!_.isArray(channels)) {
64 channels = [channels];
65 }
66 _.each(channels, function (chan) {
67 var html, channel;
68 html = '<tr><td><a class="chan">' + chan.channel + '</a></td><td class="num_users" style="text-align: center;">' + chan.num_users + '</td><td style="padding-left: 2em;">' + formatIRCMsg(chan.topic) + '</td></tr>';
69 chan.html = html;
70 that.view.channels.push(chan);
71 });
72
73 if (!that.view.waiting) {
74 that.view.waiting = true;
75 _.defer(function () {
76 that.view.render();
77 that.view.waiting = false;
78 });
79 }
80 },
81
82
83 dispose: function () {
84 this.view.channels = null;
85 this.view.unbind();
86 this.view.$el.html('');
87 this.view.remove();
88 this.view = null;
89 }
90 });
91
92
93 })();