|
| 1 | +<script setup> |
| 2 | +import { storeToRefs } from 'pinia'; |
| 3 | +import { computed, ref } from 'vue'; |
| 4 | +import { useI18n } from 'vue-i18n'; |
| 5 | +import { useRoute } from 'vue-router'; |
| 6 | +
|
| 7 | +import PendingSubmissionsModal from '~/components/forms/offline/PendingSubmissionsModal.vue'; |
| 8 | +import { offlineQueue } from '~/offline/queue'; |
| 9 | +import { useSimulationToggle } from '~/offline/useSimulationToggle'; |
| 10 | +import { useFormStore } from '~/store/form'; |
| 11 | +
|
| 12 | +const { t, locale } = useI18n({ useScope: 'global' }); |
| 13 | +
|
| 14 | +const route = useRoute(); |
| 15 | +const { form } = storeToRefs(useFormStore()); |
| 16 | +const { networkOnline, canSimulateOffline, simulatingOffline } = |
| 17 | + useSimulationToggle(); |
| 18 | +
|
| 19 | +const OFFLINE_ROUTES = ['FormSubmit', 'FormSuccess']; |
| 20 | +const queuedCount = computed(() => offlineQueue.entries.value.length); |
| 21 | +const showPending = ref(false); |
| 22 | +
|
| 23 | +// Only show when an offline-enabled form is actually being rendered. Login / |
| 24 | +// forms-list / admin / etc. must never see this control even when simulation |
| 25 | +// is on or the queue has items; those are unrelated contexts. |
| 26 | +const visible = computed( |
| 27 | + () => |
| 28 | + !!form.value.enableOfflineSubmission && OFFLINE_ROUTES.includes(route.name) |
| 29 | +); |
| 30 | +
|
| 31 | +const realOffline = computed(() => !networkOnline.value); |
| 32 | +// Toggle button is visible when the URL grants the simulate gate OR whenever |
| 33 | +// the user is already simulating (so they can always exit even after |
| 34 | +// navigating away from a route that carried ?simulateOffline=1). Hidden |
| 35 | +// during real offline; there's no manual "go online" to flip to. |
| 36 | +const showChevron = computed( |
| 37 | + () => |
| 38 | + networkOnline.value && (canSimulateOffline.value || simulatingOffline.value) |
| 39 | +); |
| 40 | +
|
| 41 | +const state = computed(() => { |
| 42 | + if (realOffline.value) { |
| 43 | + return { |
| 44 | + color: 'white', |
| 45 | + variant: 'outlined', |
| 46 | + icon: 'mdi:mdi-cloud-off-outline', |
| 47 | + iconColor: 'warning', |
| 48 | + label: t('trans.offlineSubmission.offlineBadge'), |
| 49 | + dataTest: 'offlineBadge', |
| 50 | + }; |
| 51 | + } |
| 52 | + if (simulatingOffline.value) { |
| 53 | + return { |
| 54 | + color: 'white', |
| 55 | + variant: 'outlined', |
| 56 | + icon: 'mdi:mdi-cloud-off-outline', |
| 57 | + iconColor: 'warning', |
| 58 | + label: t('trans.offlineSubmission.simulatingBadge'), |
| 59 | + dataTest: 'simulatingOfflineBadge', |
| 60 | + }; |
| 61 | + } |
| 62 | + return { |
| 63 | + color: 'white', |
| 64 | + variant: 'outlined', |
| 65 | + icon: 'mdi:mdi-cloud-check-outline', |
| 66 | + iconColor: '#00e676', |
| 67 | + label: t('trans.offlineSubmission.onlineBadge'), |
| 68 | + dataTest: 'onlineBadge', |
| 69 | + }; |
| 70 | +}); |
| 71 | +
|
| 72 | +// External toggle icon shows the state you'll switch TO, not the current one. |
| 73 | +const toggleIcon = computed(() => |
| 74 | + simulatingOffline.value |
| 75 | + ? 'mdi:mdi-cloud-check-outline' |
| 76 | + : 'mdi:mdi-cloud-off-outline' |
| 77 | +); |
| 78 | +const toggleTooltip = computed(() => |
| 79 | + simulatingOffline.value |
| 80 | + ? t('trans.offlineSubmission.goOnline') |
| 81 | + : t('trans.offlineSubmission.goOffline') |
| 82 | +); |
| 83 | +
|
| 84 | +function openQueue() { |
| 85 | + showPending.value = true; |
| 86 | +} |
| 87 | +
|
| 88 | +function toggleSimulate() { |
| 89 | + simulatingOffline.value = !simulatingOffline.value; |
| 90 | +} |
| 91 | +</script> |
| 92 | + |
| 93 | +<template> |
| 94 | + <div v-if="visible" class="offline-control d-flex align-center"> |
| 95 | + <v-tooltip v-if="showChevron" location="bottom" :text="toggleTooltip"> |
| 96 | + <template #activator="{ props: toggleProps }"> |
| 97 | + <v-btn |
| 98 | + color="white" |
| 99 | + variant="text" |
| 100 | + class="offline-toggle-btn" |
| 101 | + data-test="simulateOfflineToggle" |
| 102 | + v-bind="toggleProps" |
| 103 | + @click="toggleSimulate" |
| 104 | + > |
| 105 | + <v-icon :icon="toggleIcon" size="28" /> |
| 106 | + </v-btn> |
| 107 | + </template> |
| 108 | + </v-tooltip> |
| 109 | + <v-tooltip |
| 110 | + location="bottom" |
| 111 | + :text=" |
| 112 | + t('trans.offlineSubmission.headerButtonTooltip', queuedCount, { |
| 113 | + count: queuedCount, |
| 114 | + }) |
| 115 | + " |
| 116 | + :open-delay="400" |
| 117 | + > |
| 118 | + <template #activator="{ props: tipProps }"> |
| 119 | + <v-badge |
| 120 | + :model-value="queuedCount > 0" |
| 121 | + color="warning" |
| 122 | + location="top end" |
| 123 | + offset-x="0" |
| 124 | + offset-y="0" |
| 125 | + data-test="offlineSubmissionsBadge" |
| 126 | + > |
| 127 | + <template #badge> |
| 128 | + <v-tooltip |
| 129 | + location="bottom" |
| 130 | + :text=" |
| 131 | + t('trans.offlineSubmission.headerButtonTooltip', queuedCount, { |
| 132 | + count: queuedCount, |
| 133 | + }) |
| 134 | + " |
| 135 | + > |
| 136 | + <template #activator="{ props: badgeTipProps }"> |
| 137 | + <span v-bind="badgeTipProps" @click="openQueue">{{ |
| 138 | + queuedCount |
| 139 | + }}</span> |
| 140 | + </template> |
| 141 | + </v-tooltip> |
| 142 | + </template> |
| 143 | + <v-btn |
| 144 | + :color="state.color" |
| 145 | + :variant="state.variant" |
| 146 | + :data-test="state.dataTest" |
| 147 | + class="offline-status-btn" |
| 148 | + v-bind="tipProps" |
| 149 | + @click="openQueue" |
| 150 | + > |
| 151 | + <template #prepend> |
| 152 | + <v-icon |
| 153 | + :color="state.iconColor" |
| 154 | + :icon="state.icon" |
| 155 | + size="28" |
| 156 | + class="offline-status-icon" |
| 157 | + /> |
| 158 | + </template> |
| 159 | + <span :lang="locale" class="offline-status-label">{{ |
| 160 | + state.label |
| 161 | + }}</span> |
| 162 | + </v-btn> |
| 163 | + </v-badge> |
| 164 | + </template> |
| 165 | + </v-tooltip> |
| 166 | + <PendingSubmissionsModal v-model="showPending" /> |
| 167 | + </div> |
| 168 | +</template> |
| 169 | + |
| 170 | +<style scoped lang="scss"> |
| 171 | +.offline-status-btn { |
| 172 | + height: 40px !important; |
| 173 | + width: 120px; |
| 174 | + min-width: 120px; |
| 175 | + padding-inline: 12px !important; |
| 176 | + letter-spacing: 0; |
| 177 | +
|
| 178 | + @media (max-width: 599px) { |
| 179 | + min-width: 40px; |
| 180 | + padding-inline: 8px !important; |
| 181 | +
|
| 182 | + .offline-status-label { |
| 183 | + display: none; |
| 184 | + } |
| 185 | + } |
| 186 | +} |
| 187 | +
|
| 188 | +.offline-toggle-btn { |
| 189 | + height: 32px !important; |
| 190 | + width: 32px !important; |
| 191 | + min-width: 32px !important; |
| 192 | + padding: 0 !important; |
| 193 | + margin-inline-end: -4px; |
| 194 | +} |
| 195 | +
|
| 196 | +:deep(.v-badge__badge) { |
| 197 | + cursor: pointer; |
| 198 | +} |
| 199 | +</style> |
0 commit comments