|
| 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.textContent = value; |
| 26 | + } else { |
| 27 | + elem.setAttribute(key, value); |
| 28 | + } |
| 29 | + }); |
| 30 | + return elem; |
| 31 | + } |
| 32 | + //header |
| 33 | + |
| 34 | + function createAnRepoList(array) { |
| 35 | + createAndAppend('header', root, { class: 'header' }); |
| 36 | + let header = document.getElementsByClassName('header')[0]; |
| 37 | + createAndAppend('p', header, { text: 'HYF Repositories' }); |
| 38 | + createAndAppend('select', header, { class: 'repo-selector', id: 'RepoList' }); |
| 39 | + let listRoot = document.getElementById('RepoList'); |
| 40 | + for (let i = 0; i < array.length; i++) { |
| 41 | + createAndAppend('option', listRoot, { text: array[i].name, value: i }); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + function selectInfo(arr) { |
| 46 | + const root = document.getElementById('root'); |
| 47 | + createAndAppend('div', root, { class: 'header', id: 'header' }); |
| 48 | + let header = document.getElementsByClassName('header')[0]; |
| 49 | + createAndAppend('h1', header, { text: 'HYF Repositories' }); |
| 50 | + createAndAppend('select', header, { class: 'repo-selector', id: 'RepoList' }); |
| 51 | + const listRoot = document.getElementById('RepoList'); |
| 52 | + |
| 53 | + for (let i = 0; i < arr.length; i++) { |
| 54 | + createAndAppend('option', listRoot, { text: arr[i].name, value: i }); |
| 55 | + } |
| 56 | + } |
| 57 | + //left |
| 58 | + function createRepo(element) { |
| 59 | + const container = document.querySelector('#container'); |
| 60 | + //creating table for repository overwiew |
| 61 | + let table = createAndAppend('table', container, { |
| 62 | + id: 'RepositoryOverview', |
| 63 | + class: 'leftDiv whiteFrame', |
| 64 | + }); |
| 65 | + let repoRow = createAndAppend('tr', table); |
| 66 | + //creating row for repository and link |
| 67 | + createAndAppend('td', repoRow, { text: 'Repository:', style: 'font-weight:bold' }); |
| 68 | + let repoLink = createAndAppend('td', repoRow); |
| 69 | + createAndAppend('a', repoLink, { |
| 70 | + href: element.html_url, |
| 71 | + text: element.name, |
| 72 | + target: '_blank', |
| 73 | + }); |
| 74 | + //creating row for repo description |
| 75 | + let descriptionRow = createAndAppend('tr', table); |
| 76 | + createAndAppend('td', descriptionRow, { text: 'Description:', style: 'font-weight:bold' }); |
| 77 | + createAndAppend('td', descriptionRow, { text: element.description }); |
| 78 | + //creating row for forks |
| 79 | + let forkRow = createAndAppend('tr', table); |
| 80 | + createAndAppend('td', forkRow, { text: 'Fork:', style: 'font-weight:bold' }); |
| 81 | + createAndAppend('td', forkRow, { text: element.forks_count }); |
| 82 | + // creating 'last time updated' row |
| 83 | + let updatedRow = createAndAppend('tr', table); |
| 84 | + createAndAppend('td', updatedRow, { text: 'Updated:', style: 'font-weight:bold' }); |
| 85 | + let date = new Date(element.updated_at); |
| 86 | + date = date.toUTCString(); |
| 87 | + createAndAppend('td', updatedRow, { text: date }); |
| 88 | + //const repolist = getElementByClassName('left-div'); |
| 89 | + } |
| 90 | + //right |
| 91 | + |
| 92 | + function createContributor(element) { |
| 93 | + fetchJSON(element.contributors_url, (err, data) => { |
| 94 | + const root = document.getElementById('container'); |
| 95 | + if (err) { |
| 96 | + createAndAppend('div', root, { text: err.message, class: 'alert-error' }); |
| 97 | + } else { |
| 98 | + let contributors = createAndAppend('div', root, { |
| 99 | + id: 'contributors', |
| 100 | + class: 'rightDiv whiteFrame', |
| 101 | + }); |
| 102 | + createAndAppend('p', contributors, { class: 'contributorsHeader', text: 'Contributors' }); |
| 103 | + |
| 104 | + let ul = createAndAppend('ul', contributors, { class: 'contributorsList' }); |
| 105 | + for (let i = 0; i < data.length; i++) { |
| 106 | + let li = createAndAppend('li', ul, { class: 'contributorItem' }); |
| 107 | + let img = createAndAppend('img', li, { |
| 108 | + src: data[i].avatar_url, |
| 109 | + class: 'contributorsAvatar', |
| 110 | + height: 48, |
| 111 | + }); |
| 112 | + let login = createAndAppend('a', li, { |
| 113 | + text: data[i].login, |
| 114 | + href: data[i].html_url, |
| 115 | + target: '_blank', |
| 116 | + class: 'contributorName', |
| 117 | + }); |
| 118 | + let badge = createAndAppend('div', li, { |
| 119 | + text: data[i].contributions, |
| 120 | + class: 'contributorBadge', |
| 121 | + }); |
| 122 | + } |
| 123 | + } |
| 124 | + }); |
| 125 | + } |
| 126 | + |
| 127 | + //main for run program |
| 128 | + function main(url) { |
| 129 | + fetchJSON(url, (err, data) => { |
| 130 | + const root = document.getElementById('root'); |
| 131 | + if (err) { |
| 132 | + createAndAppend('div', root, { text: err.message, class: 'alert-error' }); |
| 133 | + } else { |
| 134 | + data.sort(function(item1, item2) { |
| 135 | + if (item1.name.toUpperCase() < item2.name.toUpperCase()) return -1; |
| 136 | + if (item1.name > item2.name) return 1; |
| 137 | + return 0; |
| 138 | + }); |
| 139 | + |
| 140 | + //Show Header |
| 141 | + selectInfo(data); |
| 142 | + //Create and show Container left-right |
| 143 | + createAndAppend('div', root, { id: 'container', class: 'container' }); |
| 144 | + //left-side |
| 145 | + createRepo(data[0]); |
| 146 | + //right-side |
| 147 | + createContributor(data[0]); |
| 148 | + |
| 149 | + //change value if select repo in drop down |
| 150 | + |
| 151 | + document.getElementById('RepoList').onchange = function() { |
| 152 | + let selectedItem = this.options[this.selectedIndex].value; |
| 153 | + let table = document.getElementById('RepositoryOverview'); |
| 154 | + table.parentNode.removeChild(table); |
| 155 | + let contributors = document.getElementById('contributors'); |
| 156 | + contributors.parentNode.removeChild(contributors); |
| 157 | + |
| 158 | + createRepo(data[selectedItem]); |
| 159 | + createContributor(data[selectedItem]); |
| 160 | + }; |
| 161 | + } |
| 162 | + }); |
| 163 | + } |
| 164 | + |
| 165 | + const HYF_REPOS_URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100'; |
| 166 | + |
| 167 | + window.onload = () => main(HYF_REPOS_URL); |
| 168 | +} |
0 commit comments