UNPKG

6.02 kBPlain TextView Raw
1import {OriginalColumnGroupChild} from "./originalColumnGroupChild";
2import {ColGroupDef} from "./colDef";
3import {ColumnGroup} from "./columnGroup";
4import {Column} from "./column";
5import {EventService} from "../eventService";
6import {IEventEmitter} from "../interfaces/iEventEmitter";
7import {Autowired} from "../context/context";
8import {ColumnApi} from "../columnController/columnApi";
9import {GridApi} from "../gridApi";
10import {AgEvent} from "../events";
11
12export 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 // return true if at least one child is visible
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 // if this is padding we have exactly only child. we then
116 // take the value from the child and push it up, making
117 // this group 'invisible'.
118 return this.children[0].getColumnGroupShow();
119 }
120 }
121
122 // need to check that this group has at least one col showing when both expanded and contracted.
123 // if not, then we don't allow expanding and contracting on this group
124
125 public setupExpandable() {
126 this.setExpandable();
127 // note - we should be removing this event listener
128 this.getLeafColumns().forEach( col => col.addEventListener(Column.EVENT_VISIBLE_CHANGED, this.onColumnVisibilityChanged.bind(this)));
129 }
130
131 public setExpandable() {
132 // want to make sure the group doesn't disappear when it's open
133 let atLeastOneShowingWhenOpen = false;
134 // want to make sure the group doesn't disappear when it's closed
135 let atLeastOneShowingWhenClosed = false;
136 // want to make sure the group has something to show / hide
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 // if the abstractColumn is a grid generated group, there will be no colDef
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}