Files
privacy.sexy/src/presentation/Scripts/ScriptsTree/ScriptNodeParser.ts
undergroundwires 89862b2775 🔍 support for search
2020-01-10 01:35:09 +01:00

71 lines
2.3 KiB
TypeScript

import { IApplicationState, IUserSelection } from '@/application/State/IApplicationState';
import { ICategory, IScript } from '@/domain/ICategory';
import { INode } from './SelectableTree/INode';
export function parseAllCategories(state: IApplicationState): INode[] | undefined {
const nodes = new Array<INode>();
for (const category of state.app.categories) {
const children = parseCategoryRecursively(category, state.selection);
nodes.push(convertCategoryToNode(category, children));
}
return nodes;
}
export function parseSingleCategory(categoryId: number, state: IApplicationState): INode[] | undefined {
const category = state.app.findCategory(categoryId);
if (!category) {
throw new Error(`Category with id ${categoryId} does not exist`);
}
const tree = parseCategoryRecursively(category, state.selection);
return tree;
}
export function getScriptNodeId(script: IScript): string {
return script.id;
}
export function getCategoryNodeId(category: ICategory): string {
return `${category.id}`;
}
function parseCategoryRecursively(
parentCategory: ICategory,
selection: IUserSelection): INode[] {
if (!parentCategory) { throw new Error('parentCategory is undefined'); }
if (!selection) { throw new Error('selection is undefined'); }
const nodes = new Array<INode>();
if (parentCategory.subCategories && parentCategory.subCategories.length > 0) {
for (const subCategory of parentCategory.subCategories) {
const subCategoryNodes = parseCategoryRecursively(subCategory, selection);
nodes.push(convertCategoryToNode(subCategory, subCategoryNodes));
}
}
if (parentCategory.scripts && parentCategory.scripts.length > 0) {
for (const script of parentCategory.scripts) {
nodes.push(convertScriptToNode(script, selection));
}
}
return nodes;
}
function convertCategoryToNode(
category: ICategory, children: readonly INode[]): INode {
return {
id: getCategoryNodeId(category),
text: category.name,
selected: false,
children,
documentationUrls: category.documentationUrls,
};
}
function convertScriptToNode(script: IScript, selection: IUserSelection): INode {
return {
id: getScriptNodeId(script),
text: script.name,
selected: selection.isSelected(script),
children: undefined,
documentationUrls: script.documentationUrls,
};
}