/
CompositeColumn.ts
324 lines (292 loc) · 8.87 KB
/
CompositeColumn.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import { suffix, type IEventListener } from '../internal';
import Column, {
widthChanged,
labelChanged,
metaDataChanged,
dirty,
dirtyHeader,
dirtyValues,
rendererTypeChanged,
groupRendererChanged,
summaryRendererChanged,
visibilityChanged,
dirtyCaches,
} from './Column';
import { Category, toolbar } from './annotations';
import type { IDataRow, IColumnParent, IFlatColumn, ITypeFactory } from './interfaces';
import { isNumberColumn } from './INumberColumn';
import ValueColumn from './ValueColumn';
/**
* emitted when the filter property changes
* @asMemberOf CompositeColumn
* @event
*/
export declare function filterChanged(previous: any | null, current: any | null): void;
/**
* emitted when a column has been added
* @asMemberOf CompositeColumn
* @event
*/
export declare function addColumn(col: Column, index: number): void;
/**
* emitted when a column has been moved within this composite columm
* @asMemberOf CompositeColumn
* @event
*/
export declare function moveColumn(col: Column, index: number, oldIndex: number): void;
/**
* emitted when a column has been removed
* @asMemberOf CompositeColumn
* @event
*/
export declare function removeColumn(col: Column, index: number): void;
/**
* implementation of a combine column, standard operations how to select
*/
@toolbar('compositeContained', 'splitCombined')
@Category('composite')
export default class CompositeColumn extends Column implements IColumnParent {
static readonly EVENT_FILTER_CHANGED = 'filterChanged';
static readonly EVENT_ADD_COLUMN = 'addColumn';
static readonly EVENT_MOVE_COLUMN = 'moveColumn';
static readonly EVENT_REMOVE_COLUMN = 'removeColumn';
protected readonly _children: Column[] = [];
protected createEventList() {
return super
.createEventList()
.concat([
CompositeColumn.EVENT_FILTER_CHANGED,
CompositeColumn.EVENT_ADD_COLUMN,
CompositeColumn.EVENT_MOVE_COLUMN,
CompositeColumn.EVENT_REMOVE_COLUMN,
]);
}
on(type: typeof CompositeColumn.EVENT_FILTER_CHANGED, listener: typeof filterChanged | null): this;
on(type: typeof CompositeColumn.EVENT_ADD_COLUMN, listener: typeof addColumn | null): this;
on(type: typeof CompositeColumn.EVENT_MOVE_COLUMN, listener: typeof moveColumn | null): this;
on(type: typeof CompositeColumn.EVENT_REMOVE_COLUMN, listener: typeof removeColumn | null): this;
on(type: typeof Column.EVENT_WIDTH_CHANGED, listener: typeof widthChanged | null): this;
on(type: typeof Column.EVENT_LABEL_CHANGED, listener: typeof labelChanged | null): this;
on(type: typeof Column.EVENT_METADATA_CHANGED, listener: typeof metaDataChanged | null): this;
on(type: typeof Column.EVENT_DIRTY, listener: typeof dirty | null): this;
on(type: typeof Column.EVENT_DIRTY_HEADER, listener: typeof dirtyHeader | null): this;
on(type: typeof Column.EVENT_DIRTY_VALUES, listener: typeof dirtyValues | null): this;
on(type: typeof Column.EVENT_DIRTY_CACHES, listener: typeof dirtyCaches | null): this;
on(type: typeof Column.EVENT_RENDERER_TYPE_CHANGED, listener: typeof rendererTypeChanged | null): this;
on(type: typeof Column.EVENT_GROUP_RENDERER_TYPE_CHANGED, listener: typeof groupRendererChanged | null): this;
on(type: typeof Column.EVENT_SUMMARY_RENDERER_TYPE_CHANGED, listener: typeof summaryRendererChanged | null): this;
on(type: typeof Column.EVENT_VISIBILITY_CHANGED, listener: typeof visibilityChanged | null): this;
on(type: string | string[], listener: IEventListener | null): this; // required for correct typings in *.d.ts
on(type: string | string[], listener: IEventListener | null): this {
return super.on(type, listener);
}
assignNewId(idGenerator: () => string) {
super.assignNewId(idGenerator);
this._children.forEach((c) => c.assignNewId(idGenerator));
}
get children() {
return this._children.slice();
}
get length() {
return this._children.length;
}
flatten(r: IFlatColumn[], offset: number, levelsToGo = 0, padding = 0) {
let w = 0;
//no more levels or just this one
if (levelsToGo === 0 || levelsToGo <= Column.FLAT_ALL_COLUMNS) {
w = this.getWidth();
r.push({ col: this, offset, width: w });
if (levelsToGo === 0) {
return w;
}
}
//push children
this._children.forEach((c) => {
if (c.isVisible() && levelsToGo <= Column.FLAT_ALL_COLUMNS) {
c.flatten(r, offset, levelsToGo - 1, padding);
}
});
return w;
}
dump(toDescRef: (desc: any) => any) {
const r = super.dump(toDescRef);
r.children = this._children.map((d) => d.dump(toDescRef));
return r;
}
restore(dump: any, factory: ITypeFactory) {
dump.children.forEach((child: any) => {
const c = factory(child);
if (c) {
this.push(c);
}
});
super.restore(dump, factory);
}
/**
* inserts a column at a the given position
* @param col
* @param index
* @returns {any}
*/
insert(col: Column, index: number): Column | null {
if (!isNumberColumn(col) && this.canJustAddNumbers) {
//indicator it is a number type
return null;
}
this._children.splice(index, 0, col);
//listen and propagate events
return this.insertImpl(col, index);
}
move(col: Column, index: number): Column | null {
if (col.parent !== this) {
//not moving
return null;
}
const old = this._children.indexOf(col);
if (index === old) {
// no move needed
return col;
}
//delete first
this._children.splice(old, 1);
// adapt target index based on previous index, i.e shift by one
this._children.splice(old < index ? index - 1 : index, 0, col);
//listen and propagate events
return this.moveImpl(col, index, old);
}
protected insertImpl(col: Column, index: number) {
col.attach(this);
this.forward(
col,
...suffix(
'.combine',
Column.EVENT_DIRTY_HEADER,
Column.EVENT_DIRTY_VALUES,
Column.EVENT_DIRTY_CACHES,
Column.EVENT_DIRTY,
CompositeColumn.EVENT_FILTER_CHANGED,
Column.EVENT_RENDERER_TYPE_CHANGED,
Column.EVENT_GROUP_RENDERER_TYPE_CHANGED
)
);
this.fire(
[
CompositeColumn.EVENT_ADD_COLUMN,
Column.EVENT_DIRTY_HEADER,
Column.EVENT_DIRTY_VALUES,
Column.EVENT_DIRTY_CACHES,
Column.EVENT_DIRTY,
],
col,
index
);
return col;
}
protected moveImpl(col: Column, index: number, oldIndex: number) {
this.fire(
[
CompositeColumn.EVENT_MOVE_COLUMN,
Column.EVENT_DIRTY_HEADER,
Column.EVENT_DIRTY_VALUES,
Column.EVENT_DIRTY_CACHES,
Column.EVENT_DIRTY,
Column.EVENT_RENDERER_TYPE_CHANGED,
Column.EVENT_GROUP_RENDERER_TYPE_CHANGED,
],
col,
index,
oldIndex
);
return col;
}
push(col: Column) {
return this.insert(col, this._children.length);
}
at(index: number) {
return this._children[index];
}
indexOf(col: Column) {
return this._children.indexOf(col);
}
insertAfter(col: Column, ref: Column) {
const i = this.indexOf(ref);
if (i < 0) {
return null;
}
return this.insert(col, i + 1);
}
moveAfter(col: Column, ref: Column) {
const i = this.indexOf(ref);
if (i < 0) {
return null;
}
return this.move(col, i + 1);
}
insertBefore(col: Column, ref: Column) {
const i = this.indexOf(ref);
if (i < 0) {
return null;
}
return this.insert(col, i);
}
moveBefore(col: Column, ref: Column) {
const i = this.indexOf(ref);
if (i < 0) {
return null;
}
return this.move(col, i);
}
remove(col: Column) {
const i = this._children.indexOf(col);
if (i < 0) {
return false;
}
this._children.splice(i, 1); //remove and deregister listeners
return this.removeImpl(col, i);
}
protected removeImpl(col: Column, index: number) {
col.detach();
this.unforward(
col,
...suffix(
'.combine',
Column.EVENT_DIRTY_HEADER,
Column.EVENT_DIRTY_VALUES,
Column.EVENT_DIRTY_CACHES,
Column.EVENT_DIRTY,
CompositeColumn.EVENT_FILTER_CHANGED
)
);
this.fire(
[
CompositeColumn.EVENT_REMOVE_COLUMN,
Column.EVENT_DIRTY_HEADER,
Column.EVENT_DIRTY_VALUES,
Column.EVENT_DIRTY_CACHES,
Column.EVENT_DIRTY,
],
col,
index
);
return true;
}
isFiltered() {
return this._children.some((d) => d.isFiltered());
}
clearFilter() {
return this._children.map((d) => d.clearFilter()).some((d) => d);
}
filter(row: IDataRow) {
return this._children.every((d) => d.filter(row));
}
isLoaded(): boolean {
return this._children.every(
(c) =>
!(c instanceof ValueColumn || c instanceof CompositeColumn) ||
(c as ValueColumn<any> | CompositeColumn).isLoaded()
);
}
get canJustAddNumbers() {
return false;
}
}