1 | import {RowNode} from "../entities/rowNode";
|
2 | import {Autowired, Bean, PostConstruct} from "../context/context";
|
3 | import {_} from "../utils";
|
4 | import {GridOptionsWrapper} from "../gridOptionsWrapper";
|
5 | import {IsRowSelectable} from "../entities/gridOptions";
|
6 |
|
7 | @Bean('selectableService')
|
8 | export class SelectableService {
|
9 |
|
10 | @Autowired('gridOptionsWrapper') private gridOptionsWrapper: GridOptionsWrapper;
|
11 |
|
12 | private groupSelectsChildren: boolean;
|
13 | private isRowSelectableFunc: IsRowSelectable;
|
14 |
|
15 | @PostConstruct
|
16 | public init(): void {
|
17 | this.groupSelectsChildren = this.gridOptionsWrapper.isGroupSelectsChildren();
|
18 | this.isRowSelectableFunc = this.gridOptionsWrapper.getIsRowSelectableFunc();
|
19 | }
|
20 |
|
21 | public updateSelectableAfterGrouping(rowNode: RowNode): void {
|
22 | if (this.isRowSelectableFunc) {
|
23 | let nextChildrenFunc = (rowNode: RowNode) => rowNode.childrenAfterGroup;
|
24 | this.recurseDown(rowNode.childrenAfterGroup, nextChildrenFunc);
|
25 | }
|
26 | }
|
27 |
|
28 | public updateSelectableAfterFiltering(rowNode: RowNode): void {
|
29 | if (this.isRowSelectableFunc) {
|
30 | let nextChildrenFunc = (rowNode: RowNode) => rowNode.childrenAfterFilter;
|
31 | this.recurseDown(rowNode.childrenAfterGroup, nextChildrenFunc);
|
32 | }
|
33 | }
|
34 |
|
35 | private recurseDown(children: RowNode[], nextChildrenFunc: (rowNode: RowNode) => RowNode[]): void {
|
36 | children.forEach((child: RowNode) => {
|
37 |
|
38 | if (!child.group) { return; }
|
39 |
|
40 | if (child.hasChildren()) {
|
41 | this.recurseDown(nextChildrenFunc(child), nextChildrenFunc);
|
42 | }
|
43 |
|
44 | let rowSelectable: boolean;
|
45 |
|
46 | if (this.groupSelectsChildren) {
|
47 |
|
48 | let firstSelectable = _.find(nextChildrenFunc(child), 'selectable', true);
|
49 | rowSelectable = _.exists(firstSelectable);
|
50 | } else {
|
51 |
|
52 | rowSelectable = this.isRowSelectableFunc(child);
|
53 | }
|
54 |
|
55 | child.setRowSelectable(rowSelectable);
|
56 | });
|
57 | }
|
58 |
|
59 | } |
\ | No newline at end of file |