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

fix(viewer.js): Trigger ROI selection event even when no select interaction is active #117

Merged
merged 13 commits into from
May 9, 2024
Merged
2 changes: 2 additions & 0 deletions src/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const EVENTS = {
ROI_DRAWN: `${PROJECT_NAME}_roi_drawn`,
/** Triggered when a ROI was selected. */
ROI_SELECTED: `${PROJECT_NAME}_roi_selected`,
/** Triggered when a ROI was double clicked. */
ROI_DOUBLE_CLICKED: `${PROJECT_NAME}_roi_double_clicked`,
/** Triggered when a ROI was modified. */
ROI_MODIFIED: `${PROJECT_NAME}_roi_modified`,
/** Triggered when a viewport move has started. */
Expand Down
131 changes: 97 additions & 34 deletions src/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,68 @@ class VolumeImageViewer {
})
})

let clickEvent = null
this[_map].on('dblclick', (event) => {
clickEvent = 'dblclick'
this[_map].forEachFeatureAtPixel(
event.pixel,
(feature) => {
const correctFeature = feature.values_?.features?.[0] || feature
console.debug('dblclick feature id:', correctFeature)
if (correctFeature?.getId()) {
publish(
this[_map].getTargetElement(),
EVENT.ROI_SELECTED,
this._getROIFromFeature(
correctFeature,
this[_pyramid].metadata,
this[_affine]
)
)
publish(
this[_map].getTargetElement(),
EVENT.ROI_DOUBLE_CLICKED,
this._getROIFromFeature(
correctFeature,
this[_pyramid].metadata,
this[_affine]
)
)
}
clickEvent = null
},
{ hitTolerance: 1 }
)
})
this[_map].on('click', (event) => {
if (clickEvent === 'dblclick') {
event.preventDefault()
event.stopPropagation()
return
}
clickEvent = 'click'
this[_map].forEachFeatureAtPixel(
event.pixel,
(feature) => {
const correctFeature = feature.values_?.features?.[0] || feature
console.debug('click feature id:', correctFeature)
if (correctFeature?.getId()) {
publish(
this[_map].getTargetElement(),
EVENT.ROI_SELECTED,
this._getROIFromFeature(
correctFeature,
this[_pyramid].metadata,
this[_affine]
)
)
}
clickEvent = null
},
{ hitTolerance: 1 }
)
})

view.fit(this[_projection].getExtent(), { size: this[_map].getSize() })

/**
Expand Down Expand Up @@ -2500,15 +2562,18 @@ class VolumeImageViewer {
const container = this[_map].getTargetElement()

this[_interactions].select.on('select', (e) => {
publish(
container,
EVENT.ROI_SELECTED,
this._getROIFromFeature(
e.selected[0],
this[_pyramid].metadata,
this[_affine]
console.debug('select roi')
if (e.selected[0]?.getId()) {
publish(
container,
EVENT.ROI_SELECTED,
this._getROIFromFeature(
e.selected[0],
this[_pyramid].metadata,
this[_affine]
)
)
)
}
})

this[_map].addInteraction(this[_interactions].select)
Expand Down Expand Up @@ -3362,33 +3427,31 @@ class VolumeImageViewer {

let selectedAnnotation = null
this[_map].on('singleclick', (e) => {
if (e != null) {
if (selectedAnnotation != null) {
selectedAnnotation.set('selected', 0)
selectedAnnotation = null
}
const container = this[_map].getTargetElement()
this[_map].forEachFeatureAtPixel(
e.pixel,
(feature) => {
if (feature != null) {
feature.set('selected', 1)
selectedAnnotation = feature
publish(
container,
EVENT.ROI_SELECTED,
_getROIFromFeature(feature)
)
return true
}
return false
},
{
hitTolerance: 1,
layerFilter: (layer) => (layer instanceof PointsLayer)
}
)
if (selectedAnnotation !== null) {
selectedAnnotation.set('selected', 0)
selectedAnnotation = null
}
const container = this[_map].getTargetElement()
this[_map].forEachFeatureAtPixel(
e.pixel,
(feature) => {
if (feature != null) {
feature.set('selected', 1)
selectedAnnotation = feature
publish(
container,
EVENT.ROI_SELECTED,
_getROIFromFeature(feature)
)
return true
}
return false
},
{
hitTolerance: 1,
layerFilter: (layer) => (layer instanceof PointsLayer)
}
)
})
}

Expand Down
Loading