// 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
User
now : do like this
how many cols are allocated for fields and choices, and buttons.
for ex. if it is 8
create a div with col-8 and inside that
these three should inside that div with same class.
Assistant
User
no based on the field,choice,button container count the col-md and create the div
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 provided snippet is a piece of JavaScript code designed to manage a user interface for creating and managing ticket workflow conditions. It appears to be meant for a web application, possibly built using a framework that leverages components (like fw-select or fw-button), indicating a customized component library.
Here's a breakdown of the core components and functionalities implemented in the code:
Core Variables and Functions:
let ticketworkflowconditions = [];
An array that holds the existing workflow conditions for ticket workflows. Each element of this array represents a rule or condition that can be configured.
async function loadExistingWorkflowConditions()
This function loads existing ticket workflow conditions. It appears to use addNewRule to initialize each entry in the ticketworkflowconditions array within the UI.
async function addNewRule(_event, obj, ruleIndex)
This function is responsible for creating a new UI component that represents a rule condition. It creates a div (eventContainer) for each rule, appends this to a container (ticketworkflowContainer), and initializes dropdowns based on existing conditions.
Dropdown Creation and Handling:
async function createEventDropdownSet(obj, ruleIndex)
Constructs the dropdown set for selecting events. It creates a selection dropdown for different event types (TicketCreate, TicketUpdate, etc.). It also allows the user to change the selected event, which adjusts the internal state (ticketworkflowconditions).
Event Handling within Dropdown:
When an event is selected or changed:
It validates the selection.
Updates the relevant entry in the ticketworkflowconditions array.
Manages visibility and enables/disables other UI elements based on current selections.
Field and Choice Management:
async function fieldschoosingvalues(...)
This function manages the choices associated with the selected event. It makes an API call to fetch available ticket fields and allows users to select fields for the workflow conditions.
async function getFieldnamesfrom1stapi(...)
Creates dropdowns for fields based on available ticket fields retrieved from an API. It handles adding and deleting field mappings dynamically.
async function showingitschoices(...)
Displays the additional choices for the selected field, enabling users to select one or more options for further customization of the workflow condition.
UI Interaction and State Management:
Add/Delete Buttons:
The code contains button handlers that manage the addition and deletion of rules, ensuring that the state of ticketworkflowconditions is correctly updated when the user interacts with the UI.
Toggle and Visibility Controls:
The code includes logic to manage the enabling/disabling of dropdowns and buttons based on user interactions. For example, if a rule is disabled, related dropdowns and buttons are also disabled.
Modal Confirmation:
When deleting an event or mapping, a modal is shown to confirm the action before it is executed.
Summary
The code is structured to provide users with an interactive interface where they can define and configure rules for ticket workflows via dropdowns for selecting events and associated conditions. It handles API interactions, manages the internal state of workflow conditions, and dynamically updates the UI in response to user actions, ensuring a responsive and intuitive user experience.
Overall, it represents a significant component of a ticketing application, likely tied to a backend system providing ticket management capabilities.
To reorganize the structure of the UI code you provided by consolidating the fieldNameContainer, choicesContainer, and buttonContainer into a single parent div with column classes (e.g., col-8), you will need to create a wrapper div that encapsulates these three containers. Here’s how you could implement that:
Modified Code Snippet
I’ll assume from your request that you want to modify the parts of the code where these containers are created and appended. Here’s a representation of how to make this change.
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 wrapper div for fields, choices and buttons
const wrapperDiv = document.createElement('div');
wrapperDiv.classList.add('col-8'); // Adjusted class as per requirement
// Create field name container
const fieldNameContainer = document.createElement('div');
fieldNameContainer.classList.add('col-md-3', 'field-name-container');
wrapperDiv.appendChild(fieldNameContainer);
// Create choices container
const choicesContainer = document.createElement('div');
choicesContainer.classList.add('col-md-3', 'choices-container');
wrapperDiv.appendChild(choicesContainer);
// Create button container
const buttonContainer = document.createElement('div');
buttonContainer.classList.add('col-md-2', 'button-container');
buttonContainer.style.marginTop = '-25px'; // As per your existing logic
wrapperDiv.appendChild(buttonContainer);
// Append the wrapper div to the event container
eventContainer.appendChild(wrapperDiv);
const newDropdownSet = await createEventDropdownSet(obj, ruleIndex);
eventContainer.appendChild(newDropdownSet);
ticketworkflowContainer.appendChild(eventContainer);
await initializeEventDropdown(newDropdownSet, obj, ruleIndex);
}
// ... continue with the rest of your event handling logic ...
Explanation of the Changes:
Wrapper Div Creation:
A new div (wrapperDiv) is created with the class col-8 to act as the parent container for the fields, choices, and buttons.
Appending Sub-Containers:
The fieldNameContainer, choicesContainer, and buttonContainer are all appended to this new wrapperDiv.
Appending Wrapper to Event Container:
Finally, the wrapperDiv is appended to the eventContainer, ensuring that all inner elements are now encapsulated within the same column structure.
In this manner, you ensure that the layout is organized correctly with respect to the desired column structure. This keeps your UI clean and structured, while also allowing the use of CSS grid or flex properties on the parent wrapper as needed later.
To dynamically allocate the columns (col-md-*) for the fieldNameContainer, choicesContainer, and buttonContainer based on their existing classes and the desired total columns (in your example, 8 columns), you need to calculate how many columns are currently set for each of those containers.
Here's an approach to achieve this:
Count the Columns: Determine the number of columns each container will use and sum them up.
Calculate Remaining Columns: Compute the remaining column space based on a total of 8 columns.
Create a Div: Create a parent div that accommodates all the sub-containers.
Assign Remaining Columns: Adjust the parent div's column class to reflect the total number of columns used by the child containers.
Here’s how you might implement this in code:
JavaScript Code Implementation
Assuming that you have already specified col-md classes for fieldNameContainer, choicesContainer, and buttonContainer, you can implement it as follows:
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 and determine sizes for containers
const fieldNameContainer = document.createElement('div');
const choicesContainer = document.createElement('div');
const buttonContainer = document.createElement('div');
const fieldCols = 3; // Assuming col-md-3
const choiceCols = 3; // Assuming col-md-3
const buttonCols = 2; // Assuming col-md-2
// Total columns allocated
const totalCols = fieldCols + choiceCols + buttonCols; // 3 + 3 + 2 = 8
const remainingCols = 8 - totalCols; // Calculate remaining columns, should be 0
// Create wrapper div with dynamic col-md size
const wrapperDiv = document.createElement('div');
wrapperDiv.classList.add(`col-md-${totalCols}`);
// Set classes for inner containers
fieldNameContainer.classList.add('col-md-3', 'field-name-container');
choicesContainer.classList.add('col-md-3', 'choices-container');
buttonContainer.classList.add('col-md-2', 'button-container');
// Organize and append containers to wrapper
wrapperDiv.appendChild(fieldNameContainer);
wrapperDiv.appendChild(choicesContainer);
wrapperDiv.appendChild(buttonContainer);
// Append the wrapper div to the event container
eventContainer.appendChild(wrapperDiv);
const newDropdownSet = await createEventDropdownSet(obj, ruleIndex);
eventContainer.appendChild(newDropdownSet);
ticketworkflowContainer.appendChild(eventContainer);
await initializeEventDropdown(newDropdownSet, obj, ruleIndex);
}
Explanation:
Column Count: Here, I've defined the columns for fieldNameContainer, choicesContainer, and buttonContainer statically. You can adjust these based on your actual classes if they vary.
Total and Remaining Columns: The total column count is computed. This check ensures that you have a total of 8 columns and accommodates the existing containers.
Dynamic Wrapper: The wrapperDiv is created with a dynamic class that reflects how many columns are used.
Appending to Event Container: Finally, the newly created wrapper containing all three containers is appended to the event container's structure.
This approach allows for flexibility and scalability, wherein you can easily modify the number of columns allotted to each container without losing the overall structure. Additionally, if the layout of fields or buttons changes, you only need to adjust the counts in their respective definitions.