2017-06-16 01:20:27 +02:00
|
|
|
window.addEventListener('load', () => {
|
2018-09-17 00:02:55 +02:00
|
|
|
initDT() // Initialize the DatatTable and window.columnNames variables
|
|
|
|
|
2018-10-05 03:29:09 +02:00
|
|
|
const repo = getRepoFromUrl();
|
|
|
|
|
2017-06-16 01:20:27 +02:00
|
|
|
if (repo) {
|
|
|
|
document.getElementById('q').value = repo;
|
|
|
|
fetchData();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-03-30 20:17:39 +02:00
|
|
|
document.getElementById('form').addEventListener('submit', (e) => {
|
2017-06-16 01:20:27 +02:00
|
|
|
e.preventDefault();
|
|
|
|
fetchData();
|
|
|
|
});
|
2017-03-30 20:17:39 +02:00
|
|
|
|
2017-03-30 19:44:00 +02:00
|
|
|
function fetchData() {
|
2017-06-16 01:20:27 +02:00
|
|
|
const repo = document.getElementById('q').value;
|
|
|
|
const re = /[-_\w]+\/[-_.\w]+/;
|
|
|
|
|
2018-10-05 03:29:09 +02:00
|
|
|
const urlRepo = getRepoFromUrl();
|
|
|
|
|
|
|
|
if (!urlRepo || urlRepo !== repo) {
|
2018-10-08 22:08:32 +02:00
|
|
|
window.history.pushState('', '', `#${repo}`);
|
2018-10-05 03:29:09 +02:00
|
|
|
}
|
2017-03-30 19:44:00 +02:00
|
|
|
|
|
|
|
if (re.test(repo)) {
|
2017-06-16 01:20:27 +02:00
|
|
|
fetchAndShow(repo);
|
2017-03-30 19:44:00 +02:00
|
|
|
} else {
|
2017-06-16 01:20:27 +02:00
|
|
|
showMsg('Invalid GitHub repository! Format is <username>/<repo>', 'danger');
|
2017-03-30 19:44:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-17 00:02:55 +02:00
|
|
|
function updateDT( data ) {
|
|
|
|
// Remove any alerts, if any:
|
|
|
|
if ( $( '.alert' ) )
|
|
|
|
$( '.alert' ).remove()
|
|
|
|
|
|
|
|
// Format dataset and redraw DataTable. Use second index for key name
|
|
|
|
const forks = []
|
|
|
|
for ( let fork of data ) {
|
|
|
|
fork.repoLink = `<a href="https://github.com/${fork.full_name}">Link</a>`
|
|
|
|
fork.ownerName = fork.owner.login
|
2018-10-05 23:26:29 +02:00
|
|
|
fork.pushed_at = moment(fork.pushed_at).fromNow()
|
2018-09-17 00:02:55 +02:00
|
|
|
forks.push( fork )
|
|
|
|
}
|
|
|
|
const dataSet = forks.map( fork => window.columnNamesMap.map( colNM => fork[colNM[1]] ) )
|
|
|
|
window.forkTable.clear().rows.add( dataSet ).draw()
|
|
|
|
}
|
|
|
|
|
|
|
|
function initDT() {
|
|
|
|
// Create ordered Object with column name and mapped display name
|
|
|
|
window.columnNamesMap = [
|
|
|
|
// [ 'Repository', 'full_name' ],
|
|
|
|
['Link', 'repoLink'], // custom key
|
|
|
|
['Owner', 'ownerName'], // custom key
|
|
|
|
['Name', 'name'],
|
|
|
|
['Branch', 'default_branch'],
|
|
|
|
['Stars', 'stargazers_count'],
|
|
|
|
['Forks', 'forks'],
|
2018-10-05 23:27:02 +02:00
|
|
|
['Open Issues', 'open_issues_count'],
|
2018-09-17 00:02:55 +02:00
|
|
|
['Size', 'size'],
|
|
|
|
['Last Push', 'pushed_at'],
|
|
|
|
]
|
|
|
|
|
|
|
|
// Sort by stars:
|
|
|
|
const sortColName = 'Stars'
|
|
|
|
const sortColumnIdx = window.columnNamesMap.map( pair => pair[0] ).indexOf( sortColName )
|
|
|
|
|
|
|
|
// Use first index for readable column name
|
|
|
|
window.forkTable = $( '#forkTable' ).DataTable( {
|
|
|
|
columns: window.columnNamesMap.map( colNM => {
|
|
|
|
return {'title': colNM[0]}
|
|
|
|
} ),
|
|
|
|
'order': [[sortColumnIdx, 'desc']],
|
|
|
|
} )
|
|
|
|
}
|
2017-06-16 01:20:27 +02:00
|
|
|
|
2018-09-17 00:02:55 +02:00
|
|
|
function fetchAndShow( repo ) {
|
2018-10-18 21:26:55 +02:00
|
|
|
|
|
|
|
repo = repo.replace('https://github.com/', '');
|
|
|
|
repo = repo.replace('http://github.com/', '');
|
|
|
|
repo = repo.replace('.git', '');
|
2017-06-16 01:20:27 +02:00
|
|
|
|
2018-10-30 04:08:33 +01:00
|
|
|
fetch( `https://api.github.com/repos/${repo}/forks?sort=stargazers&per_page=100` )
|
2018-09-17 00:02:55 +02:00
|
|
|
.then( ( response ) => {
|
|
|
|
if ( !response.ok )
|
|
|
|
throw Error( response.statusText )
|
|
|
|
return response.json()
|
|
|
|
} )
|
|
|
|
.then( ( data ) => {
|
|
|
|
console.log( data )
|
|
|
|
updateDT( data )
|
|
|
|
} )
|
|
|
|
.catch( ( error ) => {
|
|
|
|
const msg = error.toString().indexOf( 'Forbidden' ) >= 0 ? 'Error: API Rate Limit Exceeded' : error
|
|
|
|
showMsg( `${msg}. Additional info in console`, 'danger' )
|
|
|
|
console.error( error )
|
|
|
|
} )
|
2017-03-30 19:44:00 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 01:20:27 +02:00
|
|
|
function showMsg(msg, type) {
|
|
|
|
let alert_type = 'alert-info';
|
|
|
|
|
|
|
|
if (type === 'danger') {
|
|
|
|
alert_type = 'alert-danger';
|
|
|
|
}
|
|
|
|
|
|
|
|
document.getElementById('footer').innerHTML = '';
|
|
|
|
|
|
|
|
document.getElementById('data-body').innerHTML = `
|
|
|
|
<div class="alert ${alert_type} alert-dismissible fade show" role="alert">
|
|
|
|
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
|
|
|
|
<span aria-hidden="true">×</span>
|
|
|
|
</button>
|
|
|
|
${msg}
|
|
|
|
</div>
|
|
|
|
`;
|
2017-03-30 19:44:00 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 03:29:09 +02:00
|
|
|
function getRepoFromUrl() {
|
|
|
|
const urlRepo = location.hash && location.hash.slice(1);
|
2017-06-16 01:20:27 +02:00
|
|
|
|
2018-10-05 03:29:09 +02:00
|
|
|
return urlRepo && decodeURIComponent(urlRepo);
|
2017-03-30 19:44:00 +02:00
|
|
|
}
|