-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
vis-state-merger.ts
614 lines (542 loc) · 17 KB
/
vis-state-merger.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
// Copyright (c) 2022 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import uniq from 'lodash.uniq';
import pick from 'lodash.pick';
import flattenDeep from 'lodash.flattendeep';
import {isObject, arrayInsert, getInitialMapLayersForSplitMap} from '@kepler.gl/utils';
import {LayerColumns, LayerColumn, Layer} from '@kepler.gl/layers';
import {LAYER_BLENDINGS} from '@kepler.gl/constants';
import {
CURRENT_VERSION,
Merger,
VisState,
VisStateMergers,
visStateSchema
} from '@kepler.gl/schemas';
import {ParsedConfig} from 'schemas';
import {ParsedLayer, SavedInteractionConfig, TooltipInfo} from '@kepler.gl/types';
import {KeplerTable, Datasets} from '@kepler.gl/table';
import {
applyFiltersToDatasets,
validateFiltersUpdateDatasets,
assignGpuChannels,
resetFilterGpuMode
} from '@kepler.gl/table';
/**
* Merge loaded filters with current state, if no fields or data are loaded
* save it for later
*
*/
export function mergeFilters<S extends VisState>(
state: S,
filtersToMerge: NonNullable<ParsedConfig['visState']>['filters'],
fromConfig?: boolean
): S {
if (!Array.isArray(filtersToMerge) || !filtersToMerge.length) {
return state;
}
const {validated, failed, updatedDatasets} = validateFiltersUpdateDatasets(state, filtersToMerge);
// merge filter with existing
let updatedFilters = [...(state.filters || []), ...validated];
updatedFilters = resetFilterGpuMode(updatedFilters);
updatedFilters = assignGpuChannels(updatedFilters);
// filter data
const datasetsToFilter = uniq(flattenDeep(validated.map(f => f.dataId)));
const filtered = applyFiltersToDatasets(
datasetsToFilter,
updatedDatasets,
updatedFilters,
state.layers
);
return {
...state,
filters: updatedFilters,
datasets: filtered,
filterToBeMerged: [...state.filterToBeMerged, ...failed]
};
}
export function createLayerFromConfig(state: VisState, layerConfig: any): Layer | null {
// first validate config against dataset
const {validated, failed} = validateLayersByDatasets(state.datasets, state.layerClasses, [
layerConfig
]);
if (failed?.length || !validated.length) {
// failed
return null;
}
const newLayer = validated[0];
newLayer.updateLayerDomain(state.datasets);
return newLayer;
}
export function serializeLayer(newLayer): ParsedLayer {
const savedVisState = visStateSchema[CURRENT_VERSION].save(
// @ts-expect-error not all expected properties are provided
{
layers: [newLayer],
layerOrder: [0]
}
).visState;
const loadedLayer = visStateSchema[CURRENT_VERSION].load(savedVisState).visState?.layers?.[0];
// @ts-expect-error
return loadedLayer;
}
/**
* Merge layers from de-serialized state, if no fields or data are loaded
* save it for later
*
*/
export function mergeLayers<S extends VisState>(
state: S,
layersToMerge: NonNullable<ParsedConfig['visState']>['layers'] = [],
fromConfig?: boolean
): S {
const preserveLayerOrder = fromConfig ? layersToMerge.map(l => l.id) : state.preserveLayerOrder;
if (!Array.isArray(layersToMerge) || !layersToMerge.length) {
return state;
}
const {validated: mergedLayer, failed: unmerged} = validateLayersByDatasets(
state.datasets,
state.layerClasses,
layersToMerge
);
// put new layers in front of current layers
const {newLayerOrder, newLayers} = insertLayerAtRightOrder(
state.layers,
mergedLayer,
state.layerOrder,
// @ts-expect-error
preserveLayerOrder
);
return {
...state,
layers: newLayers,
layerOrder: newLayerOrder,
preserveLayerOrder,
layerToBeMerged: [...state.layerToBeMerged, ...unmerged]
};
}
export function insertLayerAtRightOrder(
currentLayers,
layersToInsert,
currentOrder,
preservedOrder: string[] = []
) {
// perservedOrder ['a', 'b', 'c'];
// layerOrder [1, 0, 3]
// layerOrderMap ['a', 'c']
let layerOrderQueue = currentOrder.map(i => currentLayers[i].id);
let newLayers = currentLayers;
for (const newLayer of layersToInsert) {
// find where to insert it
const expectedIdx = preservedOrder.indexOf(newLayer.id);
// if cant find place to insert, insert at the front
let insertAt = 0;
if (expectedIdx > 0) {
// look for layer to insert after
let i = expectedIdx + 1;
let preceedIdx = -1;
while (i-- > 0 && preceedIdx < 0) {
// keep looking for preceed layer that is already loaded
const preceedLayer = preservedOrder[i - 1];
preceedIdx = layerOrderQueue.indexOf(preceedLayer);
}
if (preceedIdx > -1) {
// if found
insertAt = preceedIdx + 1;
}
}
layerOrderQueue = arrayInsert(layerOrderQueue, insertAt, newLayer.id);
newLayers = newLayers.concat(newLayer);
}
// reconstruct layerOrder after insert
const newLayerOrder = layerOrderQueue.map(id => newLayers.findIndex(l => l.id === id));
return {
newLayerOrder,
newLayers
};
}
/**
* Merge interactions with saved config
*
*/
export function mergeInteractions<S extends VisState>(
state: S,
interactionToBeMerged: Partial<SavedInteractionConfig> | undefined,
fromConfig?: boolean
): S {
const merged: Partial<SavedInteractionConfig> = {};
const unmerged: Partial<SavedInteractionConfig> = {};
if (interactionToBeMerged) {
(Object.keys(interactionToBeMerged) as Array<keyof SavedInteractionConfig>).forEach(key => {
if (!state.interactionConfig[key]) {
return;
}
const currentConfig =
key === 'tooltip' || key === 'brush' ? state.interactionConfig[key].config : null;
const {enabled, ...configSaved} = interactionToBeMerged[key] || {};
let configToMerge = configSaved;
if (key === 'tooltip') {
const {mergedTooltip, unmergedTooltip} = mergeInteractionTooltipConfig(
state,
configSaved as SavedInteractionConfig['tooltip']
);
// merge new dataset tooltips with original dataset tooltips
configToMerge = {
fieldsToShow: {
...(currentConfig as TooltipInfo['config']).fieldsToShow,
...mergedTooltip
}
};
if (Object.keys(unmergedTooltip).length) {
// @ts-expect-error
unmerged.tooltip = {fieldsToShow: unmergedTooltip, enabled: Boolean(enabled)};
}
}
merged[key] = {
...state.interactionConfig[key],
enabled: Boolean(enabled),
...(currentConfig
? {
config: pick(
{
...currentConfig,
...configToMerge
},
Object.keys(currentConfig)
)
}
: {})
};
});
}
return {
...state,
interactionConfig: {
...state.interactionConfig,
...merged
},
interactionToBeMerged: unmerged
};
}
/**
* Merge splitMaps config with current visStete.
* 1. if current map is split, but splitMap DOESNOT contain maps
* : don't merge anything
* 2. if current map is NOT split, but splitMaps contain maps
* : add to splitMaps, and add current layers to splitMaps
*/
export function mergeSplitMaps<S extends VisState>(
state: S,
splitMaps: NonNullable<ParsedConfig['visState']>['splitMaps'] = [],
fromConfig?: boolean
): S {
const merged = [...state.splitMaps];
const unmerged = [];
splitMaps.forEach((sm, i) => {
Object.entries(sm.layers).forEach(([id, value]) => {
// check if layer exists
const pushTo = state.layers.find(l => l.id === id) ? merged : unmerged;
// create map panel if current map is not split
pushTo[i] = pushTo[i] || {
layers: pushTo === merged ? getInitialMapLayersForSplitMap(state.layers) : []
};
pushTo[i].layers = {
...pushTo[i].layers,
[id]: value
};
});
});
return {
...state,
splitMaps: merged,
splitMapsToBeMerged: [...state.splitMapsToBeMerged, ...unmerged]
};
}
/**
* Merge interactionConfig.tooltip with saved config,
* validate fieldsToShow
*
* @param state
* @param tooltipConfig
* @return - {mergedTooltip: {}, unmergedTooltip: {}}
*/
export function mergeInteractionTooltipConfig(
state: VisState,
tooltipConfig: Pick<TooltipInfo['config'], 'fieldsToShow'> | null = null
) {
const unmergedTooltip: TooltipInfo['config']['fieldsToShow'] = {};
const mergedTooltip: TooltipInfo['config']['fieldsToShow'] = {};
if (
!tooltipConfig ||
!tooltipConfig.fieldsToShow ||
!Object.keys(tooltipConfig.fieldsToShow).length
) {
return {mergedTooltip, unmergedTooltip};
}
for (const dataId in tooltipConfig.fieldsToShow) {
if (!state.datasets[dataId]) {
// is not yet loaded
unmergedTooltip[dataId] = tooltipConfig.fieldsToShow[dataId];
} else {
// if dataset is loaded
const allFields = state.datasets[dataId].fields.map(d => d.name);
const foundFieldsToShow = tooltipConfig.fieldsToShow[dataId].filter(field =>
allFields.includes(field.name)
);
mergedTooltip[dataId] = foundFieldsToShow;
}
}
return {mergedTooltip, unmergedTooltip};
}
/**
* Merge layerBlending with saved
*
*/
export function mergeLayerBlending<S extends VisState>(
state: S,
layerBlending: NonNullable<ParsedConfig['visState']>['layerBlending'],
fromConfig?: boolean
): S {
if (layerBlending && LAYER_BLENDINGS[layerBlending]) {
return {
...state,
layerBlending
};
}
return state;
}
/**
* Merge animation config
*/
export function mergeAnimationConfig<S extends VisState>(
state: S,
animation: NonNullable<ParsedConfig['visState']>['animationConfig'],
fromConfig?: boolean
): S {
if (animation && animation.currentTime) {
return {
...state,
animationConfig: {
...state.animationConfig,
...animation,
domain: null
}
};
}
return state;
}
/**
* Validate saved layer columns with new data,
* update fieldIdx based on new fields
*
* @param fields
* @param savedCols
* @param emptyCols
* @return - validated columns or null
*/
export function validateSavedLayerColumns(
fields: KeplerTable['fields'],
savedCols: {
[key: string]: string;
} = {},
emptyCols: LayerColumns
) {
// Prepare columns for the validator
const columns: typeof emptyCols = {};
for (const key of Object.keys(emptyCols)) {
columns[key] = {...emptyCols[key]};
const saved = savedCols[key];
if (saved) {
const fieldIdx = fields.findIndex(({name}) => name === saved);
if (fieldIdx > -1) {
// update found columns
columns[key].fieldIdx = fieldIdx;
columns[key].value = saved;
}
}
}
// find actual column fieldIdx, in case it has changed
const allColFound = Object.keys(columns).every(key =>
validateColumn(columns[key], columns, fields)
);
if (allColFound) {
return columns;
}
return null;
}
export function validateColumn(
column: LayerColumn & {validator?: typeof validateColumn},
columns: LayerColumns,
allFields: KeplerTable['fields']
): boolean {
if (column.optional || column.value) {
return true;
}
if (column.validator) {
return column.validator(column, columns, allFields);
}
return false;
}
/**
* Validate saved text label config with new data
* refer to vis-state-schema.js TextLabelSchemaV1
*
* @param {Array<Object>} fields
* @param {Object} savedTextLabel
* @return {Object} - validated textlabel
*/
export function validateSavedTextLabel(fields, [layerTextLabel], savedTextLabel) {
const savedTextLabels = Array.isArray(savedTextLabel) ? savedTextLabel : [savedTextLabel];
// validate field
return savedTextLabels.map(textLabel => {
const field = textLabel.field
? fields.find(fd =>
Object.keys(textLabel.field).every(key => textLabel.field[key] === fd[key])
)
: null;
return Object.keys(layerTextLabel).reduce(
(accu, key) => ({
...accu,
[key]: key === 'field' ? field : textLabel[key] || layerTextLabel[key]
}),
{}
);
});
}
/**
* Validate saved visual channels config with new data,
* refer to vis-state-schema.js VisualChannelSchemaV1
*/
export function validateSavedVisualChannels(
fields: KeplerTable['fields'],
newLayer: Layer,
savedLayer: ParsedLayer
): null | Layer {
Object.values(newLayer.visualChannels).forEach(({field, scale, key}) => {
let foundField;
if (savedLayer.config) {
if (savedLayer.config[field]) {
foundField = fields.find(
fd => savedLayer.config && fd.name === savedLayer.config[field].name
);
}
const foundChannel = {
...(foundField ? {[field]: foundField} : {}),
...(savedLayer.config[scale] ? {[scale]: savedLayer.config[scale]} : {})
};
if (Object.keys(foundChannel).length) {
newLayer.updateLayerConfig(foundChannel);
}
newLayer.validateVisualChannel(key);
}
});
return newLayer;
}
export function validateLayersByDatasets(
datasets: Datasets,
layerClasses: VisState['layerClasses'],
layers: NonNullable<ParsedConfig['visState']>['layers'] = []
) {
const validated: Layer[] = [];
const failed: NonNullable<ParsedConfig['visState']>['layers'] = [];
layers.forEach(layer => {
let validateLayer: Layer | null = null;
if (layer?.config?.dataId) {
if (datasets[layer.config.dataId]) {
// datasets are already loaded
validateLayer = validateLayerWithData(datasets[layer.config.dataId], layer, layerClasses);
}
}
if (validateLayer) {
validated.push(validateLayer);
} else {
// datasets not yet loaded
failed.push(layer);
}
});
return {validated, failed};
}
/**
* Validate saved layer config with new data,
* update fieldIdx based on new fields
*/
export function validateLayerWithData(
{fields, id: dataId}: KeplerTable,
savedLayer: ParsedLayer,
layerClasses: VisState['layerClasses'],
options: {
allowEmptyColumn?: boolean;
} = {}
): Layer | null {
const {type} = savedLayer;
// layer doesnt have a valid type
if (!type || !layerClasses.hasOwnProperty(type) || !savedLayer.config) {
return null;
}
let newLayer = new layerClasses[type]({
id: savedLayer.id,
dataId,
label: savedLayer.config.label,
color: savedLayer.config.color,
isVisible: savedLayer.config.isVisible,
hidden: savedLayer.config.hidden,
highlightColor: savedLayer.config.highlightColor
});
// find column fieldIdx
const columnConfig = newLayer.getLayerColumns();
if (Object.keys(columnConfig).length) {
const columns = validateSavedLayerColumns(fields, savedLayer.config.columns, columnConfig);
if (columns) {
newLayer.updateLayerConfig({columns});
} else if (!options.allowEmptyColumn) {
return null;
}
}
// visual channel field is saved to be {name, type}
// find visual channel field by matching both name and type
// refer to vis-state-schema.js VisualChannelSchemaV1
newLayer = validateSavedVisualChannels(fields, newLayer, savedLayer);
const textLabel =
savedLayer.config.textLabel && newLayer.config.textLabel
? validateSavedTextLabel(fields, newLayer.config.textLabel, savedLayer.config.textLabel)
: newLayer.config.textLabel;
// copy visConfig over to emptyLayer to make sure it has all the props
const visConfig = newLayer.copyLayerConfig(
newLayer.config.visConfig,
savedLayer.config.visConfig || {},
{shallowCopy: ['colorRange', 'strokeColorRange']}
);
newLayer.updateLayerConfig({
visConfig,
textLabel
});
return newLayer;
}
export function isValidMerger(merger: Merger): boolean {
return isObject(merger) && typeof merger.merge === 'function' && typeof merger.prop === 'string';
}
export const VIS_STATE_MERGERS: VisStateMergers = [
{merge: mergeLayers, prop: 'layers', toMergeProp: 'layerToBeMerged'},
{merge: mergeFilters, prop: 'filters', toMergeProp: 'filterToBeMerged'},
{merge: mergeInteractions, prop: 'interactionConfig', toMergeProp: 'interactionToBeMerged'},
{merge: mergeLayerBlending, prop: 'layerBlending'},
{merge: mergeSplitMaps, prop: 'splitMaps', toMergeProp: 'splitMapsToBeMerged'},
{merge: mergeAnimationConfig, prop: 'animationConfig'}
];