This repository has been archived by the owner on Jun 4, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 74
/
AppMode.ts
409 lines (364 loc) · 12 KB
/
AppMode.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import * as R from 'ramda';
import Environment from 'core/environment';
import { generateMockData, IDataMock, generateSpaceMockData, generateMarkdownMockData, generateMixedMarkdownMockData } from './data';
import {
PropsWithDefaults,
ChangeAction,
ChangeFailure,
ColumnType,
TableAction,
IProps,
Columns
} from 'dash-table/components/Table/props';
import { TooltipSyntax } from 'dash-table/tooltips/props';
export enum AppMode {
Actionable = 'actionable',
Date = 'date',
Default = 'default',
Formatting = 'formatting',
Markdown = 'markdown',
MixedMarkdown = 'mixedmarkdown',
ReadOnly = 'readonly',
SomeReadOnly = 'someReadonly',
ColumnsInSpace = 'columnsInSpace',
SingleHeaders = 'singleHeaders',
TaleOfTwoTables = 'taleOfTwoTables',
Tooltips = 'tooltips',
Typed = 'typed',
Virtualized = 'virtualized'
}
export enum AppFlavor {
ColumnSelectableSingle = 'column_selectable="single"',
ColumnSelectableMulti = 'column_selectable="multi"',
FilterNative = 'filter_action="native"',
FixedColumn = 'fixed_columns={ "headers": true }',
FixedColumnPlus1 = 'fixed_columns={ "headers": true, "data": 1 }',
FixedRow = 'fixed_rows={ "headers": true }',
FixedRowPlus1 = 'fixed_rows={ "headers": true, "data": 1 }',
Merged = 'merge_duplicate_headers=true',
NoId = 'id=null'
}
export const ReadWriteModes = [
AppMode.Default,
AppMode.Virtualized
];
export const BasicModes = [
...ReadWriteModes,
AppMode.ReadOnly
];
function getBaseTableProps(mock: IDataMock): Partial<IProps> {
return {
id: 'table',
columns: mock.columns.map((col: any) => R.merge(col, {
name: col.name || col.id,
on_change: {
action: ChangeAction.None
},
renamable: true,
deletable: true
})) as Columns,
dropdown: {
bbb: {
clearable: true,
options: ['Humid', 'Wet', 'Snowy', 'Tropical Beaches'].map(i => ({
label: `label: ${i}`,
value: i
}))
},
'bbb-readonly': {
clearable: true,
options: ['Humid', 'Wet', 'Snowy', 'Tropical Beaches'].map(i => ({
label: `label: ${i}`,
value: i
}))
}
},
page_action: TableAction.None,
style_table: {
maxHeight: '800px',
height: '800px',
maxWidth: '1000px',
width: '1000px'
},
style_cell: {
maxWidth: 150,
minWidth: 150,
width: 150
},
style_cell_conditional: [
{ if: { column_id: 'rows' }, maxWidth: 60, minWidth: 60, width: 60 },
{ if: { column_id: 'bbb' }, maxWidth: 200, minWidth: 200, width: 200 },
{ if: { column_id: 'bbb-readonly' }, maxWidth: 200, minWidth: 200, width: 200 }
]
};
}
function getDefaultState(
generateData: Function = generateMockData
): {
filter_query: string,
tableProps: Partial<PropsWithDefaults>
} {
const mock = generateData(5000);
return {
filter_query: '',
tableProps: R.merge(getBaseTableProps(mock), {
data: mock.data,
editable: true,
sort_action: TableAction.Native,
fill_width: false,
fixed_rows: { headers: true },
fixed_columns: { headers: true },
merge_duplicate_headers: false,
row_deletable: true,
row_selectable: 'single',
page_action: TableAction.Native
}) as Partial<PropsWithDefaults>
};
}
function getDefaultMarkdownState() {
const state = getDefaultState(generateMarkdownMockData);
state.tableProps.editable = false;
state.tableProps.style_cell = {};
state.tableProps.style_cell_conditional = [];
return state;
}
function getDefaultMixedMarkdownState() {
const state = getDefaultState(generateMixedMarkdownMockData);
state.tableProps.editable = false;
state.tableProps.style_cell = {};
state.tableProps.style_cell_conditional = [];
return state;
}
function getReadonlyState() {
const state = getDefaultState();
state.tableProps.editable = false;
state.tableProps.row_deletable = false;
R.forEach(column => {
column.editable = false;
}, state.tableProps.columns || []);
return state;
}
function getSomeReadonlyState() {
const state = getDefaultState();
state.tableProps.editable = true;
state.tableProps.row_deletable = false;
R.forEach(column => {
column.editable = !R.includes(column.id, ['bbb', 'eee', 'fff']);
}, state.tableProps.columns || []);
return state;
}
function getSpaceInColumn() {
const state = getDefaultState(generateSpaceMockData);
state.tableProps.filter_action = TableAction.Native;
return state;
}
function getTooltipsState() {
const state = getDefaultState();
state.tableProps.tooltip_delay = 250;
state.tableProps.tooltip_duration = 1000;
state.tableProps.tooltip_data = [
{ ccc: { type: TooltipSyntax.Markdown, value: `### Go Proverb\nThe enemy's key point is yours` } },
{ ccc: { type: TooltipSyntax.Markdown, value: `### Go Proverb\nPlay on the point of symmetry` } },
{ ccc: { type: TooltipSyntax.Markdown, value: `### Go Proverb\nSente gains nothing` } },
{ ccc: { type: TooltipSyntax.Text, value: `Beware of going back to patch up` } },
{ ccc: { type: TooltipSyntax.Text, value: `When in doubt, Tenuki` } },
{ ccc: `People in glass houses should not throw stones` }
];
state.tableProps.tooltip = {
ccc: { type: TooltipSyntax.Text, value: `There is death in the hane` },
ddd: { type: TooltipSyntax.Markdown, value: `Hane, Cut, Placement` },
rows: `Learn the eyestealing tesuji`
};
state.tableProps.tooltip_conditional = [{
if: {
column_id: 'aaa-readonly',
filter_query: `{aaa} is prime`
},
type: TooltipSyntax.Markdown,
value: `### Go Proverbs\nCapture three to get an eye`
}, {
if: {
column_id: 'bbb-readonly',
row_index: 'odd'
},
type: TooltipSyntax.Markdown,
value: `### Go Proverbs\nSix die but eight live`
}, {
if: {
column_id: 'bbb-readonly'
},
type: TooltipSyntax.Markdown,
value: `### Go Proverbs\nUrgent points before big points\n![Sensei](https://senseis.xmp.net/images/stone-hello.png)`
}];
return state;
}
function getTypedState() {
const state = getDefaultState();
R.forEach(column => {
column.on_change = {
action: ChangeAction.Coerce,
failure: ChangeFailure.Reject
};
}, state.tableProps.columns || []);
return state;
}
function getSingleHeaderState() {
const state = getDefaultState();
R.forEach(column => {
if (Array.isArray(column.name)) {
column.name = column.name[column.name.length - 1];
}
}, state.tableProps.columns || []);
return state;
}
function getActionableState() {
const state = getDefaultState();
state.tableProps.filter_action = TableAction.Native;
R.forEach(c => {
c.clearable = true;
c.hideable = 'last';
c.selectable = true;
}, state.tableProps.columns || []);
return state;
}
function getDateState() {
const state = getTypedState();
(state.tableProps.columns || []).forEach(column => {
if (column.id === 'ccc') {
column.name = ['Date', 'only'];
column.type = ColumnType.Datetime;
column.validation = { allow_YY: true };
(state.tableProps.data || []).forEach((row, i) => {
const d = new Date(Date.UTC(2018, 0, 1));
// three day increment
d.setUTCDate(3 * i + 1);
// date only
row.ccc = d.toISOString().substr(0, 10);
});
} else if (column.id === 'ddd') {
column.name = ['Date', 'with', 'time'];
column.type = ColumnType.Datetime;
(state.tableProps.data || []).forEach((row, i) => {
const d = new Date(Date.UTC(2018, 0, 1));
// two hours and 11 seconds increment
d.setUTCSeconds(i * 7211);
// datetime ending with seconds
row.ddd = d.toISOString().substr(0, 19).replace('T', ' ');
});
}
});
return state;
}
function getVirtualizedState() {
const mock = generateMockData(5000);
return {
filter_query: '',
tableProps: R.merge(getBaseTableProps(mock), {
data: mock.data,
editable: true,
fill_width: false,
sort_action: TableAction.Native,
merge_duplicate_headers: false,
row_deletable: true,
row_selectable: 'single',
virtualization: true
})
};
}
function getFormattingState() {
const state = getDefaultState();
R.forEach((datum: any) => {
if (datum.eee % 2 === 0) {
datum.eee = undefined;
} else if (datum.eee % 10 === 5) {
datum.eee = `xx-${datum.eee}-xx`;
}
}, state.tableProps.data as any);
R.forEach((column: any) => {
if (column.id === 'rows') {
column.format = {
specifier: '.^5'
};
} else if (column.id === 'ccc') {
column.format = {
locale: {
separate_4digits: false
},
prefix: 1000,
specifier: '.3f'
};
} else if (column.id === 'ddd') {
column.format = {
locale: {
symbol: ['eq. $ ', ''],
separate_4digits: false
},
nully: 0,
specifier: '$,.2f'
};
column.on_change = {
action: 'coerce',
failure: 'default'
};
column.validation = {
allow_nully: true
};
} else if (column.id === 'eee') {
column.format = {
nully: 'N/A',
specifier: ''
};
column.on_change = {
action: 'coerce',
failure: 'default'
};
}
}, state.tableProps.columns as any);
return state;
}
function getModeState(mode: string | null) {
switch (mode) {
case AppMode.Actionable:
return getActionableState();
case AppMode.Date:
return getDateState();
case AppMode.Formatting:
return getFormattingState();
case AppMode.Markdown:
return getDefaultMarkdownState();
case AppMode.MixedMarkdown:
return getDefaultMixedMarkdownState();
case AppMode.ReadOnly:
return getReadonlyState();
case AppMode.SomeReadOnly:
return getSomeReadonlyState();
case AppMode.ColumnsInSpace:
return getSpaceInColumn();
case AppMode.Tooltips:
return getTooltipsState();
case AppMode.Virtualized:
return getVirtualizedState();
case AppMode.Typed:
return getTypedState();
case AppMode.SingleHeaders:
return getSingleHeaderState();
case AppMode.TaleOfTwoTables:
return getActionableState();
case AppMode.Default:
default:
return getDefaultState();
}
}
function getState() {
const mode = Environment.searchParams.get('mode');
const flavorParam = Environment.searchParams.get('flavor');
const flavors = flavorParam ? flavorParam.split(';') : [];
let state = getModeState(mode);
flavors.forEach(flavor => {
const [key, valueString] = flavor.split('=');
const value = JSON.parse(valueString);
(state.tableProps as any)[key] = value;
});
return state;
}
export default getState();