index.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <template>
  2. <transition name="fade">
  3. <div
  4. class="control-panl pc"
  5. :class="{ full, 'strengthen-right': full }"
  6. :ref="(dom: any) => exposeConfig.dom = dom"
  7. v-if="!hide"
  8. >
  9. <div class="control-layer">
  10. <div class="scroll-view">
  11. <div c
  12. lass="panl"
  13. v-for="(groupItem, i) in group"
  14. :class="{ apart: groupItem.apart }"
  15. >
  16. <p v-if="groupItem.label">{{ groupItem.label }}</p>
  17. <VCtrl
  18. v-for="ctrl in groupItem.items"
  19. :activeCtrls="modelValue"
  20. :ctrl="ctrl"
  21. :key="ctrl.key"
  22. @click="ctrl => clickHandler(ctrl)"
  23. />
  24. </div>
  25. </div>
  26. </div>
  27. </div>
  28. </transition>
  29. </template>
  30. <script lang="ts" setup>
  31. import { ref, watchEffect, watch, reactive, toRaw } from 'vue'
  32. import VCtrl from './ctrl.vue'
  33. import { useActive } from '@/hook'
  34. import type { Groups, Items, Item, ControlExpose } from './index'
  35. const props = withDefaults(
  36. defineProps<{
  37. group: Groups
  38. show?: boolean
  39. modelValue: Items
  40. full?: boolean
  41. }>(),
  42. {
  43. show: true,
  44. full: false
  45. }
  46. )
  47. const active = useActive()
  48. const hide = ref(true)
  49. watch(
  50. () => [active, props.show],
  51. () => {
  52. if (active.value) {
  53. hide.value = active.value
  54. }
  55. setTimeout(() => hide.value = !props.show)
  56. },
  57. { immediate: true }
  58. )
  59. const emit = defineEmits<{
  60. (e: 'update:modelValue', items: Item[], oldItems: Item[]): void
  61. (e: 'select', item: Item): void
  62. }>()
  63. const clickHandler = (ctrl: Item) => {
  64. if (ctrl.inClick) {
  65. return emit('select', ctrl)
  66. }
  67. const newRuns = reactive([...props.modelValue])
  68. const index = newRuns.indexOf(reactive(ctrl))
  69. if (~index) {
  70. newRuns.splice(index, 1)
  71. } else if (ctrl.makeup) {
  72. newRuns.push(ctrl)
  73. } else {
  74. const makeupRuns = newRuns.filter(item => item.makeup)
  75. newRuns.length = 0
  76. newRuns.push(...makeupRuns, ctrl)
  77. }
  78. emit('update:modelValue', newRuns, props.modelValue)
  79. }
  80. watchEffect(() => {
  81. if (!props.show) {
  82. const items: Item[] = []
  83. for (const { items } of props.group) {
  84. items.push(...items.filter(item => item.def))
  85. }
  86. emit('update:modelValue', items, props.modelValue)
  87. }
  88. })
  89. const exposeConfig = reactive<ControlExpose>({ dom: null })
  90. defineExpose(exposeConfig)
  91. </script>
  92. <style lang="sass" scoped>
  93. @import './style.scss'
  94. </style>