2019-11-08 03:40:20 +01:00
|
|
|
import { useStaticQuery } from 'gatsby';
|
|
|
|
import { graphql } from 'gatsby';
|
|
|
|
import { HierarchyQuery, SilverstripeDocument } from '../types';
|
|
|
|
import sortFiles from './sortFiles';
|
|
|
|
|
|
|
|
let __nodes: SilverstripeDocument[] | undefined;
|
|
|
|
let __currentNode: SilverstripeDocument | null = null;
|
2019-11-12 04:09:29 +01:00
|
|
|
let __currentVersion: string | null = null;
|
2019-11-08 03:40:20 +01:00
|
|
|
let __home: SilverstripeDocument | null = null;
|
|
|
|
|
|
|
|
const childrenMap = new Map();
|
|
|
|
const navChildrenMap = new Map();
|
|
|
|
const siblingMap = new Map();
|
|
|
|
const parentMap = new Map();
|
|
|
|
|
2019-11-19 04:29:29 +01:00
|
|
|
/**
|
|
|
|
* Get all documents in the source
|
|
|
|
*/
|
2019-11-08 03:40:20 +01:00
|
|
|
const getNodes = (): SilverstripeDocument[] => {
|
|
|
|
if (__nodes) {
|
|
|
|
return __nodes;
|
|
|
|
}
|
|
|
|
const result:HierarchyQuery = useStaticQuery(graphql`
|
|
|
|
{
|
|
|
|
allSilverstripeDocument {
|
|
|
|
nodes {
|
|
|
|
title
|
|
|
|
summary
|
|
|
|
isIndex
|
|
|
|
introduction
|
|
|
|
icon
|
2019-11-12 05:05:31 +01:00
|
|
|
iconBrand
|
2019-11-08 03:40:20 +01:00
|
|
|
hideChildren
|
|
|
|
slug
|
|
|
|
parentSlug
|
|
|
|
fileTitle
|
|
|
|
fileAbsolutePath
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
);
|
|
|
|
__nodes = result.allSilverstripeDocument.nodes.map(node => ({
|
|
|
|
...node,
|
|
|
|
}));
|
|
|
|
|
|
|
|
return __nodes;
|
|
|
|
};
|
|
|
|
|
2019-11-19 04:29:29 +01:00
|
|
|
/**
|
|
|
|
* Get the children of a given node
|
|
|
|
*
|
|
|
|
* @param node
|
|
|
|
* @param includeFolders
|
|
|
|
*/
|
2019-11-08 03:40:20 +01:00
|
|
|
const getChildren = (
|
|
|
|
node: SilverstripeDocument,
|
|
|
|
includeFolders: boolean = true
|
|
|
|
): SilverstripeDocument[] => {
|
|
|
|
const sku = `${node.slug}${includeFolders ? '1' : '0'}`;
|
|
|
|
if (childrenMap.has(sku)) {
|
|
|
|
return childrenMap.get(sku);
|
|
|
|
}
|
|
|
|
const nodes = getNodes();
|
|
|
|
let children: SilverstripeDocument[] = [];
|
|
|
|
if (node.isIndex) {
|
|
|
|
children = nodes.filter(n => {
|
|
|
|
return n.parentSlug === node.slug && (includeFolders || !n.isIndex);
|
|
|
|
}).sort(sortFiles);
|
|
|
|
}
|
|
|
|
childrenMap.set(sku, children);
|
|
|
|
|
|
|
|
return childrenMap.get(sku);
|
|
|
|
}
|
|
|
|
|
2019-11-19 04:29:29 +01:00
|
|
|
/**
|
|
|
|
* Get children of a given node that should be shown in navigation
|
|
|
|
*
|
|
|
|
* @param node
|
|
|
|
*/
|
2019-11-08 03:40:20 +01:00
|
|
|
const getNavChildren = (node: SilverstripeDocument): SilverstripeDocument[] => {
|
|
|
|
if (navChildrenMap.has(node.slug)) {
|
|
|
|
return navChildrenMap.get(node.slug);
|
|
|
|
}
|
|
|
|
let children: SilverstripeDocument[] = [];
|
|
|
|
if (!node.hideChildren) {
|
|
|
|
children = getChildren(node, true).filter(n => !n.hideSelf);
|
|
|
|
}
|
|
|
|
|
|
|
|
navChildrenMap.set(node.slug, children);
|
|
|
|
|
|
|
|
return navChildrenMap.get(node.slug);
|
|
|
|
};
|
|
|
|
|
2019-11-19 04:29:29 +01:00
|
|
|
/**
|
|
|
|
* Get the siblings of a given node
|
|
|
|
*
|
|
|
|
* @param node
|
|
|
|
*/
|
2019-11-08 03:40:20 +01:00
|
|
|
const getSiblings = (node: SilverstripeDocument): SilverstripeDocument[] => {
|
|
|
|
if (siblingMap.has(node.slug)) {
|
|
|
|
return siblingMap.get(node.slug);
|
|
|
|
}
|
|
|
|
const nodes = getNodes();
|
|
|
|
const siblings = nodes.filter(n => n.parentSlug === node.parentSlug);
|
|
|
|
|
|
|
|
siblingMap.set(node.slug, siblings);
|
|
|
|
|
|
|
|
return siblingMap.get(node.slug);
|
|
|
|
};
|
|
|
|
|
2019-11-19 04:29:29 +01:00
|
|
|
/**
|
|
|
|
* Get the parent of a given node
|
|
|
|
* @param node
|
|
|
|
*/
|
2019-11-08 03:40:20 +01:00
|
|
|
const getParent = (node: SilverstripeDocument): SilverstripeDocument | null => {
|
|
|
|
if (parentMap.has(node.slug)) {
|
|
|
|
return parentMap.get(node.slug);
|
|
|
|
}
|
|
|
|
|
|
|
|
const nodes = getNodes();
|
|
|
|
const parent = nodes.find(n => n.slug === node.parentSlug) || null;
|
|
|
|
|
|
|
|
parentMap.set(node.slug, parent);
|
|
|
|
|
|
|
|
return parentMap.get(node.slug);
|
|
|
|
};
|
|
|
|
|
2019-11-19 04:29:29 +01:00
|
|
|
/**
|
|
|
|
* Get the current node. Must be set by setCurrentNode(string: slug)
|
|
|
|
*/
|
2019-11-12 04:09:29 +01:00
|
|
|
const getCurrentNode = (): SilverstripeDocument | null => __currentNode;
|
2019-11-08 03:40:20 +01:00
|
|
|
|
2019-11-19 04:29:29 +01:00
|
|
|
/**
|
|
|
|
* Get the home page
|
|
|
|
*/
|
2019-11-08 03:40:20 +01:00
|
|
|
const getHomePage = (): SilverstripeDocument | null => {
|
|
|
|
if (__home) {
|
|
|
|
return __home;
|
|
|
|
}
|
|
|
|
const nodes = getNodes();
|
2019-11-12 04:09:29 +01:00
|
|
|
const version = getCurrentVersion();
|
|
|
|
const homePage = nodes.find(n => n.slug === `/en/${version}/`) || null;
|
2019-11-08 03:40:20 +01:00
|
|
|
|
|
|
|
__home = homePage;
|
|
|
|
|
|
|
|
return __home;
|
|
|
|
};
|
|
|
|
|
2019-11-19 04:29:29 +01:00
|
|
|
/**
|
|
|
|
* Get the selected version
|
|
|
|
*/
|
2019-11-14 03:05:03 +01:00
|
|
|
const getCurrentVersion = (): string => __currentVersion || '4';
|
2019-11-12 04:09:29 +01:00
|
|
|
|
2019-11-19 04:29:29 +01:00
|
|
|
/**
|
|
|
|
* Set the current node by its slug.
|
|
|
|
* @param slug
|
|
|
|
*/
|
2019-11-12 04:09:29 +01:00
|
|
|
const setCurrentNode = (slug: string): void => {
|
|
|
|
const currentNode = getNodes().find(n => n.slug === slug) || null;
|
|
|
|
__currentNode = currentNode;
|
|
|
|
|
|
|
|
if (currentNode) {
|
|
|
|
const matches = currentNode.slug.match(/^\/en\/([0-9]+)\//);
|
|
|
|
if (matches) {
|
|
|
|
__currentVersion = matches[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-11-08 03:40:20 +01:00
|
|
|
export {
|
|
|
|
getNodes,
|
|
|
|
getChildren,
|
|
|
|
getSiblings,
|
|
|
|
getParent,
|
|
|
|
getCurrentNode,
|
|
|
|
getHomePage,
|
|
|
|
getNavChildren,
|
2019-11-12 04:09:29 +01:00
|
|
|
getCurrentVersion,
|
|
|
|
setCurrentNode
|
2019-11-08 03:40:20 +01:00
|
|
|
};
|