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: {
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);

View file

@ -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);

View file

@ -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;

View file

@ -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;
},
},

View file

@ -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();
},

View file

@ -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({

View file

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

View file

@ -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) {

View file

@ -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);
});

View file

@ -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;

View file

@ -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;

View file

@ -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) {

View file

@ -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);
});

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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(

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;
},
},
});

View file

@ -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,
};