keanu-weblite/src/services/navigation.service.js

71 lines
2.2 KiB
JavaScript
Raw Normal View History

2021-01-08 11:07:02 +01:00
export default {
install(Vue, router) {
var routes = [];
var zeroIndex = undefined;
router.beforeResolve((to, ignoredfrom, next) => {
if (!zeroIndex) {
routes = [to];
zeroIndex = window.history.length;
}
next();
})
router.beforeEach((to, from, next) => {
const index = routes.findIndex((item) => {
return item.path == to.path || item.name == to.name;
});
if (index < 0 && routes.length > 0) {
next(routes[0]);
return;
}
// If we have a room id param, it needs to be the same, else we call "next" with the correct one
if (index >= 0 && routes[index].params && to.params && routes[index].params.roomId != to.params.roomId) {
next(routes[0]);
return;
}
2021-01-08 11:07:02 +01:00
if (index >= 0) {
routes.splice(index + 1);
}
next();
})
const navigationService = {
2021-01-11 17:42:58 +01:00
/***
* @param mode Mode of operation. -1 = push as root, 0 = replace, 1 = normal push
*/
push(route, mode) {
if (mode === undefined) {
mode = 1;
}
if (mode == -1) {
const i = routes.length - 1;
2021-01-08 11:07:02 +01:00
routes = [route];
if (i > 0) {
router.go(-i);
} else {
router.replace(route).catch((ignoredErr) => {});
}
2021-01-11 17:42:58 +01:00
} else if (mode == 0) {
// Replace
routes.pop()
routes.push(route);
router.replace(route).catch((ignoredErr) => {});
2021-01-08 11:07:02 +01:00
} else {
routes.push(route);
router.push(route).catch((ignoredErr) => {});
}
},
canPop() {
return routes.length > 1;
},
pop() {
router.go(-1);
}
}
Vue.prototype.$navigation = navigationService;
}
}