222
schangxiang@126.com
2025-04-30 9bec4dcae002f36aa23231da11cb03a156b40110
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { defineComponent, ref, computed } from 'vue'
import Icon from '@/components/Icon/Icon'
import styles from './Menu.module.scss'
import './index.scss'
import { isNil } from 'lodash'
 
interface contextMenuItemType {
  current: Record<string, any> | null
  options: any
}
interface CurrentType {
  row: any
  index: number
}
 
interface ItemType {
  label: string
  icon: string
  fn: (c: CurrentType) => void
  disabled?: boolean
  divided?: boolean
}
 
export default defineComponent({
  name: '右键菜单',
  props: {
    contextMenu: {
      type: Array,
    },
    graph: {
      type: Object,
    },
    visible: {
      type: Boolean,
      default: false,
    },
    options: {
      type: Object,
      default: () => {},
    },
    model: {
      type: Object,
      default: () => {},
    },
    isShow: {
      type: Boolean,
      default: false,
    },
  },
  setup(props, { emit }) {
    const menuRef = ref<any>()
    // const contextMenu: ItemType[] | unknown[] = props.contextMenu || []
    const contextMenu = computed(() => {
      return props.contextMenu || []
    })
    //  [
    //   {
    //     label: '复制',
    //     fn: (c: CurrentType) => {},
    //     divided: true,
    //     icon: 'o',
    //   },
    //   {
    //     label: '删除',
    //     fn: (c: CurrentType) => {},
    //     divided: true,
    //     disabled: false,
    //     icon: 'up',
    //   },
    //   {
    //     label: '查看属性',
    //     fn: (c: CurrentType) => {},
    //     divided: true,
    //     disabled: false,
    //     icon: 'up',
    //   },
    // ]
 
    const visible = computed({
      get: () => {
        return props.visible
      },
      set(value) {
        emit('update:visible', value)
      },
    })
 
    const isShow = computed({
      get: () => {
        return props.isShow
      },
      set(value) {
        emit('update:isShow', value)
      },
    })
 
    const contextMenuConfig = computed(() => {
      return {
        options: {
          zIndex: 2000,
          minWidth: 132,
          x: 0,
          y: 0,
          ...props.options,
        },
      }
    })
 
    const contextDisabled: any = computed(() => {
      return (item: any) => {
        if (item.disabled !== undefined) {
          if (!isNil(item.disabled?.value)) {
            return item.disabled?.value
          } else {
            return item.disabled
          }
        }
        return false
      }
    })
    /**
     * 菜单
     * @param item
     */
    const onHandleMenuItem = (event: TouchEvent | MouseEvent, item: any) => {
      event?.stopPropagation()
      item.fn && item.fn(props.model, isShow)
    }
    return () => {
      return isShow.value ? (
        <context-menu
          // v-if="contextMenu?.length > 0"
          ref={menuRef}
          v-model:show={visible.value}
          options={contextMenuConfig.value.options}
        >
          {
            // @ts-ignore
            contextMenu.value.map((item: ItemType, index: number) => {
              return (
                <span
                  onTouchstart={(event) => onHandleMenuItem(event, item)}
                  onClick={(event) => onHandleMenuItem(event, item)}
                >
                  <context-menu-item
                    key={index}
                    label={item.label}
                    disabled={!!contextDisabled.value(item)}
                    style={{
                      filter: contextDisabled.value(item)
                        ? 'opacity(0.4)'
                        : 'none',
                    }}
                  >
                    <div
                      style={{
                        cursor: !contextDisabled.value(item)
                          ? 'pointer'
                          : 'not-allowed',
                      }}
                      class={styles.contextMenuItemC}
                    >
                      <div style="width: 16px; margin-right: 7px">
                        <Icon
                          height={16}
                          class={styles.iconBox}
                          icon={item.icon}
                        />
                      </div>
                      <div class={styles.labelC}>{item.label}</div>
                    </div>
                  </context-menu-item>
                </span>
              )
            })
          }
        </context-menu>
      ) : null
    }
  },
})