This repository has been archived by the owner on Jan 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
117 lines (117 loc) · 4.74 KB
/
client.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
var selectedPackage = null;
var curUser = null;
var Ajax = {
request: function (url, method, headers, data, success, failure) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
// the request is complete, parse data and call callback
var response = JSON.parse(xhr.responseText);
success(response);
}
else if (xhr.readyState === XMLHttpRequest.DONE) {
failure();
}
};
xhr.open(method, url, true);
for (var header in headers) {
xhr.setRequestHeader(header, headers[header]);
}
if (data) {
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.send(JSON.stringify(data));
}
else {
xhr.send();
}
},
};
function getHeaders(user) {
var headers = null;
if (user != null) {
headers = { userid: user.upn, tenantid: user.tenantId };
}
return headers;
}
function renderContributors(contributors) {
document.getElementById("contributors_container").style.display = "block";
document.querySelectorAll("#contributors_title span")[0].innerHTML = selectedPackage;
var container = document.querySelectorAll("#contributors tbody")[0];
while (container.hasChildNodes()) {
container.removeChild(container.firstChild);
}
for (var index = 0; index < contributors.length; index++) {
var contributor = contributors[index];
var tableRow = document.createElement("tr");
tableRow.innerHTML = "<td><img src='" + contributor.avatar_url + "' /></td>" +
"<td>" + contributor.login + "</td>" +
"<td>" + contributor.contributions + "</td>";
container.appendChild(tableRow);
}
}
function renderPackages(packages) {
var container = document.querySelectorAll("#packages tbody")[0];
while (container.hasChildNodes()) {
container.removeChild(container.firstChild);
}
for (var index = 0; index < packages.length; index++) {
var package = packages[index];
var btnClass = "btn-default";
if (package.favorite) {
btnClass = "btn-success";
}
var tableRow = document.createElement("tr");
tableRow.innerHTML = '<td><a data-repo="' + package.name + '" href="#" class="favorite btn ' + btnClass + '"><span class="glyphicon glyphicon-star"></span></a></td>' +
"<td><a class='repo' href='#'>" + package.name + "</a></td>";
container.appendChild(tableRow);
}
var repoElements = document.getElementsByClassName("repo");
for (var index = 0; index < repoElements.length; index++) {
repoElements[index].addEventListener("click", function (e) {
selectedPackage = e.target.innerHTML;
Ajax.request("/contributors/" + selectedPackage, "GET", null, null, renderContributors, null);
});
}
var favorites = document.getElementsByClassName("favorite");
for (var index = 0; index < favorites.length; index++) {
favorites[index].addEventListener("click", function (e) {
e.stopPropagation();
e.preventDefault();
var button = e.target;
if (button.tagName != "A") {
button = button.parentNode;
}
var isFavorite = button.classList.contains("btn-default");
Ajax.request("/favorite/" + button.getAttribute("data-repo"), "POST", getHeaders(curUser), {
isFavorite: isFavorite
}, function (data) {
if (isFavorite) {
button.classList.remove("btn-default");
button.classList.add("btn-success");
}
else {
button.classList.remove("btn-success");
button.classList.add("btn-default");
}
}, null);
});
}
}
function getPackages() {
Ajax.request("/packages", "GET", getHeaders(curUser), null, renderPackages, null);
}
document.addEventListener("DOMContentLoaded", function () {
getPackages();
document.getElementById("login").addEventListener("click", function (e) {
e.preventDefault();
var identity = new Kurve.Identity("212065fa-ca9e-472a-8b0e-84a0b36c5f64", window.location.origin + "/login.html");
identity.login(function (error) {
if (identity.isLoggedIn()) {
curUser = identity.getIdToken();
document.getElementById("contributors_container").style.display = "none";
document.getElementById("login").style.display = "none";
getPackages();
}
});
});
});