forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgithub-explorer.html
409 lines (376 loc) · 13.7 KB
/
github-explorer.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://ogp.me/ns#" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<meta charset="UTF-8">
<title>MakeCode GitHub Explorer</title>
<link rel="stylesheet"
href="https://cdn.makecode.com/blob/2163189fd5e35c0981ed55318415582a7c9aeb12/doccdn/semantic.css"
type="text/css">
<style>
@targetstyle@ label,
nav.ui.menu {
font-size: 0.8rem !important;
}
#signin {
margin-left: calc(50% - 10rem);
margin-top: 10rem;
width: 20rem;
text-align: center;
margin-right: calc(50% - 10rem);
}
#user {
width: 14rem;
}
#repoes {
overflow-y: auto;
}
#repocolumn {
padding: 0.5rem;
position: absolute;
width: 18rem;
height: calc(100% - 5.3rem);
border: none;
left: 0;
right: calc(100% - 18rem);
bottom: 3rem;
top: 2.3rem;
overflow-x: hidden;
overflow-y: auto;
}
#makecodecolumn {
position: absolute;
width: calc(100% - 18rem);
height: calc(100% - 2.3rem);
border: none;
left: 18rem;
right: 0;
bottom: 0rem;
top: 2.3rem;
}
#footer {
position: absolute !important;
left: 0;
right: 0;
width: 18rem;
bottom: 0 !important;
font-size: 0.7rem;
margin: 0.5rem;
}
#footer a {
color: black;
}
.ui.selection.list>.item.active {
border: solid 2px grey;
}
.ui.selection.list>.item>.content {
width:18rem;
}
.ui.selection.list>.item:not(.active) .extra {
display: none;
}
</style>
<script type="text/javascript"
src="https://cdn.makecode.com/blob/4d9b3a258759c53e7bc66b6fc554c51e2434437c/doccdn/jquery.js"></script>
<script type="text/javascript"
src="https://cdn.makecode.com/blob/fc2f56f46189a2e8be3743a1a5abed3b676f9318/doccdn/semantic.js"></script>
<script>
(async function () {
const api = "https://api.github.com";
const targets = await fetchJSON("/editors.json");
let token = localStorage["core/githubtoken"];
async function fetchJSON(url) {
const resp = await fetch(url);
const json = await resp.json();
return json;
}
function trackClick(id) {
pxt.setInteractiveConsent(true);
if (typeof pxt !== "undefined")
pxt.aiTrackEvent(id);
}
async function fetchGraphQL(query) {
const response = await fetch("https://api.github.com/graphql", {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
mode: 'cors', // no-cors, *cors, same-origin
headers: {
"Authorization": "bearer " + token
},
body: JSON.stringify({ query: query })
});
if (response.status == 401) {
console.log('token expired...')
navigateSignout();
}
return response;
}
function sniffTarget(repo) {
try {
const pxtJson = JSON.parse(repo.object.text);
return pxtJson.supportedTargets && pxtJson.supportedTargets[0];
} catch(e) {
return undefined;
}
}
async function fetchUserRepos(user) {
const query = `
{
user(login:"${user}") {
repositories(first: 100, orderBy: {field: UPDATED_AT, direction: DESC}) {
nodes {
name
owner {
login
}
nameWithOwner
object(expression: "master:pxt.json") {
... on Blob {
text
}
}
}
}
}
}
`;
const response = await fetchGraphQL(query);
const responseJSON = await response.json();
const items = responseJSON.data.user.repositories.nodes
.filter(node => node.object);
for (let i = 0; i < items.length; ++i) {
const target = sniffTarget(items[i]);
if (!target)
items[i] = undefined;
else
items[i].target = target;
}
return items.filter(m => !!m);
}
async function fetchUser(user) {
if (!token) return undefined;
const response = await fetchGraphQL(`{
user(login:"${user}") {
name
login
avatarUrl
}
}`);
if (response.status != 200)
return undefined;
const msg = await response.json();
return msg && msg.data && msg.data.user;
}
async function fetchCurrentUser() {
if (!token) return undefined;
const response = await fetchGraphQL(`{
viewer {
login
}
}`);
const msg = await response.json();
return msg && msg.data && msg.data.viewer && msg.data.viewer.login;
}
async function update(userName) {
$('#userparent').addClass('loading');
try {
$("#repoes")
.empty();
$("#makecodecolumn").attr("src", "");
const user = await fetchUser(userName);
if (!user) {
$('#repoes').append(`<div class="ui item">
user not found
</div>`);
return;
}
const userEl = $(`
<a href="" target="_blank" class="ui link item" title="Open user profile">
<div class="ui mini image">
<img src="">
</div>
<div class="content">
<div class="header"></div>
<div class="description"></div>
</div>
</a>`);
userEl.attr("href", `https://github.com/${user.login}`);
userEl.find("img").attr('src', user.avatarUrl);
userEl.find(".header").text(user.name);
userEl.find(".description").text(user.login);
$('#repoes')
.append(userEl)
const repoes = await fetchUserRepos(userName);
$("#repoes")
.append(`<div class="ui divider"></div>`)
.append(
repoes.map(repo => {
const target = targets[repo.target];
const icon = target.icon || (target.url.replace(/\/\w+$/, '') + "/favicon.ico");
const item = $(
`<div class="ui link item">
<div class="ui mini image">
<img src="">
</div>
<div class="content">
<div class="header"></div>
<div class="description"></a></div>
<div class="extra"><button class="ui right floated button icon" title="Open repository in GitHub.com"><i class="icon external"></i></button></div>
</div>
</div>`);
item.find("img").attr("src", icon);
item.find(".header").text(repo.name);
item.find(".description").text(repo.owner.login);
item.find(".extra>button").click(event => {
event.stopPropagation();
window.open(targets[repo.target].url, "_blank")
});
item.click(event => {
trackClick("github.explorer.open")
$('#repoes').children().removeClass('active');
item.addClass('active');
const readOnly = !$('#edit').is(':checked');
const url = targets[repo.target].url +
`?nocookiebanner=1&${readOnly ? 'controller=1&readonly=1&ws=mem&' : 'editorLayout=ide&nosandbox=1'}#pub:github:`
+ repo.nameWithOwner + "#master";
$("#makecodecolumn").attr("src", url);
});
return item;
}))
} finally {
$('#userparent').removeClass('loading');
}
}
function checkToken() {
// sniff oauth
let keys = {};
window.location.hash.replace(/^#/, '').split('&')
.map(v => v.split('=', 2))
.forEach(a => keys[a[0]] = a[1]);
if (keys["access_token"]) {
if (keys["state"] == localStorage["core/oauthState"]) {
token = keys["access_token"]
if (keys["state"] == localStorage["core/oauthRememberState"])
localStorage["core/githubtoken"] = token;
}
delete localStorage["core/oauthState"];
delete localStorage["core/oauthRememberState"]
window.location.hash = "";
}
// force sign...
if (!token) {
$("#signin").show();
$('#signout').hide();
$('#userparent').hide();
$('#repocolumn').hide();
$('#makecodecolumn').hide();
} else {
$("#signin").hide();
$('#signout').show();
$('#userparent').show();
$('#repocolumn').show();
$('#makecodecolumn').show();
}
}
function navigateSignin() {
trackClick("github.explorer.signin")
const state = Math.random().toString();
localStorage["core/oauthState"] = state;
if (document.getElementById('remembermeinput').checked)
localStorage["core/oauthRememberState"] = state;
const login = "https://makecode.com/oauth/login?state=" + state +
"&response_type=token&client_id=gh-token&redirect_uri=" +
encodeURIComponent(window.location.href.split('#', 1)[0])
window.location.href = login;
}
function navigateSignout() {
trackClick("github.explorer.signout")
delete localStorage["core/oauthState"];
delete localStorage["core/githubtoken"];
window.location.reload();
}
$(function () {
console.log(`loaded...`);
checkToken();
$('#signinbtn').click(navigateSignin);
$('#signout').click(navigateSignout);
$("#user").on("keydown", function search(e) {
if (e.keyCode == 13) {
trackClick("github.explorer.search")
const userName = $("#user").val();
update(userName);
}
});
$('#edit').change(function () {
trackClick("github.explorer.edit")
const userName = $("#user").val();
update(userName);
})
console.log("starting...");
if (typeof pxt !== "undefined")
pxt.aiTrackEvent("github.explorer.loaded")
fetchCurrentUser()
.then(user => {
if (user) {
$("#user").val(user);
update(user);
}
});
});
})();
</script>
</head>
<body id='root' class='root'>
<nav class="ui menu fixed borderless">
<div class="menu left">
<div class="ui item">
MakeCode GitHub Explorer
</div>
</div>
<div class="menu right">
<!--
<div class="ui item">
<div class="ui toggle checkbox">
<input type="checkbox" name="edit" id="edit">
<label id="editlabel">Edit</label>
</div>
</div>-->
<a href="https://forum.makecode.com" target="_blank" class="ui item">
Forum
</a>
<a href="/github/explorer" target="_blank" class="ui item">
Docs
</a>
<div id="signout" class="ui link item">
Sign out
</div>
</div>
</nav>
<div id="repocolumn">
<div id="userparent" class="ui left icon input">
<input id="user" class="ui fluid" type="text" placeholder="Enter GitHub user" />
<i class="search icon"></i>
</div>
<div id="repoes" class="ui selection aligned list"></div>
</div>
<iframe id="makecodecolumn"></iframe>
<div id="signin">
<p>
Sign in to access your MakeCode repositories.
</p>
<button id="signinbtn" class="ui icon basic button">
<i class="ui github icon"></i>
Sign in
</button>
<input type="checkbox" id="remembermeinput" />
<label for="remembermeinput">Remember me</label>
</div>
<footer id="footer" class="hideprint">
<a class="item" href="https://makecode.com/privacy" target="_blank" rel="noopener">Privacy & Cookies</a>
<a class="item" href="https://makecode.com/termsofuse" target="_blank" rel="noopener"> Terms Of Use</a>
<a class="item" href="https://makecode.com/trademarks" target="_blank" rel="noopener">Trademarks</a>
<div class="item">© 2022 Microsoft</div>
</footer>
<script type="text/javascript"
src="https://cdn.makecode.com/blob/795d8506c80a04f5ca26f577a8d6152e2fa3e7a6/doccdn/pxtweb.js"></script>
<!-- @include tracking.html -->
</body>
</html>