/
sample2.html
168 lines (155 loc) · 3.63 KB
/
sample2.html
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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Dux Sample</title>
<style>
.container {
width: 60%;
margin: 0 auto;
text-align: center;
}
.container img {
max-width: 128px;
}
#inc,
#dec
{
width: 20%;
padding: 0.5em;
margin: 0.5em auto;
background: linear-gradient(#f9d114, #d7ad1a);
cursor: pointer;
box-shadow: 0px 0px 1px 1px #e2a100;
user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
#inc:hover {
box-shadow: 0px 0px 1px 1px #7b5802;
}
#block {
width: 128px;
height: 128px;
margin: 1em auto;
line-height: 128px;
color: white;
user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
.block {
width: 32px;
height: 32px;
line-height: 32px;
display: inline-block;
user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
margin: 1px;
}
</style>
</head>
<body>
<div class=container>
<h1>Dux</h1>
<div>
<img src="./dux.jpg"></img>
</div>
<div id=inc>inc</div>
<div id=dec>dec</div>
<div id=counter>0</div>
<div id=block class=block>click me too</div>
<div id=blocks>
</div>
</div>
<script src="./dux.js"></script>
<script>
const store = dux.createStore(reducer, {
counter: 0,
colors: [
{ color: "blue" },
{ color: "green" },
{ color: "red" },
],
current: 0,
blocks: []
})
store.react(() => console.log(store.get()))
let inc = document.getElementById('inc')
inc.onclick = () => store.event('inc/clicked')
let dec = document.getElementById('dec')
dec.onclick = () => store.event('dec/clicked')
let counterDisp = document.getElementById('counter')
store.react('counter', counter => {
counterDisp.innerText = counter
})
let block = document.getElementById('block')
block.className = 'block'
store.react('current', current => {
block.style.backgroundColor = getCurrentColor(store)
})
block.onclick = () => store.event('block/clicked')
let blockDisp = document.getElementById('blocks')
store.react('counter', counter => {
if(counter < 0) return
let blocks = store.get('blocks')
if(blocks.length == counter) return
blocks = blocks.slice()
if(blocks.length > counter) {
while(counter < blocks.length) {
let { block, fn } = blocks.pop()
store.unreact(fn)
blockDisp.removeChild(block)
}
} else {
while(blocks.length < counter) {
let block = document.createElement('span')
block.className = 'block'
blockDisp.appendChild(block)
let b = { block, color: getCurrentColor(store) }
blocks.push(b)
b.fn = store.react(`blocks.${blocks.length-1}`, b => {
if(!b) return
b.block.style.backgroundColor = b.color
})
}
}
store.event('blocks/updated', blocks)
})
function getCurrentColor(store) {
let colors = store.get('colors')
let current = store.get('current')
return colors[current % colors.length].color
}
function reducer(state, type, payload) {
return {
colors: state.colors,
counter: reduceCounter(state.counter, type, payload),
current: reduceCurrent(state.current, type, payload),
blocks: reduceBlocks(state.blocks, type, payload),
}
}
function reduceCounter(counter, type, payload) {
switch(type) {
case 'inc/clicked': return counter + 1
case 'dec/clicked': return counter - 1
case 'devtools/counter/set': return payload
default: return counter
}
}
function reduceCurrent(current, type, payload) {
switch(type) {
case 'block/clicked': return current + 1
default: return current
}
}
function reduceBlocks(blocks, type, payload) {
switch(type) {
case 'blocks/updated': return payload
default: return blocks
}
}
</script>
</body>
</html>