UNPKG

3.54 kBPlain TextView Raw
1import {Bean, Autowired, PostConstruct} from "../context/context";
2import {RowNode} from "../entities/rowNode";
3import {FilterManager} from "../filter/filterManager";
4import {GridOptionsWrapper} from "../gridOptionsWrapper";
5@Bean("filterService")
6export class FilterService {
7
8 @Autowired('filterManager') private filterManager: FilterManager;
9 @Autowired('gridOptionsWrapper') private gridOptionsWrapper: GridOptionsWrapper;
10
11 private doingTreeData: boolean;
12
13 @PostConstruct
14 private postConstruct(): void {
15 this.doingTreeData = this.gridOptionsWrapper.isTreeData();
16 }
17
18 public filterAccordingToColumnState(rowNode: RowNode): void {
19 let filterActive: boolean = this.filterManager.isAnyFilterPresent();
20 this.filter (rowNode, filterActive);
21 }
22
23 public filter(rowNode: RowNode, filterActive: boolean): void {
24
25 // recursively get all children that are groups to also filter
26 if (rowNode.hasChildren()) {
27
28 rowNode.childrenAfterGroup.forEach( node => this.filter(node, filterActive));
29
30 // result of filter for this node
31 if (filterActive) {
32 rowNode.childrenAfterFilter = rowNode.childrenAfterGroup.filter(childNode => {
33 // a group is included in the result if it has any children of it's own.
34 // by this stage, the child groups are already filtered
35 let passBecauseChildren = childNode.childrenAfterFilter && childNode.childrenAfterFilter.length > 0;
36
37 // both leaf level nodes and tree data nodes have data. these get added if
38 // the data passes the filter
39 let passBecauseDataPasses = childNode.data && this.filterManager.doesRowPassFilter(childNode);
40
41 // note - tree data nodes pass either if a) they pass themselves or b) any children of that node pass
42
43 return passBecauseChildren || passBecauseDataPasses;
44 });
45 } else {
46 // if not filtering, the result is the original list
47 rowNode.childrenAfterFilter = rowNode.childrenAfterGroup;
48 }
49
50 this.setAllChildrenCount(rowNode);
51
52 } else {
53 rowNode.childrenAfterFilter = rowNode.childrenAfterGroup;
54 rowNode.setAllChildrenCount(null);
55 }
56 }
57
58 private setAllChildrenCountTreeData(rowNode: RowNode) {
59 // for tree data, we include all children, groups and leafs
60 let allChildrenCount = 0;
61 rowNode.childrenAfterFilter.forEach((child: RowNode) => {
62 // include child itself
63 allChildrenCount++;
64 // include children of children
65 allChildrenCount += child.allChildrenCount;
66 });
67 rowNode.setAllChildrenCount(allChildrenCount);
68 }
69
70 private setAllChildrenCountGridGrouping(rowNode: RowNode) {
71 // for grid data, we only count the leafs
72 let allChildrenCount = 0;
73 rowNode.childrenAfterFilter.forEach((child: RowNode) => {
74 if (child.group) {
75 allChildrenCount += child.allChildrenCount;
76 } else {
77 allChildrenCount++;
78 }
79 });
80 rowNode.setAllChildrenCount(allChildrenCount);
81 }
82
83 private setAllChildrenCount(rowNode: RowNode) {
84 if (this.doingTreeData) {
85 this.setAllChildrenCountTreeData(rowNode);
86 } else {
87 this.setAllChildrenCountGridGrouping(rowNode);
88 }
89 }
90}
\No newline at end of file