100 lines
3.2 KiB
TypeScript
100 lines
3.2 KiB
TypeScript
import { defineStore } from 'pinia';
|
||
import { ref } from 'vue';
|
||
import dayjs from 'dayjs';
|
||
|
||
export const useMapViewStore = defineStore('map-view', () => {
|
||
const checkedLayerKeys = ref<string[]>([]);
|
||
const legendCheckedState = ref<Record<string, number>>({});
|
||
const searchTimeRange = ref<[any, any]>([dayjs().subtract(1, 'M'), dayjs()]);
|
||
const selectedBaseId = ref('');
|
||
const currentZoomLevel = ref(4.5);
|
||
const activeBaseLayerKey = ref('s_province_boundaries');
|
||
|
||
// 备注:统一写入当前选中的图层 key,始终保持去重后的结果。
|
||
const setCheckedLayerKeys = (keys: string[] = []) => {
|
||
checkedLayerKeys.value = Array.from(new Set(keys.filter(Boolean)));
|
||
};
|
||
|
||
// 备注:获取当前选中的图层 key,供图层树、动态图层和筛选联动复用。
|
||
const getCheckedLayerKeys = (): string[] => {
|
||
return checkedLayerKeys.value;
|
||
};
|
||
|
||
// 备注:覆盖写入完整图例运行态勾选状态,适合图例初始化和整批替换。
|
||
const setLegendCheckedState = (state: Record<string, number> = {}) => {
|
||
legendCheckedState.value = { ...state };
|
||
};
|
||
|
||
// 备注:写入单个图例运行态勾选状态,供点击图例项时复用。
|
||
const setLegendChecked = (nameEn: string, checked: number) => {
|
||
if (!nameEn) return;
|
||
legendCheckedState.value = {
|
||
...legendCheckedState.value,
|
||
[nameEn]: checked
|
||
};
|
||
};
|
||
|
||
// 备注:批量写入多个图例运行态勾选状态,供分组图例和筛选联动复用。
|
||
const setLegendCheckedBatch = (
|
||
stateMap: Record<string, number> = {}
|
||
) => {
|
||
legendCheckedState.value = {
|
||
...legendCheckedState.value,
|
||
...stateMap
|
||
};
|
||
};
|
||
|
||
// 备注:获取单个图例当前运行态勾选状态,未命中时默认返回 0。
|
||
const getLegendChecked = (nameEn: string): number => {
|
||
return legendCheckedState.value[nameEn] ?? 0;
|
||
};
|
||
|
||
// 备注:统一更新当前时间筛选范围,供筛选组件和地图数据重载复用。
|
||
const setSearchTimeRange = (range: [any, any]) => {
|
||
searchTimeRange.value = range;
|
||
};
|
||
|
||
// 备注:统一更新当前基地 ID,供地图筛选和基地联动复用。
|
||
const setSelectedBaseId = (baseId: string) => {
|
||
selectedBaseId.value = baseId || '';
|
||
};
|
||
|
||
// 备注:统一更新当前地图缩放级别,供动态图层联动复用。
|
||
const setCurrentZoomLevel = (zoom: number) => {
|
||
currentZoomLevel.value = zoom;
|
||
};
|
||
|
||
// 备注:在菜单切换或地图销毁时重置运行态到默认值。
|
||
const resetViewState = () => {
|
||
checkedLayerKeys.value = [];
|
||
legendCheckedState.value = {};
|
||
searchTimeRange.value = [dayjs().subtract(1, 'M'), dayjs()];
|
||
selectedBaseId.value = '';
|
||
currentZoomLevel.value = 4.5;
|
||
};
|
||
|
||
const setActiveBaseLayerKey = (key: string) => {
|
||
activeBaseLayerKey.value = key;
|
||
};
|
||
|
||
return {
|
||
checkedLayerKeys,
|
||
legendCheckedState,
|
||
searchTimeRange,
|
||
selectedBaseId,
|
||
currentZoomLevel,
|
||
activeBaseLayerKey,
|
||
setActiveBaseLayerKey,
|
||
setCheckedLayerKeys,
|
||
getCheckedLayerKeys,
|
||
setLegendCheckedState,
|
||
setLegendChecked,
|
||
setLegendCheckedBatch,
|
||
getLegendChecked,
|
||
setSearchTimeRange,
|
||
setSelectedBaseId,
|
||
setCurrentZoomLevel,
|
||
resetViewState
|
||
};
|
||
});
|