PluginProbe
The Innovative Form Builder – IvyForms / 1.1.1
The Innovative Form Builder – IvyForms v1.1.1
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / frontend / src / stores / contextMenuStore.ts

contextMenuStore.ts in The Innovative Form Builder – IvyForms 1.1.1, at frontend/src/stores/contextMenuStore.ts

79 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import type { ShallowRef, DefineComponent, Ref } from 'vue'
2 import { defineStore } from 'pinia'
3 import { ref, shallowRef, markRaw } from 'vue'
4 import IvyContextMenuActions from '@/views/_components/context-menu/IvyContextMenuActions.vue'
5
6 export const useContextMenuStore = defineStore('contextMenu', () => {
7 const isOpen: Ref<boolean> = ref(false)
8 const contextMenuContent: ShallowRef<DefineComponent | null> = shallowRef(null)
9 const contextMenuProps: Ref<{
10 actions?: []
11 menuTitle?: string
12 contextMenuButtonRef?: HTMLElement | DefineComponent
13 entityId?: number
14 onClose?: () => void
15 selectionType?: 'singleselect' | 'multiselect'
16 }> = ref({})
17
18 const openContextMenu = (props = {}) => {
19 if (isOpen.value) {
20 closeContextMenu() // Close any existing menu
21 }
22
23 // Use markRaw to prevent the component from being made reactive
24 contextMenuContent.value = markRaw(IvyContextMenuActions as unknown as DefineComponent)
25 contextMenuProps.value = props
26
27 const buttonRef = contextMenuProps.value.contextMenuButtonRef
28 let resolvedButtonRef: HTMLElement | null = null
29
30 if (buttonRef && '$el' in buttonRef) {
31 resolvedButtonRef = (buttonRef as DefineComponent).$el as HTMLElement
32 } else if (buttonRef instanceof HTMLElement) {
33 resolvedButtonRef = buttonRef
34 }
35
36 if (resolvedButtonRef) {
37 resolvedButtonRef.classList.add('opened')
38 }
39
40 isOpen.value = true
41 }
42
43 const closeContextMenu = () => {
44 isOpen.value = false
45 const buttonRef = contextMenuProps.value.contextMenuButtonRef
46 let resolvedButtonRef: HTMLElement | null = null
47
48 if (buttonRef && '$el' in buttonRef) {
49 resolvedButtonRef = (buttonRef as DefineComponent).$el as HTMLElement
50 } else if (buttonRef instanceof HTMLElement) {
51 resolvedButtonRef = buttonRef
52 }
53
54 if (resolvedButtonRef) {
55 resolvedButtonRef.classList.remove('opened')
56 }
57
58 // Trigger the onClose callback if it exists
59 if (typeof contextMenuProps.value.onClose === 'function') {
60 contextMenuProps.value.onClose()
61 }
62
63 setTimeout(() => {
64 if (!isOpen.value) {
65 contextMenuProps.value = {}
66 contextMenuContent.value = null
67 }
68 }, 200)
69 }
70
71 return {
72 isOpen,
73 contextMenuContent,
74 contextMenuProps,
75 openContextMenu,
76 closeContextMenu,
77 }
78 })
79