thelounge/client/js/router.js

146 lines
3.2 KiB
JavaScript
Raw Normal View History

2019-10-17 18:56:44 +02:00
"use strict";
2019-12-17 23:10:50 +01:00
const constants = require("./constants");
2019-11-16 18:24:03 +01:00
import Vue from "vue";
import VueRouter from "vue-router";
2019-10-17 18:56:44 +02:00
2019-11-16 18:24:03 +01:00
Vue.use(VueRouter);
2019-11-16 18:24:03 +01:00
import SignIn from "../components/Windows/SignIn.vue";
import Connect from "../components/Windows/Connect.vue";
import Settings from "../components/Windows/Settings.vue";
import Help from "../components/Windows/Help.vue";
import Changelog from "../components/Windows/Changelog.vue";
import NetworkEdit from "../components/Windows/NetworkEdit.vue";
import RoutedChat from "../components/RoutedChat.vue";
import store from "./store";
2019-10-17 18:56:44 +02:00
const router = new VueRouter({
routes: [
2019-10-20 18:17:21 +02:00
{
2019-11-03 18:09:10 +01:00
name: "SignIn",
2019-10-20 18:17:21 +02:00
path: "/sign-in",
component: SignIn,
2019-11-07 17:48:45 +01:00
beforeEnter(to, from, next) {
// Prevent navigating to sign-in when already signed in
if (store.state.appLoaded) {
next(false);
return;
}
next();
},
2019-10-20 18:17:21 +02:00
},
2019-11-03 16:56:41 +01:00
],
});
2019-11-07 17:48:45 +01:00
router.beforeEach((to, from, next) => {
// Disallow navigating to non-existing routes
2019-11-08 19:50:28 +01:00
if (store.state.appLoaded && !to.matched.length) {
next(false);
return;
}
// Disallow navigating to invalid channels
2019-11-11 20:18:55 +01:00
if (to.name === "RoutedChat" && !store.getters.findChannel(Number(to.params.id))) {
next(false);
return;
}
2019-11-07 17:48:45 +01:00
// Handle closing image viewer with the browser back button
if (!router.app.$refs.app) {
next();
return;
}
const imageViewer = router.app.$root.$refs.app.$refs.imageViewer;
if (imageViewer && imageViewer.link) {
imageViewer.closeViewer();
next(false);
return;
}
next();
});
router.afterEach((to) => {
2019-11-05 20:29:51 +01:00
if (store.state.appLoaded) {
if (window.innerWidth <= constants.mobileViewportPixels) {
store.commit("sidebarOpen", false);
}
2019-11-03 16:56:41 +01:00
}
2019-11-11 20:18:55 +01:00
if (store.state.activeChannel) {
const channel = store.state.activeChannel.channel;
if (to.name !== "RoutedChat") {
store.commit("activeChannel", null);
}
2019-11-03 16:56:41 +01:00
2019-11-11 20:18:55 +01:00
// When switching out of a channel, mark everything as read
if (channel.messages.length > 0) {
channel.firstUnread = channel.messages[channel.messages.length - 1].id;
2019-11-03 16:56:41 +01:00
}
2019-11-11 20:18:55 +01:00
if (channel.messages.length > 100) {
channel.messages.splice(0, channel.messages.length - 100);
channel.moreHistoryAvailable = true;
}
2019-11-03 16:56:41 +01:00
}
});
function initialize() {
router.addRoutes([
{
2019-11-03 18:09:10 +01:00
name: "Connect",
path: "/connect",
2019-10-20 18:17:21 +02:00
component: Connect,
2019-11-08 19:50:28 +01:00
props: (route) => ({queryParams: route.query}),
2019-10-20 18:17:21 +02:00
},
{
2019-11-03 18:09:10 +01:00
name: "Settings",
2019-10-20 18:17:21 +02:00
path: "/settings",
component: Settings,
},
{
2019-11-03 18:09:10 +01:00
name: "Help",
2019-10-20 18:17:21 +02:00
path: "/help",
component: Help,
},
{
2019-11-03 18:09:10 +01:00
name: "Changelog",
2019-10-20 18:17:21 +02:00
path: "/changelog",
component: Changelog,
},
{
2019-11-03 18:09:10 +01:00
name: "NetworkEdit",
path: "/edit-network/:uuid",
component: NetworkEdit,
},
2019-10-20 18:17:21 +02:00
{
2019-11-03 18:09:10 +01:00
name: "RoutedChat",
2019-11-11 20:18:55 +01:00
path: "/chan-:id",
2019-10-20 18:17:21 +02:00
component: RoutedChat,
},
2019-11-03 16:56:41 +01:00
]);
}
2019-10-20 18:17:21 +02:00
2019-11-11 20:18:55 +01:00
function navigate(routeName, params = {}) {
if (router.currentRoute.name) {
router.push({name: routeName, params}).catch(() => {});
} else {
// If current route is null, replace the history entry
// This prevents invalid entries from lingering in history,
// and then the route guard preventing proper navigation
router.replace({name: routeName, params}).catch(() => {});
}
2019-11-11 20:18:55 +01:00
}
2019-11-16 18:24:03 +01:00
function switchToChannel(channel) {
return navigate("RoutedChat", {id: channel.id});
}
export {initialize, router, navigate, switchToChannel};