// Finally Worked
let ticketworkflowconditions = [];
async function loadExistingWorkflowConditions() {
// console.log("Mapping Data for Workflows:", alldata.ticket_workflow_conditions);
// for (const obj of alldata.ticket_workflow_conditions) {
// console.log('Loading rule for event:', eventName);
// await addNewRule(obj);
// }
//thayammal
for (let index = 0; index < ticketworkflowconditions.length; index++) {
const element = ticketworkflowconditions[index];
await addNewRule(null, element, index);
}
}
// Check if the event is unselected or invalid
if (!newEvent) {
console.log("Event unselected or invalid. Resetting ticketworkflowconditions to its initial state.");
code
Assistant
// Reset ticketworkflowconditions to its initial state
ticketworkflowconditions = [];
// Remove all related containers
newDropdownSet.querySelectorAll('.field-name-container, .choices-container, .button-container').forEach(container => container.remove());
console.log("ticketworkflowconditions after reset:", ticketworkflowconditions);
return; // Exit early since no further action is needed
}
// Proceed with the existing logic if a valid event is selected
console.log("ticketworkflowconditions in eventSelect", ticketworkflowconditions, newEvent, selectedEvent);
let fieldNameContainer = container.querySelector('.field-name-container');
let choicesContainer = container.querySelector('.choices-container');
let buttonContainer = container.querySelector('.button-container');
let toggleContainer = container.querySelector('.toggle-container');
if (!fieldNameContainer) {
fieldNameContainer = document.createElement('div');
fieldNameContainer.classList.add('col-md-3', 'field-name-container');
container.appendChild(fieldNameContainer);
}
if (!choicesContainer) {
choicesContainer = document.createElement('div');
choicesContainer.classList.add('col-md-3', 'choices-container');
container.appendChild(choicesContainer);
}
if (obj && obj.event.combination.length > 0) {
const fields = obj.event.combination;
for (let index = 0; index < fields.length; index++) {
const element = fields[index];
for (const [fieldName, fieldValue] of Object.entries(element)) {
await fieldschoosingvalues(
selectedEventValue,
fieldNameContainer,
choicesContainer,
buttonContainer,
container,
fieldName,
Array.isArray(fieldValue) ? fieldValue : [fieldValue],
obj,
ruleIndex,
index
);
}
}
}
if (ticketworkflowconditions[ruleIndex] && ticketworkflowconditions[ruleIndex].combination) {
delete ticketworkflowconditions[ruleIndex].combination;
console.log(Removed outer combination from ticketworkflowconditions at index ${ruleIndex});
}
console.log("Delete Event button clicked.");
const eventContainer = container.closest('.event-container');
if (eventContainer) {
const modal = document.getElementById('composition');
if (modal) {
modal.setAttribute('is-open', 'true');
modal.querySelector('fw-modal-title').innerHTML = `<span>Delete Event</span>`;
modal.querySelector('fw-modal-content').innerHTML = `
<span>Are you sure you want to delete this event?</span>
`;
modal.addEventListener(
'fwSubmit',
() => {
eventContainer.remove();
console.log("Event container successfully removed from DOM.");
if (Array.isArray(ticketworkflowconditions)) {
ticketworkflowconditions.splice(ruleIndex, 1);
console.log(`Event at index ${ruleIndex} successfully removed from ticketworkflowconditions array.`);
} else if (ticketworkflowconditions[ruleIndex]) {
delete ticketworkflowconditions[ruleIndex];
console.log(`Event at index ${ruleIndex} successfully removed from ticketworkflowconditions object.`);
}
console.log("Updated ticketworkflowconditions:", JSON.stringify(ticketworkflowconditions, null, 2));
showSuccessToast("Event successfully deleted!");
modal.removeAttribute('is-open');
},
{ once: true }
);
}
}
});
function updateIsEnabledState(selectedEventValue, isChecked) {
if (!selectedEventValue) {
console.error("Event is not selected.");
return;
}
let data = {
...ticketworkflowconditions[ruleIndex]
};
ticketworkflowconditions[ruleIndex] = {
...data,
checked: isChecked
};
code
console.log(`Toggle for ${selectedEventValue} set to ${isChecked ? 'enabled' : 'disabled'}.`);
console.log(`Updated ticketworkflowconditions after toggle change: ${JSON.stringify(ticketworkflowconditions, null, 2)}`);
deleteButton.addEventListener("click", () => {
console.log(Delete button clicked for mapping at index ${fieldIndex});
const modal = document.getElementById("composition");
if (modal) {
modal.setAttribute("is-open", "true");
modal.querySelector("fw-modal-title").innerHTML = <span>Delete Mapping</span>;
modal.querySelector("fw-modal-content").innerHTML = <span>Are you sure you want to delete this field mapping?</span>;
fieldDropdown.disabled = true;
const findRule = ticketworkflowconditions[ruleIndex];
if (findRule) {
// Ensure the field index is updated rather than appending
findRule.event.combination[fieldIndex] = {
[selectedFieldName]: selectedChoices || [],
};
console.log(`Updated ticketworkflowconditions: ${JSON.stringify(ticketworkflowconditions, null, 2)}`);
}
} else {
console.error(Field not found in Dropdown for existing mapping: ${selectedFieldName});
}
}
function checkAddButtonVisibility() {
const dropdowns = container.querySelectorAll(".field-name-dropdown");
const selectedFieldValues = Array.from(dropdowns)
.map((dropdown) => dropdown.value)
.filter((value) => value !== "");
code
const areAllFieldsSelected = selectedFieldValues.length >= fields.length;
const lastDropdownWrapper =
dropdowns.length > 0
? dropdowns[dropdowns.length - 1].closest(".button-container")
: null;
if (areAllFieldsSelected) {
console.log("All fields are selected. Hiding Add Button.");
if (addButton.parentNode) {
if (lastDropdownWrapper && lastDropdownWrapper.contains(addButton)) {
lastDropdownWrapper.removeChild(addButton);
}
}
} else {
console.log("Fields are still available. Add Button will be shown.");
if (dropdowns.length > 0 && lastDropdownWrapper) {
if (!lastDropdownWrapper.contains(addButton)) {
lastDropdownWrapper.appendChild(addButton);
addButton.style.visibility = "visible";
}
}
}
addButton.disabled = false;
The code snippet provided is a complex JavaScript application that handles the creation and management of a dynamic form interface for configuring ticket workflow conditions. The div elements play a crucial role in organizing the visual and functional structure of this interface. Below is an explanation of how various div elements are handled throughout the code:
High-Level Overview
Dynamic Event Containers:
Each event in the workflow is represented by a .event-container, which is created dynamically for each ticket workflow condition in the loadExistingWorkflowConditions function. This container holds all the elements related to a specific ticket workflow rule.
Styling and Structure:
The .event-container is styled with CSS properties like boxShadow, padding, margin, borderRadius, and backgroundColor, making it visually distinct and user-friendly.
Child Div Elements:
Inside each event container, multiple child div elements are created:
Field Name Container (field-name-container): To hold dropdowns for field selection.
Choices Container (choices-container): To pull in choices based on the fields selected.
Button Container (button-container): To hold action buttons like "Add" and "Delete".
Event Dropdowns:
In the createEventDropdownSet function, a row of dropdowns for selecting event types is generated. Each dropdown is wrapped in a div with the class row ticketworkflow-dropdown.
Adding Field Dropdowns:
The fieldschoosingvalues function creates additional dropdowns for selecting fields depending on the workflow rule conditions. Whenever a valid event is selected, these dropdowns are dynamically added to the corresponding containers.
Dynamic Button Creation:
Dynamic buttons (like 'Add' and 'Delete') are created and appended to the buttonContainer. The layout is adjusted accordingly based on whether buttons are necessary based on user interactions.
Detail Explanation of Div Handling
Event Handling:
Each dropdown and button has event listeners that respond to user interactions (like dropdown selection or button clicks), which lead to the creation or deletion of additional div elements dynamically.
Creating Additional Dropdowns:
The createAdditionalDropdowns function dynamically creates new instances of the three containers (fieldNameContainer, choicesContainer, buttonContainer) and adds them to the main event container when the user selects an event from the dropdown.
Managing Deletion:
When the delete button is clicked, the corresponding containers are removed from the DOM, and the underlying data structure (ticketworkflowconditions) is updated to reflect this change. This includes removing any associated div elements from the interface.
Responsiveness:
The containers are set to show and hide based on user inputs. For example, if all required fields are selected, the 'Add' button may be hidden.
Data Handling:
The data associated with the dropdown selections is structured as objects within the ticketworkflowconditions array. Each change in the dropdown selection updates this structure, allowing for dynamic data handling based on user interactions.
Conclusion
The handling of div elements in this code is a sophisticated interplay of DOM manipulation, event handling, and state management. The use of divs allows for a modular and flexible UI that can adjust dynamically based on the user's actions, ensuring that the form can handle a variety of user inputs while maintaining clear organization and functionality.