Reworked client-side /list implementation
authorJack Allnutt <m2ys4u@Gmail.com>
Tue, 27 Sep 2011 11:22:54 +0000 (12:22 +0100)
committerJack Allnutt <m2ys4u@Gmail.com>
Tue, 27 Sep 2011 11:22:54 +0000 (12:22 +0100)
js/front.events.js
js/front.js

index ad0716a9f87000c1fc489a5a709ddb0d9e84ab13..08f78dac7a186bcb3419633b3725df52a62e1676 100644 (file)
@@ -225,7 +225,7 @@ kiwi.front.events = {
             return;\r
         }\r
 \r
-        if (typeof data.idle !== 'undefined'){\r
+        if (typeof data.idle !== 'undefined') {\r
             idle_time = secondsToTime(parseInt(data.idle, 10));\r
             idle_time = idle_time.h.toString().lpad(2, "0") + ':' + idle_time.m.toString().lpad(2, "0") + ':' + idle_time.s.toString().lpad(2, "0");\r
         }\r
@@ -283,64 +283,16 @@ kiwi.front.events = {
     },\r
 \r
     onChannelListStart: function (e, data) {\r
-        /*global Utilityview */\r
-        var tab, table;\r
-\r
-        tab = new Utilityview('Channel List');\r
-        tab.div.css('overflow-y', 'scroll');\r
-        table = $('<table style="margin:1em 2em;"><thead style="font-weight: bold;"><tr><td>Channel Name</td><td>Members</td><td style="padding-left: 2em;">Topic</td></tr></thead><tbody style="vertical-align: top;"></tbody>');\r
-        tab.div.append(table);\r
-\r
-        kiwi.front.cache.list = {chans: [], tab: tab, table: table,\r
-            update: function (newChans) {\r
-                var body = this.table.children('tbody:first').detach(),\r
-                    chan,\r
-                    html;\r
-\r
-                html = '';\r
-                for (chan in newChans) {\r
-                    this.chans.push(newChans[chan]);\r
-                    html += newChans[chan].html;\r
-                }\r
-                body.append(html);\r
-                this.table.append(body);\r
-\r
-            },\r
-            finalise: function () {\r
-                var body = this.table.children('tbody:first').detach(),\r
-                    list,\r
-                    chan;\r
-\r
-                list = $.makeArray($(body).children());\r
-\r
-                for (chan in list) {\r
-                    list[chan] = $(list[chan]).detach();\r
-                }\r
-\r
-                list = _.sortBy(list, function (channel) {\r
-                    return parseInt(channel.children('.num_users').first().text(), 10);\r
-                }).reverse();\r
-\r
-                for (chan in list) {\r
-                    body.append(list[chan]);\r
-                }\r
-\r
-                this.table.append(body);\r
-\r
-            }};\r
+        /*global ChannelList */\r
+        kiwi.front.cache.list = new ChannelList();\r
+        console.profile('list');\r
     },\r
     onChannelList: function (e, data) {\r
-        var chans;\r
-        data = data.chans;\r
-        //data = [data];\r
-        for (chans in data) {\r
-            data[chans] = {data: data[chans], html: '<tr><td><a class="chan">' + data[chans].channel + '</a></td><td class="num_users" style="text-align: center;">' + data[chans].num_users + '</td><td style="padding-left: 2em;">' + kiwi.front.formatIRCMsg(data[chans].topic) + '</td></tr>'};\r
-        }\r
-        kiwi.front.cache.list.update(data);\r
+        kiwi.front.cache.list.addChannel(data.chans);\r
     },\r
     onChannelListEnd: function (e, data) {\r
-        kiwi.front.cache.list.finalise();\r
-        kiwi.front.cache.list.tab.show();\r
+        kiwi.front.cache.list.show();\r
+        console.profileEnd();\r
     },\r
 \r
     onBanList: function (e, data) {\r
@@ -447,7 +399,7 @@ kiwi.front.events = {
         case 'invite_only_channel':\r
             tab.addMsg(null, ' ', '=== ' + data.channel + ' is invite only.', 'status');\r
             if (t_view !== 'server') {\r
-               tab.safe_to_close = true;\r
+                tab.safe_to_close = true;\r
             }\r
             break;\r
         case 'channel_is_full':\r
index 47d410cc24e5e0885d1d4e309bf129264b77d24a..f23973f74765a5a10fd123ca93ec785957ee47e9 100644 (file)
@@ -507,7 +507,65 @@ kiwi.front = {
 
 
 
+var ChannelList = function () {
+    /*globals Utilityview */
+    var chanList, view, table, obj, renderTable, waiting;
+    chanList = [];
+
+    view = new Utilityview('Channel List');
+    view.div.css('overflow-y', 'scroll');
+
+    table = $('<table style="margin:1em 2em;"><thead style="font-weight: bold;"><tr><td>Channel Name</td><td>Members</td><td style="padding-left: 2em;">Topic</td></tr></thead><tbody style="vertical-align: top;"></tbody>');
+    table = table.appendTo(view.div);
+
+    waiting = false;
+    renderTable = function () {
+        var tbody;
+        tbody = table.children('tbody:first').detach();
+        /*tbody.children().each(function (child) {
+            var i, chan;
+            child = $(child);
+            chan = child.children('td:first').text();
+            for (i = 0; i < chanList.length; i++) {
+                if (chanList[i].channel === chan) {
+                    chanList[i].html = child.detach();
+                    break;
+                }
+            }
+        });*/
+        _.each(chanList, function (chan) {
+            chan.html = $(chan.html).appendTo(tbody);
+        });
+        table = table.append(tbody);
+        waiting = false;
+    };
 
+    obj = {
+        addChannel: function (channels) {
+            if (!_.isArray(channels)) {
+                channels = [channels];
+            }
+            _.each(channels, function (chan) {
+                var html, channel;
+                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;">' + kiwi.front.formatIRCMsg(chan.topic) + '</td></tr>');
+                chan.html = html;
+                chanList.push(chan);
+            });
+            chanList.sort(function (a, b) {
+                return b.num_users - a.num_users;
+            });
+            if (!waiting) {
+                waiting = true;
+                _.defer(renderTable);
+            }
+        },
+        show: function () {
+            view.show();
+        },
+        prototype: {constructor: this}
+    };
+    return obj;
+};