-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
166 lines (146 loc) · 4.76 KB
/
main.js
File metadata and controls
166 lines (146 loc) · 4.76 KB
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
"use script";
var $header = $("header"),
$footer = $("footer"),
contributors = {},
$contributorContainer, headerOpacity, footerOpacity, distanceFromBottom;
/**
* Creates a script HTML Element and appends it to the page.
* Ideal for JSONP requests.
*
* @param {string} url - URL of the JS script to include.
*/
function createScript(url) {
var newScript = document.createElement("script");
newScript.src = url;
document.body.appendChild(newScript);
}
/**
* Converts a thingsSDK repo name in to a contributors API URL
*
* @param {string} repo
* @returns {string}
*/
function createContributorsURL(repo) {
return "https://api.github.com/repos/thingsSDK/" + repo + "/contributors";
}
/**
* Creates a script HTML Element with from a contributors API URL
* which triggers `contributorsCallback` on completeion
*
* @param {string} contributors_url
*/
function getContributors(contributors_url) {
createScript(contributors_url + "?callback=contributorsCallback");
}
/**
* Creates the Contributors section on the website
*/
function createContributerSection() {
$footer.prepend("<h2>Contributors</h2>", "<div id='contributor-container' class='contributor-container'></div>");
$contributorContainer = $("#contributor-container");
}
/**
* Converts a contributor JSON object in to an HTML element and appends it to
* the `$contributorContainer`
*
* @param {object} contributor
*/
function createAndAppendContributor(contributor) {
// Create element
htmlElement = "<a href='" + contributor.html_url +
"' class='contributor-link' target='_blank' ><img src='" + contributor.avatar_url +
"' alt='" + contributor.login +
"' title='" + contributor.login +
"' /></a>";
if ($contributorContainer) {
$contributorContainer.append(htmlElement);
}
}
/**
* Converts, aggregates contribution totals and displays them on the page.
*
* @todo Maybe add some kind of filler for the footer on Github request failure?
*
* @param {object} response - response from the contributors API
*/
function contributorsCallback(response) {
if (response.meta.status < 400) {
// Create Contributors Section if it doesn't exist yet
if (!$contributorContainer) {
createContributerSection();
}
// Aggregate contributions
response.data.forEach(sumContributions);
// Sort by number of contributions
var sortedContributors = Object.keys(contributors).map(function (login) {
return Object.assign({ login: login }, contributors[login]);
}).sort(function (contributor, otherContributor) {
return otherContributor.contributions - contributor.contributions;
});
// Empty contributors
$contributorContainer.html("");
// Populate with updated contributors list
sortedContributors.forEach(createAndAppendContributor);
} else {
console.log("Unable to pull contributors from Github");
}
}
/**
* Addeds or updates the total number of contributions from a `constributor` to the
* `contributors` object.
*
* @param {object} contributor
*/
function sumContributions(contributor) {
if (contributors[contributor.login]) {
contributors[contributor.login].contributions = contributors[contributor.login].contributions + contributor.contributions;
} else {
contributors[contributor.login] = {
contributions: contributor.contributions,
html_url: contributor.html_url,
avatar_url: contributor.avatar_url
};
}
}
function fadeHeaderFooter() {
// Determine distance from bottom (there's no scrollBottom)
distanceFromBottom = Math.floor($(document).height() - $(document).scrollTop() - $(window).height());
// Arbitrary algorithms that seem to look nice
// Have to use Math.max and 0.1 to fix iOS touchmove bug
headerOpacity = Math.max((1 - ($(window).scrollTop() / 250)), 0.1);
footerOpacity = Math.max((1 - (distanceFromBottom / 350)), 0.1);
// Update opacity on scroll
$header.css({ opacity: headerOpacity });
$footer.css({ opacity: footerOpacity });
}
$(function () {
// Grab contributors for all thingsSDK repos
const repos = [
"flasher.js",
"flasher.thingssdk.com",
"thingssdk.com",
"thingssdk-cli",
"thingssdk-espruino-strategy",
"thingssdk-deployer",
"guides.thingssdk.com",
"ani-matrix.thingssdk.com",
"wifiscanner",
"ht16k33"
];
repos
.map(createContributorsURL)
.forEach(getContributors);
// Fade header
$(window).scroll(fadeHeaderFooter);
$(window).bind('touchmove', fadeHeaderFooter);
// Body background is white for header and beige/brown for footer
$(window).scroll(function () {
if ($(window).scrollTop() > $("body").height() / 2) {
$("body").css({ 'background-color': '#C0B283' });
$("header").css({ 'z-index': '-2000' });
}
else {
$("body").css({ 'background-color': 'white' });
}
});
});