when i click addNewRule , a card will create. inside that event dropdown will have, in this place you have to create new wrapperdiv inside that wrapperdiv add fielddropdown, choicesdropdown, button-container
// 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
// 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;
To implement the requested functionality of creating a new card with the appropriate dropdowns and a button inside a wrapper div when an addNewRule event is triggered, you can refactor the existing addNewRule function. Below is the modified code that achieves this:
Add a new wrapper div inside the event container.
Include the fieldDropdown, choicesDropdown, and buttonContainer inside this wrapper div.
Here’s how you can do that:
javascript
async function addNewRule(_event, obj, ruleIndex = ticketworkflowconditions.length) {
const ticketworkflowContainer = document.getElementById('ticketworkflow-container');
const eventContainer = document.createElement('div');
eventContainer.classList.add('event-container');
eventContainer.style.boxShadow = '0 5px 10px 0 rgba(0, 0, 0, 0.2)';
eventContainer.style.padding = '30px';
eventContainer.style.marginBottom = '15px';
eventContainer.style.borderRadius = '5px';
eventContainer.style.backgroundColor = '#fff';
// Create a new wrapper div for dropdowns and buttons
const wrapperDiv = document.createElement('div');
wrapperDiv.classList.add('dropdown-wrapper');
const newDropdownSet = await createEventDropdownSet(obj, ruleIndex);
wrapperDiv.appendChild(newDropdownSet);
// Append the wrapper to the event container
eventContainer.appendChild(wrapperDiv);
ticketworkflowContainer.appendChild(eventContainer);
await initializeEventDropdown(newDropdownSet, obj, ruleIndex);
console.log("Initial Value of the object :", ticketworkflowconditions);
}
Changes made in createEventDropdownSet
Make sure that the modifications mentioned earlier happen correctly. Here’s how a potential modification of the createEventDropdownSet function may look, ensuring you append additional fields correctly to the wrapperDiv.
Once you've added the wrapper div and included the dropdowns and buttons inside it, ensure to test the entire functionality.
Click on the addNewRule or the equivalent button to confirm that the card creation and dropdown population are working as intended.
Conclusion
This modification will allow you to dynamically create a new card for each rule added, complete with the appropriate dropdowns and button inside a wrapper div. Ensure to maintain existing functionality and test thoroughly after the changes. If any complexities arise, feel free to reach out with additional context!