From e3f82e069e305f6d94eab335470c8e7b44295dd6 Mon Sep 17 00:00:00 2001 From: undergroundwires Date: Sat, 11 Jan 2020 05:13:03 +0100 Subject: [PATCH] refactorings --- src/global.d.ts | 14 +- .../Scripts/Cards/CardListItem.vue | 1 - .../Scripts/Grouping/TheGrouper.vue | 5 +- .../Scripts/ScriptsTree/ScriptNodeParser.ts | 28 ++- .../Scripts/ScriptsTree/ScriptsTree.vue | 30 +-- .../ScriptsTree/SelectableTree/INode.ts | 1 - .../ScriptsTree/SelectableTree/Node.vue | 2 +- .../SelectableTree/NodeTranslator.ts | 32 +++ .../SelectableTree/SelectableTree.vue | 193 ++++++++---------- 9 files changed, 159 insertions(+), 147 deletions(-) create mode 100644 src/presentation/Scripts/ScriptsTree/SelectableTree/NodeTranslator.ts diff --git a/src/global.d.ts b/src/global.d.ts index 18887b01..03b846c8 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -37,6 +37,18 @@ declare module 'liquor-tree' { children: ReadonlyArray | undefined; data: ICustomLiquorTreeData; } + + // https://amsik.github.io/liquor-tree/#Component-Options + export interface ILiquorTreeOptions { + multiple: boolean; + checkbox: boolean; + checkOnSelect: boolean; + autoCheckChildren: boolean; + parentSelect: boolean; + keyboardNavigation: boolean; + deletion: (node: ILiquorTreeExistingNode) => void; + filter: ILiquorTreeFilter; + } // https://github.com/amsik/liquor-tree/blob/master/src/lib/Node.js interface ILiquorTreeNodeState { @@ -58,7 +70,7 @@ declare module 'liquor-tree' { // https://github.com/amsik/liquor-tree/blob/master/src/components/TreeRoot.vue interface ILiquorTreeFilter { emptyText: string; - matcher(query: string, node: ILiquorTreeNewNode): boolean; + matcher(query: string, node: ILiquorTreeExistingNode): boolean; } const LiquorTree: PluginObject & VueClass; diff --git a/src/presentation/Scripts/Cards/CardListItem.vue b/src/presentation/Scripts/Cards/CardListItem.vue index 44050f14..9bd05d28 100644 --- a/src/presentation/Scripts/Cards/CardListItem.vue +++ b/src/presentation/Scripts/Cards/CardListItem.vue @@ -52,7 +52,6 @@ export default class CardListItem extends StatefulVue { this.cardTitle = value ? await this.getCardTitleAsync(value) : undefined; } - private async getCardTitleAsync(categoryId: number): Promise { const state = await this.getCurrentStateAsync(); const category = state.app.findCategory(this.categoryId); diff --git a/src/presentation/Scripts/Grouping/TheGrouper.vue b/src/presentation/Scripts/Grouping/TheGrouper.vue index 4a04b557..27b8b008 100644 --- a/src/presentation/Scripts/Grouping/TheGrouper.vue +++ b/src/presentation/Scripts/Grouping/TheGrouper.vue @@ -20,15 +20,18 @@ import { StatefulVue } from '@/presentation/StatefulVue'; import { IApplicationState } from '@/application/State/IApplicationState'; import { Grouping } from './Grouping'; +const DefaultGrouping = Grouping.Cards; + @Component export default class TheGrouper extends StatefulVue { + public cardsSelected = false; public noneSelected = false; private currentGrouping: Grouping; public mounted() { - this.changeGrouping(Grouping.Cards); + this.changeGrouping(DefaultGrouping); } public groupByCard() { diff --git a/src/presentation/Scripts/ScriptsTree/ScriptNodeParser.ts b/src/presentation/Scripts/ScriptsTree/ScriptNodeParser.ts index b269e0d9..91157d54 100644 --- a/src/presentation/Scripts/ScriptsTree/ScriptNodeParser.ts +++ b/src/presentation/Scripts/ScriptsTree/ScriptNodeParser.ts @@ -1,22 +1,22 @@ -import { IApplicationState, IUserSelection } from '@/application/State/IApplicationState'; +import { IApplication } from './../../../domain/IApplication'; import { ICategory, IScript } from '@/domain/ICategory'; import { INode } from './SelectableTree/INode'; -export function parseAllCategories(state: IApplicationState): INode[] | undefined { +export function parseAllCategories(app: IApplication): INode[] | undefined { const nodes = new Array(); - for (const category of state.app.categories) { - const children = parseCategoryRecursively(category, state.selection); + for (const category of app.categories) { + const children = parseCategoryRecursively(category); nodes.push(convertCategoryToNode(category, children)); } return nodes; } -export function parseSingleCategory(categoryId: number, state: IApplicationState): INode[] | undefined { - const category = state.app.findCategory(categoryId); +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, state.selection); + const tree = parseCategoryRecursively(category); return tree; } @@ -24,25 +24,23 @@ export function getScriptNodeId(script: IScript): string { return script.id; } export function getCategoryNodeId(category: ICategory): string { - return `${category.id}`; + return `Category${category.id}`; } function parseCategoryRecursively( - parentCategory: ICategory, - selection: IUserSelection): INode[] { + parentCategory: ICategory): INode[] { if (!parentCategory) { throw new Error('parentCategory is undefined'); } - if (!selection) { throw new Error('selection is undefined'); } const nodes = new Array(); if (parentCategory.subCategories && parentCategory.subCategories.length > 0) { for (const subCategory of parentCategory.subCategories) { - const subCategoryNodes = parseCategoryRecursively(subCategory, selection); + const subCategoryNodes = parseCategoryRecursively(subCategory); nodes.push(convertCategoryToNode(subCategory, subCategoryNodes)); } } if (parentCategory.scripts && parentCategory.scripts.length > 0) { for (const script of parentCategory.scripts) { - nodes.push(convertScriptToNode(script, selection)); + nodes.push(convertScriptToNode(script)); } } return nodes; @@ -53,17 +51,15 @@ function convertCategoryToNode( return { id: getCategoryNodeId(category), text: category.name, - selected: false, children, documentationUrls: category.documentationUrls, }; } -function convertScriptToNode(script: IScript, selection: IUserSelection): INode { +function convertScriptToNode(script: IScript): INode { return { id: getScriptNodeId(script), text: script.name, - selected: selection.isSelected(script), children: undefined, documentationUrls: script.documentationUrls, }; diff --git a/src/presentation/Scripts/ScriptsTree/ScriptsTree.vue b/src/presentation/Scripts/ScriptsTree/ScriptsTree.vue index c4812e73..fe267464 100644 --- a/src/presentation/Scripts/ScriptsTree/ScriptsTree.vue +++ b/src/presentation/Scripts/ScriptsTree/ScriptsTree.vue @@ -2,7 +2,7 @@ = null; + public selectedNodeIds?: ReadonlyArray = []; public filterText?: string = null; private filtered?: IFilterResult; @@ -56,7 +55,7 @@ return; // only interested in script nodes } const state = await this.getCurrentStateAsync(); - if (node.selected) { + if (!this.selectedNodeIds.some((id) => id === node.id)) { state.selection.addSelectedScript(node.id); } else { state.selection.removeSelectedScript(node.id); @@ -67,10 +66,12 @@ public async initializeNodesAsync(categoryId?: number) { const state = await this.getCurrentStateAsync(); if (categoryId) { - this.nodes = parseSingleCategory(categoryId, state); + this.nodes = parseSingleCategory(categoryId, state.app); } else { - this.nodes = parseAllCategories(state); + this.nodes = parseAllCategories(state.app); } + this.selectedNodeIds = state.selection.selectedScripts + .map((script) => getScriptNodeId(script)); } public filterPredicate(node: INode): boolean { @@ -80,8 +81,9 @@ (category: ICategory) => node.id === getCategoryNodeId(category)); } - private handleSelectionChanged(selectedScripts: ReadonlyArray) { - this.nodes = this.nodes.map((node: INode) => updateNodeSelection(node, selectedScripts)); + private handleSelectionChanged(selectedScripts: ReadonlyArray): void { + this.selectedNodeIds = selectedScripts + .map((node) => node.id); } private handleFilterRemoved() { @@ -94,16 +96,6 @@ } } - function updateNodeSelection(node: INode, selectedScripts: ReadonlyArray): INode { - return { - id: node.id, - text: node.text, - selected: selectedScripts.some((script) => script.id === node.id), - children: node.children ? node.children.map((child) => updateNodeSelection(child, selectedScripts)) : [], - documentationUrls: node.documentationUrls, - }; - } -