Make findChannel and findNetwork getters

This commit is contained in:
Pavel Djundik 2019-11-03 16:59:43 +02:00
parent 742cd8d4bf
commit aba2487126
23 changed files with 63 additions and 79 deletions

View File

@ -12,10 +12,7 @@ export default {
}, },
methods: { methods: {
onClick() { onClick() {
const channelToFind = this.channel.toLowerCase(); const existingChannel = this.$store.getters.findChannelOnCurrentNetwork(this.channel);
const existingChannel = this.$store.state.activeChannel.network.channels.find(
(c) => c.name.toLowerCase() === channelToFind
);
if (existingChannel) { if (existingChannel) {
this.$root.switchToChannel(existingChannel); this.$root.switchToChannel(existingChannel);

View File

@ -59,10 +59,7 @@ export default {
}, },
methods: { methods: {
onSubmit() { onSubmit() {
const channelToFind = this.inputChannel.toLowerCase(); const existingChannel = this.$store.getters.findChannelOnCurrentNetwork(this.channel);
const existingChannel = this.network.channels.find(
(c) => c.name.toLowerCase() === channelToFind
);
if (existingChannel) { if (existingChannel) {
this.$root.switchToChannel(existingChannel); this.$root.switchToChannel(existingChannel);

View File

@ -115,8 +115,7 @@ export default {
return; return;
} }
const {findChannel} = require("../js/vue"); const channel = this.$store.getters.findChannel(e.moved.element.id);
const channel = findChannel(e.moved.element.id);
if (!channel) { if (!channel) {
return; return;

View File

@ -14,7 +14,7 @@ export default {
computed: { computed: {
activeChannel() { activeChannel() {
const chan_id = parseInt(this.$route.params.pathMatch); 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; return channel;
}, },
}, },

View File

@ -35,7 +35,7 @@ export default {
methods: { methods: {
setNetworkData() { setNetworkData() {
socket.emit("network:get", this.$route.params.uuid); 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) { handleSubmit(data) {
this.disabled = true; this.disabled = true;
@ -43,7 +43,7 @@ export default {
const sidebar = $("#sidebar"); const sidebar = $("#sidebar");
// TODO: move networks to vuex and update state when the network info comes in // 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; network.name = network.channels[0].name = data.name;
sidebar.find(`.network[data-uuid="${data.uuid}"] .chan.lobby .name`).click(); sidebar.find(`.network[data-uuid="${data.uuid}"] .chan.lobby .name`).click();
}, },

View File

@ -6,7 +6,7 @@ const utils = require("./utils");
const ContextMenu = require("./contextMenu"); const ContextMenu = require("./contextMenu");
const contextMenuActions = []; const contextMenuActions = [];
const contextMenuItems = []; const contextMenuItems = [];
const {vueApp, findChannel} = require("./vue"); const {vueApp} = require("./vue");
const store = require("./store").default; const store = require("./store").default;
addDefaultItems(); addDefaultItems();
@ -323,7 +323,7 @@ function addChannelListItem() {
function addEditTopicItem() { function addEditTopicItem() {
function setEditTopic(itemData) { 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(); document.querySelector(`#sidebar .chan[data-id="${Number(itemData)}"]`).click();
vueApp.$nextTick(() => { vueApp.$nextTick(() => {
@ -359,7 +359,7 @@ function addBanListItem() {
function addJoinItem() { function addJoinItem() {
function openJoinForm(itemData) { function openJoinForm(itemData) {
findChannel(Number(itemData)).network.isJoinChannelShown = true; store.getters.findChannel(Number(itemData)).network.isJoinChannelShown = true;
} }
addContextMenuItem({ addContextMenuItem({

View File

@ -2,7 +2,6 @@
const $ = require("jquery"); const $ = require("jquery");
const Mousetrap = require("mousetrap"); const Mousetrap = require("mousetrap");
const utils = require("./utils");
const {vueApp} = require("./vue"); const {vueApp} = require("./vue");
const store = require("./store").default; const store = require("./store").default;

View File

@ -108,7 +108,7 @@ function mergeNetworkData(newNetworks) {
for (let n = 0; n < newNetworks.length; n++) { for (let n = 0; n < newNetworks.length; n++) {
const network = newNetworks[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 this network is new, set some default variables and initalize channel variables
if (!currentNetwork) { if (!currentNetwork) {

View File

@ -1,12 +1,13 @@
"use strict"; "use strict";
const socket = require("../socket"); 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) { socket.on("join", function(data) {
initChannel(data.chan); initChannel(data.chan);
const network = findNetwork(data.network); const network = store.getters.findNetwork(data.network);
if (!network) { if (!network) {
return; return;
@ -19,5 +20,5 @@ socket.on("join", function(data) {
return; return;
} }
vueApp.switchToChannel(vueApp.findChannel(data.chan.id)); vueApp.switchToChannel(store.getters.findChannel(data.chan.id).channel);
}); });

View File

@ -1,10 +1,11 @@
"use strict"; "use strict";
const socket = require("../socket"); const socket = require("../socket");
const {vueApp, findChannel} = require("../vue"); const {vueApp} = require("../vue");
const store = require("../store").default;
socket.on("more", function(data) { socket.on("more", function(data) {
const channel = findChannel(data.chan); const channel = store.getters.findChannel(data.chan);
if (!channel) { if (!channel) {
return; return;

View File

@ -5,7 +5,7 @@ const socket = require("../socket");
const options = require("../options"); const options = require("../options");
const cleanIrcMessage = require("../libs/handlebars/ircmessageparser/cleanIrcMessage"); const cleanIrcMessage = require("../libs/handlebars/ircmessageparser/cleanIrcMessage");
const webpush = require("../webpush"); const webpush = require("../webpush");
const {vueApp, findChannel} = require("../vue"); const {vueApp} = require("../vue");
const store = require("../store").default; const store = require("../store").default;
let pop; let pop;
@ -20,7 +20,7 @@ try {
} }
socket.on("msg", function(data) { socket.on("msg", function(data) {
const receivingChannel = findChannel(data.chan); const receivingChannel = store.getters.findChannel(data.chan);
if (!receivingChannel) { if (!receivingChannel) {
return; return;

View File

@ -1,10 +1,11 @@
"use strict"; "use strict";
const socket = require("../socket"); const socket = require("../socket");
const {vueApp, findChannel} = require("../vue"); const {vueApp} = require("../vue");
const store = require("../store").default;
socket.on("msg:preview", function(data) { 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); const message = channel.messages.find((m) => m.id === data.id);
if (!message) { if (!message) {

View File

@ -1,10 +1,11 @@
"use strict"; "use strict";
const socket = require("../socket"); const socket = require("../socket");
const {vueApp, findChannel} = require("../vue"); const {vueApp} = require("../vue");
const store = require("../store").default;
socket.on("msg:special", function(data) { socket.on("msg:special", function(data) {
const channel = findChannel(data.chan); const channel = store.getters.findChannel(data.chan);
channel.channel.data = data.data; channel.channel.data = data.data;
vueApp.switchToChannel(channel.channel); vueApp.switchToChannel(channel.channel);
}); });

View File

@ -1,10 +1,10 @@
"use strict"; "use strict";
const socket = require("../socket"); const socket = require("../socket");
const {findChannel} = require("../vue"); const store = require("../store").default;
socket.on("names", function(data) { socket.on("names", function(data) {
const channel = findChannel(data.id); const channel = store.getters.findChannel(data.id);
if (channel) { if (channel) {
channel.channel.users = data.users; channel.channel.users = data.users;

View File

@ -1,7 +1,7 @@
"use strict"; "use strict";
const socket = require("../socket"); const socket = require("../socket");
const {vueApp, initChannel, findChannel, findNetwork} = require("../vue"); const {vueApp, initChannel} = require("../vue");
const store = require("../store").default; const store = require("../store").default;
socket.on("network", function(data) { socket.on("network", function(data) {
@ -16,7 +16,7 @@ socket.on("network", function(data) {
}); });
socket.on("network:options", function(data) { socket.on("network:options", function(data) {
const network = findNetwork(data.network); const network = store.getters.findNetwork(data.network);
if (network) { if (network) {
network.serverOptions = data.serverOptions; network.serverOptions = data.serverOptions;
@ -24,7 +24,7 @@ socket.on("network:options", function(data) {
}); });
socket.on("network:status", function(data) { socket.on("network:status", function(data) {
const network = findNetwork(data.network); const network = store.getters.findNetwork(data.network);
if (!network) { if (!network) {
return; return;
@ -42,7 +42,7 @@ socket.on("network:status", function(data) {
}); });
socket.on("channel:state", function(data) { socket.on("channel:state", function(data) {
const channel = findChannel(data.chan); const channel = store.getters.findChannel(data.chan);
if (channel) { if (channel) {
channel.channel.state = data.state; channel.channel.state = data.state;
@ -50,7 +50,7 @@ socket.on("channel:state", function(data) {
}); });
socket.on("network:info", function(data) { socket.on("network:info", function(data) {
const network = findNetwork(data.uuid); const network = store.getters.findNetwork(data.uuid);
if (!network) { if (!network) {
return; return;

View File

@ -1,10 +1,10 @@
"use strict"; "use strict";
const socket = require("../socket"); const socket = require("../socket");
const {findNetwork} = require("../vue"); const store = require("../store").default;
socket.on("nick", function(data) { socket.on("nick", function(data) {
const network = findNetwork(data.network); const network = store.getters.findNetwork(data.network);
if (network) { if (network) {
network.nick = data.nick; network.nick = data.nick;

View File

@ -1,7 +1,7 @@
"use strict"; "use strict";
const socket = require("../socket"); const socket = require("../socket");
const {vueApp, findChannel} = require("../vue"); const {vueApp} = require("../vue");
const store = require("../store").default; const store = require("../store").default;
// Sync unread badge and marker when other clients open a channel // Sync unread badge and marker when other clients open a channel
@ -16,7 +16,7 @@ socket.on("open", function(id) {
} }
// Clear the unread badge // Clear the unread badge
const channel = findChannel(id); const channel = store.getters.findChannel(id);
if (channel) { if (channel) {
channel.channel.highlight = 0; channel.channel.highlight = 0;

View File

@ -1,16 +1,16 @@
"use strict"; "use strict";
const socket = require("../socket"); const socket = require("../socket");
const {vueApp, findChannel} = require("../vue"); const {vueApp} = require("../vue");
const store = require("../store").default; const store = require("../store").default;
socket.on("part", function(data) { socket.on("part", function(data) {
// When parting from the active channel/query, jump to the network's lobby // When parting from the active channel/query, jump to the network's lobby
if (store.state.activeChannel && store.state.activeChannel.channel.id === data.chan) { 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) { if (channel) {
channel.network.channels.splice( channel.network.channels.splice(

View File

@ -1,7 +1,6 @@
"use strict"; "use strict";
const socket = require("../socket"); const socket = require("../socket");
const {findNetwork} = require("../vue");
const store = require("../store").default; const store = require("../store").default;
socket.on("sync_sort", function(data) { socket.on("sync_sort", function(data) {
@ -14,7 +13,7 @@ socket.on("sync_sort", function(data) {
break; break;
case "channels": { case "channels": {
const network = findNetwork(data.target); const network = store.getters.findNetwork(data.target);
if (!network) { if (!network) {
return; return;

View File

@ -1,10 +1,10 @@
"use strict"; "use strict";
const socket = require("../socket"); const socket = require("../socket");
const {findChannel} = require("../vue"); const store = require("../store").default;
socket.on("topic", function(data) { socket.on("topic", function(data) {
const channel = findChannel(data.chan); const channel = store.getters.findChannel(data.chan);
if (channel) { if (channel) {
channel.channel.topic = data.topic; channel.channel.topic = data.topic;

View File

@ -1,7 +1,6 @@
"use strict"; "use strict";
const socket = require("../socket"); const socket = require("../socket");
const {findChannel} = require("../vue");
const store = require("../store").default; const store = require("../store").default;
socket.on("users", function(data) { 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) { if (channel) {
channel.channel.usersOutdated = true; channel.channel.usersOutdated = true;

View File

@ -94,6 +94,26 @@ const store = new Vuex.Store({
name = name.toLowerCase(); name = name.toLowerCase();
return state.activeChannel.network.channels.find((c) => c.name.toLowerCase() === name); 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;
},
}, },
}); });

View File

@ -96,26 +96,6 @@ const vueApp = new Vue({
toggleUserlist() { toggleUserlist() {
this.setUserlist(!this.$store.state.userlistOpen); 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) { switchToChannel(channel) {
if ( if (
this.$store.state.activeChannel && this.$store.state.activeChannel &&
@ -202,14 +182,6 @@ Vue.config.errorHandler = function(e) {
console.error(e); // eslint-disable-line console.error(e); // eslint-disable-line
}; };
function findChannel(id) {
return vueApp.findChannel(id);
}
function findNetwork(uuid) {
return vueApp.findNetwork(uuid);
}
function initChannel(channel) { function initChannel(channel) {
channel.pendingMessage = ""; channel.pendingMessage = "";
channel.inputHistoryPosition = 0; channel.inputHistoryPosition = 0;
@ -232,8 +204,6 @@ function getActiveWindowComponent() {
module.exports = { module.exports = {
vueApp, vueApp,
findChannel,
findNetwork,
initChannel, initChannel,
getActiveWindowComponent, getActiveWindowComponent,
}; };