-
Notifications
You must be signed in to change notification settings - Fork 19
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
Add popup support #70
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,49 @@ | ||
import { defineComponent, inject, onBeforeUnmount, PropType, unref, watch } from 'vue'; | ||
import { defineComponent, inject, provide, onBeforeUnmount, PropType, unref, watch, shallowRef, h } from 'vue'; | ||
import { LngLatLike, Marker, MarkerOptions, PointLike, PositionAnchor } from 'maplibre-gl'; | ||
import { MapLib } from '@/lib/lib/map.lib'; | ||
import { mapSymbol } from '@/lib/types'; | ||
import { mapSymbol, markerSymbol } from '@/lib/types'; | ||
|
||
export default /*#__PURE__*/ defineComponent({ | ||
name : 'MglMarker', | ||
props: { | ||
coordinates: { | ||
type : [ Object, Array ] as unknown as PropType<LngLatLike>, | ||
required: true | ||
}, | ||
offset : [ Object, Array ] as PropType<PointLike>, | ||
anchor : String as PropType<PositionAnchor>, | ||
color : String as PropType<string>, | ||
// draggable : Boolean as PropType<boolean>, todo implement feature | ||
clickTolerance : Number as PropType<number>, | ||
rotation : Number as PropType<number>, | ||
rotationAlignment: String as PropType<'map' | 'viewport' | 'auto'>, | ||
pitchAlignment : String as PropType<'map' | 'viewport' | 'auto'>, | ||
scale : Number as PropType<number> | ||
}, | ||
setup(props) { | ||
name : 'MglMarker', | ||
props: { | ||
coordinates: { | ||
type : [ Object, Array ] as unknown as PropType<LngLatLike>, | ||
required: true | ||
}, | ||
offset : [ Object, Array ] as PropType<PointLike>, | ||
anchor : String as PropType<PositionAnchor>, | ||
color : String as PropType<string>, | ||
// draggable : Boolean as PropType<boolean>, todo implement feature | ||
clickTolerance : Number as PropType<number>, | ||
rotation : Number as PropType<number>, | ||
rotationAlignment: String as PropType<'map' | 'viewport' | 'auto'>, | ||
pitchAlignment : String as PropType<'map' | 'viewport' | 'auto'>, | ||
scale : Number as PropType<number> | ||
}, | ||
setup(props, { slots }) { | ||
|
||
const map = inject(mapSymbol)!, | ||
opts: MarkerOptions = Object.keys(props) | ||
.filter(opt => (props as any)[ opt ] !== undefined && MapLib.MARKER_OPTION_KEYS.indexOf(opt as keyof MarkerOptions) !== -1) | ||
.reduce((obj, opt) => { | ||
(obj as any)[ opt ] = unref((props as any)[ opt ]); | ||
return obj; | ||
}, {}); | ||
const map = inject(mapSymbol)!, | ||
opts: MarkerOptions = Object.keys(props) | ||
.filter(opt => (props as any)[ opt ] !== undefined && MapLib.MARKER_OPTION_KEYS.indexOf(opt as keyof MarkerOptions) !== -1) | ||
.reduce((obj, opt) => { | ||
(obj as any)[ opt ] = unref((props as any)[ opt ]); | ||
return obj; | ||
}, {}); | ||
|
||
const marker = new Marker(opts); | ||
marker.setLngLat(props.coordinates).addTo(map.value!); | ||
const marker = new Marker(opts); | ||
marker.setLngLat(props.coordinates).addTo(map.value!); | ||
provide(markerSymbol, shallowRef(marker)); | ||
|
||
watch(() => props.coordinates, v => marker.setLngLat(v)); | ||
// watch(() => props.draggable, v => marker.setDraggable(v || false)); | ||
watch(() => props.offset, v => marker.setOffset(v || [ 0, 0 ])); | ||
watch(() => props.pitchAlignment, v => marker.setPitchAlignment(v || 'auto')); | ||
watch(() => props.rotationAlignment, v => marker.setRotationAlignment(v || 'auto')); | ||
watch(() => props.coordinates, v => marker.setLngLat(v)); | ||
// watch(() => props.draggable, v => marker.setDraggable(v || false)); | ||
watch(() => props.offset, v => marker.setOffset(v || [ 0, 0 ])); | ||
watch(() => props.pitchAlignment, v => marker.setPitchAlignment(v || 'auto')); | ||
watch(() => props.rotationAlignment, v => marker.setRotationAlignment(v || 'auto')); | ||
|
||
onBeforeUnmount(marker.remove.bind(marker)); | ||
onBeforeUnmount(marker.remove.bind(marker)); | ||
|
||
return { marker }; | ||
|
||
}, | ||
render() { | ||
// nothing | ||
} | ||
return () => [ | ||
h('div', slots.default ? slots.default({}) : undefined) | ||
]; | ||
} | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { defineComponent, inject, onMounted, onBeforeUnmount, PropType, unref, watch, ref, h } from 'vue'; | ||
import { LngLatLike, Popup, Offset, PositionAnchor, PopupOptions, PointLike } from 'maplibre-gl'; | ||
import { MapLib } from '@/lib/lib/map.lib'; | ||
import { mapSymbol, markerSymbol } from '@/lib/types'; | ||
|
||
export default /*#__PURE__*/ defineComponent({ | ||
name : 'MglPopup', | ||
emits: ['open', 'close'], | ||
props: { | ||
coordinates: { | ||
type: [ Object, Array ] as unknown as PropType<LngLatLike>, | ||
required: false | ||
}, | ||
closeButton: { | ||
type: Boolean, | ||
required: false, | ||
default: true, | ||
}, | ||
closeOnClick: { | ||
type: Boolean, | ||
required: false, | ||
default: true, | ||
}, | ||
closeOnMove: { | ||
type: Boolean, | ||
required: false, | ||
default: false, | ||
}, | ||
focusAfterOpen: { | ||
type: Boolean, | ||
required: false, | ||
default: true, | ||
}, | ||
anchor: { | ||
type: String as PropType<PositionAnchor>, | ||
required: false | ||
}, | ||
offset: { | ||
type: [Number, Object, Array] as PropType<Offset>, | ||
required: false, | ||
}, | ||
className: { | ||
type: String, | ||
required: false, | ||
}, | ||
maxWidth: { | ||
type: String, | ||
default: '240px', | ||
}, | ||
text: { | ||
type: String, | ||
required: false | ||
} | ||
}, | ||
async setup(props, { slots, emit }) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the async needed? There is no await keyword. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be flagged with linting no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The async is not needed here. I fixed it on my fork: https://github.com/indoorequal/vue-maplibre-gl |
||
const map = inject(mapSymbol); | ||
const marker = inject(markerSymbol); | ||
const root = ref(); | ||
|
||
const opts: PopupOptions = Object.keys(props) | ||
.filter(opt => (props as any)[ opt ] !== undefined && MapLib.POPUP_OPTION_KEYS.indexOf(opt as keyof PopupOptions) !== -1) | ||
.reduce((obj, opt) => { | ||
(obj as any)[ opt ] = unref((props as any)[ opt ]); | ||
return obj; | ||
}, {}); | ||
|
||
const popup = new Popup(opts); | ||
|
||
if (marker && marker.value) { | ||
marker.value.setPopup(popup); | ||
} else if (props.coordinates && map) { | ||
popup.setLngLat(props.coordinates).addTo(map.value!); | ||
} | ||
|
||
if (props.text) { | ||
popup.setText(props.text); | ||
} | ||
|
||
popup.on('open', () => emit('open')); | ||
popup.on('close', () => emit('close')); | ||
|
||
watch(() => props.coordinates, (v) => { if (v) { popup.setLngLat(v) } }); | ||
watch(() => props.text, v => popup.setText(v || '')); | ||
watch(() => props.offset, v => popup.setOffset(v)); | ||
watch(() => props.maxWidth, v => popup.setMaxWidth(v)); | ||
|
||
onMounted(() => { | ||
if (root.value && !props.text) { | ||
popup.setDOMContent(root.value!); | ||
} | ||
}); | ||
|
||
return () => [ | ||
h('div', {ref: root}, slots.default ? slots.default() : undefined) | ||
]; | ||
}, | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Provide a default value for not required props.