2019-10-17 16:56:44 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const Vue = require("vue").default;
|
|
|
|
const VueRouter = require("vue-router").default;
|
|
|
|
Vue.use(VueRouter);
|
|
|
|
|
2019-11-02 19:40:59 +00:00
|
|
|
const store = require("./store").default;
|
|
|
|
|
2019-10-17 16:56:44 +00:00
|
|
|
const SignIn = require("../components/Windows/SignIn.vue").default;
|
|
|
|
const Connect = require("../components/Windows/Connect.vue").default;
|
|
|
|
const Settings = require("../components/Windows/Settings.vue").default;
|
|
|
|
const Help = require("../components/Windows/Help.vue").default;
|
|
|
|
const Changelog = require("../components/Windows/Changelog.vue").default;
|
2019-10-28 13:48:13 +00:00
|
|
|
const NetworkEdit = require("../components/Windows/NetworkEdit.vue").default;
|
2019-10-17 16:56:44 +00:00
|
|
|
const RoutedChat = require("../components/RoutedChat.vue").default;
|
2019-11-07 23:47:45 +00:00
|
|
|
const constants = require("./constants");
|
2019-10-17 16:56:44 +00:00
|
|
|
|
|
|
|
const router = new VueRouter({
|
|
|
|
routes: [
|
2019-10-20 16:17:21 +00:00
|
|
|
{
|
2019-11-03 17:09:10 +00:00
|
|
|
name: "SignIn",
|
2019-10-20 16:17:21 +00:00
|
|
|
path: "/sign-in",
|
|
|
|
component: SignIn,
|
2019-11-07 16:48:45 +00: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 16:17:21 +00:00
|
|
|
},
|
2019-11-03 15:56:41 +00:00
|
|
|
],
|
|
|
|
});
|
|
|
|
|
2019-11-07 16:48:45 +00:00
|
|
|
router.beforeEach((to, from, next) => {
|
2019-11-08 11:58:58 +00:00
|
|
|
// Disallow navigating to non-existing routes
|
2019-11-08 18:50:28 +00:00
|
|
|
if (store.state.appLoaded && !to.matched.length) {
|
2019-11-08 11:58:58 +00:00
|
|
|
next(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-08 12:56:18 +00:00
|
|
|
// Disallow navigating to invalid channels
|
2019-11-11 19:18:55 +00:00
|
|
|
if (to.name === "RoutedChat" && !store.getters.findChannel(Number(to.params.id))) {
|
2019-11-08 12:56:18 +00:00
|
|
|
next(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-07 16:48:45 +00: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();
|
|
|
|
});
|
|
|
|
|
2019-11-11 19:18:55 +00:00
|
|
|
router.afterEach(() => {
|
2019-11-05 19:29:51 +00:00
|
|
|
if (store.state.appLoaded) {
|
2019-11-07 23:47:45 +00:00
|
|
|
if (window.innerWidth <= constants.mobileViewportPixels) {
|
|
|
|
store.commit("sidebarOpen", false);
|
|
|
|
}
|
2019-11-03 15:56:41 +00:00
|
|
|
}
|
|
|
|
|
2019-11-11 19:18:55 +00:00
|
|
|
if (store.state.activeChannel) {
|
|
|
|
const channel = store.state.activeChannel.channel;
|
|
|
|
store.commit("activeChannel", null);
|
2019-11-03 15:56:41 +00:00
|
|
|
|
2019-11-11 19:18:55 +00: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 15:56:41 +00:00
|
|
|
}
|
|
|
|
|
2019-11-11 19:18:55 +00:00
|
|
|
if (channel.messages.length > 100) {
|
|
|
|
channel.messages.splice(0, channel.messages.length - 100);
|
|
|
|
channel.moreHistoryAvailable = true;
|
|
|
|
}
|
2019-11-03 15:56:41 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function initialize() {
|
|
|
|
router.addRoutes([
|
|
|
|
{
|
2019-11-03 17:09:10 +00:00
|
|
|
name: "Connect",
|
2019-11-12 10:39:46 +00:00
|
|
|
path: "/connect",
|
2019-10-20 16:17:21 +00:00
|
|
|
component: Connect,
|
2019-11-08 18:50:28 +00:00
|
|
|
props: (route) => ({queryParams: route.query}),
|
2019-10-20 16:17:21 +00:00
|
|
|
},
|
|
|
|
{
|
2019-11-03 17:09:10 +00:00
|
|
|
name: "Settings",
|
2019-10-20 16:17:21 +00:00
|
|
|
path: "/settings",
|
|
|
|
component: Settings,
|
|
|
|
},
|
|
|
|
{
|
2019-11-03 17:09:10 +00:00
|
|
|
name: "Help",
|
2019-10-20 16:17:21 +00:00
|
|
|
path: "/help",
|
|
|
|
component: Help,
|
|
|
|
},
|
|
|
|
{
|
2019-11-03 17:09:10 +00:00
|
|
|
name: "Changelog",
|
2019-10-20 16:17:21 +00:00
|
|
|
path: "/changelog",
|
|
|
|
component: Changelog,
|
|
|
|
},
|
2019-10-28 13:48:13 +00:00
|
|
|
{
|
2019-11-03 17:09:10 +00:00
|
|
|
name: "NetworkEdit",
|
2019-10-28 13:48:13 +00:00
|
|
|
path: "/edit-network/:uuid",
|
|
|
|
component: NetworkEdit,
|
|
|
|
},
|
2019-10-20 16:17:21 +00:00
|
|
|
{
|
2019-11-03 17:09:10 +00:00
|
|
|
name: "RoutedChat",
|
2019-11-11 19:18:55 +00:00
|
|
|
path: "/chan-:id",
|
2019-10-20 16:17:21 +00:00
|
|
|
component: RoutedChat,
|
|
|
|
},
|
2019-11-03 15:56:41 +00:00
|
|
|
]);
|
|
|
|
}
|
2019-10-20 16:17:21 +00:00
|
|
|
|
2019-11-11 19:18:55 +00:00
|
|
|
function navigate(routeName, params = {}) {
|
|
|
|
router.push({name: routeName, params}).catch(() => {});
|
|
|
|
}
|
|
|
|
|
2019-11-03 15:56:41 +00:00
|
|
|
module.exports = {
|
|
|
|
initialize,
|
|
|
|
router,
|
2019-11-11 19:18:55 +00:00
|
|
|
navigate,
|
|
|
|
switchToChannel: (channel) => navigate("RoutedChat", {id: channel.id}),
|
2019-11-03 15:56:41 +00:00
|
|
|
};
|