多维表格
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
5.6 KiB

<script lang="ts" setup>
import 'leaflet/dist/leaflet.css'
import L from 'leaflet'
import 'leaflet.markercluster'
import { IsGalleryInj, IsGridInj, IsMapInj, onMounted, provide, ref } from '#imports'
import type { Row as RowType } from '~/lib'
provide(IsGalleryInj, ref(false))
provide(IsGridInj, ref(false))
provide(IsMapInj, ref(true))
const route = useRoute()
const router = useRouter()
const reloadViewDataHook = inject(ReloadViewDataHookInj)
const { formattedData, loadMapData, loadMapMeta, mapMetaData, geoDataFieldColumn } = useMapViewStoreOrThrow()
const markersClusterGroupRef = ref<L.MarkerClusterGroup>()
const mapContainerRef = ref<HTMLElement>()
const myMapRef = ref<L.Map>()
const meta = inject(MetaInj, ref())
const view = inject(ActiveViewInj, ref())
const expandedFormDlg = ref(false)
const expandedFormRow = ref<RowType>()
const expandedFormRowState = ref<Record<string, any>>()
const expandForm = (row: RowType, state?: Record<string, any>) => {
console.log('expandForm')
const rowId = extractPkFromRow(row.row, meta.value!.columns!)
// debugger
if (rowId) {
console.log('if')
router.push({
query: {
...route.query,
rowId,
},
})
} else {
console.log('else')
expandedFormRow.value = row
expandedFormRowState.value = state
expandedFormDlg.value = true
}
}
const expandedFormOnRowIdDlg = computed({
get() {
return !!route.query.rowId
},
set(val) {
if (!val)
router.push({
query: {
...route.query,
rowId: undefined,
},
})
},
})
onBeforeMount(async () => {
await loadMapMeta()
await loadMapData()
})
2 years ago
reloadViewDataHook?.on(async () => {
loadMapData()
2 years ago
loadMapMeta()
2 years ago
})
function addMarker(lat: number, long: number, row: RowType) {
if (markersClusterGroupRef.value == null) {
throw new Error('Map is null')
}
const newMarker = L.marker([lat, long]).on('click', () => {
expandForm(row)
console.log('click on marker')
})
markersClusterGroupRef.value?.addLayer(newMarker)
// if (newMarker) {
// newMarker.bindPopup(popupContent)
// }
}
watch([formattedData, mapMetaData, markersClusterGroupRef], () => {
if (markersClusterGroupRef.value == null) {
return
}
markersClusterGroupRef.value?.clearLayers()
2 years ago
formattedData.value?.forEach((row) => {
const primaryGeoDataColumnTitle = geoDataFieldColumn.value?.title
if (primaryGeoDataColumnTitle == null) {
throw new Error('Cannot find primary geo data column title')
}
const primaryGeoDataValue = row.row[primaryGeoDataColumnTitle]
// const listItems = Object.entries(row)
// .map(([key, val]) => {
// const prettyVal = val !== null && (typeof val === 'object' || Array.isArray(val)) ? JSON.stringify(val) : val
// return `<li><b>${key}</b>: <br/>${prettyVal}</li>`
// })
// .join('')
// const popupContent = `<ul>${listItems}</ul>`
if (primaryGeoDataValue == null) {
return
}
const [lat, long] = primaryGeoDataValue.split(';').map(parseFloat)
addMarker(lat, long, row)
})
})
onMounted(async () => {
// TODO: also here add/use viewId suffix approach (see comment below)
const initialZoomLevel = parseInt(localStorage.getItem(`mapView.zoom${mapMetaData.value.fk_view_id}`) || '10')
// const initialBounds = parseInt(localStorage?.getItem(`mapView.bounds${mapMetaData.value.fk_view_id}`))
const myMap = L.map(mapContainerRef.value!).setView([51.505, -0.09], initialZoomLevel)
myMapRef.value = myMap
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
}).addTo(myMap)
markersClusterGroupRef.value = L.markerClusterGroup({
iconCreateFunction(cluster) {
return L.divIcon({ html: `${cluster.getChildCount()}`, className: 'geo-map-marker-cluster', iconSize: new L.Point(40, 40) })
},
})
myMap.addLayer(markersClusterGroupRef.value)
myMap.on('zoomend', function (params) {
if (localStorage != null) {
// TODO: use current mapView id as suffix to the local storage key,
// so there are no clashes when there are multiple map views, e.g.:
// localStorage.setItem(`mapView.${meta?.value.id || 'DEFAULT_ID'}`, this.input)
localStorage.setItem(`mapView.zoom${mapMetaData.value.fk_view_id}`, myMap.getZoom().toString())
}
})
myMap.on('moveend', function () {
const bounds = myMap.getBounds()
const newSouthWest = bounds.getSouthWest
const newNorthEast = bounds.getNorthEast
console.log('bounds', bounds)
console.log('boundsSW', newSouthWest)
console.log('boundsNE', newNorthEast)
if (localStorage != null) {
localStorage.setItem(`mapView.bounds${mapMetaData.value.fk_view_id}`, myMap.getBounds().toString())
}
})
})
</script>
<template>
<div class="flex flex-col h-full w-full no-underline">
<div id="mapContainer" ref="mapContainerRef"></div>
</div>
<Suspense>
<LazySmartsheetExpandedForm
v-if="expandedFormOnRowIdDlg"
:key="route.query.rowId"
v-model="expandedFormOnRowIdDlg"
:row="{ row: {}, oldRow: {}, rowMeta: {} }"
:meta="meta"
:row-id="route.query.rowId"
:view="view"
/>
</Suspense>
</template>
<style scoped lang="scss">
#mapContainer {
height: 100vh;
}
:global(.geo-map-marker-cluster) {
background-color: pink;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
}
</style>
<style>
.no-underline a {
text-decoration: none !important;
}
.leaflet-popup-content-wrapper {
max-height: 255px;
overflow: scroll;
}
</style>