forked from hyperdivision/vhs-tape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
components.js
78 lines (66 loc) · 1.42 KB
/
components.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
const MorphComponent = require('hui/morph')
const html = require('hui/html')
// test components
class Simple extends MorphComponent {
constructor (loadMsg) {
super()
this._loadMsg = loadMsg
this._msg = 'Hello, not mounted yet'
this._count = 0
this.onclick = this.onclick.bind(this)
}
createElement () {
return html`
<div>
${this._msg}
<button onclick=${this.onclick}>Click me</button>
<div class="counter">Counter: ${this._count}</div>
</div>
`
}
onload () {
this._msg = this._loadMsg
this.update()
}
onclick () {
this._count++
this.update()
}
}
class Timer extends MorphComponent {
constructor () {
super()
this._count = 0
this._isCouting = false
this.startStop = this.startStop.bind(this)
this.counter = this.counter.bind(this)
this._interval = null
}
createElement () {
return html`
<div>
<button onclick=${this.startStop}>Click to start timer !</button>
<p id="counter">Count: ${this._count}</p>
</div>
`
}
startStop () {
if (this._isCouting) {
clearInterval(this.counter)
this._isCouting = false
this._interval = null
} else {
this._isCouting = true
this._interval = setInterval(this.counter, 100)
}
}
counter () {
if (!this._isCouting) return
this._count++
this.update()
}
}
module.exports = {
Simple,
Timer
}