|
12 | 12 | cb(new Error(`Network error: ${xhr.status} - ${xhr.statusText}`)); |
13 | 13 | } |
14 | 14 | }; |
15 | | - xhr.onerror = () => cb(new Error('Network request failed')); |
| 15 | + xhr.onerror = () => cb(new Error('Network request failed')); // “communication errors”: connection is lost or the remote server does not respond at all. |
16 | 16 | xhr.send(); |
17 | 17 | } |
18 | 18 |
|
|
30 | 30 | return elem; |
31 | 31 | } |
32 | 32 |
|
| 33 | + // Display repository options in the header |
| 34 | + function selectOptions(nameOption) { |
| 35 | + const selectRepoHYF = document.getElementById('selectRepoHYF'); |
| 36 | + for (let i = 0; i < nameOption.length; i++) { |
| 37 | + createAndAppend('option', selectRepoHYF, { value: i, text: nameOption[i].name }); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + // Information on left side inside a table |
| 42 | + function displayInformation(element) { |
| 43 | + const container = document.getElementById('container'); |
| 44 | + const infoDiv = createAndAppend('div', container, { |
| 45 | + id: 'leftArea', |
| 46 | + class: 'left-div whiteframe', |
| 47 | + }); |
| 48 | + createAndAppend('table', infoDiv, { id: 'table' }); |
| 49 | + const table = document.getElementById('table'); |
| 50 | + createAndAppend('tbody', table, { id: 'tbody' }); |
| 51 | + function createTableRow(label, description) { |
| 52 | + const tRow = createAndAppend('tr', table); |
| 53 | + createAndAppend('td', tRow, { text: label, class: 'label' }); |
| 54 | + createAndAppend('td', tRow, { text: description }); |
| 55 | + } |
| 56 | + |
| 57 | + createTableRow('Repository: ', element.name); |
| 58 | + createTableRow('Description: ', element.description); |
| 59 | + createTableRow('Forks : ', element.forks); |
| 60 | + const date2 = new Date(element.updated_at).toLocaleString(); |
| 61 | + createTableRow('Updated: ', date2); |
| 62 | + } |
| 63 | + |
| 64 | + // Show contributors |
| 65 | + function contributorsList(element) { |
| 66 | + fetchJSON(element.contributors_url, (err, data) => { |
| 67 | + const container = document.getElementById('container'); |
| 68 | + createAndAppend('div', container, { |
| 69 | + id: 'rightArea', |
| 70 | + class: 'right-div whiteframe', |
| 71 | + }); |
| 72 | + const rightArea = document.getElementById('rightArea'); |
| 73 | + createAndAppend('h7', rightArea, { |
| 74 | + text: 'Contributions', |
| 75 | + class: 'contributor-header', |
| 76 | + }); |
| 77 | + createAndAppend('ul', rightArea, { |
| 78 | + id: 'contList', |
| 79 | + class: 'contributor-list', |
| 80 | + }); |
| 81 | + let contributorURL; |
| 82 | + let contributorItem; |
| 83 | + let contributorData; |
| 84 | + const contList = document.getElementById('contList'); |
| 85 | + for (let i = 0; i < data.length; i++) { |
| 86 | + contributorURL = createAndAppend('a', contList, { |
| 87 | + href: data[i].html_url, |
| 88 | + target: '_blank', |
| 89 | + }); |
| 90 | + contributorItem = createAndAppend('li', contributorURL, { |
| 91 | + class: 'contributor-item', |
| 92 | + }); |
| 93 | + |
| 94 | + createAndAppend('img', contributorItem, { |
| 95 | + src: data[i].avatar_url, |
| 96 | + class: 'contributor-avatar', |
| 97 | + }); |
| 98 | + contributorData = createAndAppend('div', contributorItem, { |
| 99 | + class: 'contributor-data', |
| 100 | + }); |
| 101 | + createAndAppend('div', contributorData, { text: data[i].login }); |
| 102 | + createAndAppend('div', contributorData, { |
| 103 | + text: data[i].contributions, |
| 104 | + class: 'contributor-badge', |
| 105 | + }); |
| 106 | + } |
| 107 | + }); |
| 108 | + } |
| 109 | + |
33 | 110 | function main(url) { |
34 | 111 | fetchJSON(url, (err, data) => { |
35 | 112 | const root = document.getElementById('root'); |
36 | 113 | if (err) { |
37 | 114 | createAndAppend('div', root, { text: err.message, class: 'alert-error' }); |
38 | 115 | } else { |
39 | | - createAndAppend('pre', root, { text: JSON.stringify(data, null, 2) }); |
| 116 | + createAndAppend('header', root, { id: 'topArea', class: 'header' }); |
| 117 | + const topArea = document.getElementById('topArea'); |
| 118 | + createAndAppend('h7', topArea, { id: 'title', text: 'HYF Repositories' }); |
| 119 | + createAndAppend('select', topArea, { id: 'selectRepoHYF', class: 'repo-selector' }); |
| 120 | + createAndAppend('div', root, { id: 'container' }); |
| 121 | + data.sort((a, b) => a.name.localeCompare(b.name)); |
| 122 | + selectOptions(data); |
| 123 | + displayInformation(data[0]); |
| 124 | + contributorsList(data[0]); |
| 125 | + |
| 126 | + document.getElementById('selectRepoHYF').onchange = function startListener() { |
| 127 | + const selectedItem = this.options[this.selectedIndex].value; |
| 128 | + const infoLeft = document.getElementById('leftArea'); |
| 129 | + infoLeft.parentNode.removeChild(infoLeft); |
| 130 | + const contributors = document.getElementById('rightArea'); |
| 131 | + contributors.parentNode.removeChild(contributors); |
| 132 | + |
| 133 | + displayInformation(data[selectedItem]); |
| 134 | + contributorsList(data[selectedItem]); |
| 135 | + }; |
40 | 136 | } |
41 | 137 | }); |
42 | 138 | } |
|
0 commit comments