/
LinkMapColumn.ts
153 lines (138 loc) · 4.96 KB
/
LinkMapColumn.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
import { toolbar } from './annotations';
import type Column from './Column';
import type {
widthChanged,
labelChanged,
metaDataChanged,
dirty,
dirtyHeader,
dirtyValues,
rendererTypeChanged,
groupRendererChanged,
summaryRendererChanged,
visibilityChanged,
dirtyCaches,
} from './Column';
import type { dataLoaded } from './ValueColumn';
import type ValueColumn from './ValueColumn';
import type { IDataRow, ITypeFactory } from './interfaces';
import { patternFunction, integrateDefaults } from './internal';
import MapColumn, { type IMapColumnDesc } from './MapColumn';
import LinkColumn, { type ILinkDesc } from './LinkColumn';
import type { IEventListener } from '../internal';
import { EAlignment } from './StringColumn';
import type { IKeyValue } from './IArrayColumn';
import type { ILink } from './LinkColumn';
export declare type ILinkMapColumnDesc = ILinkDesc & IMapColumnDesc<string>;
/**
* emitted when the pattern property changes
* @asMemberOf LinkMapColumn
* @event
*/
export declare function patternChanged_LMC(previous: string, current: string): void;
/**
* a string column with optional alignment
*/
@toolbar('rename', 'search', 'editPattern')
export default class LinkMapColumn extends MapColumn<string> {
static readonly EVENT_PATTERN_CHANGED = LinkColumn.EVENT_PATTERN_CHANGED;
readonly alignment: EAlignment;
readonly escape: boolean;
private pattern: string;
private patternFunction: (value: string, raw: any, key: string) => string | null = null;
readonly patternTemplates: string[];
constructor(id: string, desc: Readonly<ILinkMapColumnDesc>) {
super(
id,
integrateDefaults(desc, {
width: 200,
renderer: 'map',
})
);
this.alignment = desc.alignment ?? EAlignment.left;
this.escape = desc.escape !== false;
this.pattern = desc.pattern ?? '';
this.patternTemplates = desc.patternTemplates ?? [];
}
setPattern(pattern: string) {
LinkColumn.prototype.setPattern.call(this, pattern);
}
getPattern() {
return this.pattern;
}
protected createEventList() {
return super.createEventList().concat([LinkColumn.EVENT_PATTERN_CHANGED]);
}
on(type: typeof LinkColumn.EVENT_PATTERN_CHANGED, listener: typeof patternChanged_LMC | null): this;
on(type: typeof ValueColumn.EVENT_DATA_LOADED, listener: typeof dataLoaded | 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) {
const r = this.getLinkMap(row);
return r.every((d) => d.value == null)
? null
: r.map(({ key, value }) => ({
key,
value: value ? value.href : '',
}));
}
getLabels(row: IDataRow) {
return this.getLinkMap(row).map(({ key, value }) => ({
key,
value: value ? value.alt : '',
}));
}
getLinkMap(row: IDataRow): IKeyValue<ILink>[] {
return super.getMap(row).map(({ key, value }) => ({
key,
value: this.transformValue(value, row, key),
}));
}
private transformValue(v: any, row: IDataRow, key: string) {
if (v == null || v === '') {
return null;
}
if (typeof v === 'string') {
if (!this.pattern) {
return {
alt: v,
href: v,
};
}
if (!this.patternFunction) {
this.patternFunction = patternFunction(this.pattern, 'item', 'key');
}
return {
alt: v,
href: this.patternFunction.call(this, v, row.v, key),
};
}
return v;
}
dump(toDescRef: (desc: any) => any): any {
const r = super.dump(toDescRef);
if (this.pattern !== (this.desc as any).pattern) {
r.pattern = this.pattern;
}
return r;
}
restore(dump: any, factory: ITypeFactory) {
if (dump.pattern) {
this.pattern = dump.pattern;
}
super.restore(dump, factory);
}
}