Skip to content

Commit

Permalink
[Enhancement]Allow changing MAX_DEFAULT_TOOLTIPS (#1627)
Browse files Browse the repository at this point in the history
- add maxDefaultTooltips to visState
- pass maxDefaultTooltips to findFieldsToShow

Signed-off-by: Riku Oja <riku.oja@kapsi.fi>
  • Loading branch information
Rikuoja committed Sep 24, 2021
1 parent a810ee1 commit bfcce3f
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/reducers/vis-state-updaters.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ export type VisState = {
hoverInfo: any;
clicked: any;
mousePos: any;
maxDefaultTooltips: number;
layerClasses: LayerClassesType;
animationConfig: AnimationConfig;
editor: Editor;
Expand Down
13 changes: 11 additions & 2 deletions src/reducers/vis-state-updaters.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ import {

import {Layer, LayerClasses, LAYER_ID_LENGTH} from 'layers';
import {DEFAULT_TEXT_LABEL} from 'layers/layer-factory';
import {EDITOR_MODES, SORT_ORDER, FILTER_TYPES} from 'constants/default-settings';
import {
EDITOR_MODES,
SORT_ORDER,
FILTER_TYPES,
MAX_DEFAULT_TOOLTIPS
} from 'constants/default-settings';
import {pick_, merge_, swap_} from './composer-helpers';
import {processFileContent} from 'actions/vis-state-actions';

Expand Down Expand Up @@ -191,6 +196,7 @@ export const INITIAL_VIS_STATE = {
hoverInfo: undefined,
clicked: undefined,
mousePos: {},
maxDefaultTooltips: MAX_DEFAULT_TOOLTIPS,

// this is used when user split maps
splitMaps: [
Expand Down Expand Up @@ -1741,7 +1747,10 @@ export function addDefaultLayers(state, datasets) {
* @returns {Object} nextState
*/
export function addDefaultTooltips(state, dataset) {
const tooltipFields = findFieldsToShow(dataset);
const tooltipFields = findFieldsToShow({
...dataset,
maxDefaultTooltips: state.maxDefaultTooltips
});
const merged = {
...state.interactionConfig.tooltip.config.fieldsToShow,
...tooltipFields
Expand Down
1 change: 1 addition & 0 deletions src/utils/interaction-utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export function getDefaultInteraction(): InteractionConfig;
export function findFieldsToShow(p: {
fields: Field[];
id: string;
maxDefaultTooltips: number;
}): {
[key: string]: string[];
};
Expand Down
9 changes: 4 additions & 5 deletions src/utils/interaction-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import {
DEFAULT_TOOLTIP_FIELDS,
MAX_DEFAULT_TOOLTIPS,
ALL_FIELD_TYPES,
TRIP_POINT_FIELDS
} from 'constants/default-settings';
Expand Down Expand Up @@ -87,7 +86,7 @@ export const BRUSH_CONFIG = {
/**
* @type {typeof import('./interaction-utils').findFieldsToShow}
*/
export function findFieldsToShow({fields, id}) {
export function findFieldsToShow({fields, id, maxDefaultTooltips}) {
// first find default tooltip fields for trips
const fieldsToShow = DEFAULT_TOOLTIP_FIELDS.reduce((prev, curr) => {
if (fields.find(({name}) => curr.name === name)) {
Expand All @@ -97,11 +96,11 @@ export function findFieldsToShow({fields, id}) {
}, []);

return {
[id]: fieldsToShow.length ? fieldsToShow : autoFindTooltipFields(fields)
[id]: fieldsToShow.length ? fieldsToShow : autoFindTooltipFields(fields, maxDefaultTooltips)
};
}

function autoFindTooltipFields(fields) {
function autoFindTooltipFields(fields, maxDefaultTooltips) {
const ptFields = _mergeFieldPairs(TRIP_POINT_FIELDS);
// filter out the default fields that contains lat and lng and any geometry
const fieldsToShow = fields.filter(
Expand All @@ -115,7 +114,7 @@ function autoFindTooltipFields(fields) {
type !== 'object'
);

return fieldsToShow.slice(0, MAX_DEFAULT_TOOLTIPS).map(({name}) => {
return fieldsToShow.slice(0, maxDefaultTooltips).map(({name}) => {
return {
name,
format: null
Expand Down
2 changes: 2 additions & 0 deletions test/node/reducers/vis-state-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2828,6 +2828,7 @@ test('#visStateReducer -> REMOVE_DATASET w filter and layer', t => {
hoverInfo: oldState.hoverInfo,
clicked: oldState.clicked,
mousePos: oldState.mousePos,
maxDefaultTooltips: oldState.maxDefaultTooltips,
splitMaps: oldState.splitMaps,
layerClasses: oldState.layerClasses,
animationConfig: oldState.animationConfig,
Expand Down Expand Up @@ -3077,6 +3078,7 @@ test('#visStateReducer -> SPLIT_MAP: REMOVE_DATASET', t => {
hoverInfo: oldState.hoverInfo,
clicked: oldState.clicked,
mousePos: oldState.mousePos,
maxDefaultTooltips: oldState.maxDefaultTooltips,
layerClasses: oldState.layerClasses,
animationConfig: DEFAULT_ANIMATION_CONFIG,
initialState: oldState.initialState,
Expand Down
12 changes: 2 additions & 10 deletions test/node/utils/interaction-utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,22 +118,14 @@ test('interactionUtil -> autoFindTooltipFields', t => {
{
name: 'b',
format: null
},
{
name: 'c',
format: null
},
{
name: 'd',
format: null
}
]
};

t.deepEqual(
findFieldsToShow({fields, id: 'test'}),
findFieldsToShow({fields, id: 'test', maxDefaultTooltips: 3}),
expectedFields,
'should filter out all default geometry fields and return first 5'
'should filter out all default geometry fields and return first 3'
);

t.end();
Expand Down

0 comments on commit bfcce3f

Please sign in to comment.