-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
388 lines (369 loc) · 12.8 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset='UTF-8'>
<script>
if (window.location.protocol !== 'https:' && window.location.port < 1024) {
// Not secure - Redirect to the secure version.
// Please just comment this out for your own development.
(function() {
var newLocation = (window.location + '').substring(window.location.protocol.length)
window.location.replace('https:' + newLocation)
})()
}
</script>
<link rel="stylesheet" href="css/normalize.css" />
<link rel="stylesheet" href="css/skeleton.css" />
<link rel="stylesheet" href="css/style.css" />
<link rel="stylesheet" href="css/octicons/octicons.css" />
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway:400,300,600" />
<script src='https://code.jquery.com/jquery-1.12.0.min.js'></script>
<script src='lib/index.js'></script>
<script src='lib/other/oauth.js'></script>
<script>
OAuth.initialize('KUhN5ym8PYgEIgAy1hli3zBNN-g')
</script>
<title>Let's Build a Hat Rack</title>
</head>
<body>
<div class="section header-section">
<div class="intro-header">
<div class="container">
<h1 class="section-heading">
Let's build a hat rack<span id="repo-name"></span>
</h1>
</div>
</div>
<div class="slim-icon-bar">
<div class="container">
<ul class='left-menu'>
<li id="search-again">
<button disabled>
<span class="octicon octicon-search"></span>
Build again
</button>
</li>
</ul>
<ul class='right-menu'>
<li id="permalink">
<a href="#">
<span class="octicon octicon-link"></span>
Permalink
</a>
</li>
<li id="signout">
<button disabled>
<span class="octicon octicon-sign-out"></span>
Sign out
</button>
</li>
</ul>
</div>
</div>
</div>
<div class="section">
<div class="container">
<div id='initial-form'>
<form name='auth-and-repo' id='auth-and-repo'>
<div class="row">
<div class="twelve columns">
<label for='repo'>Repository</label>
<input class="u-full-width" name='repo' value='LABHR/octohatrack' type='text' />
</div>
</div>
<div class="row center-row">
<div class="twelve columns">
<button class="button-primary" id='github-oauth'>
<span class="octicon octicon-mark-github"></span>
Login with GitHub
</button>
</div>
</div>
</form>
</div>
<div id='all-contributors' style='display:none'>
<div class='row'>
<h1><span id='contributor-count'></span>Contributors<span class='progress'></span></h1>
<div id="request-information"></div>
<div id='contributors' class='contributors'>
</div>
</div>
</div>
<footer>
<div class='site-footer-credits'>
Generated by a <a href='https://github.com/labhr/hatrack'>JavaScript</a> port
of <a href='https://github.com/LABHR/octohatrack'>octohatrack</a>. Part of the
<a href="https://labhr.github.io/">Let's All Build a Hat Rack project</a>.
</div>
</footer>
</div>
</div>
<script>
/* eslint-env jquery */
window.cancelExec = null;
function bootstrapHatRack () {
var oauth = requestOauth(true)
if (!oauth) {
throw Error("Missing OAuth")
}
if (!window.history.state) {
throw Error("Missing state")
}
var repo = window.history.state['repo']
if (!repo) {
throw Error("Missing repo")
}
var removeProgressSpinners = createSpinners($('.progress').empty())
$('#repo-name').html(' for <a href="https://github.com/' + repo + '">' + repo + '</a>')
var shouldAbort = false;
var rejected = $.Deferred().reject(Error("Aborted")).promise()
function cancelExecution() {
shouldAbort = true;
removeProgressSpinners()
}
cancelExecution.repo = repo
window.cancelExec = cancelExecution
var cache = {}
var totalRequests = 0;
function cachedRequest (url) {
if (shouldAbort) {
return rejected
}
if (cache[url]) {
return cache[url]
} else {
var response = cache[url] = oauth.get(url)
return response.then(function() {
if (shouldAbort) {
return rejected
} else {
totalRequests++;
$('#request-information').text('(in ' + totalRequests + ' requests)')
return response
}
})
}
}
var hatRack = window.Labhr('https://api.github.com', cachedRequest)
var totalContributors = 0;
hatRack.load(repo, function (contributors) {
if (shouldAbort) {
return
}
if (contributors['codeContributors']) {
totalContributors += contributors['codeContributors'].length
contributors['codeContributors'].forEach(function (contributor) {
addContributor(contributorToHtml(repo, contributor, 'code'))
})
}
if (contributors['nonCodeOnlyContributors']) {
totalContributors += contributors['nonCodeOnlyContributors'].length
contributors['nonCodeOnlyContributors'].forEach(function (contributor) {
addContributor(contributorToHtml(repo, contributor, 'non-code'))
})
}
$('#contributor-count').text(totalContributors + ' ')
}).then(function () {
// All loading is complete
removeProgressSpinners()
})
}
function addContributor (codeHtml) {
var children = $('#contributors').children()
if (children.length === 0) {
// No children so just.
$('#contributors').append(codeHtml)
} else {
var random_index = Math.floor(Math.random() * (children.length + 1))
if (random_index === children.length) {
// Add at the end.
$('#contributors').append(codeHtml)
} else {
// Otherwise insert it before the chosen element.
$(children[random_index]).before(codeHtml)
}
}
}
function contributorToHtml (project, contributor, type) {
var username = contributor['login']
var userDisplayName = contributor['name'] || username
var url = 'https://github.com/' + project + '/issues?q=involves:' + username
var avatar = contributor['avatar_url'] + '&s=128'
// Construct the contibutor's HTML using jQuery.
var surounding_div = $('<div class="' + type + '">')
var linkTag = $('<a href="' + url + '"></a>')
var avatarImg = $('<img src="' + avatar + '" width="128" alt="' + username + '">')
var overlay = $('<div class="overlay"></div>')
linkTag.append(avatarImg, overlay)
var displayName = $('<div>' + userDisplayName + '</div>')
return surounding_div.append(linkTag, displayName)
}
function createSpinners (elements) {
// I needed a progress spinner thing, and this was quick and hacky
var addedElms = $('<span>•</span><span>•</span><span>•</span>')
$(elements).append(
addedElms
)
var property = 'font-size'
var value = '110%'
var progressSpinnerTimeout = 0
function timer (currIdx) {
var progress = $('.progress')
if (progress) {
progressSpinnerTimeout = setTimeout(timer, 300, (currIdx + 1) % 3)
$(addedElms).css(property, '')
$(addedElms[currIdx]).css(property, value)
}
}
timer(1)
return function () {
window.clearTimeout(progressSpinnerTimeout)
addedElms.remove()
}
}
function validateRepo (repo) {
// Really dubm. Basically 1 slash with at least 1 non-slash either side.
return /[^\/#]+\/[^\/#]+/.test(repo)
}
function loadLocation() {
if (window.history.state) {
// We have something in the state object. So we can just use that.
console.log("We have state already. Handle it.")
setLocation(window.history.state['repo'])
return
}
var repoParamRegexp = /repo=([^&]+)/
var hash = window.location.hash + ''
if (hash.indexOf('%') !== -1) {
// URI encoded, fix that
hash = window.decodeURIComponent(hash)
}
var repoMatch = repoParamRegexp.exec(hash)
if (!repoMatch) {
// Nothing I can see in the URL.
setLocation(null, true)
} else {
// Replace this location with one that has state.
setLocation(repoMatch[1], true)
}
}
function setLocation (repo, replace) {
console.log("Setting location", repo, " - Replacing?", !!replace)
var noLocationChange = window.history.state && window.history.state['repo'] === repo
function stateChange (state, title, url) {
if (replace || noLocationChange) {
return window.history.replaceState(state, title, url)
} else {
return window.history.pushState(state, title, url)
}
}
if (repo) {
stateChange({'repo': repo}, 'Let\'s Build a Hat Rack -- ' + repo,
'#repo=' + repo)
} else {
stateChange({'repo': null}, 'Let\'s Build a Hat Rack', '#')
}
_setRepo()
}
function _setRepo() {
if (!window.history.state) {
var repo = null;
} else {
var repo = window.history.state['repo']
}
var oauth_credentials = requestOauth(true)
console.log("Setting the repo? -", repo, "Got creds?", oauth_credentials, !!oauth_credentials)
if (!repo) {
if (window.cancelExec) {
window.cancelExec()
window.cancelExec = null
}
$('#contributors').empty()
$('#contributor-count').empty()
$('#request-information').empty()
$('#repo-name').empty()
} else {
$('input[name="repo"]').val(repo)
}
// Use '!!' to coerce to a boolean
var showContrib = !!(repo && oauth_credentials)
if (showContrib) {
// Have a repo and credentials
if (window.cancelExec && window.cancelExec.repo !== repo) {
// Currently loading a different repo's things.
window.cancelExec()
window.cancelExec = null
}
if (!window.cancelExec) {
// We're clear to start another hat rack.
bootstrapHatRack()
}
}
$('.invalid').removeClass('invalid')
$('#initial-form').toggle(!showContrib)
$('#all-contributors').toggle(showContrib)
$('input[name="repo"]').prop('disabled', showContrib)
$('#github-oauth').prop('disabled', showContrib)
$('#search-again button').prop('disabled', !showContrib)
$('#permalink a').prop('href', window.location.href)
}
function requestOauth(immediate) {
var oauth = OAuth.create('github');
// If we have oauth, then enable the signout button.
$('#signout button').prop('disabled', !oauth)
if (immediate) {
return oauth
} else if (oauth) {
return $.Deferred().resolve(oauth).promise();
}
return OAuth.popup('github', {cache: true}).done(function (githubApi) {
$('#signout button').prop('disabled', false)
// bootstrapHatRack(repo, githubApi)
return githubApi
}).fail(function (err) {
console.log(err)
button.addClass('invalid')
})
}
$(document).ready(function() {
requestOauth(true) // Update the signout button.
loadLocation()
$(window).on('popstate', _setRepo)
$('#signout button').click(function (event) {
event.preventDefault()
$(this).prop('disabled', true)
OAuth.clearCache();
if (window.cancelExec) {
// Currently loading a different repo's things.
window.cancelExec()
window.cancelExec = null
}
_setRepo();
})
$('#search-again button').click(function (event) {
event.preventDefault()
setLocation(null)
})
$('#permalink a').click(function(event) {
// event.preventDefault()
})
$('button#github-oauth').click(function (event) {
event.preventDefault()
$('.invalid').removeClass('invalid')
var repo = $('input[name="repo"]').val()
if (!validateRepo(repo)) {
// Invalid repo name.
$('input[name="repo"]').prop('disabled', false).addClass('invalid')
return
}
$('input[name="repo"]').prop('disabled', true)
var button = $(this).prop('disabled', true)
var cancelSpinners = createSpinners(this)
requestOauth().then(function() {
setLocation(repo)
}).always(cancelSpinners)
})
})
</script>
</body>
</html>