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

81 lines
2.6 KiB
JavaScript
Raw Normal View History

2021-01-08 11:07:02 +01:00
export default {
install(Vue, router) {
var routes = [];
2021-01-28 22:13:08 +01:00
var nextRoutes = null;
2021-01-08 11:07:02 +01:00
var zeroIndex = undefined;
router.beforeResolve((to, ignoredfrom, next) => {
if (!zeroIndex) {
routes = [to];
zeroIndex = window.history.length;
}
next();
})
router.beforeEach((to, from, next) => {
if (nextRoutes) {
console.log("Nav: next routes set, going:", routes, nextRoutes);
routes = nextRoutes;
nextRoutes = null;
if (routes.length > 0) {
console.log("Redirecting to", routes[routes.length - 1]);
next(routes[routes.length - 1]);
2021-01-28 22:13:08 +01:00
return;
}
2021-01-08 11:07:02 +01:00
}
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) {
2021-01-28 22:13:08 +01:00
nextRoutes = [route];
2021-01-11 17:42:58 +01:00
} else if (mode == 0) {
// Replace
2021-01-28 22:13:08 +01:00
nextRoutes = [...routes];
nextRoutes.pop();
nextRoutes.push(route);
2021-01-08 11:07:02 +01:00
} else {
2021-01-28 22:13:08 +01:00
nextRoutes = [...routes];
nextRoutes.push(route);
}
const index = nextRoutes.length - routes.length;
const targetIndex = nextRoutes.length - 1;
console.log("Nav - index " + index + " Target " + targetIndex);
if (index < 0) {
console.log("Nav - go " + index);
router.go(index);
} else if (index == 0) {
console.log("Nav - replace");
routes = nextRoutes;
nextRoutes = null;
router.replace(route).catch((ignoredErr) => {});
} else {
console.log("Nav - push");
2021-01-08 11:07:02 +01:00
router.push(route).catch((ignoredErr) => {});
}
},
canPop() {
2021-01-28 22:13:08 +01:00
if (nextRoutes) {
return nextRoutes.length > 1;
}
2021-01-08 11:07:02 +01:00
return routes.length > 1;
},
pop() {
routes.pop();
2021-01-08 11:07:02 +01:00
router.go(-1);
}
}
Vue.prototype.$navigation = navigationService;
}
}