From aba2487126f7a1f85b43aee5241861bdf4ac4fc0 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Sun, 3 Nov 2019 16:59:43 +0200 Subject: [PATCH] Make findChannel and findNetwork getters --- client/components/InlineChannel.vue | 5 +--- client/components/JoinChannel.vue | 5 +--- client/components/NetworkList.vue | 3 +-- client/components/RoutedChat.vue | 2 +- client/components/Windows/NetworkEdit.vue | 4 +-- client/js/contextMenuFactory.js | 6 ++--- client/js/keybinds.js | 1 - client/js/socket-events/init.js | 2 +- client/js/socket-events/join.js | 7 +++--- client/js/socket-events/more.js | 5 ++-- client/js/socket-events/msg.js | 4 +-- client/js/socket-events/msg_preview.js | 5 ++-- client/js/socket-events/msg_special.js | 5 ++-- client/js/socket-events/names.js | 4 +-- client/js/socket-events/network.js | 10 ++++---- client/js/socket-events/nick.js | 4 +-- client/js/socket-events/open.js | 4 +-- client/js/socket-events/part.js | 6 ++--- client/js/socket-events/sync_sort.js | 3 +-- client/js/socket-events/topic.js | 4 +-- client/js/socket-events/users.js | 3 +-- client/js/store.js | 20 +++++++++++++++ client/js/vue.js | 30 ----------------------- 23 files changed, 63 insertions(+), 79 deletions(-) diff --git a/client/components/InlineChannel.vue b/client/components/InlineChannel.vue index 7ba459bd..82fbae8a 100644 --- a/client/components/InlineChannel.vue +++ b/client/components/InlineChannel.vue @@ -12,10 +12,7 @@ export default { }, methods: { onClick() { - const channelToFind = this.channel.toLowerCase(); - const existingChannel = this.$store.state.activeChannel.network.channels.find( - (c) => c.name.toLowerCase() === channelToFind - ); + const existingChannel = this.$store.getters.findChannelOnCurrentNetwork(this.channel); if (existingChannel) { this.$root.switchToChannel(existingChannel); diff --git a/client/components/JoinChannel.vue b/client/components/JoinChannel.vue index 2f8f6690..ae85f0c9 100644 --- a/client/components/JoinChannel.vue +++ b/client/components/JoinChannel.vue @@ -59,10 +59,7 @@ export default { }, methods: { onSubmit() { - const channelToFind = this.inputChannel.toLowerCase(); - const existingChannel = this.network.channels.find( - (c) => c.name.toLowerCase() === channelToFind - ); + const existingChannel = this.$store.getters.findChannelOnCurrentNetwork(this.channel); if (existingChannel) { this.$root.switchToChannel(existingChannel); diff --git a/client/components/NetworkList.vue b/client/components/NetworkList.vue index 3136411c..16fea14f 100644 --- a/client/components/NetworkList.vue +++ b/client/components/NetworkList.vue @@ -115,8 +115,7 @@ export default { return; } - const {findChannel} = require("../js/vue"); - const channel = findChannel(e.moved.element.id); + const channel = this.$store.getters.findChannel(e.moved.element.id); if (!channel) { return; diff --git a/client/components/RoutedChat.vue b/client/components/RoutedChat.vue index 2ce3ff8f..3186469f 100644 --- a/client/components/RoutedChat.vue +++ b/client/components/RoutedChat.vue @@ -14,7 +14,7 @@ export default { computed: { activeChannel() { const chan_id = parseInt(this.$route.params.pathMatch); - const channel = this.$root.findChannel(chan_id); + const channel = this.$store.getters.findChannel(chan_id); return channel; }, }, diff --git a/client/components/Windows/NetworkEdit.vue b/client/components/Windows/NetworkEdit.vue index a1444eae..1c36f7a3 100644 --- a/client/components/Windows/NetworkEdit.vue +++ b/client/components/Windows/NetworkEdit.vue @@ -35,7 +35,7 @@ export default { methods: { setNetworkData() { socket.emit("network:get", this.$route.params.uuid); - this.networkData = this.$root.findNetwork(this.$route.params.uuid); + this.networkData = this.$store.getters.findNetwork(this.$route.params.uuid); }, handleSubmit(data) { this.disabled = true; @@ -43,7 +43,7 @@ export default { const sidebar = $("#sidebar"); // TODO: move networks to vuex and update state when the network info comes in - const network = this.$root.findNetwork(data.uuid); + const network = this.$store.getters.findNetwork(data.uuid); network.name = network.channels[0].name = data.name; sidebar.find(`.network[data-uuid="${data.uuid}"] .chan.lobby .name`).click(); }, diff --git a/client/js/contextMenuFactory.js b/client/js/contextMenuFactory.js index 37146f12..c05e9970 100644 --- a/client/js/contextMenuFactory.js +++ b/client/js/contextMenuFactory.js @@ -6,7 +6,7 @@ const utils = require("./utils"); const ContextMenu = require("./contextMenu"); const contextMenuActions = []; const contextMenuItems = []; -const {vueApp, findChannel} = require("./vue"); +const {vueApp} = require("./vue"); const store = require("./store").default; addDefaultItems(); @@ -323,7 +323,7 @@ function addChannelListItem() { function addEditTopicItem() { function setEditTopic(itemData) { - findChannel(Number(itemData)).channel.editTopic = true; + store.getters.findChannel(Number(itemData)).channel.editTopic = true; document.querySelector(`#sidebar .chan[data-id="${Number(itemData)}"]`).click(); vueApp.$nextTick(() => { @@ -359,7 +359,7 @@ function addBanListItem() { function addJoinItem() { function openJoinForm(itemData) { - findChannel(Number(itemData)).network.isJoinChannelShown = true; + store.getters.findChannel(Number(itemData)).network.isJoinChannelShown = true; } addContextMenuItem({ diff --git a/client/js/keybinds.js b/client/js/keybinds.js index f164fd2f..cc31c8c6 100644 --- a/client/js/keybinds.js +++ b/client/js/keybinds.js @@ -2,7 +2,6 @@ const $ = require("jquery"); const Mousetrap = require("mousetrap"); -const utils = require("./utils"); const {vueApp} = require("./vue"); const store = require("./store").default; diff --git a/client/js/socket-events/init.js b/client/js/socket-events/init.js index f7ff1c9f..232c6365 100644 --- a/client/js/socket-events/init.js +++ b/client/js/socket-events/init.js @@ -108,7 +108,7 @@ function mergeNetworkData(newNetworks) { for (let n = 0; n < newNetworks.length; n++) { const network = newNetworks[n]; - const currentNetwork = vueApp.findNetwork(network.uuid); + const currentNetwork = store.getters.findNetwork(network.uuid); // If this network is new, set some default variables and initalize channel variables if (!currentNetwork) { diff --git a/client/js/socket-events/join.js b/client/js/socket-events/join.js index 9e0bf8d5..52c296e0 100644 --- a/client/js/socket-events/join.js +++ b/client/js/socket-events/join.js @@ -1,12 +1,13 @@ "use strict"; const socket = require("../socket"); -const {vueApp, initChannel, findNetwork} = require("../vue"); +const {vueApp, initChannel} = require("../vue"); +const store = require("../store").default; socket.on("join", function(data) { initChannel(data.chan); - const network = findNetwork(data.network); + const network = store.getters.findNetwork(data.network); if (!network) { return; @@ -19,5 +20,5 @@ socket.on("join", function(data) { return; } - vueApp.switchToChannel(vueApp.findChannel(data.chan.id)); + vueApp.switchToChannel(store.getters.findChannel(data.chan.id).channel); }); diff --git a/client/js/socket-events/more.js b/client/js/socket-events/more.js index 2a5547e9..2be668a1 100644 --- a/client/js/socket-events/more.js +++ b/client/js/socket-events/more.js @@ -1,10 +1,11 @@ "use strict"; const socket = require("../socket"); -const {vueApp, findChannel} = require("../vue"); +const {vueApp} = require("../vue"); +const store = require("../store").default; socket.on("more", function(data) { - const channel = findChannel(data.chan); + const channel = store.getters.findChannel(data.chan); if (!channel) { return; diff --git a/client/js/socket-events/msg.js b/client/js/socket-events/msg.js index 2cbe035a..a176cb4c 100644 --- a/client/js/socket-events/msg.js +++ b/client/js/socket-events/msg.js @@ -5,7 +5,7 @@ const socket = require("../socket"); const options = require("../options"); const cleanIrcMessage = require("../libs/handlebars/ircmessageparser/cleanIrcMessage"); const webpush = require("../webpush"); -const {vueApp, findChannel} = require("../vue"); +const {vueApp} = require("../vue"); const store = require("../store").default; let pop; @@ -20,7 +20,7 @@ try { } socket.on("msg", function(data) { - const receivingChannel = findChannel(data.chan); + const receivingChannel = store.getters.findChannel(data.chan); if (!receivingChannel) { return; diff --git a/client/js/socket-events/msg_preview.js b/client/js/socket-events/msg_preview.js index 8a7ed712..6b237eec 100644 --- a/client/js/socket-events/msg_preview.js +++ b/client/js/socket-events/msg_preview.js @@ -1,10 +1,11 @@ "use strict"; const socket = require("../socket"); -const {vueApp, findChannel} = require("../vue"); +const {vueApp} = require("../vue"); +const store = require("../store").default; socket.on("msg:preview", function(data) { - const {channel} = findChannel(data.chan); + const {channel} = store.getters.findChannel(data.chan); const message = channel.messages.find((m) => m.id === data.id); if (!message) { diff --git a/client/js/socket-events/msg_special.js b/client/js/socket-events/msg_special.js index 4ab89e7e..fe8d1e88 100644 --- a/client/js/socket-events/msg_special.js +++ b/client/js/socket-events/msg_special.js @@ -1,10 +1,11 @@ "use strict"; const socket = require("../socket"); -const {vueApp, findChannel} = require("../vue"); +const {vueApp} = require("../vue"); +const store = require("../store").default; socket.on("msg:special", function(data) { - const channel = findChannel(data.chan); + const channel = store.getters.findChannel(data.chan); channel.channel.data = data.data; vueApp.switchToChannel(channel.channel); }); diff --git a/client/js/socket-events/names.js b/client/js/socket-events/names.js index 73403264..624a26df 100644 --- a/client/js/socket-events/names.js +++ b/client/js/socket-events/names.js @@ -1,10 +1,10 @@ "use strict"; const socket = require("../socket"); -const {findChannel} = require("../vue"); +const store = require("../store").default; socket.on("names", function(data) { - const channel = findChannel(data.id); + const channel = store.getters.findChannel(data.id); if (channel) { channel.channel.users = data.users; diff --git a/client/js/socket-events/network.js b/client/js/socket-events/network.js index 0a329f7a..b89ba80c 100644 --- a/client/js/socket-events/network.js +++ b/client/js/socket-events/network.js @@ -1,7 +1,7 @@ "use strict"; const socket = require("../socket"); -const {vueApp, initChannel, findChannel, findNetwork} = require("../vue"); +const {vueApp, initChannel} = require("../vue"); const store = require("../store").default; socket.on("network", function(data) { @@ -16,7 +16,7 @@ socket.on("network", function(data) { }); socket.on("network:options", function(data) { - const network = findNetwork(data.network); + const network = store.getters.findNetwork(data.network); if (network) { network.serverOptions = data.serverOptions; @@ -24,7 +24,7 @@ socket.on("network:options", function(data) { }); socket.on("network:status", function(data) { - const network = findNetwork(data.network); + const network = store.getters.findNetwork(data.network); if (!network) { return; @@ -42,7 +42,7 @@ socket.on("network:status", function(data) { }); socket.on("channel:state", function(data) { - const channel = findChannel(data.chan); + const channel = store.getters.findChannel(data.chan); if (channel) { channel.channel.state = data.state; @@ -50,7 +50,7 @@ socket.on("channel:state", function(data) { }); socket.on("network:info", function(data) { - const network = findNetwork(data.uuid); + const network = store.getters.findNetwork(data.uuid); if (!network) { return; diff --git a/client/js/socket-events/nick.js b/client/js/socket-events/nick.js index 18bc9975..9189448c 100644 --- a/client/js/socket-events/nick.js +++ b/client/js/socket-events/nick.js @@ -1,10 +1,10 @@ "use strict"; const socket = require("../socket"); -const {findNetwork} = require("../vue"); +const store = require("../store").default; socket.on("nick", function(data) { - const network = findNetwork(data.network); + const network = store.getters.findNetwork(data.network); if (network) { network.nick = data.nick; diff --git a/client/js/socket-events/open.js b/client/js/socket-events/open.js index 06eab42a..8e052f46 100644 --- a/client/js/socket-events/open.js +++ b/client/js/socket-events/open.js @@ -1,7 +1,7 @@ "use strict"; const socket = require("../socket"); -const {vueApp, findChannel} = require("../vue"); +const {vueApp} = require("../vue"); const store = require("../store").default; // Sync unread badge and marker when other clients open a channel @@ -16,7 +16,7 @@ socket.on("open", function(id) { } // Clear the unread badge - const channel = findChannel(id); + const channel = store.getters.findChannel(id); if (channel) { channel.channel.highlight = 0; diff --git a/client/js/socket-events/part.js b/client/js/socket-events/part.js index 5f14011a..a1498039 100644 --- a/client/js/socket-events/part.js +++ b/client/js/socket-events/part.js @@ -1,16 +1,16 @@ "use strict"; const socket = require("../socket"); -const {vueApp, findChannel} = require("../vue"); +const {vueApp} = require("../vue"); const store = require("../store").default; socket.on("part", function(data) { // When parting from the active channel/query, jump to the network's lobby if (store.state.activeChannel && store.state.activeChannel.channel.id === data.chan) { - vueApp.switchToChannel(store.state.activeChannel.network); + vueApp.switchToChannel(store.state.activeChannel.network.channels[0]); } - const channel = findChannel(data.chan); + const channel = store.getters.findChannel(data.chan); if (channel) { channel.network.channels.splice( diff --git a/client/js/socket-events/sync_sort.js b/client/js/socket-events/sync_sort.js index db0d8226..d3c2df0b 100644 --- a/client/js/socket-events/sync_sort.js +++ b/client/js/socket-events/sync_sort.js @@ -1,7 +1,6 @@ "use strict"; const socket = require("../socket"); -const {findNetwork} = require("../vue"); const store = require("../store").default; socket.on("sync_sort", function(data) { @@ -14,7 +13,7 @@ socket.on("sync_sort", function(data) { break; case "channels": { - const network = findNetwork(data.target); + const network = store.getters.findNetwork(data.target); if (!network) { return; diff --git a/client/js/socket-events/topic.js b/client/js/socket-events/topic.js index 4bf3641b..48148765 100644 --- a/client/js/socket-events/topic.js +++ b/client/js/socket-events/topic.js @@ -1,10 +1,10 @@ "use strict"; const socket = require("../socket"); -const {findChannel} = require("../vue"); +const store = require("../store").default; socket.on("topic", function(data) { - const channel = findChannel(data.chan); + const channel = store.getters.findChannel(data.chan); if (channel) { channel.channel.topic = data.topic; diff --git a/client/js/socket-events/users.js b/client/js/socket-events/users.js index 3505e3cf..ae74ae8b 100644 --- a/client/js/socket-events/users.js +++ b/client/js/socket-events/users.js @@ -1,7 +1,6 @@ "use strict"; const socket = require("../socket"); -const {findChannel} = require("../vue"); const store = require("../store").default; socket.on("users", function(data) { @@ -11,7 +10,7 @@ socket.on("users", function(data) { }); } - const channel = findChannel(data.chan); + const channel = store.getters.findChannel(data.chan); if (channel) { channel.channel.usersOutdated = true; diff --git a/client/js/store.js b/client/js/store.js index 8952cbf8..77263fcd 100644 --- a/client/js/store.js +++ b/client/js/store.js @@ -94,6 +94,26 @@ const store = new Vuex.Store({ name = name.toLowerCase(); return state.activeChannel.network.channels.find((c) => c.name.toLowerCase() === name); }, + findChannel: (state) => (id) => { + for (const network of state.networks) { + for (const channel of network.channels) { + if (channel.id === id) { + return {network, channel}; + } + } + } + + return null; + }, + findNetwork: (state) => (uuid) => { + for (const network of state.networks) { + if (network.uuid === uuid) { + return network; + } + } + + return null; + }, }, }); diff --git a/client/js/vue.js b/client/js/vue.js index baba2f15..65e55190 100644 --- a/client/js/vue.js +++ b/client/js/vue.js @@ -96,26 +96,6 @@ const vueApp = new Vue({ toggleUserlist() { this.setUserlist(!this.$store.state.userlistOpen); }, - findChannel(id) { - for (const network of this.$store.state.networks) { - for (const channel of network.channels) { - if (channel.id === id) { - return {network, channel}; - } - } - } - - return null; - }, - findNetwork(uuid) { - for (const network of this.$store.state.networks) { - if (network.uuid === uuid) { - return network; - } - } - - return null; - }, switchToChannel(channel) { if ( this.$store.state.activeChannel && @@ -202,14 +182,6 @@ Vue.config.errorHandler = function(e) { console.error(e); // eslint-disable-line }; -function findChannel(id) { - return vueApp.findChannel(id); -} - -function findNetwork(uuid) { - return vueApp.findNetwork(uuid); -} - function initChannel(channel) { channel.pendingMessage = ""; channel.inputHistoryPosition = 0; @@ -232,8 +204,6 @@ function getActiveWindowComponent() { module.exports = { vueApp, - findChannel, - findNetwork, initChannel, getActiveWindowComponent, };