/
StringColumn.ts
355 lines (318 loc) · 10.3 KB
/
StringColumn.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import { Category, toolbar, dialogAddons } from './annotations';
import Column, {
widthChanged,
labelChanged,
metaDataChanged,
dirty,
dirtyHeader,
dirtyValues,
rendererTypeChanged,
groupRendererChanged,
summaryRendererChanged,
visibilityChanged,
dirtyCaches,
} from './Column';
import {
defaultGroup,
type IDataRow,
type IGroup,
ECompareValueType,
type IValueColumnDesc,
othersGroup,
type ITypeFactory,
} from './interfaces';
import { missingGroup, isMissingValue } from './missing';
import type { dataLoaded } from './ValueColumn';
import ValueColumn from './ValueColumn';
import { equal, type IEventListener, type ISequence, isSeqEmpty } from '../internal';
import { integrateDefaults } from './internal';
export enum EAlignment {
left = 'left',
center = 'center',
right = 'right',
}
export enum EStringGroupCriteriaType {
value = 'value',
startsWith = 'startsWith',
regex = 'regex',
}
export interface IStringGroupCriteria {
type: EStringGroupCriteriaType;
values: (string | RegExp)[];
}
export interface IStringDesc {
/**
* column alignment: left, center, right
* @default left
*/
alignment?: EAlignment;
/**
* escape html tags
*/
escape?: boolean;
}
export declare type IStringColumnDesc = IStringDesc & IValueColumnDesc<string>;
export interface IStringFilter {
filter: string | RegExp | null;
filterMissing: boolean;
}
/**
* emitted when the filter property changes
* @asMemberOf StringColumn
* @event
*/
export declare function filterChanged_SC(previous: string | RegExp | null, current: string | RegExp | null): void;
/**
* emitted when the grouping property changes
* @asMemberOf StringColumn
* @event
*/
export declare function groupingChanged_SC(previous: (RegExp | string)[][], current: (RegExp | string)[][]): void;
/**
* a string column with optional alignment
*/
@toolbar('rename', 'clone', 'sort', 'sortBy', 'search', 'groupBy', 'sortGroupBy', 'filterString')
@dialogAddons('group', 'groupString')
@Category('string')
export default class StringColumn extends ValueColumn<string> {
static readonly EVENT_FILTER_CHANGED = 'filterChanged';
static readonly EVENT_GROUPING_CHANGED = 'groupingChanged';
//magic key for filtering missing ones
private static readonly FILTER_MISSING = '__FILTER_MISSING';
private currentFilter: IStringFilter | null = null;
readonly alignment: EAlignment;
readonly escape: boolean;
private currentGroupCriteria: IStringGroupCriteria = {
type: EStringGroupCriteriaType.startsWith,
values: [],
};
constructor(id: string, desc: Readonly<IStringColumnDesc>) {
super(
id,
integrateDefaults(desc, {
width: 200,
})
);
this.alignment = desc.alignment ?? EAlignment.left;
this.escape = desc.escape !== false;
}
protected createEventList() {
return super.createEventList().concat([StringColumn.EVENT_GROUPING_CHANGED, StringColumn.EVENT_FILTER_CHANGED]);
}
on(type: typeof StringColumn.EVENT_FILTER_CHANGED, listener: typeof filterChanged_SC | null): this;
on(type: typeof ValueColumn.EVENT_DATA_LOADED, listener: typeof dataLoaded | null): this;
on(type: typeof StringColumn.EVENT_GROUPING_CHANGED, listener: typeof groupingChanged_SC | 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 as any, listener);
}
getValue(row: IDataRow): string | null {
const v: any = super.getValue(row);
return isMissingValue(v) ? null : String(v);
}
getLabel(row: IDataRow) {
return this.getValue(row) || '';
}
dump(toDescRef: (desc: any) => any): any {
const r = super.dump(toDescRef);
if (this.currentFilter instanceof RegExp) {
r.filter = `REGEX:${(this.currentFilter as RegExp).source}`;
} else {
r.filter = this.currentFilter;
}
if (this.currentGroupCriteria) {
const { type, values } = this.currentGroupCriteria;
r.groupCriteria = {
type,
values: values.map((value) =>
value instanceof RegExp && type === EStringGroupCriteriaType.regex ? value.source : value
),
};
}
return r;
}
restore(dump: any, factory: ITypeFactory) {
super.restore(dump, factory);
if (dump.filter) {
const filter = dump.filter;
if (typeof filter === 'string') {
// compatibility case
if (filter.startsWith('REGEX:')) {
this.currentFilter = {
filter: new RegExp(filter.slice(6), 'm'),
filterMissing: false,
};
} else if (filter === StringColumn.FILTER_MISSING) {
this.currentFilter = {
filter: null,
filterMissing: true,
};
} else {
this.currentFilter = {
filter,
filterMissing: false,
};
}
} else {
this.currentFilter = {
filter:
filter.filter && (filter.filter as string).startsWith('REGEX:')
? new RegExp(filter.slice(6), 'm')
: filter.filter || '',
filterMissing: filter.filterMissing === true,
};
}
} else {
this.currentFilter = null;
}
// tslint:disable-next-line: early-exit
if (dump.groupCriteria) {
const { type, values } = dump.groupCriteria as IStringGroupCriteria;
this.currentGroupCriteria = {
type,
values: values.map((value) =>
type === EStringGroupCriteriaType.regex ? new RegExp(value as string, 'm') : value
),
};
}
}
isFiltered() {
return this.currentFilter != null;
}
filter(row: IDataRow) {
if (!this.isFiltered()) {
return true;
}
const r = this.getLabel(row);
const filter = this.currentFilter!;
const ff = filter.filter;
if (r == null || r.trim() === '') {
return !filter.filterMissing;
}
if (!ff) {
return true;
}
if (ff instanceof RegExp) {
return r !== '' && r.match(ff) != null; // You can not use RegExp.test(), because of https://stackoverflow.com/a/6891667
}
return r !== '' && r.toLowerCase().includes(ff.toLowerCase());
}
getFilter() {
return this.currentFilter;
}
setFilter(filter: IStringFilter | null) {
if (filter === this.currentFilter) {
return;
}
const current = this.currentFilter || { filter: null, filterMissing: false };
const target = filter || { filter: null, filterMissing: false };
if (
current.filterMissing === target.filterMissing &&
(current.filter === target.filter ||
(current.filter instanceof RegExp &&
target.filter instanceof RegExp &&
current.filter.source === target.filter.source))
) {
return;
}
this.fire(
[StringColumn.EVENT_FILTER_CHANGED, Column.EVENT_DIRTY_VALUES, Column.EVENT_DIRTY],
this.currentFilter,
(this.currentFilter = filter)
);
}
clearFilter() {
const was = this.isFiltered();
this.setFilter(null);
return was;
}
getGroupCriteria(): IStringGroupCriteria {
return this.currentGroupCriteria;
}
setGroupCriteria(value: IStringGroupCriteria) {
if (equal(this.currentGroupCriteria, value) || value == null) {
return;
}
const bak = this.getGroupCriteria();
this.currentGroupCriteria = value;
this.fire([StringColumn.EVENT_GROUPING_CHANGED, Column.EVENT_DIRTY_VALUES, Column.EVENT_DIRTY], bak, value);
}
group(row: IDataRow): IGroup {
if (this.getValue(row) == null) {
return Object.assign({}, missingGroup);
}
if (!this.currentGroupCriteria) {
return Object.assign({}, othersGroup);
}
const value = this.getLabel(row);
if (!value) {
return Object.assign({}, missingGroup);
}
const { type, values } = this.currentGroupCriteria;
if (type === EStringGroupCriteriaType.value) {
return {
name: value,
color: defaultGroup.color,
};
}
if (type === EStringGroupCriteriaType.startsWith) {
for (const groupValue of values) {
if (typeof groupValue !== 'string' || !value.startsWith(groupValue)) {
continue;
}
return {
name: groupValue,
color: defaultGroup.color,
};
}
return Object.assign({}, othersGroup);
}
for (const groupValue of values) {
if (!(groupValue instanceof RegExp) || !groupValue.test(value)) {
continue;
}
return {
name: groupValue.source,
color: defaultGroup.color,
};
}
return Object.assign({}, othersGroup);
}
toCompareValue(row: IDataRow) {
const v = this.getValue(row);
return v === '' || v == null ? null : v.toLowerCase();
}
toCompareValueType() {
return ECompareValueType.STRING;
}
toCompareGroupValue(rows: ISequence<IDataRow>, _group: IGroup, valueCache?: ISequence<any>) {
if (isSeqEmpty(rows)) {
return null;
}
// take the smallest one
if (valueCache) {
return valueCache.reduce((acc, v) => (acc == null || v < acc ? v : acc), null as null | string);
}
return rows.reduce(
(acc, d) => {
const v = this.getValue(d);
return acc == null || (v != null && v < acc) ? v : acc;
},
null as null | string
);
}
toCompareGroupValueType() {
return ECompareValueType.STRING;
}
}