2019-01-28 05:39:48 +00:00
|
|
|
// Hack to change endpoint url for each OS
|
|
|
|
function getEndpointDomain() {
|
|
|
|
let OSName;
|
|
|
|
let domain;
|
|
|
|
|
|
|
|
if (navigator.appVersion.indexOf("Win") !== -1)
|
|
|
|
OSName = "Windows";
|
|
|
|
else if (navigator.appVersion.indexOf("Mac") !== -1)
|
|
|
|
OSName = "MacOS";
|
|
|
|
else if (navigator.appVersion.indexOf("X11") !== -1)
|
|
|
|
OSName = "UNIX";
|
|
|
|
else if (navigator.appVersion.indexOf("Linux") !== -1)
|
|
|
|
OSName = "Linux";
|
|
|
|
else
|
|
|
|
OSName = "Unknown OS";
|
|
|
|
|
2019-02-02 06:24:18 +00:00
|
|
|
console.log("Detected operating system: " + OSName);
|
2019-01-28 05:39:48 +00:00
|
|
|
|
|
|
|
if (OSName === "Windows") {
|
|
|
|
domain = "https://192.168.99.100:8444/";
|
|
|
|
} else {
|
|
|
|
domain = "https://localhost:8444/"
|
|
|
|
}
|
|
|
|
|
|
|
|
return domain;
|
|
|
|
}
|
|
|
|
|
2019-02-01 07:49:36 +00:00
|
|
|
// Make a GET request to url and pass response to callback function
|
2019-01-30 23:42:09 +00:00
|
|
|
function getDataFromEndpoint(url, callback) {
|
|
|
|
const token = localStorage.getItem("token");
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
|
2019-02-02 06:24:18 +00:00
|
|
|
console.log("Attempting a connection to the following endpoint: " + url);
|
2019-01-30 23:42:09 +00:00
|
|
|
|
|
|
|
xhr.open("GET", url, true);
|
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
if (this.readyState === 4) {
|
|
|
|
if (this.status === 200) {
|
|
|
|
console.log("GET SUCCESS!");
|
2019-02-02 06:24:18 +00:00
|
|
|
console.log("Server response:\n" + this.response);
|
2019-01-30 23:42:09 +00:00
|
|
|
parsedData = JSON.parse(this.response);
|
|
|
|
callback(parsedData);
|
|
|
|
} else {
|
|
|
|
console.error("GET FAILURE!");
|
2019-02-02 06:24:18 +00:00
|
|
|
console.error("Server status: " + this.status);
|
|
|
|
console.error("Server response:\n" + this.response);
|
2019-01-30 23:42:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.onerror = function() {
|
|
|
|
alert("Connection error!");
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.send();
|
|
|
|
}
|
|
|
|
|
2019-02-01 07:49:36 +00:00
|
|
|
// Wraps a Bootstrap form group around a field
|
2019-01-31 23:48:33 +00:00
|
|
|
function createFormGroup(key, field) {
|
2019-02-01 00:27:29 +00:00
|
|
|
const formGroup = document.createElement("div")
|
2019-01-31 07:54:27 +00:00
|
|
|
formGroup.classList.add("form-group");
|
|
|
|
|
2019-02-01 00:27:29 +00:00
|
|
|
const label = document.createElement("label");
|
2019-01-31 07:54:27 +00:00
|
|
|
label.innerHTML = field.label;
|
2019-01-31 23:48:33 +00:00
|
|
|
label.setAttribute("for", key);
|
2019-02-01 07:49:36 +00:00
|
|
|
|
2019-02-01 00:27:29 +00:00
|
|
|
const input = document.createElement("input");
|
2019-01-31 23:48:33 +00:00
|
|
|
input.name = key;
|
|
|
|
input.id = key;
|
2019-01-31 07:54:27 +00:00
|
|
|
|
|
|
|
switch(field.type) {
|
|
|
|
case "boolean":
|
|
|
|
input.type = "checkbox";
|
|
|
|
if (field.value === true)
|
|
|
|
input.setAttribute("checked", "checked");
|
2019-02-01 01:48:36 +00:00
|
|
|
input.classList.add("form-check-input");
|
2019-02-01 00:27:29 +00:00
|
|
|
formGroup.classList.add("form-check");
|
|
|
|
label.classList.add("form-check-label");
|
2019-02-01 01:48:36 +00:00
|
|
|
formGroup.appendChild(input); // Reversed order compared to others
|
|
|
|
formGroup.appendChild(label);
|
2019-01-31 07:54:27 +00:00
|
|
|
break;
|
|
|
|
case "date":
|
|
|
|
input.type = "datetime";
|
|
|
|
input.value = field.value;
|
2019-02-01 01:48:36 +00:00
|
|
|
input.classList.add("form-control");
|
2019-01-31 07:54:27 +00:00
|
|
|
formGroup.appendChild(label);
|
|
|
|
formGroup.appendChild(input);
|
|
|
|
break;
|
|
|
|
case "decimal":
|
|
|
|
input.type = "text";
|
|
|
|
input.value = field.value;
|
2019-02-01 01:48:36 +00:00
|
|
|
input.classList.add("form-control");
|
2019-01-31 07:54:27 +00:00
|
|
|
formGroup.appendChild(label);
|
|
|
|
formGroup.appendChild(input);
|
|
|
|
break;
|
|
|
|
case "file":
|
|
|
|
input.type = "file";
|
2019-02-01 01:48:36 +00:00
|
|
|
input.classList.add("form-control-file");
|
2019-02-01 07:49:36 +00:00
|
|
|
formGroup.appendChild(label);
|
|
|
|
formGroup.appendChild(input);
|
|
|
|
let uploadMessage = document.createTextNode("Uploaded file:");
|
|
|
|
formGroup.appendChild(uploadMessage);
|
2019-01-31 07:54:27 +00:00
|
|
|
const link = document.createElement("a");
|
|
|
|
link.href = field.value;
|
|
|
|
link.innerHTML = field.value;
|
|
|
|
formGroup.appendChild(link);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return formGroup;
|
|
|
|
}
|
|
|
|
|
2019-01-31 23:48:33 +00:00
|
|
|
function createCollapsibleCard(key, sectionTitle) {
|
|
|
|
// Create card and header
|
|
|
|
const card = document.createElement("div");
|
|
|
|
card.classList.add("card");
|
|
|
|
const cardHeader = document.createElement("div");
|
|
|
|
cardHeader.classList.add("card-header");
|
|
|
|
|
|
|
|
// Create h2, button. Append button to h2, h2 to header, and header to card
|
|
|
|
const h2 = document.createElement("h2");
|
|
|
|
const button = document.createElement("button");
|
|
|
|
button.classList.add("btn", "btn-link");
|
|
|
|
button.type = "button";
|
|
|
|
button.setAttribute("data-toggle", "collapse");
|
|
|
|
button.setAttribute("data-target", "#collapse" + key);
|
|
|
|
button.innerHTML = sectionTitle;
|
|
|
|
h2.appendChild(button);
|
|
|
|
cardHeader.appendChild(h2);
|
|
|
|
card.appendChild(cardHeader);
|
|
|
|
|
|
|
|
return card;
|
|
|
|
}
|
|
|
|
|
2019-02-01 07:49:36 +00:00
|
|
|
function createCollapsibleCardBody(key, form, sectionDescription, sectionCompleted) {
|
2019-02-01 00:27:29 +00:00
|
|
|
// Create wrapper div
|
2019-01-31 23:48:33 +00:00
|
|
|
const div = document.createElement("div");
|
2019-02-01 07:49:36 +00:00
|
|
|
sectionCompleted ? div.classList.add("collapse") : div.classList.add("collapse", "show");
|
2019-01-31 23:48:33 +00:00
|
|
|
div.setAttribute("data-parent", "#editReportAccordion");
|
|
|
|
div.id = "collapse" + key;
|
2019-02-01 00:27:29 +00:00
|
|
|
|
|
|
|
// Create card body. Append form to body, body to wrapper div
|
2019-01-31 23:48:33 +00:00
|
|
|
const cardBody = document.createElement("div");
|
|
|
|
cardBody.classList.add("card-body");
|
2019-02-01 00:27:29 +00:00
|
|
|
cardBody.insertAdjacentHTML("beforeend", sectionDescription);
|
2019-01-31 23:48:33 +00:00
|
|
|
cardBody.appendChild(form);
|
|
|
|
div.appendChild(cardBody);
|
|
|
|
|
|
|
|
return div;
|
|
|
|
}
|
2019-01-31 07:54:27 +00:00
|
|
|
|
2019-01-31 23:48:33 +00:00
|
|
|
function createEditReportForm(parsedData) {
|
2019-02-01 01:48:36 +00:00
|
|
|
const col = document.querySelector(".col-sm-8");
|
2019-01-30 23:42:09 +00:00
|
|
|
const fragment = document.createDocumentFragment();
|
|
|
|
|
2019-02-01 01:48:36 +00:00
|
|
|
while (col.firstChild) {
|
|
|
|
col.removeChild(col.firstChild)
|
2019-01-30 23:42:09 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 23:48:33 +00:00
|
|
|
// Add report title and date to card header
|
2019-02-01 01:48:36 +00:00
|
|
|
const reportTitle = parsedData.title;
|
|
|
|
const dateCreated = new Date(parsedData.date_created).toLocaleDateString("en-US");
|
|
|
|
const h3 = document.createElement("h3");
|
2019-02-02 06:24:18 +00:00
|
|
|
h3.innerHTML = reportTitle + " " + dateCreated;
|
2019-02-01 01:48:36 +00:00
|
|
|
h3.classList.add("text-center");
|
|
|
|
fragment.appendChild(h3);
|
2019-01-31 07:54:27 +00:00
|
|
|
|
2019-01-31 23:48:33 +00:00
|
|
|
// Create accordion
|
|
|
|
const accordion = document.createElement("div");
|
|
|
|
accordion.classList.add("accordion");
|
|
|
|
accordion.id = "editReportAccordion";
|
2019-01-31 07:54:27 +00:00
|
|
|
|
2019-01-30 23:42:09 +00:00
|
|
|
|
2019-02-01 08:07:08 +00:00
|
|
|
// Traverse the report's sections array
|
2019-01-30 23:42:09 +00:00
|
|
|
const sections = parsedData.sections;
|
2019-01-31 23:48:33 +00:00
|
|
|
for (let key in sections) {
|
|
|
|
let section = sections[key];
|
2019-02-01 08:07:08 +00:00
|
|
|
let collapsibleCard = createCollapsibleCard(key, section.title)
|
2019-01-31 23:48:33 +00:00
|
|
|
|
|
|
|
// Create a new form with the section key index as id
|
|
|
|
let form = document.createElement("form");
|
|
|
|
form.classList.add("form");
|
|
|
|
form.id = "form" + key;
|
|
|
|
|
|
|
|
// Traverse the fields of this section
|
|
|
|
let fields = section.fields;
|
|
|
|
for (let key in fields) {
|
|
|
|
let field = fields[key];
|
2019-02-01 08:07:08 +00:00
|
|
|
|
2019-02-02 06:24:18 +00:00
|
|
|
console.log("Field label: " + field.label);
|
|
|
|
console.log("Field type: " + field.type);
|
|
|
|
console.log("Field value: " + field.value);
|
2019-01-30 23:42:09 +00:00
|
|
|
|
2019-02-01 08:07:08 +00:00
|
|
|
// Create a form group for each field and add it to the form
|
2019-01-31 23:48:33 +00:00
|
|
|
let formGroup = createFormGroup(key, field);
|
2019-01-31 07:54:27 +00:00
|
|
|
form.appendChild(formGroup);
|
2019-01-30 23:42:09 +00:00
|
|
|
}
|
2019-01-31 07:54:27 +00:00
|
|
|
|
2019-02-01 08:07:08 +00:00
|
|
|
// Add save button to the current form
|
2019-02-01 01:48:36 +00:00
|
|
|
let saveButton = document.createElement("button");
|
|
|
|
saveButton.innerHTML = "Save";
|
|
|
|
saveButton.type = "submit";
|
|
|
|
saveButton.classList.add("btn", "btn-primary"); // TODO: add eventListener
|
|
|
|
form.appendChild(saveButton);
|
|
|
|
|
2019-02-01 00:27:29 +00:00
|
|
|
// Create collapsible card body, append form to it, append card to accordion
|
2019-02-01 07:49:36 +00:00
|
|
|
let cardBody = createCollapsibleCardBody(key, form, section.html_description, section.completed);
|
2019-02-01 08:07:08 +00:00
|
|
|
collapsibleCard.appendChild(cardBody);
|
|
|
|
accordion.appendChild(collapsibleCard);
|
2019-01-31 23:48:33 +00:00
|
|
|
}
|
|
|
|
|
2019-02-01 01:48:36 +00:00
|
|
|
// Add submit button to accordion
|
|
|
|
let submitButton = document.createElement("button");
|
|
|
|
submitButton.innerHTML = "Submit Report";
|
|
|
|
submitButton.type = "submit";
|
|
|
|
submitButton.classList.add("btn", "btn-primary", "btn-lg", "btn-block"); // TODO: add eventListener
|
|
|
|
accordion.appendChild(submitButton);
|
|
|
|
|
2019-01-31 23:48:33 +00:00
|
|
|
fragment.appendChild(accordion)
|
2019-02-01 01:48:36 +00:00
|
|
|
col.appendChild(fragment);
|
2019-01-30 23:42:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function displayListOfReports(parsedData) {
|
2019-01-28 00:21:51 +00:00
|
|
|
const cardBody = document.querySelector(".card-body");
|
|
|
|
const table = document.createElement("table");
|
2019-01-30 23:42:09 +00:00
|
|
|
const reports = parsedData.reports;
|
|
|
|
let reportsAdded = 0;
|
2019-01-28 00:21:51 +00:00
|
|
|
|
2019-01-30 23:42:09 +00:00
|
|
|
// Create report table
|
2019-01-28 00:21:51 +00:00
|
|
|
for (let i = 0; i < reports.length; i++) {
|
2019-01-30 23:42:09 +00:00
|
|
|
let title = reports[i].title;
|
|
|
|
let dateCreated = new Date(reports[i].date_created).toLocaleDateString("en-US");
|
|
|
|
let state = reports[i].state;
|
|
|
|
let dateSubmitted;
|
|
|
|
let rid = reports[i].report_pk;
|
|
|
|
|
|
|
|
// Create edit/view button
|
|
|
|
let actionButton = document.createElement("button");
|
|
|
|
actionButton.type = "submit";
|
|
|
|
actionButton.setAttribute("data-rid", rid);
|
|
|
|
actionButton.classList.add("btn");
|
|
|
|
|
|
|
|
if (state === "created") {
|
|
|
|
// Edit button
|
|
|
|
dateSubmitted = "TBD";
|
|
|
|
actionButton.classList.add("btn-primary");
|
|
|
|
actionButton.innerHTML = "Edit";
|
|
|
|
actionButton.addEventListener("click", openEditReportForm);
|
|
|
|
} else {
|
|
|
|
// View button
|
|
|
|
dateSubmitted = new Date(reports[i].date_submitted).toLocaleDateString("en-US");
|
|
|
|
actionButton.classList.add("btn-success");
|
|
|
|
actionButton.innerHTML = "View";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert data into the table object
|
|
|
|
let bodyRow = table.insertRow(i);
|
|
|
|
bodyRow.insertCell(0).innerHTML = title;
|
|
|
|
bodyRow.insertCell(1).innerHTML = dateCreated;
|
|
|
|
|
|
|
|
let stateCell = bodyRow.insertCell(2);
|
|
|
|
stateCell.innerHTML = state;
|
|
|
|
stateCell.classList.add("d-none", "d-lg-table-cell"); // Column visible only on large displays
|
|
|
|
|
|
|
|
let dateSubmittedCell = bodyRow.insertCell(3);
|
|
|
|
dateSubmittedCell.innerHTML = dateSubmitted;
|
|
|
|
dateSubmittedCell.classList.add("d-none", "d-md-table-cell"); // Column visible on medium and larger displays
|
|
|
|
|
|
|
|
bodyRow.insertCell(4).appendChild(actionButton);
|
|
|
|
reportsAdded++;
|
2019-01-28 00:21:51 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 23:42:09 +00:00
|
|
|
if (reportsAdded === 0) {
|
|
|
|
// Report list is empty
|
2019-01-28 00:21:51 +00:00
|
|
|
const p = document.createElement("p");
|
|
|
|
p.innerHTML = "No reports found.";
|
|
|
|
cardBody.appendChild(p);
|
|
|
|
} else {
|
2019-01-30 23:42:09 +00:00
|
|
|
// Report list exists and table rows have been created
|
|
|
|
// Create table header, add it to the table, and append the result to the card body
|
|
|
|
|
2019-01-28 00:21:51 +00:00
|
|
|
const tr = document.createElement("tr");
|
|
|
|
|
|
|
|
const headTitle = document.createElement("th");
|
|
|
|
headTitle.innerHTML = "Title";
|
|
|
|
tr.appendChild(headTitle);
|
|
|
|
|
|
|
|
const headDateCreated = document.createElement("th");
|
|
|
|
headDateCreated.innerHTML = "Date Created";
|
|
|
|
tr.appendChild(headDateCreated);
|
|
|
|
|
|
|
|
const headState = document.createElement("th");
|
|
|
|
headState.innerHTML = "State";
|
2019-01-30 23:42:09 +00:00
|
|
|
headState.classList.add("d-none", "d-lg-table-cell"); // Column visible only on large displays
|
2019-01-28 00:21:51 +00:00
|
|
|
tr.appendChild(headState);
|
|
|
|
|
|
|
|
const headDateSubmitted = document.createElement("th")
|
|
|
|
headDateSubmitted.innerHTML = "Date Submitted";
|
2019-01-30 23:42:09 +00:00
|
|
|
headDateSubmitted.classList.add("d-none", "d-md-table-cell"); // Column visible on medium and larger displays
|
2019-01-28 00:21:51 +00:00
|
|
|
tr.appendChild(headDateSubmitted);
|
|
|
|
|
2019-01-30 23:42:09 +00:00
|
|
|
const headAction = document.createElement("th")
|
|
|
|
headAction.innerHTML = "Action";
|
|
|
|
tr.appendChild(headAction);
|
|
|
|
|
|
|
|
const thead = document.createElement("thead");
|
2019-01-28 00:21:51 +00:00
|
|
|
thead.appendChild(tr);
|
|
|
|
table.prepend(thead);
|
|
|
|
table.classList.add("table", "table-striped", "table-responsive-sm");
|
|
|
|
cardBody.appendChild(table);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getReportHistory(event) {
|
2019-01-30 23:42:09 +00:00
|
|
|
const url = getEndpointDomain() + "api/v1/reports";
|
|
|
|
getDataFromEndpoint(url, displayListOfReports);
|
|
|
|
}
|
2019-01-28 00:21:51 +00:00
|
|
|
|
2019-01-30 23:42:09 +00:00
|
|
|
function openEditReportForm(event) {
|
|
|
|
const url = getEndpointDomain() + "api/v1/report/" + this.dataset.rid;
|
|
|
|
getDataFromEndpoint(url, createEditReportForm);
|
|
|
|
}
|
2019-01-28 05:39:48 +00:00
|
|
|
|
2019-01-28 00:21:51 +00:00
|
|
|
|
2019-01-30 23:42:09 +00:00
|
|
|
document.addEventListener("DOMContentLoaded", getReportHistory);
|