PluginProbe
The Innovative Form Builder – IvyForms / 0.3
The Innovative Form Builder – IvyForms v0.3
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.3, at frontend/src/composables/useMenuNavigation.ts

108 lines 3.7 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 function getMenuRoute(item, fallbackRouteName = null) {
37 const { hasFormBuilderUnsaved, hasConfirmationUnsaved, hasNotificationUnsaved } =
38 getUnsavedChangesStates()
39 if (hasFormBuilderUnsaved || hasConfirmationUnsaved || hasNotificationUnsaved) {
40 return item.routeName
41 ? { name: fallbackRouteName, params: { id: formBuilderStore.formId } }
42 : undefined
43 }
44 return item.routeName
45 ? { name: item.routeName, params: { id: formBuilderStore.formId } }
46 : undefined
47 }
48
49 function onMenuClick(item, isActive) {
50 if (isActive(item.routeName)) return
51
52 const { hasFormBuilderUnsaved, hasConfirmationUnsaved, hasNotificationUnsaved } =
53 getUnsavedChangesStates()
54
55 if (hasFormBuilderUnsaved || hasConfirmationUnsaved || hasNotificationUnsaved) {
56 actionEntityStore.showUnsavedChangesDialog(() => {
57 // Reset unsaved changes for the current tab before navigating
58 if (hasFormBuilderUnsaved) {
59 unsavedChangesStore.resetUnsavedChanges('formBuilder', () =>
60 formBuilderStore.getCurrentState(),
61 )
62 }
63 if (hasConfirmationUnsaved) {
64 unsavedChangesStore.resetUnsavedChanges('confirmationBuilder', () =>
65 confirmationStore.getCurrentState(),
66 )
67 }
68 if (hasNotificationUnsaved) {
69 unsavedChangesStore.resetUnsavedChanges('notificationBuilder', () =>
70 notificationStore.getCurrentState(),
71 )
72 }
73 router.push({ name: item.routeName, params: { id: formBuilderStore.formId } })
74 })
75 } else {
76 router.push({ name: item.routeName, params: { id: formBuilderStore.formId } })
77 }
78 }
79
80 function isActive(routeName, index) {
81 const matched = router.currentRoute.value.matched
82 if (matched.some(({ name }) => name === routeName)) {
83 return true
84 }
85 if (matched.length > 1 && index === 'settings') {
86 const parent = matched[matched.length - 2]
87 if (parent && parent.name === IVYFORMS_SETTINGS) {
88 return true
89 }
90 }
91 if (
92 index === 'notifications' &&
93 matched.some(
94 ({ name }) => name === IVYFORMS_NEW_NOTIFICATIONS || name === IVYFORMS_EDIT_NOTIFICATIONS,
95 )
96 ) {
97 return true
98 }
99 return false
100 }
101
102 return {
103 getMenuRoute,
104 onMenuClick,
105 isActive,
106 }
107 }
108