Skip to content
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

[SD-490] Added hover state to pin clusters on maps #1397

Merged
merged 3 commits into from
Dec 8, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 94 additions & 32 deletions packages/ripple-ui-maps/src/components/cluster/RplMapCluster.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,80 @@ import CircleStyle from 'ol/style/Circle'
import Fill from 'ol/style/Fill'
import Stroke from 'ol/style/Stroke'
import markerIconDefaultSrc from './../feature-pin/icon-pin.svg?url'
import type { Options } from 'ol/style/Circle'
import { Style, Text } from 'ol/style'
import { inject, nextTick, onMounted, Ref, ref } from 'vue'
import { Feature, Map } from 'ol'

interface Props {
pinStyle: Function
}

const props = withDefaults(defineProps<Props>(), {})

const { rplMapRef } = inject<{ rplMapRef: Ref<Map | null> }>('rplMapInstance', {
rplMapRef: ref(null)
})

const hoveredFeature = ref<Feature | null>(null)

const setClusterStyle = (
style: Style,
{
clusterSize,
isHovering = false
}: {
clusterSize: number
isHovering?: boolean
}
) => {
const strokeWidthDefault = 16
const strokeWidthHover = 20
let circleRadius = 20 // 0.5 scale pixel size

// Increase circle size for larger clusters
if (clusterSize > 99) {
circleRadius = 22
}
if (clusterSize > 999) {
circleRadius = 24
}

const circleIcon = new CircleStyle({
radius: circleRadius,
fill: new Fill({
color: [102, 102, 102, 1]
}),
stroke: new Stroke({
color: [102, 102, 102, 0.2],
// 2x pixel size
width: isHovering ? strokeWidthHover : strokeWidthDefault
})
})

const textStyle = new Text()
textStyle.setText(clusterSize.toString())
textStyle.setStroke(null)
textStyle.setFill(
new Fill({
color: [255, 255, 255, 1]
})
)
textStyle.setFont(`700 1.6rem VIC, Arial, Helvetica, sans-serif`)
// text is rendered slightly off center in chrome and firefox, this is a middle ground so it's decent in all browsers
textStyle.setOffsetY(0.25)

style.setImage(circleIcon)
style.setText(textStyle)
}

const overrideStyleFunction = (feature, style) => {
const clusteredFeatures = feature.get('features')
const size = clusteredFeatures.length

const createCircleStyle = (
innerProperties: Omit<Options, 'fill' | 'stroke'>
) => {
return new CircleStyle({
...innerProperties,
fill: new Fill({
color: [102, 102, 102, 1]
}),
stroke: new Stroke({
color: [102, 102, 102, 0.2],
// 2x pixel size
width: 16
})
})
}

if (clusteredFeatures && clusteredFeatures.length > 1) {
let circleRadius = 20 // 0.5 scale pixel size
// Increase circle size for larger clusters
if (size > 99) {
circleRadius = 22
}
if (size > 999) {
circleRadius = 24
}

style.setImage(createCircleStyle({ radius: circleRadius }))
style.getText().setText(size.toString())
style.getText().setStroke(undefined)
style.getText().setFont(`700 1.6rem VIC, Arial, Helvetica, sans-serif`)
// text is rendered slightly off center in chrome and firefox, this is a middle ground so it's decent in all browsers
style.getText().setOffsetY(0.25)
setClusterStyle(style, {
clusterSize: size
})
} else if (Array.isArray(clusteredFeatures) && clusteredFeatures[0]) {
const icon = props.pinStyle(
clusteredFeatures[0].getProperties(),
Expand All @@ -79,4 +109,36 @@ const overrideStyleFunction = (feature, style) => {
}
return style
}

onMounted(async () => {
await nextTick()

// Reset popup
if (rplMapRef.value) {
rplMapRef.value.on('pointermove', function (e) {
if (hoveredFeature.value !== null) {
hoveredFeature.value.setStyle(undefined)
hoveredFeature.value = null
}

rplMapRef.value?.forEachFeatureAtPixel(e.pixel, function (f) {
const clusteredFeatures = f.get('features')
const currentFeature = f as Feature

if (clusteredFeatures?.length > 1) {
hoveredFeature.value = currentFeature
const style = new Style()

setClusterStyle(style, {
clusterSize: clusteredFeatures.length,
isHovering: true
})

currentFeature.setStyle(style)
return true
}
})
})
}
})
</script>
Loading