Move most things out of utils

This commit is contained in:
Pavel Djundik 2019-11-03 13:23:03 +02:00
parent 2f635069e0
commit 742cd8d4bf
7 changed files with 32 additions and 42 deletions

View file

@ -1,10 +1,10 @@
"use strict";
const socket = require("../socket");
const store = require("../store").default;
exports.input = function(args) {
const utils = require("../utils");
const socket = require("../socket");
const {vueApp} = require("../vue");
const store = require("../store").default;
if (args.length > 0) {
let channels = args[0];
@ -23,7 +23,7 @@ exports.input = function(args) {
channels = channelList.join(",");
const chan = utils.findCurrentNetworkChan(channels);
const chan = store.getters.findChannelOnCurrentNetwork(channels);
if (chan) {
vueApp.switchToChannel(chan);

View file

@ -7,6 +7,7 @@ const ContextMenu = require("./contextMenu");
const contextMenuActions = [];
const contextMenuItems = [];
const {vueApp, findChannel} = require("./vue");
const store = require("./store").default;
addDefaultItems();
registerEvents();
@ -47,7 +48,7 @@ function createContextMenu(that, event) {
function addWhoisItem() {
function whois(itemData) {
const chan = utils.findCurrentNetworkChan(itemData);
const chan = store.getters.findChannelOnCurrentNetwork(itemData);
if (chan) {
vueApp.switchToChannel(chan);
@ -82,7 +83,7 @@ function addWhoisItem() {
function addQueryItem() {
function query(itemData) {
const chan = utils.findCurrentNetworkChan(itemData);
const chan = store.getters.findChannelOnCurrentNetwork(itemData);
if (chan) {
vueApp.switchToChannel(chan);

View file

@ -24,7 +24,7 @@ Mousetrap.bind(["alt+up", "alt+down"], function(e, keys) {
}
target = channels.eq(target).click();
utils.scrollIntoViewNicely(target[0]);
scrollIntoViewNicely(target[0]);
return false;
});
@ -67,7 +67,7 @@ Mousetrap.bind(["alt+shift+up", "alt+shift+down"], function(e, keys) {
}
target = lobbies.eq(target).click();
utils.scrollIntoViewNicely(target[0]);
scrollIntoViewNicely(target[0]);
return false;
});
@ -169,3 +169,9 @@ $(document).on("keydown", (e) => {
input.trigger("focus");
});
function scrollIntoViewNicely(el) {
// Ideally this would use behavior: "smooth", but that does not consistently work in e.g. Chrome
// https://github.com/iamdustan/smoothscroll/issues/28#issuecomment-364061459
el.scrollIntoView({block: "center", inline: "nearest"});
}

View file

@ -3,14 +3,14 @@
const $ = require("jquery");
const socket = require("../socket");
const storage = require("../localStorage");
const utils = require("../utils");
const {getActiveWindowComponent} = require("../vue");
const store = require("../store").default;
let lastServerHash = -1;
socket.on("auth", function(data) {
// If we reconnected and serverHash differs, that means the server restarted
// And we will reload the page to grab the latest version
if (utils.serverHash > -1 && data.serverHash > -1 && data.serverHash !== utils.serverHash) {
if (lastServerHash > -1 && data.serverHash > -1 && data.serverHash !== lastServerHash) {
socket.disconnect();
store.commit("isConnected", false);
store.commit("currentUserVisibleError", "Server restarted, reloading…");
@ -19,7 +19,7 @@ socket.on("auth", function(data) {
}
if (data.serverHash > -1) {
utils.serverHash = data.serverHash;
lastServerHash = data.serverHash;
} else {
getActiveWindowComponent().inFlight = false;
}

View file

@ -12,7 +12,6 @@ const socket = io({
module.exports = socket;
const {requestIdleCallback} = require("./utils");
const store = require("./store").default;
socket.on("disconnect", handleDisconnect);
@ -57,3 +56,13 @@ function handleDisconnect(data) {
requestIdleCallback(() => socket.connect(), 2000);
}
}
function requestIdleCallback(callback, timeout) {
if (window.requestIdleCallback) {
// During an idle period the user agent will run idle callbacks in FIFO order
// until either the idle period ends or there are no more idle callbacks eligible to be run.
window.requestIdleCallback(callback, {timeout});
} else {
callback();
}
}

View file

@ -90,6 +90,10 @@ const store = new Vuex.Store({
getters: {
currentSession: (state) => state.sessions.find((item) => item.current),
otherSessions: (state) => state.sessions.filter((item) => !item.current),
findChannelOnCurrentNetwork: (state) => (name) => {
name = name.toLowerCase();
return state.activeChannel.network.channels.find((c) => c.name.toLowerCase() === name);
},
},
});

View file

@ -2,24 +2,11 @@
const $ = require("jquery");
const escape = require("css.escape");
const store = require("./store").default;
var serverHash = -1; // eslint-disable-line no-var
module.exports = {
findCurrentNetworkChan,
serverHash,
scrollIntoViewNicely,
hasRoleInChannel,
requestIdleCallback,
};
function findCurrentNetworkChan(name) {
name = name.toLowerCase();
return store.state.activeChannel.network.channels.find((c) => c.name.toLowerCase() === name);
}
// Given a channel element will determine if the lounge user or a given nick is one of the supplied roles.
function hasRoleInChannel(channel, roles, nick) {
if (!channel || !roles) {
@ -32,20 +19,3 @@ function hasRoleInChannel(channel, roles, nick) {
const user = channel.find(`.names .user[data-name="${escape(target)}"]`).first();
return user.parent().is("." + roles.join(", ."));
}
// Reusable scrollIntoView parameters for channel list / user list
function scrollIntoViewNicely(el) {
// Ideally this would use behavior: "smooth", but that does not consistently work in e.g. Chrome
// https://github.com/iamdustan/smoothscroll/issues/28#issuecomment-364061459
el.scrollIntoView({block: "center", inline: "nearest"});
}
function requestIdleCallback(callback, timeout) {
if (window.requestIdleCallback) {
// During an idle period the user agent will run idle callbacks in FIFO order
// until either the idle period ends or there are no more idle callbacks eligible to be run.
window.requestIdleCallback(callback, {timeout});
} else {
callback();
}
}