-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chronometer.vue
125 lines (114 loc) · 3.82 KB
/
Chronometer.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<template>
<div class="grid grid-cols-1 gap-2 place-items-center">
<div class="grid grid-cols-1 place-items-center">
<div class="my-5">
<section class="mb-5 flex justify-center content-center text-2xl">
<h5 v-if="!expired">Foco na Missão!</h5>
<h5 v-else>Inicie um novo ciclo!</h5>
</section>
<section class="flex justify-center content-center text-8xl">
<div class="minutes mx-2 relative">
{{displayMinutes}}
<div class="label text-sm botton-0">Minutos</div>
</div>
<span class="text-7xl mt-2">:</span>
<div class="seconds mx-2 relative">
{{displaySeconds}}
<div class="label text-sm botton-0">Segundos</div>
</div>
</section>
</div>
<div class="my-5 w-full">
<section>
<aside class="mb-2 flex justify-between">
<button v-on:click="incremmentInterval()" :disabled="!enableButtonIncrementInterval" type="button" class="w-full mr-1 focus:outline-none text-white text-sm py-2.5 px-5 rounded-md bg-purple-500 hover:bg-purple-600 hover:shadow-lg">+</button>
<button v-on:click="decreaseInterval()" :disbled="!enableButtonDecreaseInterval" type="button" class="w-full ml-1 focus:outline-none text-white text-sm py-2.5 px-5 rounded-md bg-purple-500 hover:bg-purple-600 hover:shadow-lg">-</button>
</aside>
<button v-if="!running" v-on:click="start()" type="button" class="w-full focus:outline-none text-white text-sm py-2.5 px-5 rounded-md bg-purple-500 hover:bg-purple-600 hover:shadow-lg">Iniciar</button>
<button v-else v-on:click="exit()" type="button" class="w-full focus:outline-none text-white text-sm py-2.5 px-5 rounded-md bg-purple-500 hover:bg-purple-600 hover:shadow-lg">Desistir</button>
</section>
</div>
</div>
</div>
</template>
<script>
export default {
props: ["minute"],
data: () => ({
intervalArray: [15, 25, 30, 40],
currentInterval: 0,
displayMinutes: '00',
displaySeconds: '00',
enableButtonIncrementInterval: true,
enableButtonDecreaseInterval: true,
expired: false,
running: false,
end: 0
}),
computed: {
_seconds: () => 1000,
_minutes() {
return this._seconds * 60
},
},
mounted() {
this.initialDisplayMode()
},
methods: {
formatNum: (num) => (num < 10 ? "0" + num : num),
currentIntervalValue() {
return this.intervalArray[this.currentInterval];
},
initialDisplayMode() {
this.displayMinutes = this.formatNum(this.currentIntervalValue())
this.displaySeconds = "00"
},
incremmentInterval() {
if(this.currentInterval < (this.intervalArray.length - 1)){
this.currentInterval += 1
this.initialDisplayMode()
this.enableButtonDecreaseInterval = true
}
else
this.enableButtonIncrementInterval = false
console.log(this.currentIntervalValue())
},
decreaseInterval(){
if(this.currentInterval > 0){
this.currentInterval -= 1
this.initialDisplayMode()
this.enableButtonIncrementInterval = true
}
else
this.enableButtonDecreaseInterval = false
},
start(){
this.initialDisplayMode()
this.end = new Date().setMinutes(new Date().getMinutes() + this.currentIntervalValue())
this.clockRunning()
},
exit(initialDisplayMode = true) {
this.expired = true
this.running = false
if(initialDisplayMode) this.initialDisplayMode()
},
clockRunning() {
this.expired = false;
this.running = true;
const timer = setInterval(() => {
const now = new Date()
const interval = this.end - now.getTime()
if(interval < 0 || !this.running) {
clearInterval(timer)
this.exit(false)
return
}
const minutes = Math.floor(interval / this._minutes)
const seconds = Math.floor((interval % this._minutes) / this._seconds)
this.displayMinutes = this.formatNum(minutes,minutes)
this.displaySeconds = this.formatNum(seconds, seconds)
}, 1000)
}
},
}
</script>