|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +{ |
| 4 | + function fetchJSON(url, cb) { |
| 5 | + const xhr = new XMLHttpRequest(); |
| 6 | + xhr.open('GET', url); |
| 7 | + xhr.responseType = 'json'; |
| 8 | + xhr.onload = () => { |
| 9 | + if (xhr.status < 400) { |
| 10 | + cb(null, xhr.response); |
| 11 | + } else { |
| 12 | + cb(new Error(`Network error: ${xhr.status} - ${xhr.statusText}`)); |
| 13 | + } |
| 14 | + }; |
| 15 | + xhr.onerror = () => cb(new Error('Network request failed')); |
| 16 | + xhr.send(); |
| 17 | + } |
| 18 | + |
| 19 | + function createAndAppend(name, parent, options = {}) { |
| 20 | + const elem = document.createElement(name); |
| 21 | + parent.appendChild(elem); |
| 22 | + Object.keys(options).forEach((key) => { |
| 23 | + const value = options[key]; |
| 24 | + if (key === 'text') { |
| 25 | + elem.innerText = value; |
| 26 | + } else { |
| 27 | + elem.setAttribute(key, value); |
| 28 | + } |
| 29 | + }); |
| 30 | + return elem; |
| 31 | + } |
| 32 | + |
| 33 | + function main(url) { |
| 34 | + fetchJSON(url, (err, data) => { |
| 35 | + //const root = document.getElementById('root'); |
| 36 | + createAndAppend("div", root, { id: "top" }); |
| 37 | + createAndAppend("div", root, { id: "left" }); |
| 38 | + createAndAppend("div", root, { id: "right" }); |
| 39 | + const top = document.getElementById('top'); |
| 40 | + const left = document.getElementById('left'); |
| 41 | + let right = document.getElementById('right'); |
| 42 | + createAndAppend("h5", right, { text: "Contributions", "class": "rightTitle" }); |
| 43 | + |
| 44 | + if (err) { |
| 45 | + createAndAppend('div', root, { text: err.message, class: 'alert-error' }); |
| 46 | + }// else { |
| 47 | + // createAndAppend('pre', root, { text: JSON.stringify(data, null, 2) }); |
| 48 | + // } |
| 49 | + |
| 50 | + data.sort((a, b) => a.name.localeCompare(b.name)); |
| 51 | + |
| 52 | + createAndAppend("h7", top, { id: "title", text: "HYF-Repository" }); |
| 53 | + createAndAppend("select", top, { id: "select" }); |
| 54 | + |
| 55 | + createAndAppend("div", left, { id: "left1" }); |
| 56 | + createAndAppend("h7", left1, { id: "repository", text: "Repository: ", class: "headings first" }); |
| 57 | + createAndAppend("h7", left1, { id: "repositoryName", class: "txt first" }); |
| 58 | + let repositoryName = document.getElementById("repositoryName") |
| 59 | + |
| 60 | + createAndAppend("div", left, { id: "left2" }); |
| 61 | + createAndAppend("h7", left2, { id: "description", text: "Description: ", class: "headings first" }); |
| 62 | + createAndAppend("p", left2, { id: "descriptionText", class: "txt first" }); |
| 63 | + let descriptionText = document.getElementById("descriptionText") |
| 64 | + |
| 65 | + createAndAppend("div", left, { id: "left3" }); |
| 66 | + createAndAppend("h7", left3, { id: "forkNumber", text: "Forks: ", class: "headings first" }); |
| 67 | + createAndAppend("h7", left3, { id: "forkText", class: "txt first" }); |
| 68 | + let forkText = document.getElementById("forkText") |
| 69 | + |
| 70 | + createAndAppend("div", left, { id: "left4" }); |
| 71 | + createAndAppend("h7", left4, { id: "update", text: "Updated: ", class: "headings first" }); |
| 72 | + createAndAppend("h7", left4, { id: "updateText", class: "txt first" }); |
| 73 | + let updateText = document.getElementById("updateText") |
| 74 | + |
| 75 | + |
| 76 | + let repoName = []; |
| 77 | + let repoDecription = []; |
| 78 | + let repoForks = []; |
| 79 | + let repoUpdate = []; |
| 80 | + let repoContributorsLinks = []; |
| 81 | + // filling the arrays with data from the JSON file |
| 82 | + for (let i = 0; i < data.length; i++) { |
| 83 | + repoName.push(data[i].name); |
| 84 | + repoDecription.push(data[i].description); |
| 85 | + repoForks.push(data[i].forks); |
| 86 | + repoUpdate.push(data[i].updated_at) |
| 87 | + repoContributorsLinks.push(data[i].contributors_url) |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + selectOptions(repoName) |
| 92 | + |
| 93 | + |
| 94 | + let select = document.getElementById("select"); |
| 95 | + select.addEventListener("change", function (e) { |
| 96 | + // let option = this.options[this.selectedIndex].value; |
| 97 | + const i = e.target.value; |
| 98 | + printRepoInfo(i, repoName, repoDecription, repoForks, repoUpdate) |
| 99 | + getContributors(right, data[i]) |
| 100 | + }); // end eventlistener |
| 101 | + }); // end of fetch() |
| 102 | + } //end of main fucntion |
| 103 | + |
| 104 | + // get all the array`s elements, insert them as options in the Select and give them the value of the indexnumber as ID |
| 105 | + function selectOptions(name) { |
| 106 | + let select = document.getElementById("select"); |
| 107 | + for (let i = 0; i < name.length; i++) { |
| 108 | + let option = document.createElement("option"); |
| 109 | + option.value = i; |
| 110 | + option.text = name[i]; |
| 111 | + select.appendChild(option); |
| 112 | + |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + function printRepoInfo(i, name, description, forks, update) { |
| 117 | + repositoryName.innerHTML = "<a href= '#' target='_blank'>" + name[i] + "</a>"; |
| 118 | + descriptionText.innerHTML = description[i]; |
| 119 | + forkText.innerHTML = forks[i] |
| 120 | + updateText.innerHTML = update[i]; |
| 121 | + } // end function |
| 122 | + |
| 123 | + |
| 124 | + function getContributors(right, data) { |
| 125 | + right.innerHTML = ""; |
| 126 | + createAndAppend("h5", right, { text: "Contributions", "class": "rightTitle" }); |
| 127 | + fetchJSON(data.contributors_url, (err, contributors) => { |
| 128 | + if (err) { |
| 129 | + createAndAppend("div", right, { html: err.message, "class": "alert-error" }); |
| 130 | + } |
| 131 | + contributors.map(c => { |
| 132 | + let ul = createAndAppend("ul", right, { "class": "ul" }); |
| 133 | + let li = createAndAppend("li", ul, { "class": "li" }); |
| 134 | + |
| 135 | + createAndAppend("img", li, { "class": "avatar", "src": c.avatar_url}); |
| 136 | + |
| 137 | + let login = createAndAppend("div", li, { id: "login", "class": "liDivs" }); |
| 138 | + const a = createAndAppend("a", login, { "target": "_blank", "href": c.html_url, "class": "link" }); |
| 139 | + createAndAppend("h8", a, { text: c.login}); |
| 140 | + |
| 141 | + let contributionsNumber = createAndAppend("div", li, { id: "contributionsNumber", "class": "liDivs" }); |
| 142 | + createAndAppend("h8", contributionsNumber, { text: c.contributions }); |
| 143 | + }); |
| 144 | + }); |
| 145 | + } |
| 146 | + |
| 147 | + const HYF_REPOS_URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100'; |
| 148 | + |
| 149 | + window.onload = () => main(HYF_REPOS_URL); |
| 150 | +} |
| 151 | + |
| 152 | + |
0 commit comments