-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
330 lines (305 loc) · 10.3 KB
/
index.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<!doctype html>
<html lang="en">
<head>
<title>Git App Manager v0.0.0-alpha.7</title>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://unpkg.com/[email protected]/css/font-awesome.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/picnic.min.css">
</head>
<div id="app">
</div>
<style>
section {
padding: 25px;
}
.card {
padding: 0.6em 0.8em;
}
.subheading {
margin-top: -20px;
}
.label {
/* Override the default PicnicCSS style here */
margin-left: 0;
}
#Search__Input {
margin: 0.3em;
margin-left: 0;
}
.Repo__Header {
display: flex;
width: 100%;
justify-content: space-between;
}
.Repo__ProgressBar {
flex-grow: 1;
padding: 0 1em;
}
.Repo__ProgressBar span {
background-color: #0366d6;
border-radius: 0.2em;
display: inline-block;
height: 1em;
vertical-align: middle;
}
@media (min-width: 900px) {
app {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: auto 1fr;
grid-template-areas:
"header header header"
"sidebar topic search";
}
#Header__Section {
grid-area: header;
}
#Installed__Section {
grid-area: sidebar;
}
#Topic__Section {
grid-area: topic;
}
#Search__Section {
grid-area: search;
}
}
</style>
<script src="https://unpkg.com/hyperhtml@latest/min.js"></script>
<script type="module">
import { fs, fsEvents, git, waitForWorker, setupServiceWorker } from 'https://unpkg.com/@wmhilton/[email protected]/dist/client.js'
const store = {
installed_apps: [],
progress: [],
topic: [],
searchresults: [],
resource: window.location && window.location.search && URLSearchParams && (new URLSearchParams(window.location.search)).get('uri').replace('web+gitapp://', '')
}
function update () {
if (store.resource) {
hyperHTML.bind(document.getElementById('app'))`
<app>
<h1>OID ${store.resource}<h1>
</app>`
} else {
hyperHTML.bind(document.getElementById('app'))`
<app>
${renderHeader__Section()}
${renderInstalled__Section()}
${renderSearch__Section()}
${renderTopic__Section()}
${renderGitappLinks__Section()}
</app>`
}
}
function renderHeader__Section () {
const firefoxReadableStreamInstructions = (navigator.userAgent.includes('Firefox') && typeof window.ReadableStream === 'undefined')
? hyperHTML`
<h2>Hello Firefox User!</h2>
<p class="subheading">This webapp uses some cutting edge tech. Go into <a href="about:config">about:config</a> and enable these two settings:
<ul>
<li>dom.streams.enabled</li>
<li>javascript.options.streams prefs.</li>
</ul>
</p>`
: ''
return hyperHTML`
<section id="Header__Section">
<h1>Git App Manager v0.0.0-alpha.7</h1>
<p class="subheading">Install static websites directly from Github. Use them offline. Edit the source code with <a href="https://github.com/wmhilton/nde" target="_blank" rel="noopener">nde</a>. <i><b>Planned features:</b> Fork sites. Control when and if sites auto-update. Rollback an app to any previous version. Install multiple versions of an app. Take advantage of a shared library modules cache. Seed your app with P2P technology of some sort. Facilitate webpush notifications.</i></p>
${firefoxReadableStreamInstructions}
</section>`
}
function renderGitappLinks__Section () {
const available = (self && self.navigator && self.navigator.registerProtocolHandler)
const title = available ? '' : 'Sorry, registerProtocolHandler is not available in this browser.'
return hyperHTML`
<section id="GitappLinks__Section">
<h2>web+gitapp:// links</h2>
<p class="subheading">
I would love the ability to use any git object id (aka SHA) as a URI resource. E.g. <code><img src="git://a69cffdcc2c25a72be08117ad708974a7429effe"></code>.
This doesn't actually do that (yet) but here is a baby step.
</p>
<p>
Click here to register "web+gitapp://" links to open using ${self.location.host}:
<button type="button" disabled=${!available} title=${title} onclick=${registerProtocol}>web+gitapp://</button>
</p>
<p>
Here's an example link you can click <a href="web+gitapp://0fda723910a6952176e73eeb5bbeaece1ef99110">web+gitapp://0fda723910a6952176e73eeb5bbeaece1ef99110</a>
Here's an image but I haven't gotten it to display in any browser yet: <img src="web+gitapp://0fda723910a6952176e73eeb5bbeaece1ef99110" width="96" height="96">
</p>
</section>`
}
function renderInstalled__Section () {
return hyperHTML`
<section id="Installed__Section">
<h2>Installed Git Apps</h2>
<p class="subheading">Apps are just folders. Click on the folder link to open the app.</p>
<div id="Installed__Results">
${store.installed_apps.map(renderInstallApp)}
</div>
</section>
`
function renderInstallApp (x) {
return hyperHTML(x)`
<div class="card">
<div style="float: right;">
<button type="button" class="pseudo" onclick=${e => uninstall(x)}><i class="fa fa-trash" style="color: red"></i></button>
</div>
<p><a href="${x + '/'}">${x}/</a><p>
</div>`
}
}
async function sync () {
let json = await fetch('https://api.github.com/search/repositories?q=topic:gitapp', {
headers: {
'accept': 'application/vnd.github.mercy-preview+json'
}
}).then(res => res.json())
console.log('json =', json)
store.topic = json.items
update()
}
async function renderTopic__Section () {
return hyperHTML`
<section id="Topic__Section">
<h2>Public Directory</h2>
<p class="subheading">Add the <span class="label" style="background: #f1f8ff; color: #0366d6;">gitapp</span> topic to your repo to display it here! <a href="https://help.github.com/articles/classifying-your-repository-with-topics/" target="_blank" rel="noopener">Instructions</a></p>
<div id="Topic__Results">
${store.topic.map(renderRepo)}
</div>
</section>`
}
const renderSearch__Section = () => hyperHTML`
<section id="Search__Section">
<h2>Search on Github</h2>
<p class="subheading">Find and install static sites, such as Github Pages content.</p>
<div style="display: flex">
<input type="text" id="Search__Input" onkeydown=${Search__Input__KeyDown}></input>
<button type="button" onclick=${Search__Button__Click}>Search</button>
</div>
<div id="Search__Results">
${store.searchresults.map(renderRepo)}
</div>
</section>`
const $_ = x => document.getElementById(x)
async function renderRepo(x) {
return hyperHTML(x)`
<div class="card">
<div class="Repo__Header">
<div>
<a href=${`https://github.com/${x.owner.login}`} target="_blank" rel="noopener">${x.owner.login}</a> / <a href=${`https://github.com/${x.owner.login}/${x.name}`} target="_blank" rel="noopener">${x.name}</a>
</div>
<div class="Repo__ProgressBar">
<span style=${`width: ${Math.floor(store.progress[x.owner.login + '/' + x.name] * 100)}%`}></span>
</div>
<div>
<span><i class="fa fa-eye"></i>${x.watchers_count}</span> <span><i class="fa fa-star"></i>${x.stargazers_count}</span> <span><i class="fa fa-code-fork"></i>${x.forks_count}</span>
</div>
</div>
<div style="float: right;">
<button type="button" onclick=${e => install(x.owner.login, x.name, 'master')}>${store.progress[x.owner.login + '/' + x.name] ? 'Installing...' : 'Install'}</button>
</div>
<div>
${x.description}
</div>
</div>`
async function fetchLanguages(x) {
let json = await fetch(x.languages_url, {
headers: {
'accept': 'application/vnd.github.mercy-preview+json'
}
}).then(res => res.json())
let langs = Object.keys(json)
return langs.join(', ')
}
async function fetchTags(repo) {
let json = await fetch(repo.tags_url, {
headers: {
'accept': 'application/vnd.github.mercy-preview+json'
}
}).then(res => res.json())
return json.map(x => hyperHTML.hyper`<a href="" onclick=${e => { e.preventDefault(); install(repo.owner.login, repo.name, x.name)}}>${x.name}</a>, `)
}
}
function Search__Input__KeyDown (event) {
if (event.key === 'Enter') {
Search__Button__Click()
}
}
async function Search__Button__Click () {
let text = $_('Search__Input').value
let json = await fetch('https://api.github.com/search/repositories?in=name&q=' + text, {
headers: {
'accept': 'application/vnd.github.mercy-preview+json'
}
}).then(res => res.json())
store.searchresults = json.items
update()
}
async function install (owner, name, tag) {
let msg = {
type: "clone",
repo: owner + '/' + name,
ref: tag,
name: name
}
navigator.serviceWorker.controller.postMessage(msg)
}
async function uninstall (x) {
let msg = {
type: "delete",
directory: x
}
navigator.serviceWorker.controller.postMessage(msg)
}
async function updateList () {
await waitForWorker()
let files = await fs.readdir('/')
files = files.filter(x => !x.startsWith('.')).filter(x => x !== 'index.html')
store.installed_apps = files
console.log('updated list!', files)
update()
}
// Main
// renderTopic()
update()
sync()
if (!navigator.serviceWorker) {
alert("Your browser doesn't support a feature (navigator.serviceWorker) needed for this app to work. Try using a newer browser.")
} else {
navigator.serviceWorker.addEventListener('message', event => {
let data = event.data
console.log('data =', data)
if (data.type === 'status') {
if (data.status === 'complete') {
updateList()
store.progress[data.regarding.repo] = undefined
update()
} else if (data.status === 'progress') {
store.progress[data.regarding.repo] = data.progress
update()
} else if (data.status === 'error') {
alert('Error! ' + data.error.message)
}
}
})
navigator.serviceWorker
.register('service-worker.js', { scope: '/' })
.then(async (reg) => {
updateList()
})
}
function registerProtocol () {
// Register a custom protocol handler for git
if (self && self.navigator && self.navigator.registerProtocolHandler) {
self.navigator.registerProtocolHandler(
'web+gitapp',
`${self.location.protocol}//${self.location.host}/?uri=%s`,
'Git'
)
} else {
alert('Sorry, registerProtocolHandler is not supported in this browser.')
}
}
</script>