UNPKG

knowmax-quest-utils

Version:

Utilities for creating a Knowmax Quest client.

113 lines 3.67 kB
import { getLevels } from './questid'; /** Get all nodes in given node hierarchy that match given path. * @param list Hierarchy of nodes. * @param questId Specifies hierarchical identification of selected nodes. For example: hfd1/par1-1/lid1 */ export const getNodesInPath = (list, questId) => { const nodes = []; let levellist = list; getLevels(questId).forEach((level) => { if (levellist) { const node = levellist.find((n) => n.questId === level); if (node) { nodes.push(node); levellist = node.child ?? undefined; } else { levellist = undefined; } } }); return nodes; }; /** Find a node by QuestId in given hierarchy * @param list Hierarchy of nodes * @param questId Quest id of node to find. */ export const findNodeByQuestId = (list, questId) => { return findNode(list, (node) => node.questId === questId); }; /** Find a node in given node hierarchy. * @param list Hierarchy of nodes * @param isMatch Function that returns true if node is the one we are looking for. */ export const findNode = (list, isMatch) => { let requestednode = undefined; list.every((node) => { if (isMatch(node)) { requestednode = node; return false; } if (node.child) { requestednode = findNode(node.child, isMatch); } return requestednode === undefined; }); return requestednode; }; /** Get next node adjecent to node identified by quest id in given node hierarchy. * @param list Hierarchy of nodes * @param questId Quest id of document to find next node for. Undefined returns first node. */ export const getNextNode = (list, questId) => { if (questId) { return getAdjacentNode(list, questId, 'next'); } else if (list.length > 0) { return list[0]; } else { return undefined; } }; /** Get previous node adjecent to node identified by quest id in given node hierarchy. * @param list Hierarchy of nodes * @param questId Quest id of document to find previous node for. Undefined returns undefined. */ export const getPreviousNode = (list, questId) => { if (questId) { return getAdjacentNode(list, questId, 'previous'); } else { return undefined; } }; const getAdjacentNode = (list, questId, type) => { let _list = list; let levellist = []; const levels = getLevels(questId); var current = undefined; levels.forEach((level) => { if (_list) { const index = _list.findIndex((node) => node.questId === level); if (index !== -1) { levellist.push({ index: index, list: _list }); current = _list[index]; _list = _list[index].child; } } }); if (levels.length === levellist.length && current) { if (type === 'next' && !current.mimeType && current.child && current.child.length > 0) { // Jump to first child for current nodes without contents return current.child[0]; } let node = undefined; while (levellist.length > 0 && !node) { const l = levellist.pop(); if (l) { if (type === 'next' && l.index + 1 < l.list.length) { node = l.list[l.index + 1]; } else if (type === 'previous' && l.index > 0) { node = l.list[l.index - 1]; } } } return node; } else { return undefined; } }; //# sourceMappingURL=node.js.map