Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow using controls as childs of bar #248

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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