Chanlist formatting and slight tidy
authorDarren <darren@darrenwhitlen.com>
Thu, 27 Mar 2014 21:16:01 +0000 (21:16 +0000)
committerDarren <darren@darrenwhitlen.com>
Thu, 27 Mar 2014 21:16:01 +0000 (21:16 +0000)
client/src/applets/chanlist.js

index 9135712b392602a11fcae11529210e4dfb959268..8c286fdf7c598584ec26e8dc4a44555aa5e4bfff 100644 (file)
@@ -7,8 +7,8 @@
             "click .users_title": "sortChannelsByUsersClick"\r
         },\r
 \r
-        \r
-        \r
+\r
+\r
         initialize: function (options) {\r
             var text = {\r
                 channel_name: _kiwi.global.i18n.translate('client_applets_chanlist_channelname').fetch(),\r
@@ -22,7 +22,6 @@
             // Sort the table\r
             this.order = '';\r
 \r
-\r
             // Waiting to add the table back into the DOM?\r
             this.waiting = false;\r
         },\r
@@ -32,8 +31,6 @@
                 tbody = table.children('tbody:first').detach(),\r
                 that = this,\r
                 i;\r
-            \r
-            this.channels = this.sortChannels(this.channels, this.order);\r
 \r
             // Create the sort icon container and clean previous any previous ones\r
             if($('.applet_chanlist .users_title').find('span.chanlist_sort_users').length == 0) {\r
                     this.$('.channel_name_title span.chanlist_sort_names').addClass('icon-sort-down');\r
                     break;\r
             }\r
-            \r
-            tbody.children().each(function (idx, child) {\r
-                if (that.channels[idx].channel === $(child.querySelector('.chan')).data('channel')) {\r
-                    that.channels[idx].dom = tbody[0].removeChild(child);\r
-                }\r
-            });\r
 \r
+            this.channels = this.sortChannels(this.channels, this.order);\r
+\r
+            // Make sure all the channel DOM nodes are inserted in order\r
             for (i = 0; i < this.channels.length; i++) {\r
                 tbody[0].appendChild(this.channels[i].dom);\r
             }\r
+\r
             table[0].appendChild(tbody[0]);\r
         },\r
 \r
                 _kiwi.gateway.join(null, $(event.srcElement).data('channel'));\r
             }\r
         },\r
-        \r
+\r
         sortChannelsByNameClick: function (event) {\r
             // Revert the sorting to switch between orders\r
             this.order = (this.order == 'name_asc') ? 'name_desc' : 'name_asc';\r
-            \r
-            this.sortChannelsClick(this.order);\r
+\r
+            this.sortChannelsClick();\r
         },\r
-        \r
+\r
         sortChannelsByUsersClick: function (event) {\r
             // Revert the sorting to switch between orders\r
             this.order = (this.order == 'user_desc' || this.order == '') ? 'user_asc' : 'user_desc';\r
-            \r
-            this.sortChannelsClick(this.order);\r
+\r
+            this.sortChannelsClick();\r
         },\r
-        \r
-        sortChannelsClick: function(order) {\r
-            this.channels = this.sortChannels(this.channels, order);\r
 \r
+        sortChannelsClick: function() {\r
             this.render();\r
         },\r
-        \r
+\r
         sortChannels: function (channels, order) {\r
-            var counter = 0,\r
-                sort_channels = [],\r
+            var sort_channels = [],\r
                 new_channels = [];\r
-            \r
-            \r
+\r
+\r
             // First we create a light copy of the channels object to do the sorting\r
-            _.each(channels, function (chan) {\r
-                sort_channels.push({'counter': counter, 'num_users': chan.num_users, 'channel': chan.channel});\r
-                \r
-                counter += 1;\r
+            _.each(channels, function (chan, chan_idx) {\r
+                sort_channels.push({'chan_idx': chan_idx, 'num_users': chan.num_users, 'channel': chan.channel});\r
             });\r
-            \r
+\r
             // Second, we apply the sorting\r
             sort_channels.sort(function (a, b) {\r
                 switch (order) {\r
                 }\r
                 return 0;\r
             });\r
-            \r
+\r
             // Third, we re-shuffle the chanlist according to the sort order\r
             _.each(sort_channels, function (chan) {\r
-                new_channels.push(channels[chan.counter]);\r
+                new_channels.push(channels[chan.chan_idx]);\r
             });\r
-            \r
+\r
             return new_channels;\r
         }\r
     });\r