Commit 60b8591a by Lynette Lizardo

Merge branch 'feature-WG-320b1' into fetaure-WG-308

parents 9dd481de 50961daf
...@@ -50,7 +50,7 @@ export async function generateFields(inputSchema, containerId) { ...@@ -50,7 +50,7 @@ export async function generateFields(inputSchema, containerId) {
let doctype = sessionStorage.getItem('doctype'); let doctype = sessionStorage.getItem('doctype');
let section = sessionStorage.getItem('section'); let section = sessionStorage.getItem('section');
if (doctype == null && section == null) { if ((doctype === null || doctype === "undefined") && (section === null || section === "undefined")) {
Object.keys(schema).every(function (key) { Object.keys(schema).every(function (key) {
let doctypes = schema[key]; let doctypes = schema[key];
let underscoredKey = key.replaceAll(" ", "_"); let underscoredKey = key.replaceAll(" ", "_");
...@@ -121,7 +121,6 @@ export async function generateFields(inputSchema, containerId) { ...@@ -121,7 +121,6 @@ export async function generateFields(inputSchema, containerId) {
} }
$(document.body).on("change", "#DocType", async function () { $(document.body).on("change", "#DocType", async function () {
console.log("change")
const elements = document.getElementsByClassName(sessionStorage.getItem("currentSection")); const elements = document.getElementsByClassName(sessionStorage.getItem("currentSection"));
while (elements.length > 0) { while (elements.length > 0) {
elements[0].parentNode.removeChild(elements[0]); elements[0].parentNode.removeChild(elements[0]);
...@@ -135,19 +134,16 @@ export async function generateFields(inputSchema, containerId) { ...@@ -135,19 +134,16 @@ export async function generateFields(inputSchema, containerId) {
let doctypes = schema[this.value]; let doctypes = schema[this.value];
let underscoredValue = this.value.replaceAll(" ", "_"); let underscoredValue = this.value.replaceAll(" ", "_");
sessionStorage.setItem("currentDoctype", underscoredValue); sessionStorage.setItem("currentDoctype", underscoredValue);
const { const { valid, error } = validateSchema();
valid,
error
} = validateSchema()
if (!valid) { if (!valid) {
container.textContent = error container.textContent = error;
container.style.color = '#ff3333' container.style.color = '#ff3333';
} }
for (let key in doctypes) { for (let key in doctypes) {
let underscoredKey = key.replaceAll(" ", "_"); let underscoredKey = key.replaceAll(" ", "_");
sessionStorage.setItem("currentSection", underscoredKey); sessionStorage.setItem("currentSection", underscoredKey);
createSection('Section', container, doctypes, underscoredValue); createSection('Section', container, doctypes, underscoredValue);
container = await deconstruct(doctypes[key], container, underscoredKey) container = await deconstruct(doctypes[key], container, underscoredKey);
// const submit = document.createElement('input') // const submit = document.createElement('input')
// submit.classList.add("submitButtons"); // submit.classList.add("submitButtons");
...@@ -670,16 +666,23 @@ const createDocTypeDropdown = (fieldLabel, container, schema, doc) => { ...@@ -670,16 +666,23 @@ const createDocTypeDropdown = (fieldLabel, container, schema, doc) => {
docTypeDropDown.classList.add('dropdown-input') docTypeDropDown.classList.add('dropdown-input')
// docTypeDropDown.addEventListener('focusout', handleInput) // docTypeDropDown.addEventListener('focusout', handleInput)
let defaultOptionSelected = false; // Track if a matching option is found
for (const doctype in schema) { for (const doctype in schema) {
newOption = document.createElement("option") newOption = document.createElement("option")
newOption.text = doctype newOption.text = doctype
newOption.value = doctype newOption.value = doctype
if (doctype == doc) { if (doctype == doc) {
newOption.selected = 'selected' newOption.selected = 'selected'
defaultOptionSelected = true
} }
docTypeDropDown.add(newOption) docTypeDropDown.add(newOption)
} }
if (!defaultOptionSelected) {
docTypeDropDown.selectedIndex = 0; // Set the first option as selected by default
}
docTypeDropDown.classList.add('inputField') docTypeDropDown.classList.add('inputField')
inputContainer.appendChild(docTypeDropDown) inputContainer.appendChild(docTypeDropDown)
...@@ -810,7 +813,7 @@ export async function populateFields(imagePath) { ...@@ -810,7 +813,7 @@ export async function populateFields(imagePath) {
return; return;
} }
if (resp[0] != null) { if (resp[0] != null && 'DocType' in resp[0]) {
var doctype = resp[0].DocType; var doctype = resp[0].DocType;
var section = resp[0].Section; var section = resp[0].Section;
saveXMLToStorage(parseInt(sessionStorage.getItem("display_counter")), resp[0].fields); saveXMLToStorage(parseInt(sessionStorage.getItem("display_counter")), resp[0].fields);
...@@ -895,9 +898,29 @@ export async function populateFields(imagePath) { ...@@ -895,9 +898,29 @@ export async function populateFields(imagePath) {
export function clearForm() { export function clearForm() {
// Check if the form element exists // Check if the form element exists
const formElement = document.getElementById("fields"); const formElement = document.getElementById("fields");
if (formElement) { if (formElement) {
// Retain the selected 'doctype'
const docTypeField = formElement.querySelector('#DocType');
const doctype = docTypeField.value || docTypeField.options[docTypeField.selectedIndex].value;
// Clear the form by resetting its fields // Clear the form by resetting its fields
formElement.reset(); formElement.reset();
// Set the selected 'doctype' back
const options = docTypeField.options;
const { elements } = formElement
for (let i = 0; i < options.length; i++) {
if (options[i].value === doctype) {
for (let element of elements) {
const { id } = element
if (id === 'DocType') {
element.selectedIndex = i
}
}
}
}
} else { } else {
console.log("Form element not found"); console.log("Form element not found");
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment