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>;
} else {
console.error("Modal element with ID 'composition' not found.");
}
});
in this how did you remove field container same way remove choices, buttton container too
Assistant
User
Assistant
in this how it remove field-container of the clicked set, same way remove that set's choice-container, button-container
To remove additional elements like the choices container and button container in a manner similar to how the field container is removed, you just need to follow the same pattern used to find and remove the field container. You would include logic to select and remove those specified containers when the delete operation is confirmed.
Here’s an example of how you could modify the existing code to remove the choices container and button container associated with the clicked set along with the field container:
javascript
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>`;
modal.addEventListener(
"fwSubmit",
() => {
const findRule = ticketworkflowconditions[ruleIndex];
if (findRule) {
const currentDropdowns = container.querySelectorAll(".field-name-dropdown");
const currentDropdownIndex = Array.from(currentDropdowns).indexOf(fieldDropdown);
if (currentDropdownIndex !== -1) {
const combination = findRule.event.combination;
combination.splice(currentDropdownIndex, 1);
ticketworkflowconditions[ruleIndex] = {
...findRule,
event: { ...findRule.event, combination },
};
// Remove the entire field container div
const fieldContainer = fieldNameContainer.closest(".field-container");
if (fieldContainer) {
fieldContainer.remove();
}
// Additionally remove the choices container
const choicesContainer = fieldNameContainer.closest(".choices-container");
if (choicesContainer) {
choicesContainer.remove();
}
// Also remove the button container
const buttonContainer = fieldNameContainer.closest(".button-container");
if (buttonContainer) {
buttonContainer.remove();
}
console.log("Updated ticketworkflowconditions:", JSON.stringify(ticketworkflowconditions, null, 2));
}
}
modal.removeAttribute("is-open");
checkAddButtonVisibility();
},
{ once: true }
);
modal.querySelector("fw-modal-footer").addEventListener(
"fwClose",
() => {
modal.removeAttribute("is-open");
console.log("Delete operation was cancelled.");
},
{ once: true }
);
} else {
console.error("Modal element with ID 'composition' not found.");
}
});
Breakdown of Changes Made:
Identifying Additional Containers: Similar to how the fieldContainer is selected, you need to identify the choices and button containers. I've added lines of code to find the closest .choices-container and .button-container relative to fieldNameContainer.
Removing Additional Containers: After confirming the deletion, we call .remove() on these additional containers. This will remove them from the DOM just like we did for the fieldContainer.
Note:
Ensure that the classes .choices-container and .button-container exist in your HTML structure so the .closest() method works correctly to find the relevant containers. If the structure changes or if these selectors are not correct, you might need to adjust the queries accordingly.
async function getFieldnamesfrom1stapi(
fieldNameContainer,
fields,
choicesContainer,
selectedEventValue,
buttonContainer,
container,
selectedFieldName = null,
selectedChoices = [],
obj,
ruleIndex,
fieldIndex
) {
if (!selectedEventValue) {
console.error("Select an event before proceeding.");
return;
}
// Create the field dropdown
const fieldDropdown = document.createElement('fw-select');
fieldDropdown.classList.add('field-name-dropdown');
fieldDropdown.setAttribute('label', 'Ticket Fields');
fieldDropdown.setAttribute('required', 'true');
fieldDropdown.setAttribute('placeholder', 'Choose a field');
fieldDropdown.setAttribute('hint-text', 'Select singular option');
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>;
code
} else {
console.error("Modal element with ID 'composition' not found.");
}
});
if (!newSelectedFieldName) {
// Clear choices if field is deselected
choicesContainer.innerHTML = "";
const findRule = ticketworkflowconditions[ruleIndex];
if (findRule) {
findRule.event.combination[fieldIndex] = {}; // Clear the field mapping
}
console.log("Field deselected. Choices cleared.");
return;
}
const newSelectedField = fields.find((field) => field.name === newSelectedFieldName);
if (newSelectedField) {
const findRule = ticketworkflowconditions[ruleIndex];
if (findRule) {
// Instead of adding a new combination, update the existing combination at the given fieldIndex
findRule.event.combination[fieldIndex] = {
[newSelectedFieldName]: selectedChoices || [], // Preserve current choices
};
}
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;
}
}
in this, at mapping / initially choosing, 1st dropdown set button container should not have delete icon, instead of delete icon add icon should be added (for 1st set alone).
To implement the requirement where the initial dropdown set (the first dropdown) does not have a delete icon and instead has an add icon, you can modify the logic in the getFieldnamesfrom1stapi function.
Here's how you can achieve this:
Check if the fieldIndex is 0 (which indicates that it's the first dropdown).
If it is the first dropdown, you should create the add button instead of the delete button.
For subsequent dropdowns, you can follow the existing logic to create the delete buttons.
I've modified your implementation of the getFieldnamesfrom1stapi function accordingly:
javascript
async function getFieldnamesfrom1stapi(
fieldNameContainer,
fields,
choicesContainer,
selectedEventValue,
buttonContainer,
container,
selectedFieldName = null,
selectedChoices = [],
obj,
ruleIndex,
fieldIndex
) {
if (!selectedEventValue) {
console.error("Select an event before proceeding.");
return;
}
// Create the field dropdown
const fieldDropdown = document.createElement('fw-select');
fieldDropdown.classList.add('field-name-dropdown');
fieldDropdown.setAttribute('label', 'Ticket Fields');
fieldDropdown.setAttribute('required', 'true');
fieldDropdown.setAttribute('placeholder', 'Choose a field');
fieldDropdown.setAttribute('hint-text', 'Select singular option');
const selectedFields = Array.from(container.querySelectorAll('.field-name-dropdown'))
.map((dropdown) => dropdown.value)
.filter((value) => value);
fields
.filter((field) => !selectedFields.includes(field.name))
.forEach((field) => {
const option = document.createElement('fw-select-option');
option.setAttribute('value', field.name);
option.textContent = field.label;
fieldDropdown.appendChild(option);
if (selectedFieldName && field.name === selectedFieldName) {
option.setAttribute('selected', 'selected');
}
});
fieldNameContainer.appendChild(fieldDropdown);
// Check if this is the first set (index 0)
if (fieldIndex === 0) {
// Create the Add button for the first set
const addButton = document.createElement('fw-button');
addButton.setAttribute('size', 'icon');
addButton.classList.add('add-button');
addButton.innerHTML = '<fw-icon name="plus" size="20" color="white"></fw-icon>';
addButton.disabled = true;
addButton.style.marginTop = '48px'; // Add margin for spacing
// Append the Add button to the button container
buttonContainer.appendChild(addButton);
} else {
// Create the Delete button for subsequent sets
const deleteButton = document.createElement('fw-button');
deleteButton.setAttribute('size', 'icon');
deleteButton.classList.add('delete-button');
deleteButton.innerHTML = '<fw-icon name="delete" size="20" color="white"></fw-icon>';
const deleteButtonWrapper = document.createElement('div');
deleteButtonWrapper.classList.add('delete-button-container', 'mt-5');
deleteButtonWrapper.appendChild(deleteButton);
buttonContainer.appendChild(deleteButtonWrapper);
}
const existingAddButton = container.querySelector('.add-button');
if (existingAddButton) {
existingAddButton.remove();
}
// Create the Add button for subsequent sets
const addButton = document.createElement('fw-button');
addButton.setAttribute('size', 'icon');
addButton.classList.add('add-button');
addButton.innerHTML = '<fw-icon name="plus" size="20" color="white"></fw-icon>';
addButton.disabled = true;
addButton.style.marginTop = '48px';
// Append the Add button to the button container
buttonContainer.appendChild(addButton);
...
// The rest of your existing code continues here
}
Key Changes:
Create Condition for First Set:
Added a condition to check if fieldIndex === 0. If it is true, we create and append the addButton to the buttonContainer instead of a deleteButton.
Delete Button for Subsequent Sets:
If it’s not the first dropdown (i.e., when fieldIndex > 0), create and append the deleteButton.
Important Note:
Ensure that the rest of your logic for handling the add button and delete button events does not change. You still maintain the existing listeners for the add button and delete button within their respective contexts.
This way, the first dropdown set will display an Add button, while all subsequent dropdown sets will display a Delete button.