-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstopWatchView.js
192 lines (161 loc) · 3.76 KB
/
stopWatchView.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const choo = require('choo')
const html = require('choo/html')
const css = require('sheetify')
const key = require('keymaster')
const prefix = css`
:host {
position: relative;
}
.time {
flex: none;
height: 58px;
display: block;
margin: 0;
padding: 12px 20px 5px;
font-weight: 200;
font-size: 48px;
line-height: 1;
background: rgba(255, 255, 255, 0.1);
color: rgba(255, 255, 255, 0.9);
border-radius: 8px;
position: relative;
overflow: hidden;
}
.indicator {
width: 12px;
height: 12px;
border-radius: 50%;
background: #FF4545;
position: absolute;
top: 50%;
margin-top: -6px;
left: -24px;
}
.progress {
height: 2px;
position: absolute;
bottom: 0;
left: 0;
background: #FF4545;
transition: width 0.5s;
}
`
const id = 'the-stop-watch'
const app = choo()
if (process.env.NODE_ENV !== 'production') {
app.use(require('choo-devtools')())
}
app.use(store)
app.route('*', view)
const tree = app.start()
function view (state, emit) {
return html`
<div class=${prefix}>
${indicatorView(state)}
${timeView(state)}
</div>
`
}
function indicatorView (state) {
if (state.startedAt) {
return html`
<span class="indicator"></span>
`
}
}
function timeView (state) {
return html`
<p class="time">
${state.currentElapsedTime}
${progressView(state)}
</p>
`
}
function progressView (state) {
if (state.startedAt) {
const style = `width: ${state.progress}`
return html`
<span class="progress" style=${style}></span>
`
}
}
function now () {
return (new Date()).getTime()
}
function store (state, bus) {
state.startedAt = undefined
state.previousElapsedSeconds = 0
state.currentElapsedTime = '00:00'
state.progress = '0%'
function emit (...args) { bus.emit(...args) }
function render () { emit('render') }
bus.on('stopwatch:update', render)
bus.on('stopwatch:toggle', toggle)
bus.on('stopwatch:start', start)
bus.on('stopwatch:pause', pause)
bus.on('stopwatch:reset', reset)
function toggle () {
if (state.startedAt) {
pause()
} else {
start()
}
}
function start () {
state.startedAt = now()
render()
}
function pause () {
state.previousElapsedSeconds = state.previousElapsedSeconds + elapsedSeconds()
state.startedAt = undefined
state.currentElapsedTime = elapsedTime()
render()
}
function reset () {
state.previousElapsedSeconds = 0
if (state.startedAt) { state.startedAt = now() }
state.currentElapsedTime = '00:00'
render()
}
function elapsedSeconds () {
if (state.startedAt) {
return Math.floor((now() - state.startedAt) / 1000.0)
} else {
return 0
}
}
function totalSeconds () {
return state.previousElapsedSeconds + elapsedSeconds()
}
function progress () {
const ratio = (totalSeconds() % 60.0) / 60.0
const percentage = Math.floor(ratio * 100.0) + 1
return `${percentage}%`
}
function elapsedTime () {
let totalMinutes = Math.floor(totalSeconds() / 60.0)
let totalHours = Math.floor(totalMinutes / 60.0)
totalMinutes = totalMinutes - (totalHours * 60)
if (totalMinutes < 10) {
totalMinutes = `0${totalMinutes}`
}
if (totalHours < 10) {
totalHours = `0${totalHours}`
}
return `${totalHours}:${totalMinutes}`
}
setInterval(() => {
if (state.startedAt) {
state.progress = progress()
state.currentElapsedTime = elapsedTime()
emit('stopwatch:update')
}
}, 1000)
key('⇧+s', 'presenter', e => { emit('stopwatch:toggle') })
key('⇧+r', 'presenter', e => { emit('stopwatch:reset') })
}
module.exports = () => {
const el = html`<div id=${id}></div>`
el.appendChild(tree)
return el
}