Skip to content

Commit

Permalink
Merge pull request #2623 from department-of-veterans-affairs/vapo
Browse files Browse the repository at this point in the history
Vapo
  • Loading branch information
Pelentan authored Dec 18, 2024
2 parents a8ede2b + 8f51837 commit b6b3b0f
Show file tree
Hide file tree
Showing 4 changed files with 310 additions and 220 deletions.
2 changes: 1 addition & 1 deletion LEAF_Request_Portal/ajaxJSON.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
// this method does not exist in Form class
// echo $form->getProgressJSON($_GET['recordID']);
// but this one does
echo $form->getProgress($_GET['recordID']);
echo $form->getProgress((int)$_GET['recordID']);

break;
case 'getrecentactions':
Expand Down
2 changes: 1 addition & 1 deletion LEAF_Request_Portal/api/controllers/FormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function get($act)
});

$this->index['GET']->register('form/[digit]/progress', function ($args) use ($form) {
$return = $form->getProgress($args[0]);
$return = $form->getProgress((int)$args[0]);
return $return;
});

Expand Down
115 changes: 76 additions & 39 deletions LEAF_Request_Portal/js/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,20 +227,28 @@ var LeafForm = function (containerID) {
/** cross walk end */

let childRequiredValidators = {};
//store required validators for the controlled question and any subchildren on main entry and modals
const handleChildValidators = (childID) => {
if (!childRequiredValidators[childID]) {
childRequiredValidators[childID] = {
validator: formRequired[`id${childID}`]?.setRequired,
};
}
//reset the validator, if there is one, from the stored value
if (
childRequiredValidators[childID].validator !== undefined &&
dialog !== null
) {
dialog.requirements[childID] =
childRequiredValidators[childID].validator;
}
let arrValidatorIDs = [ childID ];
const arrSubchildren = Array.from(
document.querySelectorAll(`.response.blockIndicator_${childID} div.response[class*="blockIndicator_"]`)
);
arrSubchildren.forEach(element => {
const id = +element.className.match(/(\d+)$/)?.[0];
if(id > 0) {
arrValidatorIDs.push(id);
}
});
arrValidatorIDs.forEach(id => {
if (!childRequiredValidators[id]) {
childRequiredValidators[id] = {
validator: formRequired[`id${id}`]?.setRequired,
};
}
if (childRequiredValidators[id].validator !== undefined && dialog !== null) {
dialog.requirements[id] = childRequiredValidators[id].validator;
}
});
};
//validator ref for required question in a hidden state
const hideShowValidator = function () {
Expand Down Expand Up @@ -361,32 +369,61 @@ var LeafForm = function (containerID) {
return sanitize(val).trim();
};

/* clear out potential entries and set validator for hidden questions */
const clearValues = (childFormat = "", childIndID = 0) => {
$("#" + childIndID).val(""); //clears most formats
$(`input[id^="${childIndID}_"]`).prop("checked", false); //radio and checkbox(es) formats
$(`input[id^="${childIndID}_radio0"]`).prop("checked", true);

$(`#grid_${childIndID}_1_input tbody td`) //grid table data
.each(function () {
if ($("textarea", this).length) {
$("textarea", this).val('');
} else if ($("select", this).length) {
$("select", this).val('');
} else if ($("input", this).length) {
$("input", this).val('');
/*hide the question and any subquestions. clear out potential entries and set validator for hidden questions */
const clearValues = (childIndID = 0) => {
const arrSubchildren = Array.from(
document.querySelectorAll(`.response.blockIndicator_${childIndID} div.response[class*="blockIndicator_"]`)
);

//parse the IDs of any additional subquestions
let arrChildAndSubquestionIDs = [ childIndID ];
arrSubchildren.forEach(element => {
const id = +element.className.match(/(\d+)$/)?.[0];
if(id > 0) {
arrChildAndSubquestionIDs.push(id);
}
});

if (childFormat === "multiselect") {
clearMultiSelectChild($("#" + childIndID), childIndID);
}
if (
childRequiredValidators[childIndID].validator !== undefined &&
dialog !== null
) {
dialog.requirements[childIndID] = hideShowValidator;
}
arrChildAndSubquestionIDs.forEach(id => {
//clear values for questions in a hidden state.
$("#" + id).val(""); //most formats
$(`input[id^="${id}_"]`).prop("checked", false); //radio and checkbox(es) formats

$(`#grid_${id}_1_input tbody td`) //grid table data
.each(function () {
if ($("textarea", this).length) {
$("textarea", this).val('');
} else if ($("select", this).length) {
$("select", this).val('');
} else if ($("input", this).length) {
$("input", this).val('');
}
});

const isMultiselectQuestion = document.querySelector(`select[id="${id}"][multiple]`) !== null;
if (isMultiselectQuestion) {
clearMultiSelectChild($("#" + id), id);
}

const isRadioQuestion = document.querySelector(`input[id^="${id}_radio"]`) !== null;
if(isRadioQuestion) {
const radioEmpty = $(`input[id^="${id}_radio0"]`); //need to add hidden empty input to clear radio
if (radioEmpty.length === 0) {
$(`div.response.blockIndicator_${id}`).prepend(
`<input id="${id}_radio0" name="${id}" value="" style="display:none;" />`
);
}
$(`input[id^="${id}_radio0"]`).prop("checked", true);
}

//if the question is required, use the alternate validator
if (
childRequiredValidators[id].validator !== undefined &&
dialog !== null
) {
dialog.requirements[id] = hideShowValidator;
}
});
};

/**
Expand Down Expand Up @@ -526,7 +563,7 @@ var LeafForm = function (containerID) {
switch (co) {
case "hide":
if (hideShowConditionMet === true) {
clearValues(childFormat, childID);
clearValues(childID);
elChildResponse.classList.add('response-hidden');
elsChild.hide();
elsChild.attr('aria-hidden', true);
Expand All @@ -542,7 +579,7 @@ var LeafForm = function (containerID) {
elsChild.removeAttr('aria-hidden');
elsChild.show();
} else {
clearValues(childFormat, childID);
clearValues(childID);
elChildResponse.classList.add('response-hidden');
elsChild.hide();
elsChild.attr('aria-hidden', true);
Expand Down Expand Up @@ -611,7 +648,7 @@ var LeafForm = function (containerID) {
setTimeout(() => {
const closestHidden = elChildResponse.closest('.response-hidden');
if (closestHidden !== null) {
clearValues(childFormat, childID);
clearValues(childID);
}

elChildInput.trigger("change");
Expand Down
Loading

0 comments on commit b6b3b0f

Please sign in to comment.