Skip to content

Commit

Permalink
Introduce a global configuration property mapHover to customize hover…
Browse files Browse the repository at this point in the history
… tooltip behavior.
  • Loading branch information
fschmenger committed Feb 26, 2024
1 parent efacbde commit 0f8c80c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
23 changes: 16 additions & 7 deletions src/components/ol/HoverController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ import VectorSource from 'ol/source/Vector';
import VectorTileSource from 'ol/source/VectorTile';
import WMSGetFeatureInfo from 'ol/format/WMSGetFeatureInfo';
import { WguEventBus } from '../../WguEventBus';
import ObjectUtil from '../../util/Object';
import axios from 'axios';

export default class HoverController {
DEFAULT_POINTER_REST_INTERVAL = 150;
DEFAULT_HOVER_OVERLAY = 'wgu-hover-tooltip';
DEFAULT_OPTIONS = {
delay: 150,
hideOnMousemove: false,
hoverOverlay: 'wgu-hover-tooltip'
};

map = null;
conf = null;
timerHandle = null;
activeOverlayId = null;
pendingRequestsCancelSrc = null;
Expand All @@ -23,22 +28,26 @@ export default class HoverController {
* 'hoverAttribute' if the layer is configured as 'hoverable'
*
* @param {ol.Map} map OpenLayers map.
* @param {Number} pointerRestInterval Timespan in milliseconds, by which displaying the tooltip is deferred.
* @param {object} hoverConf Global configuration options.
*/
constructor (map, pointerRestInterval) {
constructor (map, hoverConf) {
const me = this;
me.map = map;
me.conf = me.DEFAULT_OPTIONS;
ObjectUtil.mergeDeep(me.conf, hoverConf);

// To limit the amount of asynchronous requests, implement a "pointer rest" behavior,
// which will potentially show a tooltip after the mouse has not moved for a given time period.
const timeout = pointerRestInterval ?? me.DEFAULT_POINTER_REST_INTERVAL;
map.on('pointermove', (event) => {
if (me.timerHandle) {
clearTimeout(me.timerHandle);
}
me.timerHandle = setTimeout(() => {
me.onPointerRest(event);
}, timeout);
}, me.conf.delay);
if (me.conf.hideOnMousemove) {
me.displayTooltip(null);
}
});

// If the mouse leaves the map canvas, clear out the "pointer rest" timer and hide
Expand Down Expand Up @@ -208,7 +217,7 @@ export default class HoverController {
const feature = featureInfo.feature;
const layer = featureInfo.layer;
const hoverAttr = layer.get('hoverAttribute');
const overlayId = layer.get('hoverOverlay') || me.DEFAULT_HOVER_OVERLAY;
const overlayId = layer.get('hoverOverlay') || me.conf.hoverOverlay;

if (me.activeOverlayId !== overlayId) {
WguEventBus.$emit(me.activeOverlayId + '-update-overlay', false);
Expand Down
2 changes: 1 addition & 1 deletion src/components/ol/Map.vue
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export default {
* @return {HoverController} HoverController instance.
*/
createHoverController () {
return new HoverController(this.map);
return new HoverController(this.map, this.$appConfig.mapHover);
},
/**
Expand Down

0 comments on commit 0f8c80c

Please sign in to comment.