From 1fa545152fa9def0cbdef3a38cf0d323b52bf290 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Sat, 2 Jul 2016 11:26:23 +0300 Subject: [PATCH] Create a single function to render networks to reduce code duplication --- client/js/lounge.js | 65 +++++++++++++++++++++------------------------ src/client.js | 2 +- 2 files changed, 31 insertions(+), 36 deletions(-) diff --git a/client/js/lounge.js b/client/js/lounge.js index b537541a..133dd4d4 100644 --- a/client/js/lounge.js +++ b/client/js/lounge.js @@ -140,26 +140,7 @@ $(function() { if (data.networks.length === 0) { $("#footer").find(".connect").trigger("click"); } else { - sidebar.find(".empty").hide(); - sidebar.find(".networks").html( - render("network", { - networks: data.networks - }) - ); - var channels = $.map(data.networks, function(n) { - return n.channels; - }); - chat.html( - render("chat", { - channels: channels - }) - ); - channels.forEach(renderChannel); - confirmExit(); - - if (sidebar.find(".highlight").length) { - toggleNotificationMarkers(true); - } + renderNetworks(data); } if (data.token && $("#sign-in-remember").is(":checked")) { @@ -182,8 +163,6 @@ $(function() { $("#footer").find(".connect").trigger("click"); } } - - sortable(); }); socket.on("join", function(data) { @@ -325,6 +304,32 @@ $(function() { users.html(render("user", data)).data("nicks", nicks); } + function renderNetworks(data) { + sidebar.find(".empty").hide(); + sidebar.find(".networks").append( + render("network", { + networks: data.networks + }) + ); + + var channels = $.map(data.networks, function(n) { + return n.channels; + }); + chat.append( + render("chat", { + channels: channels + }) + ); + channels.forEach(renderChannel); + + confirmExit(); + sortable(); + + if (sidebar.find(".highlight").length) { + toggleNotificationMarkers(true); + } + } + socket.on("msg", function(data) { var msg = buildChatMessage(data); var target = "#chan-" + data.chan; @@ -357,26 +362,16 @@ $(function() { }); socket.on("network", function(data) { - sidebar.find(".empty").hide(); - sidebar.find(".networks").append( - render("network", { - networks: [data.network] - }) - ); - chat.append( - render("chat", { - channels: data.network.channels - }) - ); + renderNetworks(data); + sidebar.find(".chan") .last() .trigger("click"); + $("#connect") .find(".btn") .prop("disabled", false) .end(); - confirmExit(); - sortable(); }); socket.on("network_changed", function(data) { diff --git a/src/client.js b/src/client.js index e682dfad..5fd87b64 100644 --- a/src/client.js +++ b/src/client.js @@ -189,7 +189,7 @@ Client.prototype.connect = function(args) { client.networks.push(network); client.emit("network", { - network: network + networks: [network] }); if (config.lockNetwork) {