-
Notifications
You must be signed in to change notification settings - Fork 4
/
example.html
156 lines (149 loc) · 3.93 KB
/
example.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
<!doctype html>
<link rel="stylesheet" href="example.css">
<todo-app></todo-app>
<script type="module">
import html from './yhtml.js'
const store = {
filter: 'all',
items: Array(10).fill(0).map(i =>
({ title: Math.random().toString(36).slice(2), done: 0 })
),
setFilter(v) {
this.filter = v;
window.dispatchEvent(new Event('state-change'))
},
setItem(i, data) {
Object.assign(this.items[i], data);
window.dispatchEvent(new Event('state-change'))
},
removeItem(i) {
this.items.splice(i, 1)
window.dispatchEvent(new Event('state-change'))
},
getFilteredItems() {
return this.items.filter(i => {
if (this.filter == 'all') return true
if (this.filter == 'completed' && i.done) return true
if (this.filter == 'active' && !i.done) return true
})
},
clearCompleted() {
this.items = this.items.filter(i => !i.done)
window.dispatchEvent(new Event('state-change'))
},
addItem(title) {
store.items.unshift({ title, done: false })
window.dispatchEvent(new Event('state-change'))
}
}
class TodoApp extends HTMLElement {
connectedCallback() {
this.render()
window.addEventListener('state-change', _ => this.render())
}
get $input() {
return this.querySelector('[ref="input"]')
}
addItem(e) {
e.preventDefault()
store.addItem(this.$input.value)
this.$input.focus()
}
render() {
this.innerHTML = html`
<card>
<h1>todos</h1>
<form class="add-item" @submit="addItem">
<input ref="input" name="title" type="text" placeholder="What needs doing?">
</form>
<todo-list></todo-list>
</card>
<footer class="muted">
Double-click to edit a todo
</footer>
`
}
}
class TodoList extends HTMLElement {
connectedCallback() {
this.render()
}
setFilter(e) {
store.setFilter(e.target.dataset.filter)
}
clearCompleted() {
store.clearCompleted();
}
render() {
const items = store.getFilteredItems()
this.innerHTML = html`
<div class="items">
${items.map(i => html`
<todo-item data-index="${store.items.findIndex(item => i == item)}"></todo-item>
`)}
</div>
${store.items.length > 0 && html`
<footer class="flex">
<span>${items.length} items</span>
<span>
${['all', 'active', 'completed'].map(label => html`
<button
class="${store.filter == label && 'selected'}"
@click="setFilter"
data-filter="${label}"
>
${label}
</button>
`)}
</span>
<button class="flat" @click="clearCompleted">Clear completed</button>
</footer>
`}
`
}
}
class TodoItem extends HTMLElement {
connectedCallback() {
this.index = this.getAttribute('data-index')
this.item = store.items[this.index]
this.editing = false
this.render()
}
get $input() {
return this.querySelector('[ref="input"]')
}
edit(e) {
e.preventDefault()
this.editing = true
this.render()
this.querySelector('input[type="text"]').focus()
}
save(e) {
this.editing = false
store.setItem(this.index, { title: this.$input.value })
this.render()
}
remove(e) {
store.removeItem(this.index)
}
done(e) {
store.setItem(this.index, { done: e.target.checked })
}
render() {
this.innerHTML = html`
<input type="checkbox" @click="done" ${this.item.done && 'checked'}>
${this.editing ? html`
<form @submit="save" class="title">
<input ref="input" type="text" @focusout="save" value="${this.item.title}">
</form>
` : html`
<span class="title" @dblclick="edit">${this.item.title}</span>
`}
<button @click="remove">×</button>
`
}
}
customElements.define('todo-app', TodoApp)
customElements.define('todo-list', TodoList)
customElements.define('todo-item', TodoItem)
</script>