fix search (got broken in b789250) with tests and refactorings
This commit is contained in:
@@ -10,9 +10,9 @@ declare module 'liquor-tree' {
|
||||
clearFilter(): void;
|
||||
setModel(nodes: ReadonlyArray<ILiquorTreeNewNode>): void;
|
||||
// getNodeById(id: string): ILiquorTreeExistingNode;
|
||||
// recurseDown(fn: (node: ILiquorTreeExistingNode) => void): void;
|
||||
recurseDown(fn: (node: ILiquorTreeExistingNode) => void): void;
|
||||
}
|
||||
interface ICustomLiquorTreeData {
|
||||
export interface ICustomLiquorTreeData {
|
||||
type: number;
|
||||
documentationUrls: ReadonlyArray<string>;
|
||||
isReversible: boolean;
|
||||
@@ -35,7 +35,7 @@ declare module 'liquor-tree' {
|
||||
data: ILiquorTreeNodeData;
|
||||
states: ILiquorTreeNodeState | undefined;
|
||||
children: ReadonlyArray<ILiquorTreeExistingNode> | undefined;
|
||||
expand(): void;
|
||||
// expand(): void;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,7 +60,7 @@ declare module 'liquor-tree' {
|
||||
deletion(node: ILiquorTreeNode): boolean;
|
||||
}
|
||||
|
||||
interface ILiquorTreeNodeData extends ICustomLiquorTreeData {
|
||||
export interface ILiquorTreeNodeData extends ICustomLiquorTreeData {
|
||||
text: string;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,21 @@
|
||||
import { ILiquorTreeOptions, ILiquorTreeFilter, ILiquorTreeNode } from 'liquor-tree';
|
||||
import { ILiquorTreeOptions, ILiquorTreeFilter, ILiquorTreeNode, ILiquorTreeExistingNode } from 'liquor-tree';
|
||||
|
||||
export class LiquorTreeOptions implements ILiquorTreeOptions {
|
||||
public multiple = true;
|
||||
public checkbox = true;
|
||||
public checkOnSelect = true;
|
||||
public readonly multiple = true;
|
||||
public readonly checkbox = true;
|
||||
public readonly checkOnSelect = true;
|
||||
/* For checkbox mode only. Children will have the same checked state as their parent.
|
||||
This is false as it's handled manually to be able to batch select for performance + highlighting */
|
||||
public autoCheckChildren = false;
|
||||
public parentSelect = false;
|
||||
public keyboardNavigation = true;
|
||||
constructor(public filter: ILiquorTreeFilter) { }
|
||||
public readonly autoCheckChildren = false;
|
||||
public readonly parentSelect = false;
|
||||
public readonly keyboardNavigation = true;
|
||||
public readonly filter = { // Wrap this in an arrow function as setting filter directly does not work JS APIs
|
||||
emptyText: this.liquorTreeFilter.emptyText,
|
||||
matcher: (query: string, node: ILiquorTreeExistingNode) => {
|
||||
return this.liquorTreeFilter.matcher(query, node);
|
||||
},
|
||||
};
|
||||
constructor(private readonly liquorTreeFilter: ILiquorTreeFilter) { }
|
||||
public deletion(node: ILiquorTreeNode): boolean {
|
||||
return false; // no op
|
||||
}
|
||||
|
||||
@@ -1,66 +0,0 @@
|
||||
import { ILiquorTreeExistingNode, ILiquorTreeNewNode, ILiquorTreeNode } from 'liquor-tree';
|
||||
import { NodeType } from './../Node/INode';
|
||||
|
||||
export function updateNodesCheckedState(
|
||||
oldNodes: ReadonlyArray<ILiquorTreeExistingNode>,
|
||||
selectedNodeIds: ReadonlyArray<string>): ReadonlyArray<ILiquorTreeNewNode> {
|
||||
const result = new Array<ILiquorTreeNewNode>();
|
||||
for (const oldNode of oldNodes) {
|
||||
const newState = oldNode.states;
|
||||
newState.checked = getNewCheckedState(oldNode, selectedNodeIds);
|
||||
const newNode: ILiquorTreeNewNode = {
|
||||
id: oldNode.id,
|
||||
text: oldNode.data.text,
|
||||
data: {
|
||||
type: oldNode.data.type,
|
||||
documentationUrls: oldNode.data.documentationUrls,
|
||||
isReversible: oldNode.data.isReversible,
|
||||
},
|
||||
children: !oldNode.children ? [] : updateNodesCheckedState(oldNode.children, selectedNodeIds),
|
||||
state: newState,
|
||||
};
|
||||
result.push(newNode);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export function getNewCheckedState(
|
||||
oldNode: ILiquorTreeNode,
|
||||
selectedNodeIds: ReadonlyArray<string>): boolean {
|
||||
switch (oldNode.data.type) {
|
||||
case NodeType.Script:
|
||||
return selectedNodeIds.some((id) => id === oldNode.id);
|
||||
case NodeType.Category:
|
||||
return parseAllSubScriptIds(oldNode).every((id) => selectedNodeIds.includes(id));
|
||||
default:
|
||||
throw new Error('Unknown node type');
|
||||
}
|
||||
}
|
||||
|
||||
function parseAllSubScriptIds(categoryNode: ILiquorTreeNode): ReadonlyArray<string> {
|
||||
if (categoryNode.data.type !== NodeType.Category) {
|
||||
throw new Error('Not a category node');
|
||||
}
|
||||
if (!categoryNode.children) {
|
||||
return [];
|
||||
}
|
||||
const ids = new Array<string>();
|
||||
for (const child of categoryNode.children) {
|
||||
addNodeIds(child, ids);
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
|
||||
function addNodeIds(node: ILiquorTreeNode, ids: string[]) {
|
||||
switch (node.data.type) {
|
||||
case NodeType.Script:
|
||||
ids.push(node.id);
|
||||
break;
|
||||
case NodeType.Category:
|
||||
const subCategoryIds = parseAllSubScriptIds(node);
|
||||
ids.push(...subCategoryIds);
|
||||
break;
|
||||
default:
|
||||
throw new Error('Unknown node type');
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
import { ILiquorTreeFilter, ILiquorTreeExistingNode } from 'liquor-tree';
|
||||
import { convertExistingToNode } from './NodeTranslator';
|
||||
import { INode } from '../Node/INode';
|
||||
import { INode } from './../../Node/INode';
|
||||
|
||||
export type FilterPredicate = (node: INode) => boolean;
|
||||
|
||||
export class NodePredicateFilter implements ILiquorTreeFilter {
|
||||
public emptyText: string = '🕵️Hmm.. Can not see one 🧐';
|
||||
public emptyText = ''; // Does not matter as a custom mesage is shown
|
||||
constructor(private readonly filterPredicate: FilterPredicate) {
|
||||
if (!filterPredicate) {
|
||||
throw new Error('filterPredicate is undefined');
|
||||
@@ -0,0 +1,38 @@
|
||||
import { ILiquorTreeNode } from 'liquor-tree';
|
||||
import { NodeType } from './../../Node/INode';
|
||||
|
||||
export function getNewCheckedState(
|
||||
oldNode: ILiquorTreeNode,
|
||||
selectedNodeIds: ReadonlyArray<string>): boolean {
|
||||
switch (oldNode.data.type) {
|
||||
case NodeType.Script:
|
||||
return selectedNodeIds.some((id) => id === oldNode.id);
|
||||
case NodeType.Category:
|
||||
return parseAllSubScriptIds(oldNode).every((id) => selectedNodeIds.includes(id));
|
||||
default:
|
||||
throw new Error('Unknown node type');
|
||||
}
|
||||
}
|
||||
|
||||
function parseAllSubScriptIds(categoryNode: ILiquorTreeNode): ReadonlyArray<string> {
|
||||
if (categoryNode.data.type !== NodeType.Category) {
|
||||
throw new Error('Not a category node');
|
||||
}
|
||||
if (!categoryNode.children) {
|
||||
return [];
|
||||
}
|
||||
return categoryNode
|
||||
.children
|
||||
.flatMap((child) => getNodeIds(child));
|
||||
}
|
||||
|
||||
function getNodeIds(node: ILiquorTreeNode): ReadonlyArray<string> {
|
||||
switch (node.data.type) {
|
||||
case NodeType.Script:
|
||||
return [ node.id ];
|
||||
case NodeType.Category:
|
||||
return parseAllSubScriptIds(node);
|
||||
default:
|
||||
throw new Error('Unknown node type');
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { ILiquorTreeNewNode, ILiquorTreeExistingNode } from 'liquor-tree';
|
||||
import { INode } from './../Node/INode';
|
||||
import { INode } from './../../Node/INode';
|
||||
|
||||
// Functions to translate INode to LiqourTree models and vice versa for anti-corruption
|
||||
|
||||
Reference in New Issue
Block a user