NodeSelector.vue 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <script setup lang="ts">
  2. import { computed, ref } from 'vue'
  3. import { useGettext } from 'vue3-gettext'
  4. import environment from '@/api/environment'
  5. const props = defineProps<{
  6. target: number[]
  7. map?: Record<number, string>
  8. hiddenLocal?: boolean
  9. }>()
  10. const emit = defineEmits(['update:target', 'update:map'])
  11. const { $gettext } = useGettext()
  12. const data = ref([])
  13. const data_map = ref({})
  14. environment.get_list().then(r => {
  15. data.value = r.data
  16. r.data.forEach(node => {
  17. data_map[node.id] = node
  18. })
  19. })
  20. const value = computed({
  21. get() {
  22. return props.target
  23. },
  24. set(v) {
  25. if (typeof props.map === 'object') {
  26. v.forEach(id => {
  27. if (id !== 0)
  28. emit('update:map', { ...props.map, [id]: data_map[id].name })
  29. })
  30. }
  31. emit('update:target', v)
  32. },
  33. })
  34. </script>
  35. <template>
  36. <ACheckboxGroup
  37. v-model:value="value"
  38. style="width: 100%"
  39. >
  40. <ARow :gutter="[16, 16]">
  41. <ACol
  42. v-if="!hiddenLocal"
  43. :span="8"
  44. >
  45. <ACheckbox :value="0">
  46. {{ $gettext('Local') }}
  47. </ACheckbox>
  48. <ATag color="blue">
  49. {{ $gettext('Online') }}
  50. </ATag>
  51. </ACol>
  52. <ACol
  53. v-for="(node, index) in data"
  54. :key="index"
  55. :span="8"
  56. >
  57. <ACheckbox :value="node.id">
  58. {{ node.name }}
  59. </ACheckbox>
  60. <ATag
  61. v-if="node.status"
  62. color="blue"
  63. >
  64. {{ $gettext('Online') }}
  65. </ATag>
  66. <ATag
  67. v-else
  68. color="error"
  69. >
  70. {{ $gettext('Offline') }}
  71. </ATag>
  72. </ACol>
  73. </ARow>
  74. <AEmpty v-if="hiddenLocal && data.length === 0" />
  75. </ACheckboxGroup>
  76. </template>
  77. <style scoped lang="less">
  78. </style>