-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathuseControl.ts
183 lines (169 loc) · 5.21 KB
/
useControl.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import type { Ref } from "vue";
import { inject, onMounted, onUnmounted, watch, computed } from "vue";
import Map from "ol/Map";
import type {
Attribution,
FullScreen,
MousePosition,
OverviewMap,
ScaleLine,
} from "ol/control";
import type { Options as AttributionOptions } from "ol/control/Attribution";
import type Control from "ol/control/Control";
import type ContextMenu from "ol-contextmenu";
import type { Options as OlContextmenuOptions } from "ol-contextmenu/dist/types";
import Bar from "ol-ext/control/Bar";
import type { Options as BarOptions } from "ol-ext/control/Bar";
import type Button from "ol-ext/control/Button";
import type { Options as ButtonOptions } from "ol-ext/control/Button";
import type PrintDialog from "ol-ext/control/PrintDialog";
import type { Options as PrintDialogOptions } from "ol-ext/control/PrintDialog";
import type Profile from "ol-ext/control/Profile";
import type { Options as ProfileOptions } from "ol-ext/control/Profile";
import type Toggle from "ol-ext/control/Toggle";
import type { Options as ToggleOptions } from "ol-ext/control/Toggle";
import type VideoRecorder from "ol-ext/control/VideoRecorder";
import type { Options as VideoRecorderOptions } from "ol-ext/control/VideoRecorder";
import type LayerSwitcher from "ol-ext/control/LayerSwitcher";
import type { Options as LayerSwitcherOptions } from "ol-ext/control/LayerSwitcher";
import type LayerSwitcherImage from "ol-ext/control/LayerSwitcherImage";
import type Rotate from "ol/control/Rotate";
import type { Options as RotateOptions } from "ol/control/Rotate";
import type Search from "ol-ext/control/Search";
import type { Options as SearchOptions } from "ol-ext/control/Search";
import type Swipe from "ol-ext/control/Swipe";
import type { Options as SwipeOptions } from "ol-ext/control/Swipe";
import type Zone from "ol-ext/control/MapZone";
import type { Options as MapZoneOptions } from "ol-ext/control/MapZone";
import type Zoom from "ol/control/Zoom";
import type { Options as ZoomOptions } from "ol/control/Zoom";
import type ZoomSlider from "ol/control/ZoomSlider";
import type { Options as ZoomSliderOptions } from "ol/control/ZoomSlider";
import type ZoomToExtent from "ol/control/ZoomToExtent";
import type { Options as ZoomToExtentOptions } from "ol/control/ZoomToExtent";
type ExtentedMap = Map & {
controls_?: Control[];
};
type InnerControlType = (
| Attribution
| ContextMenu
| Bar
| Button
| PrintDialog
| Profile
| Toggle
| VideoRecorder
| FullScreen
| LayerSwitcher
| LayerSwitcherImage
| MousePosition
| OverviewMap
| Rotate
| Search
| ScaleLine
| Swipe
| Zone
| Zoom
| ZoomSlider
| ZoomToExtent
) & {
controls_?: Control[];
};
type InnerControlProperties =
| AttributionOptions
| Partial<OlContextmenuOptions>
| BarOptions
| ButtonOptions
| PrintDialogOptions
| ProfileOptions
| ToggleOptions
| VideoRecorderOptions
| LayerSwitcherOptions
| SearchOptions
| RotateOptions
| SwipeOptions
| MapZoneOptions
| ZoomOptions
| ZoomSliderOptions
| ZoomToExtentOptions;
export default function useControl<T extends InnerControlType>(
ControlType: new (options?: Record<string, unknown>) => T,
properties: InnerControlProperties,
attrs: Record<string, unknown>,
) {
const map = inject<ExtentedMap>("map");
const controlBar = inject<Ref<InnerControlType | null> | null>(
"controlBar",
null,
);
const parent = controlBar !== null ? controlBar?.value : map;
const control = computed(
() =>
new ControlType({
...properties,
}),
);
control.value.set("order", attrs.order === undefined ? 0 : attrs.order);
watch(control, (newVal, oldVal) => {
if (parent) {
if (parent instanceof Map) {
parent.removeControl(oldVal);
parent.addControl(newVal);
} else if (parent instanceof Bar) {
if (parent?.controls_) {
const index = parent?.controls_.findIndex((a) => a === control.value);
if (index) {
parent?.controls_.splice(index, 1);
}
}
parent.addControl(newVal);
}
map?.changed();
}
});
onMounted(() => {
if (parent && (parent instanceof Map || parent instanceof Bar)) {
parent.addControl(control.value);
}
if (parent?.controls_ !== undefined) {
const sortedControls = [...parent.controls_];
sortedControls.sort(
(a: Control, b: Control) => a.get("order") - b.get("order"),
);
parent.controls_ = [];
if (parent && (parent instanceof Map || parent instanceof Bar)) {
sortedControls.forEach((c) => {
parent.addControl(c);
});
}
parent.changed();
}
map?.changed();
});
onUnmounted(() => {
if (parent && parent instanceof Map) {
parent
?.getControls()
.getArray()
.forEach((c) => {
if (c instanceof ControlType) {
parent.removeControl(c);
}
});
} else {
// ol-ext controls
if (parent?.controls_) {
const index = parent?.controls_.findIndex((a) => a === control.value);
if (index) {
parent?.controls_.splice(index, 1);
}
}
}
control.value.dispose();
map?.changed();
});
return {
map,
control,
};
}