Skip to content

Commit

Permalink
fix: allow using controls as childs of bar (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-zaugg committed Sep 13, 2023
1 parent 130bf7d commit 21b6703
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/composables/useControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ 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 type Bar from "ol-ext/control/Bar";
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";
Expand Down Expand Up @@ -103,15 +103,25 @@ export default function useControl<T extends InnerControlType>(
control.value.set("order", attrs.order === undefined ? 0 : attrs.order);

watch(control, (newVal, oldVal) => {
if (parent && parent instanceof Map) {
parent.removeControl(oldVal);
parent.addControl(newVal);
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) {
if (parent && (parent instanceof Map || parent instanceof Bar)) {
parent.addControl(control.value);
}

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

parent.controls_ = [];

if (parent instanceof Map) {
if (parent && (parent instanceof Map || parent instanceof Bar)) {
sortedControls.forEach((c) => {
parent.addControl(c);
});
Expand Down Expand Up @@ -153,8 +163,8 @@ export default function useControl<T extends InnerControlType>(
parent?.controls_.splice(index, 1);
}
}
control.value.dispose();
}
control.value.dispose();
map?.changed();
});

Expand Down

0 comments on commit 21b6703

Please sign in to comment.