Simplify section id assignment

This commit is contained in:
Preston Doman 2019-02-14 15:58:27 -08:00
parent 8706f998f5
commit 03a3bf9ce4

View file

@ -56,8 +56,8 @@ function makeAjaxRequest(method, url, callback, type, payload) {
} }
// Wraps a Bootstrap form group around a field // Wraps a Bootstrap form group around a field
function createFormGroup(sectionId, field) { function createFormGroup(sectionIdStr, field) {
const inputId = "section-" + sectionId + "-" + field.field_name; const inputId = sectionIdStr + field.field_name;
const formGroup = document.createElement("div") const formGroup = document.createElement("div")
formGroup.classList.add("form-group", "row"); formGroup.classList.add("form-group", "row");
@ -142,7 +142,7 @@ function createFormGroup(sectionId, field) {
return formGroup; return formGroup;
} }
function createCollapsibleCard(sectionId, sectionTitle) { function createCollapsibleCard(sectionIdStr, sectionTitle) {
// Create card and header // Create card and header
const card = document.createElement("div"); const card = document.createElement("div");
card.classList.add("card"); card.classList.add("card");
@ -155,7 +155,7 @@ function createCollapsibleCard(sectionId, sectionTitle) {
button.classList.add("btn", "btn-link"); button.classList.add("btn", "btn-link");
button.type = "button"; button.type = "button";
button.setAttribute("data-toggle", "collapse"); button.setAttribute("data-toggle", "collapse");
button.setAttribute("data-target", "#section-" + sectionId + "-collapse"); button.setAttribute("data-target", "#" + sectionIdStr + "collapse");
button.innerHTML = sectionTitle; button.innerHTML = sectionTitle;
h2.appendChild(button); h2.appendChild(button);
cardHeader.appendChild(h2); cardHeader.appendChild(h2);
@ -164,10 +164,10 @@ function createCollapsibleCard(sectionId, sectionTitle) {
return card; return card;
} }
function createCollapsibleCardBody(form, type, sectionId, sectionDescription, sectionCompleted) { function createCollapsibleCardBody(form, type, sectionIdStr, sectionDescription, sectionCompleted) {
// Create wrapper div // Create wrapper div
const div = document.createElement("div"); const div = document.createElement("div");
div.id = "section-" + sectionId +"-collapse"; div.id = sectionIdStr + "collapse";
const sectionAlert = document.createElement("div"); const sectionAlert = document.createElement("div");
const cardBody = document.createElement("div"); const cardBody = document.createElement("div");
cardBody.classList.add("card-body"); cardBody.classList.add("card-body");
@ -230,12 +230,13 @@ function createReportForm(parsedData, type) {
// Traverse the report's sections array // Traverse the report's sections array
const sections = parsedData.sections; const sections = parsedData.sections;
for (let i = 0; i < sections.length; i++) { for (let i = 0; i < sections.length; i++) {
let collapsibleCard = createCollapsibleCard(sections[i].id, sections[i].title) let sectionIdStr = "section-" + sections[i].id + "-";
let collapsibleCard = createCollapsibleCard(sectionIdStr, sections[i].title)
// Create a new form with the section key index as id // Create a new form with the section key index as id
let form = document.createElement("form"); let form = document.createElement("form");
form.classList.add("form", "section-form"); form.classList.add("form", "section-form");
form.id = "section-" + sections[i].id +"-form"; form.id = sectionIdStr + "form";
form.setAttribute("data-rid", parsedData.report_pk); form.setAttribute("data-rid", parsedData.report_pk);
form.setAttribute("data-sid", sections[i].id); form.setAttribute("data-sid", sections[i].id);
@ -248,7 +249,7 @@ function createReportForm(parsedData, type) {
console.log("Field value: " + fields[j].value); console.log("Field value: " + fields[j].value);
// Create a form group for each field and add it to the form // Create a form group for each field and add it to the form
form.appendChild(createFormGroup(sections[i].id, fields[j])); form.appendChild(createFormGroup(sectionIdStr, fields[j]));
} }
// Add save button to the current form // Add save button to the current form
@ -259,7 +260,7 @@ function createReportForm(parsedData, type) {
form.appendChild(saveButton); form.appendChild(saveButton);
// Create collapsible card body, append form to it, append card to accordion // Create collapsible card body, append form to it, append card to accordion
let cardBody = createCollapsibleCardBody(form, type, sections[i].id, sections[i].html_description, sections[i].completed); let cardBody = createCollapsibleCardBody(form, type, sectionIdStr, sections[i].html_description, sections[i].completed);
collapsibleCard.appendChild(cardBody); collapsibleCard.appendChild(cardBody);
accordion.appendChild(collapsibleCard); accordion.appendChild(collapsibleCard);
} }