You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
3.3 KiB
122 lines
3.3 KiB
2 years ago
|
"use strict";
|
||
|
|
||
|
function openTournament(tournament: Tournament){
|
||
|
$('#tournament-selection > button').attr('disabled', 'disabled');
|
||
|
//Enable Loading gif
|
||
|
|
||
|
tournament.fetchTeams().then(() => {
|
||
|
tournament.buildMainHtml();
|
||
|
|
||
|
$('#tournament-selection').hide();
|
||
|
//Disable Loading gif
|
||
|
|
||
|
$('#tournament-teams').show();
|
||
|
}, e => {
|
||
|
console.error('Cant fetch teams', e);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
function openTournamentWithId(){
|
||
|
$('#tournament-selection > button').attr('disabled', 'disabled');
|
||
|
let id = $('#tournament-selection > input[name=id]').val() as string;
|
||
|
fetchTournament(id).then(openTournament, goBackToSelection);
|
||
|
}
|
||
|
|
||
|
function goBackToSelection(){
|
||
|
$('#tournament-teams').hide();
|
||
|
|
||
|
$('#tournament-selection > button').attr('disabled', null);
|
||
|
$('#tournament-selection').show();
|
||
|
}
|
||
|
|
||
|
let delay = (() => {
|
||
|
let timer = 0;
|
||
|
return function(callback, ms){
|
||
|
clearTimeout(timer);
|
||
|
// @ts-ignore
|
||
|
timer = setTimeout(callback, ms);
|
||
|
};
|
||
|
})();
|
||
|
|
||
|
function filterFromSearch(element){
|
||
|
let query = element.value.toLowerCase();
|
||
|
|
||
|
$('.team-teaser').each(function() {
|
||
|
let name = this.getElementsByTagName("span")[0].textContent.toLowerCase();
|
||
|
if (name.indexOf(query) != -1){
|
||
|
$(this).show();
|
||
|
} else {
|
||
|
$(this).hide();
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
async function fetchTournament(id: string){
|
||
|
return new Tournament(await APICall("viewer/v2/tournaments/" + id));
|
||
|
}
|
||
|
|
||
|
function APICall(endpoint: string, parameters?: Object, headers?: Object){
|
||
|
|
||
|
if (parameters){
|
||
|
endpoint += "?";
|
||
|
for (let key in parameters){
|
||
|
endpoint += key + "=" + parameters[key] + "&";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return new Promise((resolve: (value: Object) => void, reject) => {
|
||
|
$.get({
|
||
|
url: "data/php/api_request.php",
|
||
|
method: "POST",
|
||
|
data: {
|
||
|
endpoint: endpoint,
|
||
|
headers: headers
|
||
|
}
|
||
|
}).done((data, result, request) => {
|
||
|
if (!data){
|
||
|
reject('API Call failed at endpoint ' + endpoint);
|
||
|
} else {
|
||
|
try{
|
||
|
resolve(JSON.parse(data));
|
||
|
} catch (e) {
|
||
|
reject('Failed to resolve API response at endpoint ' + endpoint + ' \nReason: ' + e);
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
|
||
|
function fallbackCopyTextToClipboard(text) {
|
||
|
let textArea = document.createElement("textarea");
|
||
|
textArea.value = text;
|
||
|
|
||
|
// Avoid scrolling to bottom
|
||
|
textArea.style.top = "0";
|
||
|
textArea.style.left = "0";
|
||
|
textArea.style.position = "fixed";
|
||
|
|
||
|
document.body.appendChild(textArea);
|
||
|
textArea.focus();
|
||
|
textArea.select();
|
||
|
|
||
|
try {
|
||
|
let successful = document.execCommand('copy');
|
||
|
let msg = successful ? 'successful' : 'unsuccessful';
|
||
|
console.log('Fallback: Copying text command was ' + msg);
|
||
|
} catch (err) {
|
||
|
console.error('Fallback: Oops, unable to copy', err);
|
||
|
}
|
||
|
|
||
|
document.body.removeChild(textArea);
|
||
|
}
|
||
|
function copyTextToClipboard(text) {
|
||
|
if (!navigator.clipboard) {
|
||
|
fallbackCopyTextToClipboard(text);
|
||
|
return;
|
||
|
}
|
||
|
navigator.clipboard.writeText(text).then(function() {
|
||
|
console.log('Async: Copying to clipboard was successful!');
|
||
|
}, function(err) {
|
||
|
console.error('Async: Could not copy text: ', err);
|
||
|
});
|
||
|
}
|