forked from StephanHoyer/how-to-mithril
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
219 lines (205 loc) · 4.54 KB
/
index.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
const baseUrl = document.location.pathname
let examples = []
const KEY_CODE_ESC = 27
let currentFilter = {}
function parseParams(filterString = '') {
return filterString.split('/').reduce((params, filter) => {
const [key, value] = filter.split('=')
params[key] = value
return params
}, {})
}
function getRouteByFilter(filter) {
return (
(filter.tags.length ? `/tags=${filter.tags.join(',')}` : '') +
(filter.q ? `/q=${filter.q}` : '') +
(filter.version ? `/version=${filter.version}` : '') +
(filter.author ? `/author=${filter.author}` : '')
)
}
function tagView(tag, filterUpdates) {
return m('li.tag', linkView(filterUpdates, tag))
}
function linkView(filterUpdates, children) {
return m(
'a',
{
className: filterUpdates.className || '',
href: getRouteByFilter(Object.assign({}, currentFilter, filterUpdates)),
oncreate: m.route.link,
},
children
)
}
function currentFilterView() {
return [
m('input.q[autofocus]', {
oncreate: ({ dom }) => setTimeout(dom.focus),
value: currentFilter.q,
onkeydown: e => {
if (e.keyCode === KEY_CODE_ESC) {
currentFilter.q = ''
m.route.set(getRouteByFilter(currentFilter))
}
},
oninput: e => {
currentFilter.q = event.target.value
m.route.set(getRouteByFilter(currentFilter))
},
}),
currentFilter.q &&
linkView(
{
className: 'clear',
q: '',
},
'×'
),
m(
'ul.tags.currentTags.tags',
currentFilter.tags.map(tag =>
tagView(tag, {
tags: currentFilter.tags.filter(t => tag !== t),
})
)
),
currentFilter.version &&
linkView(
{
className: 'version current',
version: '',
},
currentFilter.version
),
currentFilter.author &&
linkView(
{
className: 'author current',
author: '',
},
currentFilter.author
),
]
}
function exampleTagsView(example) {
return m(
'ul.tags',
example.tags.map(tag =>
tagView(tag, {
tags: currentFilter.tags.includes(tag)
? currentFilter.tags
: currentFilter.tags.concat(tag),
})
)
)
}
function versionView(example) {
return linkView(
{
className: 'version',
version: example.mithrilVersion,
},
example.mithrilVersion
)
}
function versionView(example) {
return linkView(
{
className: 'version',
version: example.mithrilVersion,
},
example.mithrilVersion
)
}
function authorView(example) {
return linkView(
{
className: 'author',
author: example.author,
},
example.author
)
}
function matchesFilter(example) {
if (
currentFilter.tags.length &&
currentFilter.tags.some(tag => !example.tags.includes(tag))
) {
return false
}
if (
currentFilter.q &&
example.tags
.concat([example.name, example.description])
.join('')
.indexOf(currentFilter.q) < 0
) {
return false
}
if (
currentFilter.version &&
example.mithrilVersion !== currentFilter.version
) {
return false
}
if (currentFilter.author && example.author !== currentFilter.author) {
return false
}
return true
}
function examplesView(examples) {
return m(
'ul.examples',
examples.filter(example => matchesFilter(example)).map(example =>
m(
'li.example',
{
title: example.description,
},
[
m(
'a.name',
{
href: example.link,
},
example.name
),
exampleTagsView(example),
versionView(example),
authorView(example),
]
)
)
)
}
function contributeView() {
return m('.contribute', [
'Something is missing? Add your own ',
m('a[href=https://flems.io/mithril]', 'flems'),
' ',
m(
'a[href=https://github.com/StephanHoyer/how-to-mithril/blob/master/examples.json]',
'here'
),
'.',
])
}
const app = {
oninit: async function() {
examples = await m.request(`${baseUrl}examples.json`)
},
view: ({ attrs }) => {
const params = parseParams(attrs.filter)
currentFilter = {
tags: params.tags ? params.tags.split(',') : [],
q: params.q || '',
version: params.version || '',
author: params.author || '',
}
return [currentFilterView(), examplesView(examples), contributeView()]
},
}
m.route(document.body, '/', {
'/': app,
'/:filter...': app,
})