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

View File

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

View File

@ -24,7 +24,7 @@ Mousetrap.bind(["alt+up", "alt+down"], function(e, keys) {
} }
target = channels.eq(target).click(); target = channels.eq(target).click();
utils.scrollIntoViewNicely(target[0]); scrollIntoViewNicely(target[0]);
return false; return false;
}); });
@ -67,7 +67,7 @@ Mousetrap.bind(["alt+shift+up", "alt+shift+down"], function(e, keys) {
} }
target = lobbies.eq(target).click(); target = lobbies.eq(target).click();
utils.scrollIntoViewNicely(target[0]); scrollIntoViewNicely(target[0]);
return false; return false;
}); });
@ -169,3 +169,9 @@ $(document).on("keydown", (e) => {
input.trigger("focus"); 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 $ = require("jquery");
const socket = require("../socket"); const socket = require("../socket");
const storage = require("../localStorage"); const storage = require("../localStorage");
const utils = require("../utils");
const {getActiveWindowComponent} = require("../vue"); const {getActiveWindowComponent} = require("../vue");
const store = require("../store").default; const store = require("../store").default;
let lastServerHash = -1;
socket.on("auth", function(data) { socket.on("auth", function(data) {
// If we reconnected and serverHash differs, that means the server restarted // If we reconnected and serverHash differs, that means the server restarted
// And we will reload the page to grab the latest version // 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(); socket.disconnect();
store.commit("isConnected", false); store.commit("isConnected", false);
store.commit("currentUserVisibleError", "Server restarted, reloading…"); store.commit("currentUserVisibleError", "Server restarted, reloading…");
@ -19,7 +19,7 @@ socket.on("auth", function(data) {
} }
if (data.serverHash > -1) { if (data.serverHash > -1) {
utils.serverHash = data.serverHash; lastServerHash = data.serverHash;
} else { } else {
getActiveWindowComponent().inFlight = false; getActiveWindowComponent().inFlight = false;
} }

View File

@ -12,7 +12,6 @@ const socket = io({
module.exports = socket; module.exports = socket;
const {requestIdleCallback} = require("./utils");
const store = require("./store").default; const store = require("./store").default;
socket.on("disconnect", handleDisconnect); socket.on("disconnect", handleDisconnect);
@ -57,3 +56,13 @@ function handleDisconnect(data) {
requestIdleCallback(() => socket.connect(), 2000); 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: { getters: {
currentSession: (state) => state.sessions.find((item) => item.current), currentSession: (state) => state.sessions.find((item) => item.current),
otherSessions: (state) => state.sessions.filter((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 $ = require("jquery");
const escape = require("css.escape"); const escape = require("css.escape");
const store = require("./store").default;
var serverHash = -1; // eslint-disable-line no-var
module.exports = { module.exports = {
findCurrentNetworkChan,
serverHash,
scrollIntoViewNicely,
hasRoleInChannel, 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. // 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) { function hasRoleInChannel(channel, roles, nick) {
if (!channel || !roles) { if (!channel || !roles) {
@ -32,20 +19,3 @@ function hasRoleInChannel(channel, roles, nick) {
const user = channel.find(`.names .user[data-name="${escape(target)}"]`).first(); const user = channel.find(`.names .user[data-name="${escape(target)}"]`).first();
return user.parent().is("." + roles.join(", .")); 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();
}
}