-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5374ea2
commit 6a294f2
Showing
5 changed files
with
89 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<template> | ||
<div ref="infoWindowRef" v-if="hasSlotContent" v-show="mapTilesLoaded"> | ||
<slot /> | ||
</div> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { defineComponent, PropType, watch, ref, computed, inject, onBeforeUnmount, Comment } from "vue"; | ||
import { apiSymbol, mapSymbol, mapTilesLoadedSymbol } from "../shared/index"; | ||
const infoWindowEvents = ["closeclick", "content_changed", "domready", "position_changed", "visible", "zindex_changed"]; | ||
export default defineComponent({ | ||
props: { | ||
options: { | ||
type: Object as PropType<google.maps.InfoWindowOptions>, | ||
required: true, | ||
}, | ||
}, | ||
emits: infoWindowEvents, | ||
setup(props, { slots, emit }) { | ||
let _infoWindow: google.maps.InfoWindow; | ||
const infoWindow = ref<google.maps.InfoWindow>(); | ||
const infoWindowRef = ref<HTMLElement>(); | ||
const map = inject(mapSymbol, ref(null)); | ||
const api = inject(apiSymbol, ref(null)); | ||
const mapTilesLoaded = inject(mapTilesLoadedSymbol, ref(false)); | ||
const hasSlotContent = computed(() => slots.default?.().some((vnode) => vnode.type !== Comment)); | ||
watch( | ||
[map, () => props.options, mapTilesLoaded], | ||
([_, options, newMapTilesLoaded], [oldMap, oldOptions, oldMapTilesLoaded]) => { | ||
const checkIfChanged = JSON.stringify(options) !== JSON.stringify(oldOptions) || map.value !== oldMap; | ||
if (map.value && api.value && (checkIfChanged || (newMapTilesLoaded && !oldMapTilesLoaded))) { | ||
if (_infoWindow) { | ||
_infoWindow.setOptions({ | ||
...options, | ||
content: hasSlotContent.value ? infoWindowRef.value : options.content, | ||
}); | ||
if (!hasSlotContent.value || mapTilesLoaded.value) _infoWindow.open({ map: map.value }); | ||
} else { | ||
infoWindow.value = _infoWindow = new api.value.InfoWindow({ | ||
...options, | ||
content: hasSlotContent.value ? infoWindowRef.value : options.content, | ||
}); | ||
if (!hasSlotContent.value || mapTilesLoaded.value) _infoWindow.open({ map: map.value }); | ||
infoWindowEvents.forEach((event) => { | ||
_infoWindow?.addListener(event, (e: unknown) => emit(event, e)); | ||
}); | ||
} | ||
} | ||
}, | ||
{ | ||
immediate: true, | ||
} | ||
); | ||
onBeforeUnmount(() => { | ||
if (_infoWindow) { | ||
api.value?.event.clearInstanceListeners(_infoWindow); | ||
_infoWindow.close(); | ||
} | ||
}); | ||
return { infoWindow, infoWindowRef, hasSlotContent, mapTilesLoaded }; | ||
}, | ||
}); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters