Website/docs/assets/js/docs.js

404 lines
11 KiB
JavaScript
Raw Normal View History

2020-11-26 09:53:18 +01:00
// TODO: Override the marker.js renderer for urls to enable proper anchor support.
2020-11-25 13:34:49 +01:00
class Docs {
/**
* @param {string} defaultView
* @param {string} routesPath
*/
constructor(defaultViewPath, fetchPath = 'content', routesPath = './assets/data/routes.json') {
/** @type {string} */
this.defaultViewPath = defaultViewPath;
/** @type {string} */
this.fetchPath = fetchPath;
/** @type {string} */
this.routesPath = routesPath;
/** @type {Array.<JSON>} */
this.routesData = [];
/** @type {Array.<(Route, RoutingNode)} */
this.routes = [];
/** @type {Route} */
this.currentRoute = undefined;
this.onExec();
}
2020-11-26 09:53:18 +01:00
/**
* Called on instanciation by the constructor.
*/
2020-11-25 13:34:49 +01:00
async onExec() {
this.routesData = await this.fetchRoutes();
2020-11-26 09:53:18 +01:00
this.updateRoutes(this.routesData);
2020-11-25 13:34:49 +01:00
2020-11-26 09:53:18 +01:00
this.generateNav(this.routes, document.querySelector('aside'));
2020-11-25 13:34:49 +01:00
2020-11-26 09:53:18 +01:00
window.addEventListener('hashchange', () => this.handleLocation(window.location));
2020-11-25 13:34:49 +01:00
2020-11-26 09:53:18 +01:00
// Check for initial condition.
2020-11-25 13:34:49 +01:00
if (window.location.hash) {
2020-11-26 09:53:18 +01:00
// There is a hash in the address bar, load the view.
2020-11-25 13:34:49 +01:00
this.handleLocation(window.location);
} else if (typeof this.defaultViewPath !== 'undefined') {
2020-11-26 09:53:18 +01:00
// TODO: Load a default view.
console.log('Missing Feature !\nShould load the default view');
2020-11-25 13:34:49 +01:00
}
}
/**
2020-11-26 09:53:18 +01:00
* Fetch the json file containing the routes.
2020-11-25 13:34:49 +01:00
* @returns {Array.<JSON>} The array containing the routes.
*/
async fetchRoutes() {
const response = await fetch(this.routesPath);
const json = await response.json();
return json['routes'];
2020-11-25 13:34:49 +01:00
}
/**
2020-11-26 09:53:18 +01:00
* Update the routes.
* @param {Array.<JSON>} routesData
2020-11-25 13:34:49 +01:00
*/
2020-11-26 09:53:18 +01:00
updateRoutes(routesData) {
2020-11-25 13:34:49 +01:00
const routes = [];
routesData.forEach(routeData => {
if ('routes' in routeData) {
2020-11-26 09:53:18 +01:00
const node = new RoutingNode(
routeData['name'],
routeData['hidden'],
routeData['routes']
);
routes.push(node);
2020-11-25 13:34:49 +01:00
} else {
2020-11-26 09:53:18 +01:00
const route = new Route(
routeData['name'],
routeData['hidden'],
routeData['filepath']
);
routes.push(route);
2020-11-25 13:34:49 +01:00
}
});
2020-11-26 09:53:18 +01:00
this.routes = routes;
}
/**
* Generate the side nav HTML based on routing data.
* @param {Array.<RoutingItem>} routes
* @param {HTMLElement} target
*/
generateNav(routes, target) {
const nav = document.createElement('nav');
2020-11-25 13:34:49 +01:00
routes.forEach(routingItem => {
if (routingItem['hidden'] === false || typeof routingItem['hidden'] === 'undefined') {
const el = routingItem.buildSelf();
if (typeof el !== 'undefined') {
nav.appendChild(el);
}
2020-11-25 13:34:49 +01:00
}
});
target.appendChild(nav);
}
/**
2020-11-26 09:53:18 +01:00
* Handle if the view needs to be changed or to move to in-page anchor
* base on the location data.
2020-11-25 13:34:49 +01:00
* @param {Location} location
*/
handleLocation(location) {
2020-11-26 09:53:18 +01:00
const hashes = location.hash.split('#');
2020-11-25 13:34:49 +01:00
hashes.splice(0, 1);
if (typeof this.currentRoute === 'undefined' || this.currentRoute.filepath !== hashes[0]) {
2020-11-26 09:53:18 +01:00
// TODO: add check in case of no matching routes.
2020-11-25 13:34:49 +01:00
const route = this.findMatchingRoute(hashes[0], this.routes);
2020-11-26 09:53:18 +01:00
this.currentRoute = route;
// Scroll back to top of the content outlet.
document.querySelector('#router-outlet').scrollTop = 0;
2020-11-25 13:34:49 +01:00
this.displayRoute(route);
2020-11-26 09:53:18 +01:00
// Changing classes based on the new active route.
2020-11-25 13:34:49 +01:00
const activeLinks = document.querySelectorAll('a.active');
const newActiveLinks = document.querySelectorAll(`a[href='#${hashes[0]}']`);
if (activeLinks.length > 0) {
activeLinks.forEach(link => link.classList.remove('active'));
}
newActiveLinks.forEach(newLink => newLink.classList.add('active'));
if (hashes.length > 1) {
this.handleLocation(window.location);
}
} else {
2020-11-26 09:53:18 +01:00
// Page don't need to be changed, just move to anchor.
2020-11-25 13:34:49 +01:00
const el = document.querySelector(`#${hashes[1]}`);
el.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
}
/**
2020-11-26 09:53:18 +01:00
* Recursively search for a matching route based on a filepath.
2020-11-25 13:34:49 +01:00
* @param {string} filepath The filepath to search.
* @returns {Route} The matching route.
*/
findMatchingRoute(filepath, routes) {
2020-11-26 09:53:18 +01:00
// TODO: Break the loops in a more elegant way.
2020-11-25 13:34:49 +01:00
let match = undefined;
for (let routingItem of routes) {
2020-11-26 09:53:18 +01:00
// If the routingItem is a Route and have what we are looking for.
2020-11-25 13:34:49 +01:00
if (routingItem.hasOwnProperty('filepath') && routingItem['filepath'] === filepath) {
match = routingItem;
break;
}
// Recursive search to go deeper.
for (let v of Object.keys(routingItem)) {
if (typeof routingItem[v] === 'object' && v === 'routes') {
const o = this.findMatchingRoute(filepath, routingItem[v]);
if (o != null) {
match = o;
break;
}
}
}
}
return match;
}
/**
2020-11-26 09:53:18 +01:00
* Load and displays a route's content.
2020-11-25 13:34:49 +01:00
* @param {Route} route The route to load.
*/
async displayRoute(route) {
const content = await route.loadSelf(this.fetchPath);
const contentOutlet = document.querySelector('#router-outlet');
this.removeChildren(contentOutlet);
contentOutlet.innerHTML = marked(content);
Prism.highlightAll(); // edited
}
2020-11-26 09:53:18 +01:00
/**
* Utilitary function to remove any content inside an HTML element
* @param {HTMLElement} element
*/
2020-11-25 13:34:49 +01:00
removeChildren(element) {
let count = element.childNodes.length;
while (count--) {
element.removeChild(element.lastChild);
}
}
}
class RoutingItem {
/**
* @param {string} name
* @param {boolean} hidden
*/
constructor(name, hidden) {
this.name = name;
this.hidden = hidden;
}
buildSelf() {
const element = document.createElement('div');
2020-11-25 13:34:49 +01:00
element.innerText = this.name;
return element;
}
}
class Route extends RoutingItem {
/**
* @param {string} name
* @param {boolean} hidden
* @param {string} filepath
*/
constructor(name, hidden, filepath) {
super(name, hidden);
/** @type {string} */
this.filepath = filepath;
}
/**
* @param {boolean} force Wether to force generation or not despite the hidden property.
2020-11-25 13:34:49 +01:00
* @returns {HTMLAnchorElement} The Route as an HTML link.
*/
buildSelf(force = false) {
if (this.hidden === false || typeof this.hidden === 'undefined') {
force = true
}
2020-11-25 13:34:49 +01:00
if (force) {
const element = document.createElement('a');
element.href = `#${this.filepath}`;
element.innerText = this.name;
return element;
} else {
return;
}
2020-11-25 13:34:49 +01:00
}
/**
* @returns {string} The document content to load as a string.
*/
async loadSelf(prefix, suffix) {
if (typeof prefix === 'undefined') {
prefix = '';
} else {
prefix += '/';
}
if (typeof suffix === 'undefined') {
suffix = '';
} else {
suffix += '/';
}
const response = await fetch(`${prefix}${this.filepath}${suffix}`);
const text = await response.text();
return text;
}
}
class RoutingNode extends RoutingItem {
/**
* @param {string} name
* @param {boolean} hidden
* @param {Array.<JSON>} routesData
*/
constructor(name, hidden, routesData) {
super(name, hidden);
/** @type {Array.<JSON>} */
this.routesData = routesData;
/** @type {Array.<(Route,RoutingNode)>} */
this.routes = [];
this.onExec();
}
onExec() {
this.routesData.forEach(routeData => {
if ('routes' in routeData) {
this.routes.push(new RoutingNode(routeData['name'], routeData['hidden'], routeData['routes']));
2020-11-25 13:34:49 +01:00
} else {
this.routes.push(new Route(routeData['name'], routeData['hidden'], routeData['filepath']));
2020-11-25 13:34:49 +01:00
}
});
}
/**
* @returns {HTMLUListElement} The RoutingNode as an HTML unordered list.
*/
buildSelf(force = false) {
if (this.hidden === false || typeof this.hidden === 'undefined') {
force = true
2020-11-25 13:34:49 +01:00
}
if (force) {
const element = document.createElement('section');
element.classList.add('node', 'hide');
2020-11-25 13:34:49 +01:00
const heading = this.buildSelf_Heading(this.name, toggleDisplay);
const list = this.buildSelf_List(this.routes);
2020-11-25 13:34:49 +01:00
element.appendChild(heading);
element.appendChild(list);
2020-11-25 13:34:49 +01:00
return element;
} else {
return;
}
2020-11-25 13:34:49 +01:00
}
buildSelf_Heading(text, onClickCallback) {
const wrapper = document.createElement('div');
const headingText = document.createElement('span');
const headingIcon = document.createElement('img');
headingText.innerText = text;
headingIcon.src = './assets/img/icons/baseline_expand_more_white_24dp.png';
wrapper.appendChild(headingText);
wrapper.appendChild(headingIcon);
if (typeof onClickCallback === "function") {
wrapper.addEventListener('click', onClickCallback);
}
return wrapper;
}
/**
* @param {Array.<(Route,RoutingNode)>} entries
*/
buildSelf_List(entries) {
const list = document.createElement('ul');
entries.forEach(entry => {
const listItem = document.createElement('li');
const itemContent = entry.buildSelf();
if (typeof itemContent !== 'undefined') {
listItem.appendChild(itemContent);
list.appendChild(listItem);
}
});
return list;
}
2020-11-25 13:34:49 +01:00
}
// TODO: find them a new home
/**
* @param {Event} event
*/
function toggleDisplay(event) {
const dropdown = event.currentTarget.parentNode;
// const list = dropdown.querySelector('ul');
toggleClass(dropdown, 'hide');
};
/**
* @param {HTMLElement} el
* @param {string} className
*/
function toggleClass(el, className) {
if (el.classList.contains(className)) {
el.classList.remove(className);
} else {
el.classList.add(className);
}
};