juggl/src/services/path.service.js

141 lines
3.7 KiB
JavaScript

import router from "@/router";
import { userService } from "@/services/user.service";
// TODO: Do I need this file?
/**
* Contains a bunch of path helper functions.
*/
export const pathService = {
/**
* Redirects to the not found page.
*
* @param {*} reason Optional: A reason to explain to the user, why they got redirected
* @param {*} sourceRoute Optional: Adds additional information from the source route to the query
*/
notFound(reason, sourceRoute) {
var payload = {};
if (reason !== undefined) {
payload.reason = reason;
}
if (sourceRoute !== undefined) {
payload.source = sourceRoute.path;
}
router.push({
path: "/404",
query: payload
});
},
/**
* Validates and gets all data to a path of the form /dashboard/:sem:-sem
*
* @param {*} route Object that describes the current path and its parameters
*
* @returns A promise of the form ({semester: semester})
*/
validateSemPath(route) {
var semesterNr = this.getSemesterNrFromParam(route.params.semesterName);
// Invalid path?
if (semesterNr === undefined) {
this.notFound("Invalid URL", route);
}
// Gather more data
return userService
.getSemester(semesterNr)
.then(semester => {
return { semester: semester.data };
})
.catch(() => {
// Semester could not be found
this.notFound(semesterNr + ". semester not found", route);
});
},
/**
* Validates and gets all data to a path of the form /dashboard/:sem:-sem/:module:
*
* @param {*} route Object that describes the current path and its parameters
*
* @returns A promise of the form ({semester: semester, module: module})
*/
validateSemModulePath(route) {
// First evaluate semester and than the module
return this.validateSemPath(route).then(obj => {
var moduleName = route.params.moduleName;
// Gather more data
return userService
.getModule(obj.semester.nr, moduleName)
.then(module => {
return { semester: obj.semester, module: module.data };
})
.catch(() => {
// Module could not be found
this.notFound('"' + moduleName + '" module not found', route);
});
});
},
/**
* Extracts the semester nr based on the url parameter in the format ":semesterNr:-sem".
*
* @param {*} semesterParam In the format ":semesterNr:-sem"
*
* @returns Semester Nr. if valid, otherwise undefined
*/
getSemesterNrFromParam(semesterParam) {
let re = new RegExp("^([1-9][0-9]*)-sem$");
var res = semesterParam.match(re);
// Found a match?
if (res === null) {
return undefined; // No
} else {
// First group is semester nr
return res[1];
}
},
getBreadcrumbs(route) {
let sites = route.path.split("/");
let siteCount = sites.length;
return sites.reduce((crumbs, current, index) => {
// Ignore empty parts
if (current.length <= 0) {
return crumbs;
}
var name = current;
// Handle semester name
var semNr = this.getSemesterNrFromParam(current);
if (semNr !== undefined) {
name = semNr + ". Semester";
}
// Handle decoding and capitalization
name = decodeURIComponent(name);
name = name.charAt(0).toUpperCase() + name.slice(1);
let part = {
text: name
};
let isActive = index + 1 == siteCount;
if (isActive) {
part.active = true;
} else {
part.to = crumbs[crumbs.length - 1]
? crumbs[crumbs.length - 1].to + "/" + current
: "/" + current;
}
crumbs.push(part);
return crumbs;
}, []);
}
};