-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathuseOpenLayersEvents.ts
93 lines (84 loc) · 1.99 KB
/
useOpenLayersEvents.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import {
type ComputedRef,
getCurrentInstance,
inject,
isRef,
onMounted,
type Ref,
type ShallowRef,
} from "vue";
import type BaseObject from "ol/Object";
import type { EventTypes } from "ol/Observable";
export const COMMON_EVENTS = ["change", "error", "propertychange"];
export const LAYER_EVENTS = [
"change:extent",
"change:maxResolution",
"change:maxZoom",
"change:minResolution",
"change:minZoom",
"change:opacity",
"change:source",
"change:visible",
"change:zIndex",
"postrender",
"prerender",
"sourceready",
];
export const TILE_SOURCE_EVENTS = [
"tileloadend",
"tileloaderror",
"tileloadstart",
];
export const IMAGE_SOURCE_EVENTS = [
"imageloadend",
"imageloaderror",
"imageloadstart",
];
export const FEATURE_EVENTS = [
"addfeature",
"changefeature",
"clear",
"featuresloadend",
"featuresloaderror",
"featuresloadstart",
"removefeature",
];
// Define the composable function
export function useOpenLayersEvents(
feature:
| BaseObject
| Ref<BaseObject>
| ShallowRef<BaseObject>
| ComputedRef<BaseObject>,
eventNames: string[],
) {
const instance = getCurrentInstance();
const globalOptions = inject("ol-options");
function updateOpenLayersEventHandlers() {
([...COMMON_EVENTS, ...eventNames] as EventTypes[]).forEach((eventName) => {
let unwrappedFeature: Pick<BaseObject, "on">;
if (!isRef(feature)) {
unwrappedFeature = feature;
} else {
unwrappedFeature =
typeof feature.value === "function" ? feature.value() : feature.value;
}
unwrappedFeature.on(eventName, (...args: unknown[]) => {
if (globalOptions?.debug) {
console.debug("[Vue3-OpenLayers Debug] EVENT", eventName, {
eventName,
args,
source: feature,
});
}
instance?.emit(eventName, ...args);
});
});
}
onMounted(() => {
updateOpenLayersEventHandlers();
});
return {
updateOpenLayersEventHandlers,
};
}