Skip to content

Commit 31dda22

Browse files
committed
update event listeners and prevent accidental navigation
1 parent e9687a2 commit 31dda22

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

src/views/Score.vue

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,33 @@
2626

2727
<script lang="ts" setup>
2828
import { computed, watch, onMounted, onUnmounted, ref } from 'vue'
29-
import { useRoute } from 'vue-router'
30-
import { useWakeLock } from '@vueuse/core'
29+
import { onBeforeRouteLeave, useRoute } from 'vue-router'
30+
import { useWakeLock, useEventListener } from '@vueuse/core'
3131
import models from '../models'
3232
import ScoreNavigation from '../components/ScoreNavigation.vue'
3333
import BatteryStatus from '../components/BatteryStatus.vue'
3434
import { useScoresheet } from '../hooks/scoresheet'
3535
36-
function preventDefualt (event: TouchEvent) {
36+
function preventDefault (event: TouchEvent) {
3737
event.preventDefault()
3838
}
3939
4040
const route = useRoute()
4141
const scsh = useScoresheet()
4242
const wakeLock = useWakeLock()
43+
const touchCleanup = ref<ReturnType<typeof useEventListener>>()
4344
4445
const currentStep = ref<string>()
4546
4647
onMounted(async () => {
47-
document.body.addEventListener('touchmove', preventDefualt, { passive: false })
48+
touchCleanup.value = useEventListener('touchmove', preventDefault, { passive: false })
4849
await wakeLock.request('screen')
49-
await scsh.open(route.params.system as string, ...route.params.vendor)
50+
await scsh.open(route.params.system as string, ...route.params.vendor as string[])
5051
})
5152
5253
onUnmounted(async () => {
53-
await scsh.close()
54+
await scsh.close(false)
5455
await wakeLock.release()
55-
document.body.removeEventListener('touchmove', preventDefualt)
5656
})
5757
5858
watch(() => route.params, async (next, prev) => {
@@ -65,8 +65,8 @@ watch(() => route.params, async (next, prev) => {
6565
return
6666
}
6767
if (next.system && next.vendor) {
68-
await scsh.close()
69-
await scsh.open(next.system as string, ...next.vendor)
68+
await scsh.close(false)
69+
await scsh.open(next.system as string, ...next.vendor as string[])
7070
}
7171
})
7272
@@ -78,13 +78,30 @@ const model = computed(() => {
7878
))
7979
if (!model) return null
8080
81+
return model
82+
})
83+
84+
watch(model, model => {
85+
if (model == null) return
86+
8187
if (model.allowScroll) {
82-
document.body.removeEventListener('touchmove', preventDefualt)
88+
touchCleanup.value?.()
8389
} else {
84-
document.body.addEventListener('touchmove', preventDefualt, { passive: false })
90+
touchCleanup.value = useEventListener('touchmove', preventDefault, { passive: false })
8591
}
92+
})
8693
87-
return model
94+
useEventListener('beforeunload', event => {
95+
event.preventDefault()
96+
// eslint-disable-next-line @typescript-eslint/no-deprecated
97+
event.returnValue = 'Do you really want to leave, the scoresheet will not be saved.'
98+
})
99+
100+
onBeforeRouteLeave(() => {
101+
if (scsh.scoresheet.value) {
102+
const confirm = window.confirm('Do you really want to leave, the scoresheet will not be saved.')
103+
if (!confirm) return false
104+
}
88105
})
89106
90107
watch(model, (newModel, oldModel) => {

0 commit comments

Comments
 (0)