1 | import {OriginalColumnGroupChild} from "./originalColumnGroupChild";
|
2 | import {ColGroupDef} from "./colDef";
|
3 | import {ColumnGroup} from "./columnGroup";
|
4 | import {Column} from "./column";
|
5 | import {EventService} from "../eventService";
|
6 | import {IEventEmitter} from "../interfaces/iEventEmitter";
|
7 | import {Autowired} from "../context/context";
|
8 | import {ColumnApi} from "../columnController/columnApi";
|
9 | import {GridApi} from "../gridApi";
|
10 | import {AgEvent} from "../events";
|
11 |
|
12 | export class OriginalColumnGroup implements OriginalColumnGroupChild, IEventEmitter {
|
13 |
|
14 | public static EVENT_EXPANDED_CHANGED = 'expandedChanged';
|
15 | public static EVENT_EXPANDABLE_CHANGED = 'expandableChanged';
|
16 |
|
17 | @Autowired('columnApi') private columnApi: ColumnApi;
|
18 | @Autowired('gridApi') private gridApi: GridApi;
|
19 |
|
20 | private localEventService = new EventService();
|
21 |
|
22 | private colGroupDef: ColGroupDef;
|
23 |
|
24 | private children: OriginalColumnGroupChild[];
|
25 | private groupId: string;
|
26 | private expandable = false;
|
27 |
|
28 | private expanded: boolean;
|
29 | private padding: boolean;
|
30 |
|
31 | private level: number;
|
32 |
|
33 | constructor(colGroupDef: ColGroupDef, groupId: string, padding: boolean, level: number) {
|
34 | this.colGroupDef = colGroupDef;
|
35 | this.groupId = groupId;
|
36 | this.expanded = colGroupDef && !!colGroupDef.openByDefault;
|
37 | this.padding = padding;
|
38 | this.level = level;
|
39 | }
|
40 |
|
41 | public getLevel(): number {
|
42 | return this.level;
|
43 | }
|
44 |
|
45 | public isVisible(): boolean {
|
46 |
|
47 | if (this.children) {
|
48 | return this.children.some(child => child.isVisible());
|
49 | } else {
|
50 | return false;
|
51 | }
|
52 | }
|
53 |
|
54 | public isPadding(): boolean {
|
55 | return this.padding;
|
56 | }
|
57 |
|
58 | public setExpanded(expanded: boolean): void {
|
59 | this.expanded = expanded;
|
60 | let event: AgEvent = {
|
61 | type: OriginalColumnGroup.EVENT_EXPANDED_CHANGED
|
62 | };
|
63 | this.localEventService.dispatchEvent(event);
|
64 | }
|
65 |
|
66 | public isExpandable(): boolean {
|
67 | return this.expandable;
|
68 | }
|
69 |
|
70 | public isExpanded(): boolean {
|
71 | return this.expanded;
|
72 | }
|
73 |
|
74 | public getGroupId(): string {
|
75 | return this.groupId;
|
76 | }
|
77 |
|
78 | public getId(): string {
|
79 | return this.getGroupId();
|
80 | }
|
81 |
|
82 | public setChildren(children: OriginalColumnGroupChild[]): void {
|
83 | this.children = children;
|
84 | }
|
85 |
|
86 | public getChildren(): OriginalColumnGroupChild[] {
|
87 | return this.children;
|
88 | }
|
89 |
|
90 | public getColGroupDef(): ColGroupDef {
|
91 | return this.colGroupDef;
|
92 | }
|
93 |
|
94 | public getLeafColumns(): Column[] {
|
95 | let result: Column[] = [];
|
96 | this.addLeafColumns(result);
|
97 | return result;
|
98 | }
|
99 |
|
100 | private addLeafColumns(leafColumns: Column[]): void {
|
101 | if (!this.children) { return; }
|
102 | this.children.forEach( (child: OriginalColumnGroupChild) => {
|
103 | if (child instanceof Column) {
|
104 | leafColumns.push(<Column>child);
|
105 | } else if (child instanceof OriginalColumnGroup) {
|
106 | (<OriginalColumnGroup>child).addLeafColumns(leafColumns);
|
107 | }
|
108 | });
|
109 | }
|
110 |
|
111 | public getColumnGroupShow(): string {
|
112 | if (!this.padding) {
|
113 | return this.colGroupDef.columnGroupShow;
|
114 | } else {
|
115 |
|
116 |
|
117 |
|
118 | return this.children[0].getColumnGroupShow();
|
119 | }
|
120 | }
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | public setupExpandable() {
|
126 | this.setExpandable();
|
127 |
|
128 | this.getLeafColumns().forEach( col => col.addEventListener(Column.EVENT_VISIBLE_CHANGED, this.onColumnVisibilityChanged.bind(this)));
|
129 | }
|
130 |
|
131 | public setExpandable() {
|
132 |
|
133 | let atLeastOneShowingWhenOpen = false;
|
134 |
|
135 | let atLeastOneShowingWhenClosed = false;
|
136 |
|
137 | let atLeastOneChangeable = false;
|
138 |
|
139 | for (let i = 0, j = this.children.length; i < j; i++) {
|
140 | let abstractColumn = this.children[i];
|
141 | if (!abstractColumn.isVisible()) {
|
142 | continue;
|
143 | }
|
144 |
|
145 | let headerGroupShow = abstractColumn.getColumnGroupShow();
|
146 | if (headerGroupShow === ColumnGroup.HEADER_GROUP_SHOW_OPEN) {
|
147 | atLeastOneShowingWhenOpen = true;
|
148 | atLeastOneChangeable = true;
|
149 | } else if (headerGroupShow === ColumnGroup.HEADER_GROUP_SHOW_CLOSED) {
|
150 | atLeastOneShowingWhenClosed = true;
|
151 | atLeastOneChangeable = true;
|
152 | } else {
|
153 | atLeastOneShowingWhenOpen = true;
|
154 | atLeastOneShowingWhenClosed = true;
|
155 | }
|
156 | }
|
157 |
|
158 | let expandable = atLeastOneShowingWhenOpen && atLeastOneShowingWhenClosed && atLeastOneChangeable;
|
159 |
|
160 | if (this.expandable !== expandable) {
|
161 | this.expandable = expandable;
|
162 | let event: AgEvent = {
|
163 | type: OriginalColumnGroup.EVENT_EXPANDABLE_CHANGED
|
164 | };
|
165 | this.localEventService.dispatchEvent(event);
|
166 | }
|
167 | }
|
168 |
|
169 | private onColumnVisibilityChanged(): void {
|
170 | this.setExpandable();
|
171 | }
|
172 |
|
173 | public addEventListener(eventType: string, listener: Function): void {
|
174 | this.localEventService.addEventListener(eventType, listener);
|
175 | }
|
176 |
|
177 | public removeEventListener(eventType: string, listener: Function): void {
|
178 | this.localEventService.removeEventListener(eventType, listener);
|
179 | }
|
180 | }
|