PluginProbe
The Innovative Form Builder – IvyForms / 0.5
The Innovative Form Builder – IvyForms v0.5
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 / composables / useMenuNavigation.ts

useMenuNavigation.ts in The Innovative Form Builder – IvyForms 0.5, at frontend/src/composables/useMenuNavigation.ts

170 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { useRouter } from 'vue-router'
2 import { useUnsavedChangesStore } from '@/stores/useUnsavedChangesStore'
3 import { useActionEntityStore } from '@/stores/actionEntityStore.ts'
4 import { useFormBuilder } from '@/stores/useFormBuilder.ts'
5 import { useConfirmationSettingBuilder } from '@/stores/useConfirmationSettingBuilder.ts'
6 import { useNotificationSettingBuilder } from '@/stores/useNotificationSettingBuilder.ts'
7 import {
8 IVYFORMS_EDIT_NOTIFICATIONS,
9 IVYFORMS_NEW_NOTIFICATIONS,
10 IVYFORMS_SETTINGS,
11 } from '@/constants/pages.ts'
12
13 export function useMenuNavigation() {
14 const router = useRouter()
15 const unsavedChangesStore = useUnsavedChangesStore()
16 const actionEntityStore = useActionEntityStore()
17 const formBuilderStore = useFormBuilder()
18 const confirmationStore = useConfirmationSettingBuilder()
19 const notificationStore = useNotificationSettingBuilder()
20
21 // Helper to get all unsaved changes states at once
22 function getUnsavedChangesStates() {
23 return {
24 hasFormBuilderUnsaved: unsavedChangesStore.hasUnsavedChanges('formBuilder', () =>
25 formBuilderStore.getCurrentState(),
26 ).value,
27 hasConfirmationUnsaved: unsavedChangesStore.hasUnsavedChanges('confirmationBuilder', () =>
28 confirmationStore.getCurrentState(),
29 ).value,
30 hasNotificationUnsaved: unsavedChangesStore.hasUnsavedChanges('notificationBuilder', () =>
31 notificationStore.getCurrentState(),
32 ).value,
33 }
34 }
35
36 async function getConfirmationIdForForm(formId) {
37 // Try to get confirmationId from store
38 if (confirmationStore.id && confirmationStore.formId == formId) {
39 return confirmationStore.id
40 }
41 // Try to fetch confirmation for this form
42 await confirmationStore.fetchAllConfirmations(formId)
43 if (confirmationStore.id) {
44 return confirmationStore.id
45 }
46 // If still not found, create one
47 confirmationStore.formId = formId
48 await confirmationStore.createConfirmation()
49 return confirmationStore.id
50 }
51
52 async function getMenuRoute(item, fallbackRouteName = null) {
53 const { hasFormBuilderUnsaved, hasConfirmationUnsaved, hasNotificationUnsaved } =
54 getUnsavedChangesStates()
55 if (hasFormBuilderUnsaved || hasConfirmationUnsaved || hasNotificationUnsaved) {
56 return item.routeName
57 ? { name: fallbackRouteName, params: { id: formBuilderStore.formId } }
58 : undefined
59 }
60
61 if (item.routeName === 'form-confirmation') {
62 const formId = formBuilderStore.formId
63 const confirmationId = await getConfirmationIdForForm(formId)
64 return { name: item.routeName, params: { formId, confirmationId } }
65 }
66 // Notifications table only needs formId
67 if (item.routeName === 'form-notifications') {
68 return { name: item.routeName, params: { formId: formBuilderStore.formId } }
69 }
70
71 // Default: use id for other routes
72 return item.routeName
73 ? { name: item.routeName, params: { id: formBuilderStore.formId } }
74 : undefined
75 }
76
77 async function onMenuClick(item, isActive) {
78 if (isActive(item.routeName, item.index)) return
79 const { hasFormBuilderUnsaved, hasConfirmationUnsaved, hasNotificationUnsaved } =
80 getUnsavedChangesStates()
81 const doNavigate = async () => {
82 // Handle items with sub-items (like Integrations)
83 if (item.subItems && item.subItems.length > 0 && item.autoNavigateToFirst) {
84 const firstSubItem = item.subItems[0]
85 const path = `/manage/${formBuilderStore.formId}/settings/${item.index}${firstSubItem.index}`
86 await router.push(path)
87 return
88 }
89
90 if (item.routeName === 'form-confirmation') {
91 const formId = formBuilderStore.formId
92 const confirmationId = await getConfirmationIdForForm(formId)
93 await router.push({ name: item.routeName, params: { formId, confirmationId } })
94 return
95 }
96 if (item.routeName === 'form-notifications') {
97 await router.push({ name: item.routeName, params: { formId: formBuilderStore.formId } })
98 return
99 }
100 // TODO integrations route
101 if (item.routeName === 'form-integrations') {
102 await router.push({
103 name: item.routeName,
104 params: { formId: formBuilderStore.formId, integration: 'wpdatatables' },
105 })
106 return
107 }
108 // Default: use id for other routes
109 await router.push({ name: item.routeName, params: { id: formBuilderStore.formId } })
110 }
111 if (hasFormBuilderUnsaved || hasConfirmationUnsaved || hasNotificationUnsaved) {
112 actionEntityStore.showUnsavedChangesDialog(async () => {
113 if (hasFormBuilderUnsaved) {
114 unsavedChangesStore.resetUnsavedChanges('formBuilder', () =>
115 formBuilderStore.getCurrentState(),
116 )
117 }
118 if (hasConfirmationUnsaved) {
119 unsavedChangesStore.resetUnsavedChanges('confirmationBuilder', () =>
120 confirmationStore.getCurrentState(),
121 )
122 }
123 if (hasNotificationUnsaved) {
124 unsavedChangesStore.resetUnsavedChanges('notificationBuilder', () =>
125 notificationStore.getCurrentState(),
126 )
127 }
128 await doNavigate()
129 })
130 } else {
131 await doNavigate()
132 }
133 }
134
135 function isActive(routeName, index) {
136 const matched = router.currentRoute.value.matched
137 const currentPath = router.currentRoute.value.path
138
139 // Check if we're on an integrations sub-route (must include the full path)
140 if (index === 'integrations') {
141 return currentPath.includes('/settings/integrations/')
142 }
143
144 if (routeName && matched.some(({ name }) => name === routeName)) {
145 return true
146 }
147 if (matched.length > 1 && index === 'settings') {
148 const parent = matched[matched.length - 2]
149 if (parent && parent.name === IVYFORMS_SETTINGS) {
150 return true
151 }
152 }
153 if (
154 index === 'notifications' &&
155 matched.some(
156 ({ name }) => name === IVYFORMS_NEW_NOTIFICATIONS || name === IVYFORMS_EDIT_NOTIFICATIONS,
157 )
158 ) {
159 return true
160 }
161 return false
162 }
163
164 return {
165 getMenuRoute,
166 onMenuClick,
167 isActive,
168 }
169 }
170