/
StringCellRenderer.ts
192 lines (180 loc) · 6.05 KB
/
StringCellRenderer.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
import { StringColumn, Column, type IDataRow, type IOrderedGroup, type IStringFilter } from '../model';
import { filterMissingMarkup, findFilterMissing } from '../ui/missing';
import type {
IRenderContext,
ICellRendererFactory,
ISummaryRenderer,
IGroupCellRenderer,
ICellRenderer,
} from './interfaces';
import { renderMissingDOM } from './missing';
import { setText, exampleText } from './utils';
import { cssClass } from '../styles';
import { debounce } from '../internal';
/**
* renders a string with additional alignment behavior
* one instance factory shared among strings
*/
export default class StringCellRenderer implements ICellRendererFactory {
readonly title: string = 'Default';
canRender(col: Column): boolean {
return col instanceof StringColumn;
}
create(col: StringColumn, context: IRenderContext): ICellRenderer {
const align = context.sanitize(col.alignment || 'left');
return {
template: `<div${align !== 'left' ? ` class="${cssClass(align)}"` : ''}> </div>`,
update: (n: HTMLDivElement, d: IDataRow) => {
renderMissingDOM(n, col, d);
if (col.escape) {
setText(n, col.getLabel(d));
} else {
n.innerHTML = col.getLabel(d);
}
n.title = n.textContent!;
},
};
}
createGroup(col: StringColumn, context: IRenderContext): IGroupCellRenderer {
return {
template: `<div> </div>`,
update: (n: HTMLDivElement, group: IOrderedGroup) => {
return context.tasks
.groupExampleRows(col, group, 'string', (rows) => exampleText(col, rows))
.then((text) => {
if (typeof text === 'symbol') {
return;
}
n.classList.toggle(cssClass('missing'), !text);
if (col.escape) {
setText(n, text);
} else {
n.innerHTML = text;
n.title = text;
}
});
},
};
}
private static interactiveSummary(col: StringColumn, node: HTMLElement) {
const form = node as HTMLFormElement;
const filterMissing = findFilterMissing(node);
const input = node.querySelector<HTMLInputElement>('input[type="text"]');
const isRegex = node.querySelector<HTMLInputElement>('input[type="checkbox"]');
let isInputFocused = false;
const update = () => {
const valid = input.value.trim();
if (valid.length <= 0) {
const filter = filterMissing.checked ? { filter: null, filterMissing: filterMissing.checked } : null;
col.setFilter(filter);
return;
}
col.setFilter({
filter: isRegex.checked ? new RegExp(input.value) : input.value,
filterMissing: filterMissing.checked,
});
};
filterMissing.onchange = update;
input.onchange = update;
input.oninput = debounce(update, 100);
input.onfocus = () => {
isInputFocused = true;
};
input.onblur = () => {
isInputFocused = false;
};
isRegex.onchange = update;
form.onsubmit = (evt) => {
evt.preventDefault();
evt.stopPropagation();
update();
return false;
};
return (actCol: StringColumn) => {
// skip update of form fields if search input is currently in focus
// otherwise this function sets an old value while typing
if (isInputFocused) {
return;
}
col = actCol;
const f = col.getFilter() || { filter: null, filterMissing: false };
const bak = f.filter;
filterMissing.checked = f.filterMissing;
input.value = bak instanceof RegExp ? bak.source : bak || '';
isRegex.checked = bak instanceof RegExp;
};
}
createSummary(col: StringColumn, context: IRenderContext, interactive: boolean): ISummaryRenderer {
if (!interactive) {
return {
template: `<div></div>`,
update: (node: HTMLElement) => {
const filter = col.getFilter();
node.textContent = filterToString(filter);
},
};
}
const f = col.getFilter() || { filter: null, filterMissing: false };
const bak = f.filter || '';
let update: (col: StringColumn) => void;
return {
template: `<form><input type="text" placeholder="Filter ${context.sanitize(col.desc.label)}..." autofocus
list="${context.idPrefix}${col.id}_dl" value="${context.sanitize(filterToString(f))}">
<label class="${cssClass('checkbox')}">
<input type="checkbox" ${bak instanceof RegExp ? 'checked="checked"' : ''}>
<span>Use regular expressions</span>
</label>
${filterMissingMarkup(f.filterMissing)}
<datalist id="${context.idPrefix}${col.id}_dl"></datalist></form>`,
update: (node: HTMLElement) => {
if (!update) {
update = StringCellRenderer.interactiveSummary(col, node);
}
update(col);
const dl = node.querySelector('datalist')!;
// no return here = loading indicator since it won't affect the rendering
void context.tasks.summaryStringStats(col).then((r) => {
if (typeof r === 'symbol') {
return;
}
const { summary } = r;
matchDataList(dl, summary.topN);
});
},
};
}
}
/**
* @internal
*/
export function filterToString(filter: IStringFilter | null) {
if (filter == null || !filter.filter) {
return '';
}
if (filter.filter instanceof RegExp) {
return filter.filter.source;
}
return filter.filter;
}
/**
* matches the given stats to a datalist
* @internal
*/
export function matchDataList(node: HTMLDataListElement, matches: readonly { value: string; count: number }[]) {
const children = Array.from(node.options);
// update existing
for (let i = 0; i < matches.length; i++) {
const m = matches[i];
let child = children[i];
if (!child) {
child = node.ownerDocument.createElement('option');
node.appendChild(child);
}
child.value = m.value;
setText(child, m.count > 1 ? `${m.value} (${m.count.toLocaleString()})` : m.value);
}
// remove extra
for (let i = children.length - 1; i >= matches.length; i--) {
children[i].remove();
}
}