26
26
27
27
<script lang="ts" setup>
28
28
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'
31
31
import models from ' ../models'
32
32
import ScoreNavigation from ' ../components/ScoreNavigation.vue'
33
33
import BatteryStatus from ' ../components/BatteryStatus.vue'
34
34
import { useScoresheet } from ' ../hooks/scoresheet'
35
35
36
- function preventDefualt (event : TouchEvent ) {
36
+ function preventDefault (event : TouchEvent ) {
37
37
event .preventDefault ()
38
38
}
39
39
40
40
const route = useRoute ()
41
41
const scsh = useScoresheet ()
42
42
const wakeLock = useWakeLock ()
43
+ const touchCleanup = ref <ReturnType <typeof useEventListener >>()
43
44
44
45
const currentStep = ref <string >()
45
46
46
47
onMounted (async () => {
47
- document . body . addEventListener (' touchmove' , preventDefualt , { passive: false })
48
+ touchCleanup . value = useEventListener (' touchmove' , preventDefault , { passive: false })
48
49
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 [] )
50
51
})
51
52
52
53
onUnmounted (async () => {
53
- await scsh .close ()
54
+ await scsh .close (false )
54
55
await wakeLock .release ()
55
- document .body .removeEventListener (' touchmove' , preventDefualt )
56
56
})
57
57
58
58
watch (() => route .params , async (next , prev ) => {
@@ -65,8 +65,8 @@ watch(() => route.params, async (next, prev) => {
65
65
return
66
66
}
67
67
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 [] )
70
70
}
71
71
})
72
72
@@ -78,13 +78,30 @@ const model = computed(() => {
78
78
))
79
79
if (! model ) return null
80
80
81
+ return model
82
+ })
83
+
84
+ watch (model , model => {
85
+ if (model == null ) return
86
+
81
87
if (model .allowScroll ) {
82
- document . body . removeEventListener ( ' touchmove ' , preventDefualt )
88
+ touchCleanup . value ?.( )
83
89
} else {
84
- document . body . addEventListener (' touchmove' , preventDefualt , { passive: false })
90
+ touchCleanup . value = useEventListener (' touchmove' , preventDefault , { passive: false })
85
91
}
92
+ })
86
93
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
+ }
88
105
})
89
106
90
107
watch (model , (newModel , oldModel ) => {
0 commit comments