knowmax-quest-utils
Version:
Utilities for creating a Knowmax Quest client.
121 lines • 4.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPreviousNode = exports.getNextNode = exports.findNode = exports.findNodeByQuestId = exports.getNodesInPath = void 0;
const questid_1 = require("./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
*/
const getNodesInPath = (list, questId) => {
const nodes = [];
let levellist = list;
(0, questid_1.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;
};
exports.getNodesInPath = getNodesInPath;
/** Find a node by QuestId in given hierarchy
* @param list Hierarchy of nodes
* @param questId Quest id of node to find.
*/
const findNodeByQuestId = (list, questId) => {
return (0, exports.findNode)(list, (node) => node.questId === questId);
};
exports.findNodeByQuestId = findNodeByQuestId;
/** 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.
*/
const findNode = (list, isMatch) => {
let requestednode = undefined;
list.every((node) => {
if (isMatch(node)) {
requestednode = node;
return false;
}
if (node.child) {
requestednode = (0, exports.findNode)(node.child, isMatch);
}
return requestednode === undefined;
});
return requestednode;
};
exports.findNode = findNode;
/** 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.
*/
const getNextNode = (list, questId) => {
if (questId) {
return getAdjacentNode(list, questId, 'next');
}
else if (list.length > 0) {
return list[0];
}
else {
return undefined;
}
};
exports.getNextNode = getNextNode;
/** 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.
*/
const getPreviousNode = (list, questId) => {
if (questId) {
return getAdjacentNode(list, questId, 'previous');
}
else {
return undefined;
}
};
exports.getPreviousNode = getPreviousNode;
const getAdjacentNode = (list, questId, type) => {
let _list = list;
let levellist = [];
const levels = (0, questid_1.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