From 2049a16d648ab80293b2b90efac7c7ace7c5f4d3 Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Fri, 25 Oct 2019 21:37:40 +0000 Subject: [PATCH] Implement switchToChannel method. --- client/components/ChannelWrapper.vue | 3 +-- client/components/InlineChannel.vue | 2 +- client/components/JoinChannel.vue | 2 +- client/js/commands/join.js | 2 +- client/js/contextMenuFactory.js | 4 ++-- client/js/keybinds.js | 2 +- client/js/socket-events/join.js | 2 +- client/js/socket-events/msg_special.js | 5 +++-- client/js/socket-events/network.js | 2 +- client/js/socket-events/part.js | 2 +- client/js/vue.js | 7 +++++++ 11 files changed, 20 insertions(+), 13 deletions(-) diff --git a/client/components/ChannelWrapper.vue b/client/components/ChannelWrapper.vue index fddf1e14..b61f9bb9 100644 --- a/client/components/ChannelWrapper.vue +++ b/client/components/ChannelWrapper.vue @@ -82,8 +82,7 @@ export default { return this.channel.name; }, click() { - // TODO: Find out why this sometimes throws `uncaught exception: Object` - this.$router.push("chan-" + this.channel.id); + this.$root.switchToChannel(this.channel); }, }, }; diff --git a/client/components/InlineChannel.vue b/client/components/InlineChannel.vue index 82edb4f8..be6401ee 100644 --- a/client/components/InlineChannel.vue +++ b/client/components/InlineChannel.vue @@ -18,7 +18,7 @@ export default { ); if (existingChannel) { - this.$router.push("chan-" + existingChannel.id); + this.$root.switchToChannel(existingChannel); } // TODO: Required here because it breaks tests diff --git a/client/components/JoinChannel.vue b/client/components/JoinChannel.vue index bf1cc905..2f8f6690 100644 --- a/client/components/JoinChannel.vue +++ b/client/components/JoinChannel.vue @@ -65,7 +65,7 @@ export default { ); if (existingChannel) { - this.$router.push("chan-" + existingChannel.id); + this.$root.switchToChannel(existingChannel); } else { const chanTypes = this.network.serverOptions.CHANTYPES; let channel = this.inputChannel; diff --git a/client/js/commands/join.js b/client/js/commands/join.js index 09eb4c86..ab8d4008 100644 --- a/client/js/commands/join.js +++ b/client/js/commands/join.js @@ -25,7 +25,7 @@ exports.input = function(args) { const chan = utils.findCurrentNetworkChan(channels); if (chan) { - vueApp.$router.push("chan-" + chan.id); + vueApp.switchToChannel(chan); } else { socket.emit("input", { text: `/join ${channels} ${args.length > 1 ? args[1] : ""}`, diff --git a/client/js/contextMenuFactory.js b/client/js/contextMenuFactory.js index 7e473a12..cd39f7c0 100644 --- a/client/js/contextMenuFactory.js +++ b/client/js/contextMenuFactory.js @@ -50,7 +50,7 @@ function addWhoisItem() { const chan = utils.findCurrentNetworkChan(itemData); if (chan) { - vueApp.$router.push("chan-" + chan.id); + vueApp.switchToChannel(chan); } socket.emit("input", { @@ -85,7 +85,7 @@ function addQueryItem() { const chan = utils.findCurrentNetworkChan(itemData); if (chan) { - vueApp.$router.push("chan-" + chan.id); + vueApp.switchToChannel(chan); } socket.emit("input", { diff --git a/client/js/keybinds.js b/client/js/keybinds.js index 6a1162cf..21be4480 100644 --- a/client/js/keybinds.js +++ b/client/js/keybinds.js @@ -90,7 +90,7 @@ Mousetrap.bind(["alt+a"], function() { } if (targetchan) { - vueApp.$router.push("chan-" + targetchan.id); + vueApp.switchToChannel(targetchan); } return false; diff --git a/client/js/socket-events/join.js b/client/js/socket-events/join.js index 25393c95..523efe77 100644 --- a/client/js/socket-events/join.js +++ b/client/js/socket-events/join.js @@ -15,5 +15,5 @@ socket.on("join", function(data) { return; } - vueApp.$router.push("chan-" + data.chan.id); + vueApp.switchToChannel(vueApp.findChannel(data.chan.id)); }); diff --git a/client/js/socket-events/msg_special.js b/client/js/socket-events/msg_special.js index 22706492..4ab89e7e 100644 --- a/client/js/socket-events/msg_special.js +++ b/client/js/socket-events/msg_special.js @@ -4,6 +4,7 @@ const socket = require("../socket"); const {vueApp, findChannel} = require("../vue"); socket.on("msg:special", function(data) { - findChannel(data.chan).channel.data = data.data; - vueApp.$router.push("chan-" + data.chan); + const channel = findChannel(data.chan); + channel.channel.data = data.data; + vueApp.switchToChannel(channel.channel); }); diff --git a/client/js/socket-events/network.js b/client/js/socket-events/network.js index 41cc58b0..8186480e 100644 --- a/client/js/socket-events/network.js +++ b/client/js/socket-events/network.js @@ -12,7 +12,7 @@ socket.on("network", function(data) { network.channels.forEach(initChannel); vueApp.networks.push(network); - vueApp.$router.push("chan-" + network.channels[0].id); + vueApp.switchToChannel(network.channels[0]); $("#connect") .find(".btn") diff --git a/client/js/socket-events/part.js b/client/js/socket-events/part.js index 064b05e6..31120c61 100644 --- a/client/js/socket-events/part.js +++ b/client/js/socket-events/part.js @@ -6,7 +6,7 @@ const {vueApp, findChannel} = require("../vue"); socket.on("part", function(data) { // When parting from the active channel/query, jump to the network's lobby if (vueApp.activeChannel && vueApp.activeChannel.channel.id === data.chan) { - vueApp.$router.push("chan-" + vueApp.activeChannel.network.id); + vueApp.switchToChannel(vueApp.activeChannel.network); } const channel = findChannel(data.chan); diff --git a/client/js/vue.js b/client/js/vue.js index 99d4de89..ec862398 100644 --- a/client/js/vue.js +++ b/client/js/vue.js @@ -102,6 +102,13 @@ const vueApp = new Vue({ return null; }, + switchToChannel(channel) { + if (this.activeChannel && this.activeChannel.channel.id === channel.id) { + return; + } + + this.$router.push("chan-" + channel.id); + }, switchOutOfChannel(channel) { // When switching out of a channel, mark everything as read if (channel.messages.length > 0) {