83 lines
2.3 KiB
TypeScript
83 lines
2.3 KiB
TypeScript
import { IApplication } from './../../../domain/IApplication';
|
|
import { ICategory, IScript } from '@/domain/ICategory';
|
|
import { INode } from './SelectableTree/INode';
|
|
|
|
export function parseAllCategories(app: IApplication): INode[] | undefined {
|
|
const nodes = new Array<INode>();
|
|
for (const category of app.actions) {
|
|
const children = parseCategoryRecursively(category);
|
|
nodes.push(convertCategoryToNode(category, children));
|
|
}
|
|
return nodes;
|
|
}
|
|
|
|
export function parseSingleCategory(categoryId: number, app: IApplication): INode[] | undefined {
|
|
const category = app.findCategory(categoryId);
|
|
if (!category) {
|
|
throw new Error(`Category with id ${categoryId} does not exist`);
|
|
}
|
|
const tree = parseCategoryRecursively(category);
|
|
return tree;
|
|
}
|
|
|
|
export function getScriptNodeId(script: IScript): string {
|
|
return script.id;
|
|
}
|
|
|
|
export function getCategoryNodeId(category: ICategory): string {
|
|
return `Category${category.id}`;
|
|
}
|
|
|
|
function parseCategoryRecursively(
|
|
parentCategory: ICategory): INode[] {
|
|
if (!parentCategory) {
|
|
throw new Error('parentCategory is undefined');
|
|
}
|
|
let nodes = new Array<INode>();
|
|
nodes = addCategories(parentCategory.subCategories, nodes);
|
|
nodes = addScripts(parentCategory.scripts, nodes);
|
|
return nodes;
|
|
}
|
|
|
|
function addScripts(scripts: ReadonlyArray<IScript>, nodes: INode[]): INode[] {
|
|
if (!scripts || scripts.length === 0) {
|
|
return nodes;
|
|
}
|
|
for (const script of scripts) {
|
|
nodes.push(convertScriptToNode(script));
|
|
}
|
|
return nodes;
|
|
}
|
|
|
|
function addCategories(categories: ReadonlyArray<ICategory>, nodes: INode[]): INode[] {
|
|
if (!categories || categories.length === 0) {
|
|
return nodes;
|
|
}
|
|
for (const category of categories) {
|
|
const subCategoryNodes = parseCategoryRecursively(category);
|
|
nodes.push(convertCategoryToNode(category, subCategoryNodes));
|
|
}
|
|
return nodes;
|
|
}
|
|
|
|
function convertCategoryToNode(
|
|
category: ICategory, children: readonly INode[]): INode {
|
|
return {
|
|
id: getCategoryNodeId(category),
|
|
text: category.name,
|
|
children,
|
|
documentationUrls: category.documentationUrls,
|
|
isReversible: false,
|
|
};
|
|
}
|
|
|
|
function convertScriptToNode(script: IScript): INode {
|
|
return {
|
|
id: getScriptNodeId(script),
|
|
text: script.name,
|
|
children: undefined,
|
|
documentationUrls: script.documentationUrls,
|
|
isReversible: script.canRevert(),
|
|
};
|
|
}
|