Files
admin-govern/src/components/tree/govern/deviceTree.vue

86 lines
2.8 KiB
Vue
Raw Normal View History

2024-01-09 13:49:21 +08:00
<template>
2026-06-08 18:34:49 +08:00
<Tree ref="treRef" @check-change="handleCheckChange" :default-checked-keys="defaultCheckedKeys" default-expand-all
2026-06-04 19:06:36 +08:00
:show-checkbox="props.showCheckbox" :data="tree" :height="props.height" @changeDeviceType="changeDeviceType"
@changeTreeType="loadTree" :engineering="props.engineering" leaf-mode="device" />
2024-01-09 13:49:21 +08:00
</template>
<script lang="ts" setup>
2026-06-08 18:34:49 +08:00
import { ref, onMounted, onBeforeUnmount } from 'vue'
import { debounce } from 'lodash-es'
2024-06-27 09:39:53 +08:00
import Tree from '../device.vue'
2024-01-09 13:49:21 +08:00
import { getDeviceTree } from '@/api/cs-device-boot/csLedger'
import { useConfig } from '@/stores/config'
import { waitForTreeRef, type TreeRefKey } from './lineTreeUtils'
import { createLineTreeDecorators } from './lineTreeUtils'
import { decorateDeviceTree } from './deviceTreeUtils'
import type { LineTreeLeaves } from './lineTreeUtils'
defineOptions({ name: 'govern/deviceTree' })
2026-04-02 09:08:57 +08:00
2024-01-15 20:29:12 +08:00
const props = withDefaults(
defineProps<{
showCheckbox?: boolean
defaultCheckedKeys?: any[]
2026-01-04 14:55:31 +08:00
height?: number
2026-04-02 09:08:57 +08:00
engineering?: boolean
2024-01-15 20:29:12 +08:00
}>(),
{
showCheckbox: false,
defaultCheckedKeys: () => [],
2026-04-02 09:08:57 +08:00
height: 0,
engineering: false
2024-01-15 20:29:12 +08:00
}
)
const emit = defineEmits(['init', 'checkChange', 'deviceTypeChange'])
2024-01-09 13:49:21 +08:00
const config = useConfig()
const tree = ref<any[]>([])
const treRef = ref<InstanceType<typeof Tree>>()
const decorators = createLineTreeDecorators(() => config.getColorVal('elementUiPrimary'))
2026-03-06 09:36:42 +08:00
2026-06-08 18:34:49 +08:00
const changeDeviceType = debounce((val: any, obj: any) => {
emit('deviceTypeChange', val, obj)
}, 300)
onBeforeUnmount(() => changeDeviceType.cancel())
2025-12-20 23:44:46 +08:00
async function selectInitialNode(type: string | undefined, leaves: LineTreeLeaves) {
const candidates: { refKey: TreeRefKey; list: any[]; level: number }[] =
type === '2'
? [{ refKey: 'treeRef4', list: leaves.engineering, level: 2 }]
: [
2026-06-04 19:06:36 +08:00
{ refKey: 'treeRef1', list: leaves.govern, level: 2 },
{ refKey: 'treeRef2', list: leaves.portable, level: 2 },
{ refKey: 'treeRef3', list: leaves.monitor, level: 2 }
]
2026-01-04 14:55:31 +08:00
for (const { refKey, list, level } of candidates) {
const node = list[0]
if (!node) continue
const treeInstance = await waitForTreeRef(treRef.value, refKey)
treeInstance?.setCurrentKey(node.id)
2026-06-04 19:06:36 +08:00
emit('init', { ...node })
return
}
emit('init')
}
2026-03-06 09:36:42 +08:00
const loadTree = (type?: string) => {
getDeviceTree({ type: type === '2' ? 'engineering' : '' }).then(res => {
const leaves = decorateDeviceTree(res.data, type, decorators)
tree.value = res.data
selectInitialNode(type, leaves)
2024-01-11 08:54:09 +08:00
})
2026-03-06 09:36:42 +08:00
}
2026-06-08 18:34:49 +08:00
onMounted(() => loadTree( '2' ))
2026-01-07 13:14:26 +08:00
2026-06-04 19:06:36 +08:00
const handleCheckChange =
2026-01-07 13:14:26 +08:00
(data: any, checked: any, indeterminate: any) => {
emit('checkChange', { data, checked, indeterminate })
2026-06-04 19:06:36 +08:00
}
2026-01-07 13:14:26 +08:00
defineExpose({ treRef })
2024-01-09 13:49:21 +08:00
</script>