Skip to content

Commit

Permalink
feat(tree): added description for getValue and getViewValue, also add…
Browse files Browse the repository at this point in the history
…ed compareValues and compareViewValues (#UIM-174) (#308)
  • Loading branch information
lskramarov authored and pimenovoleg committed Oct 18, 2019
1 parent 0fa79b9 commit 42d27b4
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions packages/cdk/tree/control/flat-tree-control.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
import { BaseTreeControl } from './base-tree-control';


export function defaultCompareValues(firstValue, secondValue): boolean {
return firstValue === secondValue;
}

export function defaultCompareViewValues(firstViewValue, secondViewValue): boolean {
return RegExp(secondViewValue, 'gi').test(firstViewValue);
}

/** Flat tree control. Able to expand/collapse a subtree recursively for flattened tree. */
export class FlatTreeControl<T> extends BaseTreeControl<T> {
/** Construct with flat tree data node functions getLevel and isExpandable. */
/** Construct with flat tree data node functions getLevel, isExpandable, getValue and getViewValue. */
constructor(
public getLevel: (dataNode: T) => number,
public isExpandable: (dataNode: T) => boolean,
public getValue: (dataNode) => string,
public getViewValue: (dataNode) => string
/** getValue will be used to determine if the tree contains value or not. Used in method hasValue */
public getValue: (dataNode) => any,
/** getViewValue will be used for filter nodes. Returned value will be first argument in filterNodesFunction */
public getViewValue: (dataNode) => string,
/** compareValues will be used to comparing values. */
public compareValues: (firstValue, secondValue) => boolean = defaultCompareValues,
/** compareValues will be used to comparing values. */
public compareViewValues: (firstViewValue, secondViewValue) => boolean = defaultCompareViewValues
) {
super();
}
Expand Down Expand Up @@ -60,18 +74,14 @@ export class FlatTreeControl<T> extends BaseTreeControl<T> {
}

hasValue(value: string): T | undefined {
return this.dataNodes.find((node: any) => this.getValue(node) === value);
}

filterNodesFunction(name: string, value: string): boolean {
return RegExp(value, 'gi').test(name);
return this.dataNodes.find((node: any) => this.compareValues(this.getValue(node), value));
}

filterNodes(value: string): void {
this.filterModel.clear();

const filteredNodes = this.dataNodes.filter(
(node: any) => this.filterNodesFunction(this.getViewValue(node), value)
(node: any) => this.compareViewValues(this.getViewValue(node), value)
);

const filteredNodesWithTheirParents = new Set();
Expand Down

0 comments on commit 42d27b4

Please sign in to comment.