forked from zenika-open-source/the-duck-gallery
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
54 lines (45 loc) · 1.73 KB
/
main.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
const gallery = document.getElementById('gallery');
const count = document.getElementById('count')
const loading = document.getElementsByClassName('loading')
function createContainer() {
const container = document.createElement('div');
container.setAttribute('class', 'duck');
gallery.appendChild(container);
return container;
}
function addDuckImage(container, contributor) {
const duck = document.createElement('img');
duck.setAttribute('src', `./ducks/${contributor.username}.png`);
duck.setAttribute('width', '300');
duck.setAttribute('height', '300');
duck.setAttribute('loading', 'lazy');
duck.setAttribute('alt', contributor.username);
container.appendChild(duck);
}
function addContributorInfo(container, contributor) {
const contributorName = document.createElement('a');
contributorName.setAttribute('href', `https://github.com/${contributor.username}`);
contributorName.setAttribute('aria-label', contributor.username);
contributorName.setAttribute('target', 'NEW');
contributorName.append(contributor.username);
container.appendChild(contributorName);
const contributorMessage = document.createElement('div');
contributorMessage.setAttribute('class', 'message');
contributorMessage.append(contributor.message);
container.appendChild(contributorMessage);
}
function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
/* global contributors */
count.innerText = contributors.length;
shuffle(contributors);
for (const contributor of contributors) {
const container = createContainer();
addDuckImage(container, contributor);
addContributorInfo(container, contributor);
}
loading[0].remove()