Skip to content

Commit 8d59c76

Browse files
lukas-zauggd-koppenhagen
authored andcommitted
fix: allow using controls as childs of bar (#248)
1 parent 9a3b133 commit 8d59c76

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

Diff for: src/composables/useControl.ts

+17-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type { Options as AttributionOptions } from "ol/control/Attribution";
1212
import type Control from "ol/control/Control";
1313
import type ContextMenu from "ol-contextmenu";
1414
import type { Options as OlContextmenuOptions } from "ol-contextmenu/dist/types";
15-
import type Bar from "ol-ext/control/Bar";
15+
import Bar from "ol-ext/control/Bar";
1616
import type { Options as BarOptions } from "ol-ext/control/Bar";
1717
import type Button from "ol-ext/control/Button";
1818
import type { Options as ButtonOptions } from "ol-ext/control/Button";
@@ -103,15 +103,25 @@ export default function useControl<T extends InnerControlType>(
103103
control.value.set("order", attrs.order === undefined ? 0 : attrs.order);
104104

105105
watch(control, (newVal, oldVal) => {
106-
if (parent && parent instanceof Map) {
107-
parent.removeControl(oldVal);
108-
parent.addControl(newVal);
106+
if (parent) {
107+
if (parent instanceof Map) {
108+
parent.removeControl(oldVal);
109+
parent.addControl(newVal);
110+
} else if (parent instanceof Bar) {
111+
if (parent?.controls_) {
112+
const index = parent?.controls_.findIndex((a) => a === control.value);
113+
if (index) {
114+
parent?.controls_.splice(index, 1);
115+
}
116+
}
117+
parent.addControl(newVal);
118+
}
109119
map?.changed();
110120
}
111121
});
112122

113123
onMounted(() => {
114-
if (parent && parent instanceof Map) {
124+
if (parent && (parent instanceof Map || parent instanceof Bar)) {
115125
parent.addControl(control.value);
116126
}
117127

@@ -123,7 +133,7 @@ export default function useControl<T extends InnerControlType>(
123133

124134
parent.controls_ = [];
125135

126-
if (parent instanceof Map) {
136+
if (parent && (parent instanceof Map || parent instanceof Bar)) {
127137
sortedControls.forEach((c) => {
128138
parent.addControl(c);
129139
});
@@ -153,8 +163,8 @@ export default function useControl<T extends InnerControlType>(
153163
parent?.controls_.splice(index, 1);
154164
}
155165
}
156-
control.value.dispose();
157166
}
167+
control.value.dispose();
158168
map?.changed();
159169
});
160170

0 commit comments

Comments
 (0)