-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.js
275 lines (173 loc) · 5.57 KB
/
script.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
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
function issuelister (config) {
var DEBUG = config.debug || false;
var DATETIMEFORMAT = config.datetimeformat || 'DD.MM.YYYY HH:mm';
var type = config.type;
var name = config.name;
if (type == undefined || name == undefined) {
var error = document.createElement('div');
error.textContent = 'To use Issue Lister propperly, you have to set up at least a GitHub type and GitHub name. You find the instructions in the README.md file.';
error.style.padding = '1em';
error.style.backgroundColor = 'lightcoral';
error.style.color = 'darkred';
error.style.border = '0.2em solid darkred';
error.style.margin = '1em';
document.body.appendChild(error);
return;
}
var listSource = document.querySelector('#list-template').innerHTML;
var listTemplate = Handlebars.compile(listSource);
var detailSource = document.querySelector('#detail-template').innerHTML;
var detailTemplate = Handlebars.compile(detailSource);
var issuelist = document.querySelector('.issues');
var detail = document.querySelector('.detail');
var detailoverlay = document.querySelector('.detailoverlay');
var repos = 'https://api.github.com/' + config.type + '/' + config.name + '/repos?per_page=100';
var allissues = {};
var xhr = new XMLHttpRequest();
xhr.open('GET', repos);
xhr.send();
xhr.onload = handleResponse;
function handleResponse () {
var repos = JSON.parse(xhr.responseText);
clog(repos);
for (var i = 0; i < repos.length; i++) {
var repo = repos[i];
clog(repo);
if (repo.open_issues_count > 0) {
getIssues(repo.url)
}
}
}
function getIssues(repourl) {
var issueXhr = new XMLHttpRequest();
issueXhr.open('GET', repourl+'/issues');
issueXhr.send();
issueXhr.onload = function () {
var issues = JSON.parse(issueXhr.responseText);
handleIssues(issues);
}
}
function handleIssues(issues) {
for (var i = 0; i < issues.length; i++) {
var issue = issues[i];
var isPullRequest = (issue.pull_request) ? true : false;
handleIssue(issue, isPullRequest);
}
}
function handleIssue(issue, isPullRequest) {
var id = issue.id;
var time = moment(issue.created_at).format(DATETIMEFORMAT);
var type = (isPullRequest) ? 'pr' : 'i';
issue.type = type;
allissues[id] = issue;
var data = {
id: id,
repo: getRepoName( issue.url ),
title: issue.title,
author: issue.user.login,
time: time,
type: type
};
issuelist.innerHTML += listTemplate(data);
clog(issue);
}
function getRepoName (url) {
var u = url.split('/');
u.pop();
u.pop();
var repo = u.pop();
var user = u.pop();
return [user, repo].join('/');
}
function clog (anything) {
if (DEBUG) {
console.log(anything);
}
}
window.onhashchange = hashChanged;
function hashChanged (e) {
e.preventDefault();
var hash = window.location.hash.split('/');
clog(hash);
if (hash[1] == 'id') {
var id = hash[2];
showIssueInDetail(id);
} else {
hideDetail();
emptyDetail();
}
}
function showIssueInDetail (id) {
var issue = allissues[id];
var time = moment(issue.created_at).format(DATETIMEFORMAT);
var writtentype = (issue.type == 'i') ? 'Issue' : 'Pull Request';
var repo = getRepoName( issue.url );
var repourl = ['https://github.com', repo].join('/');
var body = marked(issue.body);
var labels = [];
for (var i = 0; i < issue.labels.length; i++) {
var label = issue.labels[i];
var u = label.url.split('/');
var p = [];
p.push(u.pop());
p.push(u.pop());
p.push(u.pop());
p.push(u.pop());
p.push('https://github.com');
p.sort(function(){
return 1
});
label.url = p.join('/');
labels.push(label);
}
var data = {
id: id,
repo: repo,
title: issue.title,
author: issue.user.login,
time: time,
type: issue.type,
writtentype: writtentype,
url: issue.html_url,
issuenumber: issue.number,
repourl: repourl,
description: body,
authorlink: issue.user.html_url,
labels: labels
};
clog(labels)
fillDetail(detailTemplate(data));
showDetail();
if (DEBUG) fillDetail('<pre>'+JSON.stringify(issue, null, 2)+'</pre>');
}
function showDetail() {
document.body.classList.add('blocked');
detail.classList.add('visible');
detail.scrollTop = 0;
detailoverlay.classList.add('visible');
detailoverlay.classList.add('unhidden');
}
function hideDetail () {
document.body.classList.remove('blocked');
detail.classList.remove('visible');
detailoverlay.classList.remove('visible');
setTimeout(function () {
detailoverlay.classList.remove('unhidden');
}, 500);
}
function fillDetail (html) {
detail.innerHTML += html;
}
function emptyDetail () {
detail.innerHTML = '';
}
detailoverlay.onclick = exitDetail;
document.onkeydown = function (e) {
if (e.keyCode == 27) {
exitDetail();
}
}
function exitDetail () {
window.location.hash = '#/';
}
}