|
30 | 30 | return elem;
|
31 | 31 | }
|
32 | 32 |
|
| 33 | + // Load the Contributors information into section2 div element. |
| 34 | + |
| 35 | + function addContributors(contribution) { |
| 36 | + const divElement = []; |
| 37 | + |
| 38 | + divElement.push('<div id="header">', `${'<h3>'} Contributions: ${'</h3>'} ${'</div>'}`); |
| 39 | + |
| 40 | + // eslint-disable-next-line no-restricted-syntax |
| 41 | + for (const details of contribution) { |
| 42 | + divElement.push( |
| 43 | + '<ul>', |
| 44 | + `${'<li>'} ${'<img src="'}${details.avatar_url}" width="40" height="40">`, |
| 45 | + `</li>`, |
| 46 | + `<li>${details.login}</li>`, |
| 47 | + `<li>${details.contributions}</li>`, |
| 48 | + '</ul>', |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + const htmlString = divElement.join(''); |
| 53 | + document.getElementById('contributor-information').innerHTML = htmlString; |
| 54 | + } |
| 55 | + |
| 56 | + // Load the Repository information into section1 div element. |
| 57 | + |
| 58 | + function loadRepoDetails(repoInfo, optionValue) { |
| 59 | + const templateElement = []; |
| 60 | + |
| 61 | + // eslint-disable-next-line no-restricted-syntax |
| 62 | + for (const repo in repoInfo) { |
| 63 | + if (Object.prototype.hasOwnProperty.call(repoInfo, repo)) { |
| 64 | + if (optionValue === repoInfo[repo].name) { |
| 65 | + // format the date |
| 66 | + const upDate = new Date(repoInfo[repo].updated_at); |
| 67 | + const amOrPm = upDate.getHours() < 12 ? 'AM' : 'PM'; |
| 68 | + const dateHours = upDate.getHours() % 12 || 12; |
| 69 | + const formatedUpdate = `${upDate.getMonth()}/${upDate.getDate()}/${upDate.getFullYear()} ${dateHours} |
| 70 | + : ${upDate.getMinutes()}:${upDate.getSeconds()} ${amOrPm}`; |
| 71 | + |
| 72 | + templateElement.push( |
| 73 | + '<div id="row">', |
| 74 | + `${'<p id="name-info">'} Repository name : ${'<a href="'}${ |
| 75 | + repoInfo[repo].html_url |
| 76 | + }"${'/>'} ${repoInfo[repo].name}</a></p>`, |
| 77 | + `${'<p id="desc">'} Description : ${repoInfo[repo].description}</p>`, |
| 78 | + `${'<p id="forks">'} Forks : ${repoInfo[repo].forks_count}</p>`, |
| 79 | + |
| 80 | + `${'<p id="updated">'} Updated : ${formatedUpdate}</p>`, |
| 81 | + '</div>', |
| 82 | + ); |
| 83 | + |
| 84 | + // Call another function(addContributors) to fill i contributor information. |
| 85 | + fetchJSON(repoInfo[repo].contributors_url, (err, data) => { |
| 86 | + const root = document.getElementById('root'); |
| 87 | + |
| 88 | + if (err) { |
| 89 | + createAndAppend('div', root, { text: err.message, class: 'alert-error' }); |
| 90 | + } else { |
| 91 | + addContributors(data); |
| 92 | + } |
| 93 | + }); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + const htmlString = templateElement.join(''); |
| 98 | + document.getElementById('repo-details').innerHTML = htmlString; |
| 99 | + } |
| 100 | + |
| 101 | + // Create options under 'SELECT' element which should have HYF Repositories. |
| 102 | + |
| 103 | + function loadSelectionValues(userRepo) { |
| 104 | + const sortRepoName = []; |
| 105 | + |
| 106 | + const selectRepo = document.getElementById('select-repo'); |
| 107 | + |
| 108 | + // push all the HYP repo names to sort array. |
| 109 | + |
| 110 | + // eslint-disable-next-line no-restricted-syntax |
| 111 | + for (const repo in userRepo) { |
| 112 | + if (Object.prototype.hasOwnProperty.call(userRepo, repo)) { |
| 113 | + sortRepoName.push(userRepo[repo].name); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // sort the repo name using sort function and localeComapare for uppercase and lowercase sorting. |
| 118 | + sortRepoName.sort((a, b) => a.localeCompare(b)); |
| 119 | + |
| 120 | + // Create Option under Select element and attach the same with SELECT element. |
| 121 | + |
| 122 | + // eslint-disable-next-line no-restricted-syntax |
| 123 | + for (const repo of sortRepoName) { |
| 124 | + const option = document.createElement('option'); |
| 125 | + option.value = repo; |
| 126 | + option.text = repo; |
| 127 | + selectRepo.appendChild(option); |
| 128 | + } |
| 129 | + |
| 130 | + const selectBox = document.getElementById('select-repo'); |
| 131 | + const selectedValue = selectBox.options[selectBox.selectedIndex].value; |
| 132 | + |
| 133 | + // Load Repository information for the choose repository name in the select box. |
| 134 | + loadRepoDetails(userRepo, selectedValue); |
| 135 | + selectBox.onchange = function() { |
| 136 | + loadRepoDetails(userRepo, selectBox.value); |
| 137 | + }; |
| 138 | + } |
| 139 | + |
33 | 140 | function main(url) {
|
34 | 141 | fetchJSON(url, (err, data) => {
|
35 | 142 | const root = document.getElementById('root');
|
| 143 | + const BodyEl = document.body; |
| 144 | + |
36 | 145 | if (err) {
|
37 | 146 | createAndAppend('div', root, { text: err.message, class: 'alert-error' });
|
38 | 147 | } else {
|
39 |
| - createAndAppend('pre', root, { text: JSON.stringify(data, null, 2) }); |
| 148 | + // Create div element 'select' in document body to hold the label element and list box. |
| 149 | + |
| 150 | + createAndAppend('div', BodyEl, { id: 'select-container' }); |
| 151 | + const select = document.getElementById('select-container'); |
| 152 | + createAndAppend('LABEL', select, { |
| 153 | + text: 'HYF Repositories: ', |
| 154 | + id: 'label', |
| 155 | + for: 'repo', |
| 156 | + }); |
| 157 | + createAndAppend('select', select, { id: 'select-repo' }); |
| 158 | + |
| 159 | + // Create two div elements section1 and section2 under 'Root' div to have |
| 160 | + // section1 - Repository Information. |
| 161 | + // section2 - Contributions. |
| 162 | + |
| 163 | + createAndAppend('div', root, { id: 'repo-details' }); |
| 164 | + createAndAppend('div', root, { id: 'contributor-information' }); |
| 165 | + |
| 166 | + // Insert section1 before section2 div element under 'root' div. |
| 167 | + const newNode = document.getElementById('contributor-information'); |
| 168 | + const referenceNode = document.querySelector('repo-details'); |
| 169 | + root.insertBefore(newNode, referenceNode); |
| 170 | + |
| 171 | + // Insert Select div first in the body before root div. |
| 172 | + BodyEl.insertBefore(select, document.getElementById('root')); |
| 173 | + |
| 174 | + loadSelectionValues(data); |
40 | 175 | }
|
41 | 176 | });
|
42 | 177 | }
|
|
0 commit comments