import { ref } from 'vue' import { autoSetModeCallback, createTemploraryID } from './sys' import { getFuseModel, getFuseModelShowVariable } from './fuse-model' import { fetchPaths, postAddPath, postDeletePath, postUpdatePath, } from '@/api' import { deleteStoreItem, addStoreItem, updateStoreItem, fetchStoreItems, saveStoreItems, recoverStoreItems, asyncTimeout } from '@/utils' import type { Path } from '@/api' import { custom, params } from '@/env' import { Message } from 'bill/expose-common' import { useSelects } from '@/hook/ids' export type { Path } from '@/api' export type Paths = Path[] export const paths = ref([]) export const selectPaths = useSelects(paths, true) export const getPath = (id: Path['id']) => paths.value.find(path => path.id === id) export const getPathIsShow = (path: Path) => { if (!custom.showPaths && custom.showPath !== path.id) return false; return true; // const modelIds = path.points.map(item => item.modelId) // if (path.linePosition?.modelId) { // modelIds.push(path.linePosition.modelId) // } // return modelIds.every(modelId => { // if (modelId === '-100') return true; // const model = getFuseModel(modelId) // return model && getFuseModelShowVariable(model).value // }) } export const createPath = (path: Partial = {}): Path => { return { id: createTemploraryID(), lineColor: '#ffffff', lineAltitudeAboveGround: 10, reverseDirection: false, lineWidth: 0.5, name: '', fontSize: 12, opacity: 1, showDirection: false, showName: true, visibilityRange: 30, globalVisibility: false, points: [], ...path } } let bcPaths: Paths = [] export const getBackupPaths = () => { console.log('bcPaths', bcPaths) return bcPaths } export const backupPaths = () => { bcPaths = JSON.parse(JSON.stringify(paths.value)) } export const initialPaths = fetchStoreItems(paths, async () => { const paths = await fetchPaths() return paths }, () => { backupPaths() selectPaths.all.value = true }) export const recoverPaths = async () => { const backupItems = bcPaths; // paths.value.length = 0 // await asyncTimeout(16) paths.value = backupItems.map((oldItem) => { const model = paths.value.find((item) => item.id === oldItem.id); return model ? Object.assign(model, oldItem) : oldItem; }); } export const addPath = addStoreItem(paths, postAddPath) export const deletePath = deleteStoreItem(paths, path => postDeletePath(path.id)) export const updatePath = updateStoreItem(paths, postUpdatePath) export const savePaths = saveStoreItems( paths, getBackupPaths, { add: addPath, delete: deletePath, update: updatePath } ) export const autoSavePaths = autoSetModeCallback(paths, { backup: backupPaths, recovery: recoverPaths, save: async () => { if (!paths.value.every(record => record.name)) { Message.warning('路径名称不可为空') throw '路径名称不可为空' } else if (paths.value.some(path => path.points.length <= 1)) { Message.warning('路径点不可少于两个') throw '路径点不可少于两个' } await savePaths() } })