app/src/components/Timer.vue

172 lines
3.9 KiB
Vue
Raw Normal View History

2021-04-19 18:13:54 +02:00
<template>
<div class="timer">
2021-05-04 20:54:07 +02:00
<div class="end" v-on:click="resetTime()">X</div>
2021-05-05 12:50:35 +02:00
<p v-if="warning == false" v-bind:class="{ danger: danger == true }" class="timeleft reverse">{{ countDown.toString() }}</p>
2021-04-20 13:30:23 +02:00
<p v-if="(warning == true) || (typeof warning == 'undefined')" class="timeout reverse">
2021-04-19 22:46:31 +02:00
<img src="joker.png" alt="joker">
<img src="joker.png" alt="joker">
<img src="joker.png" alt="joker">
<img src="joker.png" alt="joker">
<img src="joker.png" alt="joker">
</p>
2021-04-20 13:30:23 +02:00
<button v-on:click="restartTimer" class="timerBtn"><span class="timerBtn-inner"></span></button>
2021-05-05 12:50:35 +02:00
<p v-if="warning == false" v-bind:class="{ danger: danger == true }" class="timeleft">{{ countDown.toString() }}</p>
2021-04-20 13:30:23 +02:00
<p v-if="warning == true || (typeof warning == 'undefined')" class="timeout">
2021-04-19 22:46:31 +02:00
<img src="joker.png" alt="joker">
<img src="joker.png" alt="joker">
<img src="joker.png" alt="joker">
<img src="joker.png" alt="joker">
<img src="joker.png" alt="joker">
</p>
2021-04-19 18:13:54 +02:00
</div>
</template>
<script>
2021-04-19 22:46:31 +02:00
var Interval;
2021-04-19 18:13:54 +02:00
export default {
name: "Timer",
props: {
time: Number,
2021-05-01 10:29:28 +02:00
playsound: Boolean,
2021-04-20 13:30:23 +02:00
},
data: function() {
return {
countDown: -2,
warning: true,
2021-05-05 12:50:35 +02:00
danger: false,
2021-04-30 13:14:23 +02:00
wakeLock: null,
2021-05-05 12:50:35 +02:00
sfxTimeout: this.playsound ? new Audio('sfx/timeout.wav') : null,
sfxWarning: this.playsound ? new Audio('sfx/warning.wav') : null,
sfxNext: this.playsound ? new Audio('sfx/next.wav') : null,
2021-04-20 13:30:23 +02:00
}
2021-04-19 18:13:54 +02:00
},
methods: {
2021-05-04 20:54:07 +02:00
resetTime: function() {
this.$emit('set-time', 0);
},
2021-04-19 18:13:54 +02:00
timeleft: function(){
this.countDown--;
2021-05-05 12:50:35 +02:00
switch (this.countDown) {
case 6:
if(this.playsound) {
this.sfxWarning = new Audio('sfx/warning.wav');
}
break;
case 5:
if(this.playsound) {
this.sfxWarning.play();
}
this.danger = true;
break;
case 4:
if(this.playsound) {
this.sfxWarning.play();
}
break;
case 3:
if(this.playsound) {
this.sfxWarning.play();
}
break;
case 2:
if(this.playsound) {
this.sfxWarning.play();
}
break;
case 1:
if(this.playsound) {
this.sfxWarning.play();
}
break;
case 0:
this.warning = true
2021-05-01 10:29:28 +02:00
2021-05-05 12:50:35 +02:00
if(this.playsound) {
this.sfxTimeout.play();
}
clearInterval(Interval);
break;
2021-04-19 22:46:31 +02:00
}
2021-04-19 18:13:54 +02:00
},
restartTimer: function() {
2021-04-30 13:14:23 +02:00
if(this.wakeLock == null && 'wakeLock' in navigator) {
this.wakeLock = navigator.wakeLock.request('screen');
}
2021-05-05 12:50:35 +02:00
if(this.playsound) {
this.sfxNext.play();
}
2021-04-19 18:13:54 +02:00
this.countDown = this.time;
2021-04-19 22:46:31 +02:00
this.warning = false;
2021-05-05 12:50:35 +02:00
this.danger = false;
2021-04-19 22:46:31 +02:00
clearInterval(Interval);
Interval = setInterval(() => {
this.timeleft()
}, 1000);
2021-05-05 12:50:35 +02:00
},
2021-04-19 18:13:54 +02:00
},
}
</script>
2021-04-22 13:39:38 +02:00
<style lang="scss" scoped>
2021-04-19 22:46:31 +02:00
.timer {
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
.timeleft {
2021-05-01 10:29:28 +02:00
font-size: 60px;
padding: 23px 20px 84px 20px;
margin: 10px 0;
color: $black;
background-image: url("~@/img/tile-back.svg");
background-size: cover;
text-align: center;
width: 80px;
height: 60px;
2021-05-05 12:50:35 +02:00
&.danger {
color: $red;
}
2021-04-19 22:46:31 +02:00
}
.timeout {
margin: 40px;
2021-04-22 13:39:38 +02:00
img {
height: 3rem;
}
2021-04-19 22:46:31 +02:00
}
.timerBtn {
appearance: none;
2021-05-01 10:29:28 +02:00
border: 1px solid $white;
2021-04-19 22:46:31 +02:00
border-radius: 3px;
font-size: 3rem;
background: none;
padding: 2rem;
2021-04-22 13:39:38 +02:00
.timerBtn-inner {
display: inline-block;
height: 2rem;
2021-05-03 17:07:15 +02:00
width: 8rem;
2021-05-01 10:29:28 +02:00
background-color: $white;
2021-04-22 13:39:38 +02:00
}
2021-04-20 13:30:23 +02:00
}
2021-04-19 22:46:31 +02:00
.reverse {
transform: scale(-1, -1);
2021-04-19 18:13:54 +02:00
}
2021-04-22 13:39:38 +02:00
2021-05-04 20:54:07 +02:00
.end {
display: block;
position: absolute;
right: 1px;
top: 1px;
font-size: 32px;
}
2021-04-22 13:39:38 +02:00
}
2021-04-19 18:13:54 +02:00
</style>