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

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