PluginProbe
The Innovative Form Builder – IvyForms / 0.4
The Innovative Form Builder – IvyForms v0.4
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 0.4, at frontend/src/stores/contextMenuStore.ts

77 lines 2.2 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 } 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 }> = ref({})
16
17 const openContextMenu = (props = {}) => {
18 if (isOpen.value) {
19 closeContextMenu() // Close any existing menu
20 }
21
22 contextMenuContent.value = IvyContextMenuActions as unknown as DefineComponent
23 contextMenuProps.value = props
24
25 const buttonRef = contextMenuProps.value.contextMenuButtonRef
26 let resolvedButtonRef: HTMLElement | null = null
27
28 if (buttonRef && '$el' in buttonRef) {
29 resolvedButtonRef = (buttonRef as DefineComponent).$el as HTMLElement
30 } else if (buttonRef instanceof HTMLElement) {
31 resolvedButtonRef = buttonRef
32 }
33
34 if (resolvedButtonRef) {
35 resolvedButtonRef.classList.add('opened')
36 }
37
38 isOpen.value = true
39 }
40
41 const closeContextMenu = () => {
42 isOpen.value = false
43 const buttonRef = contextMenuProps.value.contextMenuButtonRef
44 let resolvedButtonRef: HTMLElement | null = null
45
46 if (buttonRef && '$el' in buttonRef) {
47 resolvedButtonRef = (buttonRef as DefineComponent).$el as HTMLElement
48 } else if (buttonRef instanceof HTMLElement) {
49 resolvedButtonRef = buttonRef
50 }
51
52 if (resolvedButtonRef) {
53 resolvedButtonRef.classList.remove('opened')
54 }
55
56 // Trigger the onClose callback if it exists
57 if (typeof contextMenuProps.value.onClose === 'function') {
58 contextMenuProps.value.onClose()
59 }
60
61 setTimeout(() => {
62 if (!isOpen.value) {
63 contextMenuProps.value = {}
64 contextMenuContent.value = null
65 }
66 }, 200)
67 }
68
69 return {
70 isOpen,
71 contextMenuContent,
72 contextMenuProps,
73 openContextMenu,
74 closeContextMenu,
75 }
76 })
77