-
Notifications
You must be signed in to change notification settings - Fork 121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(partition): add element click, over, out events #578
Changes from all commits
0ba75c1
dff6992
98c0a97
afd5399
fe24174
981c66a
046618a
14a4da4
6a5d8ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. */ | ||
|
||
import createCachedSelector from 're-reselect'; | ||
import { Selector } from 'reselect'; | ||
import { GlobalChartState, PointerState } from '../../../../state/chart_state'; | ||
import { getSettingsSpecSelector } from '../../../../state/selectors/get_settings_specs'; | ||
import { SettingsSpec, LayerValue } from '../../../../specs'; | ||
import { getPickedShapesLayerValues } from './picked_shapes'; | ||
import { getPieSpecOrNull } from './pie_spec'; | ||
import { ChartTypes } from '../../..'; | ||
import { SeriesIdentifier } from '../../../xy_chart/utils/series'; | ||
import { isClicking } from '../../../../state/utils'; | ||
import { getLastClickSelector } from '../../../../state/selectors/get_last_click'; | ||
|
||
/** | ||
* Will call the onElementClick listener every time the following preconditions are met: | ||
* - the onElementClick listener is available | ||
* - we have at least one highlighted geometry | ||
* - the pointer state goes from down state to up state | ||
*/ | ||
export function createOnElementClickCaller(): (state: GlobalChartState) => void { | ||
let prevClick: PointerState | null = null; | ||
let selector: Selector<GlobalChartState, void> | null = null; | ||
return (state: GlobalChartState) => { | ||
if (selector === null && state.chartType === ChartTypes.Partition) { | ||
selector = createCachedSelector( | ||
[getPieSpecOrNull, getLastClickSelector, getSettingsSpecSelector, getPickedShapesLayerValues], | ||
(pieSpec, lastClick: PointerState | null, settings: SettingsSpec, pickedShapes): void => { | ||
if (!pieSpec) { | ||
return; | ||
} | ||
if (!settings.onElementClick) { | ||
return; | ||
} | ||
const nextPickedShapesLength = pickedShapes.length; | ||
if (nextPickedShapesLength > 0 && isClicking(prevClick, lastClick)) { | ||
if (settings && settings.onElementClick) { | ||
const elements = pickedShapes.map<[Array<LayerValue>, SeriesIdentifier]>((values) => { | ||
return [ | ||
values, | ||
{ | ||
specId: pieSpec.id, | ||
key: `spec{${pieSpec.id}}`, | ||
}, | ||
]; | ||
}); | ||
settings.onElementClick(elements); | ||
} | ||
} | ||
prevClick = lastClick; | ||
}, | ||
)({ | ||
keySelector: (state: GlobalChartState) => state.chartId, | ||
}); | ||
} | ||
if (selector) { | ||
selector(state); | ||
} | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. */ | ||
|
||
import { getSettingsSpecSelector } from '../../../../state/selectors/get_settings_specs'; | ||
import createCachedSelector from 're-reselect'; | ||
import { getPickedShapesLayerValues } from './picked_shapes'; | ||
import { getPieSpecOrNull } from './pie_spec'; | ||
import { GlobalChartState } from '../../../../state/chart_state'; | ||
import { Selector } from 'react-redux'; | ||
import { ChartTypes } from '../../../index'; | ||
import { getChartIdSelector } from '../../../../state/selectors/get_chart_id'; | ||
|
||
/** | ||
* Will call the onElementOut listener every time the following preconditions are met: | ||
* - the onElementOut listener is available | ||
* - the highlighted geometries list goes from a list of at least one object to an empty one | ||
*/ | ||
export function createOnElementOutCaller(): (state: GlobalChartState) => void { | ||
let prevPickedShapes: number | null = null; | ||
let selector: Selector<GlobalChartState, void> | null = null; | ||
return (state: GlobalChartState) => { | ||
if (selector === null && state.chartType === ChartTypes.Partition) { | ||
selector = createCachedSelector( | ||
[getPieSpecOrNull, getPickedShapesLayerValues, getSettingsSpecSelector], | ||
(pieSpec, pickedShapes, settings): void => { | ||
if (!pieSpec) { | ||
return; | ||
} | ||
if (!settings.onElementOut) { | ||
return; | ||
} | ||
const nextPickedShapes = pickedShapes.length; | ||
|
||
if (prevPickedShapes !== null && prevPickedShapes > 0 && nextPickedShapes === 0) { | ||
settings.onElementOut(); | ||
} | ||
prevPickedShapes = nextPickedShapes; | ||
}, | ||
)({ | ||
keySelector: getChartIdSelector, | ||
}); | ||
} | ||
if (selector) { | ||
selector(state); | ||
} | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. */ | ||
|
||
import { getSettingsSpecSelector } from '../../../../state/selectors/get_settings_specs'; | ||
import createCachedSelector from 're-reselect'; | ||
import { LayerValue } from '../../../../specs'; | ||
import { GlobalChartState } from '../../../../state/chart_state'; | ||
import { Selector } from 'react-redux'; | ||
import { ChartTypes } from '../../../index'; | ||
import { getChartIdSelector } from '../../../../state/selectors/get_chart_id'; | ||
import { getPieSpecOrNull } from './pie_spec'; | ||
import { getPickedShapesLayerValues } from './picked_shapes'; | ||
import { SeriesIdentifier } from '../../../xy_chart/utils/series'; | ||
|
||
function isOverElement(prevPickedShapes: Array<Array<LayerValue>> = [], nextPickedShapes: Array<Array<LayerValue>>) { | ||
if (nextPickedShapes.length === 0) { | ||
return; | ||
} | ||
if (nextPickedShapes.length !== prevPickedShapes.length) { | ||
return true; | ||
} | ||
return !nextPickedShapes.every((nextPickedShapeValues, index) => { | ||
const prevPickedShapeValues = prevPickedShapes[index]; | ||
if (prevPickedShapeValues === null) { | ||
return false; | ||
} | ||
if (prevPickedShapeValues.length !== nextPickedShapeValues.length) { | ||
return false; | ||
} | ||
return nextPickedShapeValues.every((layerValue, i) => { | ||
const prevPickedValue = prevPickedShapeValues[i]; | ||
if (!prevPickedValue) { | ||
return false; | ||
} | ||
return layerValue.value === prevPickedValue.value && layerValue.groupByRollup === prevPickedValue.groupByRollup; | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Will call the onElementOver listener every time the following preconditions are met: | ||
* - the onElementOver listener is available | ||
* - we have a new set of highlighted geometries on our state | ||
*/ | ||
export function createOnElementOverCaller(): (state: GlobalChartState) => void { | ||
let prevPickedShapes: Array<Array<LayerValue>> = []; | ||
let selector: Selector<GlobalChartState, void> | null = null; | ||
return (state: GlobalChartState) => { | ||
if (selector === null && state.chartType === ChartTypes.Partition) { | ||
selector = createCachedSelector( | ||
[getPieSpecOrNull, getPickedShapesLayerValues, getSettingsSpecSelector], | ||
(pieSpec, nextPickedShapes, settings): void => { | ||
if (!pieSpec) { | ||
return; | ||
} | ||
if (!settings.onElementOver) { | ||
return; | ||
} | ||
|
||
if (isOverElement(prevPickedShapes, nextPickedShapes)) { | ||
const elements = nextPickedShapes.map<[Array<LayerValue>, SeriesIdentifier]>((values) => [ | ||
values, | ||
{ | ||
specId: pieSpec.id, | ||
key: `spec{${pieSpec.id}}`, | ||
}, | ||
]); | ||
settings.onElementOver(elements); | ||
} | ||
prevPickedShapes = nextPickedShapes; | ||
}, | ||
)({ | ||
keySelector: getChartIdSelector, | ||
}); | ||
} | ||
if (selector) { | ||
selector(state); | ||
} | ||
}; | ||
Comment on lines
+64
to
+94
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like there is some common logic between these event listers might be good to provide a factory function at some point. Looks fine for now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are perfectly fine, I will I've already cleaned up a bit this code, I will clean up more in a future PR |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😃