thelounge/client/js/vue.js

119 lines
2.4 KiB
JavaScript
Raw Normal View History

"use strict";
2019-12-17 23:10:50 +01:00
const constants = require("./constants");
import "../css/style.css";
2019-11-16 18:24:03 +01:00
import Vue from "vue";
import store from "./store";
import App from "../components/App.vue";
import storage from "./localStorage";
import {router, navigate} from "./router";
import socket from "./socket";
2020-03-16 18:58:40 +01:00
import eventbus from "./eventbus";
2019-11-16 18:24:03 +01:00
import "./socket-events";
import "./webpush";
import "./keybinds";
2019-11-12 16:51:40 +01:00
const favicon = document.getElementById("favicon");
const faviconNormal = favicon.getAttribute("href");
const faviconAlerted = favicon.dataset.other;
2020-03-16 18:58:40 +01:00
new Vue({
el: "#viewport",
2019-10-17 18:56:44 +02:00
router,
2018-07-10 11:40:55 +02:00
mounted() {
2019-11-12 16:51:40 +01:00
socket.open();
2018-07-10 11:40:55 +02:00
},
methods: {
2019-10-25 23:37:40 +02:00
switchToChannel(channel) {
2019-11-11 20:18:55 +01:00
navigate("RoutedChat", {id: channel.id});
2019-10-17 18:56:44 +02:00
},
2019-11-23 17:44:23 +01:00
closeChannel(channel) {
if (channel.type === "lobby") {
2020-03-16 18:58:40 +01:00
eventbus.emit(
2020-02-25 10:16:05 +01:00
"confirm-dialog",
{
title: "Remove network",
text: `Are you sure you want to quit and remove ${channel.name}? This cannot be undone.`,
button: "Remove network",
},
(result) => {
if (!result) {
return;
}
channel.closed = true;
socket.emit("input", {
target: Number(channel.id),
text: "/quit",
});
}
);
return;
2019-11-23 17:44:23 +01:00
}
channel.closed = true;
socket.emit("input", {
target: Number(channel.id),
text: "/close",
2019-11-23 17:44:23 +01:00
});
},
},
render(createElement) {
return createElement(App, {
2019-02-18 10:18:32 +01:00
ref: "app",
props: this,
});
},
store,
});
store.watch(
(state) => state.sidebarOpen,
(sidebarOpen) => {
if (window.innerWidth > constants.mobileViewportPixels) {
storage.set("thelounge.state.sidebar", sidebarOpen);
2020-03-16 18:58:40 +01:00
eventbus.emit("resize");
}
}
);
store.watch(
(state) => state.userlistOpen,
(userlistOpen) => {
storage.set("thelounge.state.userlist", userlistOpen);
2020-03-16 18:58:40 +01:00
eventbus.emit("resize");
}
);
store.watch(
(_, getters) => getters.title,
(title) => {
document.title = title;
}
);
// Toggles the favicon to red when there are unread notifications
store.watch(
(_, getters) => getters.highlightCount,
(highlightCount) => {
favicon.setAttribute("href", highlightCount > 0 ? faviconAlerted : faviconNormal);
2020-04-22 13:16:39 +02:00
if (navigator.setAppBadge) {
if (highlightCount > 0) {
navigator.setAppBadge(highlightCount);
} else {
navigator.clearAppBadge();
}
}
}
);
Vue.config.errorHandler = function (e) {
store.commit("currentUserVisibleError", `Vue error: ${e.message}`);
2018-09-14 17:44:26 +02:00
console.error(e); // eslint-disable-line
};