blob: 19edbcd098a76c519cf8fa945ec2d13000b5dc38 [file] [log] [blame]
Ittay Stern6f900cc2018-08-29 17:01:32 +03001import {Component, EventEmitter, Input, Output} from "@angular/core";
2import {IDType, ITreeNode} from "angular-tree-component/dist/defs/api";
3import * as _ from 'lodash';
4
5@Component({
6 selector: 'search-component',
7 templateUrl: './search.component.html',
8 styleUrls: ['./search.component.scss']
9})
10export class SearchComponent {
11 @Input() tree;
12 @Input() nodes;
13 @Input() inputTestId: string;
14
15 @Output() updateNodes: EventEmitter<any> = new EventEmitter();
16
17 searchTree(searchText: string): void {
18 if(_.isNil(searchText)){
19 return;
20 }
21 let __this = this;
22 let results: ITreeNode[] = [];
23 this.nodes.forEach( (node) => {
24 __this.searchTreeNode(node, searchText, results);
25 });
26 results.forEach(function (result) {
27 __this.expandParentByNodeId(result.id)
28 });
29 this.updateNodes.emit({
30 nodes: this.nodes,
31 filterValue: searchText
32 });
33 return;
34 }
35
36 expandParentByNodeId(id: IDType): void {
37 this.tree.treeModel.getNodeById(id).parent.expand();
38 }
39
40 searchTreeNode(node, searchText: string, results): void {
41 if (node.name.toLowerCase().indexOf(searchText.toLowerCase()) != -1) {
42 results.push(node);
43 }
44 if (node.children != null) {
45 for (let i = 0; i < node.children.length; i++) {
46 this.searchTreeNode(node.children[i], searchText, results);
47 }
48 }
49 }
50}