Skip to content

Commit

Permalink
fix: apply URL changes on XYZ source
Browse files Browse the repository at this point in the history
closes #383
  • Loading branch information
d-koppenhagen committed Oct 2, 2024
1 parent 2b732cc commit aba4eb6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
21 changes: 19 additions & 2 deletions src/components/sources/OlSourceXYZ.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
</template>
<script setup lang="ts">
import XYZ, { type Options } from "ol/source/XYZ";
import type { Ref } from "vue";
import { type Ref, watch } from "vue";
import { inject } from "vue";
import type TileLayer from "ol/layer/Tile";
import type { ImageTile } from "ol";
import { TILE_SOURCE_EVENTS } from "@/composables/useOpenLayersEvents";
import useSource from "@/composables/useSource";
import type { Source } from "ol/source";
import type { UrlFunction } from "ol/Tile";
// prevent warnings caused by event pass-through via useOpenLayersEvents composable
defineOptions({
Expand All @@ -35,7 +37,22 @@ const props = withDefaults(defineProps<Options>(), {
const layer = inject<Ref<TileLayer<XYZ>> | null>("tileLayer");
const { source } = useSource(XYZ, layer, props, TILE_SOURCE_EVENTS);
const { source, updateSource } = useSource(
XYZ,
layer,
props,
TILE_SOURCE_EVENTS,
(source) => {
if (props.url) {
source.setUrl(props.url);
}
},
);
watch(
() => props.url,
() => updateSource(),
);
defineExpose({
layer,
Expand Down
21 changes: 19 additions & 2 deletions src/components/sources/OlSourceXyz.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
</template>
<script setup lang="ts">
import XYZ, { type Options } from "ol/source/XYZ";
import type { Ref } from "vue";
import { type Ref, watch } from "vue";
import { inject } from "vue";
import type TileLayer from "ol/layer/Tile";
import type { ImageTile } from "ol";
import { TILE_SOURCE_EVENTS } from "@/composables/useOpenLayersEvents";
import useSource from "@/composables/useSource";
import type { Source } from "ol/source";
import type { UrlFunction } from "ol/Tile";
// prevent warnings caused by event pass-through via useOpenLayersEvents composable
defineOptions({
Expand All @@ -35,7 +37,22 @@ const props = withDefaults(defineProps<Options>(), {
const layer = inject<Ref<TileLayer<XYZ>> | null>("tileLayer");
const { source } = useSource(XYZ, layer, props, TILE_SOURCE_EVENTS);
const { source, updateSource } = useSource(
XYZ,
layer,
props,
TILE_SOURCE_EVENTS,
(source) => {
if (props.url) {
source.setUrl(props.url);
}
},
);
watch(
() => props.url,
() => updateSource(),
);
defineExpose({
layer,
Expand Down
5 changes: 5 additions & 0 deletions src/composables/useSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export default function useSource<T extends Source>(
layer: Ref<Layer> | Ref<Cluster> | null | undefined,
props: ConstructorParameters<typeof SourceClass>[0],
eventsToHandle: string[] = [],
// eslint-disable-next-line
sourceUpdateActions?: (source: T) => void,
) {
function getProperties() {
return usePropsAsObjectProperties({
Expand Down Expand Up @@ -46,6 +48,9 @@ export default function useSource<T extends Source>(
source.value.set(key, properties[keyInObj]);
}
}
if (sourceUpdateActions) {
sourceUpdateActions(source.value);
}
layer?.value?.setSource(source.value);
useOpenLayersEvents(source, eventsToHandle);
return source;
Expand Down

0 comments on commit aba4eb6

Please sign in to comment.