Modernise JS code style

This commit is contained in:
Oliver Hamlet
2022-08-23 22:59:03 +01:00
parent 2c7e2cad9b
commit 1b0cd35614
3 changed files with 85 additions and 91 deletions
+2 -2
View File
@@ -68,8 +68,8 @@ function handleFileSelect(evt) {
evt.stopPropagation();
evt.preventDefault();
var files = evt.dataTransfer.files; // FileList object.
var reader = new FileReader();
const files = evt.dataTransfer.files; // FileList object.
const reader = new FileReader();
reader.onload = function(event) {
const input = document.getElementById('input');
input.value = event.target.result;
+56 -60
View File
@@ -1,15 +1,11 @@
'use strict';
var numRepos = 0;
var numProcessedRepos = 0;
var contributors = [];
function addToList(listElement, person) {
var a = document.createElement('a');
var avatar = document.createElement('div');
var text = document.createElement('div');
var name = document.createElement('div');
var contributions = document.createElement('div');
const a = document.createElement('a');
const avatar = document.createElement('div');
const text = document.createElement('div');
const name = document.createElement('div');
const contributions = document.createElement('div');
a.classList.add('loot-contributor', 'mdl-shadow--2dp');
@@ -23,11 +19,11 @@ function addToList(listElement, person) {
contributions.textContent = person.contributions + ' contributions';
if (person.avatar_url) {
var img = document.createElement('img');
const img = document.createElement('img');
img.src = person.avatar_url;
avatar.appendChild(img);
} else {
var icon = document.createElement('i');
const icon = document.createElement('i');
icon.className = 'material-icons';
icon.textContent = 'face';
avatar.appendChild(icon);
@@ -48,60 +44,60 @@ function sortContributors(a,b) {
}
}
function getContributor(name) {
for (var i = 0; i < contributors.length; ++i) {
if (contributors[i].name === name) {
return i;
}
}
return -1;
function getStats(contributor) {
const isAnon = contributor.author.type === 'Anonymous';
return {
contributions: contributor.total,
name: isAnon ? contributor.author.name : contributor.author.login,
avatar_url: isAnon ? undefined : contributor.author.avatar_url,
html_url: isAnon ? 'mailto:' + contributor.author.email : contributor.author.html_url
};
}
function storeRepositoryContributors(response) {
response.data.forEach(function(contributor) {
var stats = {
contributions: contributor.total
}
if (contributor.author.type == 'Anonymous') {
// Doesn't have a GitHub account. Leave avatar URL undefined.
stats.name = contributor.author.name;
stats.html_url = 'mailto:' + contributor.author.email;
} else {
stats.name = contributor.author.login;
stats.avatar_url = contributor.author.avatar_url;
stats.html_url = contributor.author.html_url;
}
var j = getContributor(stats.name);
if (j === -1) {
contributors.push(stats);
} else {
contributors[j].contributions += stats.contributions;
}
});
numProcessedRepos += 1;
function statsReducer(previous, current) {
const existing = previous.find(element => element.name === current.name);
if (existing === undefined) {
return previous.concat(current);
}
if (numProcessedRepos === numRepos) {
var contrib = document.getElementById('contrib');
contributors.sort(sortContributors);
contrib.removeChild(contrib.firstElementChild);
for (var i = 0; i < contributors.length; i++) {
addToList(contrib, contributors[i]);
}
existing.contributions += current.contributions;
return previous;
}
function displayContributorsStats(contributorsStats) {
const contrib = document.getElementById('contrib');
contrib.removeChild(contrib.firstElementChild);
for (const contributor of contributorsStats) {
addToList(contrib, contributor);
}
}
function getContributors(response) {
numRepos = 0;
var github = new GitHub();
response.data.filter(function(repo) {
return !repo.fork;
}).forEach(function(repo) {
github.getRepo('loot', repo.name)
.getContributors().then(storeRepositoryContributors);
numRepos += 1;
});
function getContributorsStats(contributors) {
return contributors
.map(getStats)
.reduce(statsReducer, [])
.sort(sortContributors);
}
// Now fetch the organisation repositories.
(new GitHub()).getOrganization('loot').getRepos().then(getContributors);
async function getContributors() {
const github = new GitHub();
const reposResponse = await github.getOrganization('loot').getRepos();
const promises = reposResponse.data
.filter(repo => !repo.fork)
.map(repo => github.getRepo('loot', repo.name).getContributors());
return Promise.all(promises).then(results => results.flat());
}
async function getAndDisplayContributorsStats() {
const contributors = await getContributors();
const stats = getContributorsStats(contributors);
displayContributorsStats(stats);
}
getAndDisplayContributorsStats();
+27 -29
View File
@@ -3,7 +3,7 @@
// Globals
///////////////////
var repos = [
const repos = [
[ "Morrowind", "morrowind" ],
[ "Oblivion", "oblivion" ],
[ "Nehrim", "oblivion" ],
@@ -17,13 +17,13 @@ var repos = [
[ "Fallout 4 VR", "fallout4vr" ],
]
var repoBranch = 'master'; //The repository branch to search.
const gameSelect = document.getElementById('gameSelectMenu');
const gameButton = document.getElementById(gameSelect.getAttribute('for'));
const searchButton = document.getElementById('searchButton');
const searchBox = document.getElementById('searchBox');
const resultsDiv = document.getElementById('results');
var gameSelect = document.getElementById('gameSelectMenu');
var gameButton = document.getElementById(gameSelect.getAttribute('for'));
var searchButton = document.getElementById('searchButton');
var searchBox = document.getElementById('searchBox');
var resultsDiv = document.getElementById('results');
const newLineRegex = new RegExp('\n', 'g');
// Functions
///////////////////
@@ -34,33 +34,31 @@ function isRegexEntry(name) {
}
function readMasterlist(response) {
var masterlist = jsyaml.load(response.data);
const masterlist = load(response.data);
document.getElementById('progress').classList.add('hidden');
/* Do search here. */
console.log("Starting search.");
if (masterlist.hasOwnProperty('plugins')) {
for (var i in masterlist['plugins']) {
var index = -1;
if (isRegexEntry(masterlist["plugins"][i].name)) {
if (RegExp(masterlist["plugins"][i].name, 'i').test(searchBox.value)) {
index = i;
}
} else if (masterlist["plugins"][i].name.toLowerCase().indexOf(searchBox.value.toLowerCase()) !== -1) {
index = i;
}
if (index != -1) {
console.log("Match: " + JSON.stringify(masterlist["plugins"][index]));
var code = document.createElement('code');
code.className = 'loot-search-result';
code.textContent = ' - ' + jsyaml.dump(masterlist["plugins"][index]).replace(new RegExp('\n', 'g'), '\n ').trim();
resultsDiv.appendChild(code);
const matches = masterlist.plugins.filter(plugin => {
if (isRegexEntry(plugin.name)) {
return RegExp(plugin.name, 'i').test(searchBox.value);
}
return plugin.name.toLowerCase().includes(searchBox.value.toLowerCase());
});
for (const match of matches) {
console.log("Match: " + JSON.stringify(match));
const code = document.createElement('code');
code.className = 'loot-search-result';
code.textContent = ' - ' + dump(match).replace(newLineRegex, '\n ').trim();
resultsDiv.appendChild(code);
}
}
if (resultsDiv.children.length == 0) {
var elem = document.createElement('code');
if (resultsDiv.children.length === 0) {
const elem = document.createElement('code');
elem.className = 'loot-search-result';
elem.textContent = "No matching entries found.";
resultsDiv.appendChild(elem);
@@ -137,11 +135,11 @@ function getURLParameters() {
///////////////////
/* Fill the drop-down games box with stuff. */
for (var i=0; i < repos.length; ++i) {
var option = document.createElement('li');
for (const repo of repos) {
const option = document.createElement('li');
option.className = 'mdl-menu__item';
option.textContent = repos[i][0];
option.setAttribute('data-game', repos[i][1]);
option.textContent = repo[0];
option.setAttribute('data-game', repo[1]);
gameSelect.appendChild(option);
option.addEventListener('click', onGameSelect, false);
}