/*global WildRydes _config*/
var WildRydes = window.WildRydes || {};
//WildRydes.map = WildRydes.map || {};
(function rideScopeWrapper($) {
var authToken;
WildRydes.authToken.then(function setAuthToken(token) {
if (token) {
authToken = token;
} else {
window.location.href = '/signin.html';
}
}).catch(function handleTokenError(error) {
alert(error);
window.location.href = '/signin.html';
});
function listBoxes() {
$.ajax({
method: 'GET',
url: _config.api.invokeUrl + '/api/ctfdbox',
headers: {
Authorization: authToken
},
success: completeListRequest,
error: function ajaxError(jqXHR, textStatus, errorThrown) {
console.error('Error listing boxes: ', textStatus, ', Details: ', errorThrown);
console.error('Response: ', jqXHR.responseText);
alert('An error occured when listing CTFd boxes:\n' + jqXHR.responseText);
}
});
}
function completeListRequest(result) {
console.log('Response received from API: ', result);
if (result.length > 0) {
bindToTable(result);
}
else {
//display message saying no boxes found
var divContainer = document.getElementById("showData");
divContainer.innerHTML = "
No boxes found
";
}
$('#refresh').disabled = false;
}
function showHiddenCells() {
var tds = document.getElementsByTagName("td");
for (i in tds) {
tds[i].style.display = 'table-cell';
}
}
function bindToTable(data) {
var colTitles = ["DNS Name", "Status", "Start", "Terminate", "On", "Off", "w/e?", "state", "launched", "id", "pwd"];
var colAttributes = ["publicDNS", "status", "startDate", "terminationDate", "onTime", "offTime", "runAtWeekend", "state", "launched", "id", "pwd"];
var table = document.createElement("table");
table.id = "boxes";
table.className = "table";
var tr = table.insertRow(-1); // TABLE ROW.
for (var i = 0; i < colTitles.length; i++) {
var th = document.createElement("th"); // TABLE HEADER.
th.innerHTML = colTitles[i];
tr.appendChild(th);
}
// ADD JSON DATA TO THE TABLE AS ROWS.
for (var i = 0; i < data.length; i++) {
tr = table.insertRow(-1);
for (var j = 0; j < colAttributes.length; j++) {
var tabCell = tr.insertCell(-1);
tabCell.innerHTML = data[i][colAttributes[j]];
if (colAttributes[j] == "pwd") {
tabCell.style.display = "none";
}
else {
tabCell.style.display = 'table-cell';
}
}
}
// FINALLY ADD THE NEWLY CREATED TABLE WITH JSON DATA TO A CONTAINER.
var divContainer = document.getElementById("showData");
divContainer.innerHTML = "";
divContainer.appendChild(table);
}
function launchBox() {
$.ajax({
method: 'POST',
url: _config.api.invokeUrl + '/api/ctfdbox',
headers: {
Authorization: authToken
},
data: JSON.stringify({
eventType: $('#eventType').val(),
onTime: $('#onTime').val(),
offTime: $('#offTime').val(),
startDate: $('#startDate').val(),
terminationDate: $('#terminationDate').val(),
runAtWeekends: document.getElementById('runAtWeekends').checked
}),
contentType: 'application/json',
success: completeLaunchRequest,
error: function ajaxError(jqXHR, textStatus, errorThrown) {
console.error('Error launching box: ', textStatus, ', Details: ', errorThrown);
console.error('Response: ', jqXHR.responseText);
alert('An error occured when launching your box:\n' + jqXHR.responseText);
}
});
}
function completeLaunchRequest(result) {
console.log('Response received from API: ', result);
alert('Box is being built. Instance id is: ' + result + '. Click the "refresh list" button to see its progress');
$('#launch').disabled = false;
}
// Register click handler for #request button
$(function onDocReady() {
listBoxes();
$('#refresh').click(handleRefreshClick);
$('#pwds').click(handleShowPwdsClick);
$('#launch').click(handleLaunchClick);
populateInputs();
});
function populateInputs() {
const today = new Date();
const tomorrow = new Date();
const theDayAfter = new Date();
tomorrow.setDate(today.getDate() + 1);
theDayAfter.setDate(tomorrow.getDate() + 1);
var dd = String(tomorrow.getDate()).padStart(2, '0');
var mm = String(tomorrow.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = tomorrow.getFullYear();
startDate = dd + '/' + mm + '/' + yyyy;
$('#startDate').val(startDate);
dd = String(theDayAfter.getDate()).padStart(2, '0');
mm = String(theDayAfter.getMonth() + 1).padStart(2, '0'); //January is 0!
yyyy = theDayAfter.getFullYear();
terminationDate = dd + '/' + mm + '/' + yyyy;
$('#terminationDate').val(terminationDate);
}
function handleRefreshClick(event) {
$('#refresh').disabled = true;
event.preventDefault();
listBoxes();
}
function handleLaunchClick(event) {
$('#launch').disabled = true;
event.preventDefault();
launchBox();
}
function handleShowPwdsClick(event) {
event.preventDefault();
showHiddenCells();
}
function displayUpdate(text) {
$('#updates').append($('' + text + ''));
}
}(jQuery));