Skip to content

Commit aba4eb6

Browse files
committed
fix: apply URL changes on XYZ source
closes #383
1 parent 2b732cc commit aba4eb6

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

src/components/sources/OlSourceXYZ.vue

+19-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
</template>
44
<script setup lang="ts">
55
import XYZ, { type Options } from "ol/source/XYZ";
6-
import type { Ref } from "vue";
6+
import { type Ref, watch } from "vue";
77
import { inject } from "vue";
88
import type TileLayer from "ol/layer/Tile";
99
import type { ImageTile } from "ol";
1010
import { TILE_SOURCE_EVENTS } from "@/composables/useOpenLayersEvents";
1111
import useSource from "@/composables/useSource";
12+
import type { Source } from "ol/source";
13+
import type { UrlFunction } from "ol/Tile";
1214
1315
// prevent warnings caused by event pass-through via useOpenLayersEvents composable
1416
defineOptions({
@@ -35,7 +37,22 @@ const props = withDefaults(defineProps<Options>(), {
3537
3638
const layer = inject<Ref<TileLayer<XYZ>> | null>("tileLayer");
3739
38-
const { source } = useSource(XYZ, layer, props, TILE_SOURCE_EVENTS);
40+
const { source, updateSource } = useSource(
41+
XYZ,
42+
layer,
43+
props,
44+
TILE_SOURCE_EVENTS,
45+
(source) => {
46+
if (props.url) {
47+
source.setUrl(props.url);
48+
}
49+
},
50+
);
51+
52+
watch(
53+
() => props.url,
54+
() => updateSource(),
55+
);
3956
4057
defineExpose({
4158
layer,

src/components/sources/OlSourceXyz.vue

+19-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
</template>
44
<script setup lang="ts">
55
import XYZ, { type Options } from "ol/source/XYZ";
6-
import type { Ref } from "vue";
6+
import { type Ref, watch } from "vue";
77
import { inject } from "vue";
88
import type TileLayer from "ol/layer/Tile";
99
import type { ImageTile } from "ol";
1010
import { TILE_SOURCE_EVENTS } from "@/composables/useOpenLayersEvents";
1111
import useSource from "@/composables/useSource";
12+
import type { Source } from "ol/source";
13+
import type { UrlFunction } from "ol/Tile";
1214
1315
// prevent warnings caused by event pass-through via useOpenLayersEvents composable
1416
defineOptions({
@@ -35,7 +37,22 @@ const props = withDefaults(defineProps<Options>(), {
3537
3638
const layer = inject<Ref<TileLayer<XYZ>> | null>("tileLayer");
3739
38-
const { source } = useSource(XYZ, layer, props, TILE_SOURCE_EVENTS);
40+
const { source, updateSource } = useSource(
41+
XYZ,
42+
layer,
43+
props,
44+
TILE_SOURCE_EVENTS,
45+
(source) => {
46+
if (props.url) {
47+
source.setUrl(props.url);
48+
}
49+
},
50+
);
51+
52+
watch(
53+
() => props.url,
54+
() => updateSource(),
55+
);
3956
4057
defineExpose({
4158
layer,

src/composables/useSource.ts

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export default function useSource<T extends Source>(
1919
layer: Ref<Layer> | Ref<Cluster> | null | undefined,
2020
props: ConstructorParameters<typeof SourceClass>[0],
2121
eventsToHandle: string[] = [],
22+
// eslint-disable-next-line
23+
sourceUpdateActions?: (source: T) => void,
2224
) {
2325
function getProperties() {
2426
return usePropsAsObjectProperties({
@@ -46,6 +48,9 @@ export default function useSource<T extends Source>(
4648
source.value.set(key, properties[keyInObj]);
4749
}
4850
}
51+
if (sourceUpdateActions) {
52+
sourceUpdateActions(source.value);
53+
}
4954
layer?.value?.setSource(source.value);
5055
useOpenLayersEvents(source, eventsToHandle);
5156
return source;

0 commit comments

Comments
 (0)