-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
229 lines (202 loc) · 6.24 KB
/
main.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
220
221
222
223
224
225
226
227
228
229
import { app } from 'https://unpkg.com/hyperapp'
import html from 'https://unpkg.com/hyperlit'
import * as router from './lib/io/router.js'
import {getText} from './lib/io/http.js'
import makeRunnerInstance from './lib/io/runner.js'
import higlighter from './lib/io/syntax-higlighter.js'
const examples = {
'#counter': 'Counter',
'#temperature': 'Temperature',
'#flight': 'Flights',
'#timer': 'Timer',
'#crud': 'CRUD',
'#circles': 'Circles',
'#cells': 'Cells',
}
const initialState = {
info: null,
code: null,
route: null,
path: null,
loading: null,
}
const maybeDoneLoading = (state) => {
if (state.nextCode && state.nextInfo && state.nextRunning) {
return [
{
...state,
loading: null,
code: state.nextCode,
info: state.nextInfo,
},
higlighter(),
]
}
return state
}
const AppWillStart = (state) => ({ ...state, path: state.loading })
const AppStarted = (state) => maybeDoneLoading({ ...state, nextRunning: true })
const runner = makeRunnerInstance({
selector: 'body > main > main > *:first-child',
onWillStart: AppWillStart,
onStarted: AppStarted,
})
const SetRoute = (state, route) => {
state = { ...state, route }
if (!examples[route])
return [{ ...state, path: null, info: null, code: null }, runner(null)]
let path = './examples/' + route.replace('#', '') + '/'
return [
{
...state,
nextRunning: false,
nextInfo: null,
nextCode: null,
loading: path,
},
runner(path + 'app.js'),
getText(path + 'info.html', GotInfo),
getText(path + 'app.js', GotCode ),
]
}
const GotInfo = (state, info) => maybeDoneLoading({ ...state, nextInfo: info })
const GotCode = (state, code) => maybeDoneLoading({ ...state, nextCode: code })
const navLink = ({ current, route, ...props }, content) =>
html`
<a ${props} href=${route} class=${{ current: current === route }}>
${content}
</a>
`
const navLogo = ({ current }) => html`
<${navLink} route="#" id="logo">
<h1>
<span>Seven GUIs</span>
<br />
<span>with Hyperapp</span>
</h1>
<//>
`
const navGithubLink = () => html`
<a id="githublink" href="https://github.com/zaceno/sevenguis-hyperapp">
<img alt="GitHub repository" src="./GitHub-Mark-Light-32px.png" />
</a>
`
const navExamplesItem = (props) => html`
<li>
<${navLink} ${props}>${examples[props.route]}<//>
</li>
`
const navExamples = ({ current }) => html`
<ul>
${Object.keys(examples).map((route) =>
navExamplesItem({ route, current })
)}
</ul>
`
const navigation = (state) => [
navLogo({ current: state.route }),
navExamples({ current: state.route }),
navGithubLink(),
]
const mainInfo = () => html`
<h1 id="mainTitle">
<span>Seven GUIs</span>
<br />
<span>with Hyperapp</span>
</h1>
<p>
<a href="https://eugenkiss.github.io/7guis/">7GUIs</a>
is a series of seven gui-applications, each posing unique challenges to
front-end/GUI development. The goal of their project is to catalog
implementations of the seven tasks in a large variety of frameworks, in
order to showcase how those challenges can be solved with that
particular framework.
</p>
<p>
This is an implementation of the seven tasks using
<a href="https://github.com/jorgebucaran/hyperapp">Hyperapp</a>
– a javascript front-end micro-framework featuring a fast vdom
implementation, built in state management and declarative subscriptions.
Inspired by Elm, it is heavily slanted toward the functional and
declarative paradigm.
</p>
<p>
For each of the examples I present the code along with the running demo.
Code that isn't relevant to explaining the example has been broken out
in a separate import. Where I can, I have used third-party libraries,
but most of the imports are my own. If their implementation interests
you, find them by checking out the
<code>lib/</code>
folder in the
<a href="https://github.com/zaceno/sevenguis-hyperapp">
Github repository
</a>
for this project.
</p>
<p>
I have taken the libery of using
<a href="https://github.com/zaceno/hyperlit">hyperlit</a>
to define the views, rather than Hyperapp's built in
<code>h()</code>
and
<code>text()</code>
, or
<a href="https://github.com/jorgebucaran/hyperapp/tree/main/pkg/html">
<code>@hyperapp/html</code>
</a>
simply because I feel it makes examples more readable when the views
resemble the html they are meant to produce.
</p>
<p>
And I suppose it goes without saying: this site was itself implemented
using Hyperapp
</p>
`
const exampleRunView = (state) =>
state.path
? html`
<div key=${state.path} />
`
: mainInfo()
const exampleInfo = (state) => state.info && html([state.info])
const exampleCode = (state) =>
state.code &&
html`
<pre key=${state.path} class="language-javascript">
<code>
${state.code}
</code>
</pre>
`
const loadingSpinner = (state) =>
state.loading &&
html`
<div id="spinner-backdrop">
<div id="spinner" />
</div>
`
const appView = (state) => html`
<body>
<nav>
<${navigation} ${state} />
</nav>
<main>
<main>
<${exampleRunView} ${state} />
</main>
<section id="info">
<${exampleInfo} ${state} />
</section>
<${loadingSpinner} ${state} />
</main>
<section id="code">
<${exampleCode} ${state} />
</section>
</body>
`
app({
init: [initialState, router.current(SetRoute)],
subscriptions: () => [router.listen(SetRoute)],
view: appView,
node: document.body,
})