// 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.");
// 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
Assistant
User
explain how div works in this
Assistant
User
ok . can you change like this :
in this create whole div for dropdown like this :
inside card create whole-wrapper div inside keep event-dropdown, field-dropdown, choices-dropdown, button-container, to that div.
when i click add icon, whole-wrapper div and inside do as above but add md offset 2 for event dropdown as how handled in createadditionaldropdowns
Assistant
User
will this work in existing mapping too?
Assistant
User
give me the full fixed code
Assistant
}
// 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
};
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 !== "");
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;
This code appears to implement a user interface for managing workflow events and their associated conditions, specifically in the context of a ticketing system, such as those used in customer service applications (e.g., Zendesk, Freshdesk).
Here's a high-level breakdown of the code structure and its functionality:
Key Components
Data Structure:
ticketworkflowconditions: An array that holds various workflow conditions for tickets. Each condition can represent an event that triggers a specific workflow.
Functions:
loadExistingWorkflowConditions:
Loads existing workflow conditions from ticketworkflowconditions and calls addNewRule for each of them.
addNewRule:
Creates a new UI element (event container) for a ticket workflow rule, including dropdowns for selecting an event, and initializes it with existing data if available.
createEventDropdownSet:
Generates the dropdowns for selecting event types (e.g., "Ticket Create," "Ticket Update") and manages their interaction.
initializeEventDropdown:
Initializes the dropdowns and sets up the logic for toggle switches and delete buttons. It ensures the UI is responsive to user interactions.
fieldschoosingvalues:
Fetches fields from an API based on the selected event type and populates those fields in the UI. This is likely where the workflow gets defined.
getFieldnamesfrom1stapi:
Gets the available fields from the API and sets up UI for field selection.
showingitschoices:
Displays choices for the selected fields after a field is chosen.
createAdditionalDropdowns:
Supports adding additional fields to the existing selections dynamically.
Various Helper Functions (e.g., checkAddButtonVisibility, checkDeleteButtonVisibility):
These utility functions manage visibility for buttons related to adding or deleting fields based on the current state of the selections.
Event Handling
The code uses various event listeners to react to user changes:
Dropdown Changes: When a user selects an event from the dropdowns, it updates ticketworkflowconditions accordingly, and depending on the choice, may show/hide additional fields for configuration.
Toggle Switch: Enables/disables the entire event based on the state of the toggle.
Delete Button: Asks for confirmation before removing an event and adjusts the data structure accordingly.
UI Construction
The code appears to leverage a framework or library that supports web components (e.g., fw-select, fw-button, fw-toggle), which are custom elements for dropdowns, buttons, and toggles. These likely help in rendering consistent UI components in a structured way.
API Integration
The code fetches ticket fields from some external source (e.g., makeApiCall) to populate the dropdowns dynamically, allowing users to select applicable fields for workflow rules.
Conclusion
The purpose of this code is to facilitate the configuration of ticket workflows within a ticketing system. It provides an interactive user experience where users can define rules associated with ticket events dynamically. By allowing users to select events, fields, and options, it ensures that the necessary conditions for workflow automation are clearly established.
In this JavaScript code, div elements are used extensively to structure the user interface of the workflow management system for ticket events. Below is a detailed explanation of how div elements work within this context:
1. Creating Containers for Layout
Event Container:
Each workflow rule created by addNewRule is wrapped in a div with class="event-container". This div serves as a container to encapsulate all UI elements related to a particular event rule, such as dropdowns and buttons.
Inside the event container, there are multiple div elements that are set to utilize Bootstrap classes like col-md-2 and col-md-3. This creates a grid layout, allowing for responsive design. Each div organizes different UI components such as dropdowns, choices, and action buttons.
When creating dropdowns for event names or ticket fields, a div is used as a parent container to hold the dropdown components. For example, in the createEventDropdownSet method, the dropdowns are appended to a div labeled with classes like row and ticketworkflow-dropdown for CSS styling.
The field-name-container, choices-container, and button-container are created as div elements to manage layout and organizational structure when users select fields.
3. Dynamic Content Manipulation
Appending and Removing Elements:
The code dynamically creates and appends new div elements to the DOM based on user interactions. For instance, when a new field is configured, corresponding containers are created and added to maintain structure.
When a user changes their selection or deletes a rule, specific div containers (like the field-container or the choices related to a field) are removed from the UI using .remove(). This ensures that the UI accurately reflects the current state of the data.
const fieldremoveContainer = fieldNameContainer.closest(".field-container");
if (fieldremoveContainer) {
fieldremoveContainer.remove();
}
4. Event Handling UI Elements
Buttons Inside Divs:
Action buttons (like "Add," "Delete") are also placed inside div containers, ensuring that they are laid out correctly in relation to the dropdowns and other UI components. For example, the button for adding new fields is added to a specific buttonContainer during UI initialization.
The use of CSS classes defined in frameworks like Bootstrap or custom styles allows these div elements to be styled uniformly across different components. Classes such as event-container, field-name-container, and button-container help in assigning appropriate spacing, margins, and layout properties.
In Summary
The use of div elements in this code is crucial for managing the layout and structure of the user interface. They act as containers that can group related elements together logically and visually, allowing users to interact with the system effectively. The dynamic manipulation of these div elements ensures that the interface remains responsive and intuitive, adapting to user choices as they configure ticket workflow rules.
Certainly! To modify the code according to your requirements, we will create a whole-wrapperdiv that contains all the dropdowns (event dropdown, field dropdown, choices dropdown) and other UI elements like the button container. We'll also add an offset for the event dropdown when a new dropdown is created upon clicking the add icon.
Here’s how you can implement those changes:
Create the whole-wrapper div in the createEventDropdownSet function.
Structure the HTML accordingly within this wrapper to hold the event dropdown, field dropdown, choices dropdown, and button container.
Modify the add icon logic in the createAdditionalDropdowns function to include the required offset.
Here's how your modified code might look:
Modified createEventDropdownSet Function
Change the code inside this function to create a whole-wrapper:
async function createEventDropdownSet(obj, ruleIndex) {
console.log("Creating Event Dropdown");
// Create whole-wrapper div
const wholeWrapper = document.createElement('div');
wholeWrapper.classList.add('whole-wrapper');
const eventValue = obj?.event?.eventValue;
const newDropdownSet = document.createElement('div');
newDropdownSet.classList.add('row', 'ticketworkflow-dropdown');
const selectedEvent = typeof eventValue === 'string' ? eventValue : '';
newDropdownSet.innerHTML = `
<div class="col-md-2 offset-md-2 event-name-container">
<fw-select
class="event-name-dropdown"
label="Select Event Name"
required="true"
placeholder="Your choice"
hint-text="Select singular option">
<fw-select-option value="TicketCreate" ${selectedEvent === "TicketCreate" ? "selected" : ""}>Ticket Create</fw-select-option>
<fw-select-option value="TicketUpdate" ${selectedEvent === "TicketUpdate" ? "selected" : ""}>Ticket Update</fw-select-option>
<fw-select-option value="TicketCreateandUpdate" ${selectedEvent === "TicketCreateandUpdate" ? "selected" : ""}>Ticket Create and Update</fw-select-option>
</fw-select>
</div>
`;
const eventSelect = newDropdownSet.querySelector('.event-name-dropdown');
if (selectedEvent) {
eventSelect.value = selectedEvent;
}
eventSelect.addEventListener('fwChange', async () => {
const newEvent = eventSelect.value;
// Check if the event is unselected or invalid
if (!newEvent) {
console.log("Event unselected or invalid. Resetting ticketworkflowconditions to its initial state.");
// 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);
const rule = ticketworkflowconditions.length;
ticketworkflowconditions[ruleIndex] = {
rule,
event: {
eventName: newEvent,
eventValue: newEvent || '',
combination: []
},
checked: true
};
console.log("ticketworkflowconditions", ticketworkflowconditions);
// Setup the new dropdowns
newDropdownSet.querySelectorAll('.field-name-container, .choices-container, .button-container').forEach(container => container.remove());
const fieldNameContainer = document.createElement('div');
fieldNameContainer.classList.add('col-md-3', 'field-name-container');
newDropdownSet.appendChild(fieldNameContainer);
const choicesContainer = document.createElement('div');
choicesContainer.classList.add('col-md-3', 'choices-container');
newDropdownSet.appendChild(choicesContainer);
const buttonContainer = document.createElement('div');
buttonContainer.classList.add('col-md-2', 'button-container');
buttonContainer.style.marginTop = '-25px';
newDropdownSet.appendChild(buttonContainer);
await fieldschoosingvalues(
newEvent,
fieldNameContainer,
choicesContainer,
buttonContainer,
newDropdownSet, null, null, obj, ruleIndex, 0
);
console.log(`Updated ticketworkflowconditions: ${JSON.stringify(ticketworkflowconditions, null, 2)}`);
});
// Append the new dropdown set to the whole wrapper
wholeWrapper.appendChild(newDropdownSet);
// Append the whole wrapper to the event container
ticketworkflowContainer.appendChild(wholeWrapper);
return wholeWrapper; // Return the whole wrapper
}
Modified createAdditionalDropdowns Function
Now, update the logic for additional dropdowns in createAdditionalDropdowns to include the offset-md-2 class:
function createAdditionalDropdowns(container, obj, ruleIndex, fieldIndex) {
// Create a new whole-wrapper div
const wholeWrapper = document.createElement('div');
wholeWrapper.classList.add('whole-wrapper');
const fieldNameContainer = document.createElement('div');
fieldNameContainer.classList.add('col-md-3', 'field-name-container');
const choicesContainer = document.createElement('div');
choicesContainer.classList.add('col-md-3', 'choices-container');
const buttonContainer = document.createElement('div');
buttonContainer.classList.add('col-md-2', 'button-container');
buttonContainer.style.marginTop = '-23px';
// Append the container elements to the whole wrapper
wholeWrapper.appendChild(fieldNameContainer);
wholeWrapper.appendChild(choicesContainer);
wholeWrapper.appendChild(buttonContainer);
// Applying offset-md-2 to the event dropdown
const eventDropdownContainer = container.querySelector('.event-name-container');
eventDropdownContainer.classList.add('offset-md-2');
// Append the whole wrapper to the main container
container.appendChild(wholeWrapper);
// Call fieldschoosingvalues to set up fields
fieldschoosingvalues(
container.querySelector('.event-name-dropdown').value,
fieldNameContainer,
choicesContainer,
buttonContainer,
container,
null,
null,
obj,
ruleIndex,
fieldIndex
);
}
Summary of Changes
Whole Wrapper: Add a whole-wrapperdiv that encapsulates all event and field-related components to better organize the UI.
Add Offset Class: When creating additional dropdowns, the event dropdown includes the offset-md-2 class to ensure proper spacing consistent with your requirements.
Appending Structure: Ensure that the whole-wrapper is properly appended to the main container for proper display.
Testing
After making the changes, you should run your application and ensure that:
The new structure aligns visually as expected.
All functionality remains intact (adding/removing dropdowns, selections work, etc.).
Feel free to tweak the CSS classes to fit your desired layout further!
Yes, with the modifications mentioned earlier, the new structure should also work for existing mappings. However, to ensure compatibility with existing mappings, we must ensure that:
Existing Condition Initialization: When loading existing workflow conditions, the code should properly create the new structure (whole-wrapper) and append existing dropdown values so that they reflect correctly in the UI.
Event Listeners: Make sure that all necessary event listeners are appropriately attached to the new structure for handling user interactions (like when users select from dropdowns).
Modifications to Support Existing Mappings
To support the existing mappings with the new whole-wrapper structure, you may need to adjust the logic within your loadExistingWorkflowConditions and initializeEventDropdown functions. Below is an example of how to adjust the initialization logic to accommodate this new hierarchical structure.
Example: Adjusting loadExistingWorkflowConditions
In your loadExistingWorkflowConditions function, you will iterate through your existing workflow conditions (ticketworkflowconditions) and create the entire dropdown structure as described:
async function loadExistingWorkflowConditions() {
for (let index = 0; index < ticketworkflowconditions.length; index++) {
const element = ticketworkflowconditions[index];
await addNewRule(null, element, index);
}
}
Adjusting addNewRule
Since we are now creating the whole-wrapper structure in createEventDropdownSet, ensure that addNewRule calls this function as expected:
async function addNewRule(_event, obj, ruleIndex = ticketworkflowconditions.length) {
const ticketworkflowContainer = document.getElementById('ticketworkflow-container');
// Create a whole wrapper
const wholeWrapper = document.createElement('div');
wholeWrapper.classList.add('whole-wrapper');
// Create the event dropdown set
const newDropdownSet = await createEventDropdownSet(obj, ruleIndex);
// Append the whole wrapper containing the dropdowns
wholeWrapper.appendChild(newDropdownSet);
// Append the whole wrapper to the main container
ticketworkflowContainer.appendChild(wholeWrapper);
// Initialize other dropdowns if needed
await initializeEventDropdown(newDropdownSet, obj, ruleIndex);
}
Adjusting initializeEventDropdown
In initializeEventDropdown, you need to modify the way that dropdowns for fields and choices are set up. Ensure that all dropdowns are still initialized based on the existing data contained in obj so that their values are preserved and correctly displayed.
Conclusion
With these adjustments, the whole structure of the dropdowns and button containers, along with their event handlers, should work seamlessly for both newly added mapping rules and any previously existing ones. You’ll want to test thoroughly to ensure that loading existing rules with your new structure reflects all settings correctly and that dropdown interactions function as expected.
Summary of Things to Consider
Data Binding: Ensure that existing data is correctly loaded into the new dropdown structure. Verify that when loaded, each dropdown (event, fields, choices) displays the correct values based on the condition stored in ticketworkflowconditions.
Event Listeners: Attach all necessary event listeners after populating the dropdowns to ensure proper interaction.
User Interface: Conditionally render the whole-wrapper div and its contents so that they are correctly displayed and aligned, especially when dealing with offset classes.
By making these adjustments, your application should handle both new configurations and existing mappings effectively with the new structure.
Certainly! Below is the full modified code, incorporating the requested changes to use a whole-wrapperdiv for the event and field dropdowns, ensuring that it works for both new and existing mappings.
Complete Modified Code:
let ticketworkflowconditions = [];
async function loadExistingWorkflowConditions() {
for (let index = 0; index < ticketworkflowconditions.length; index++) {
const element = ticketworkflowconditions[index];
await addNewRule(null, element, index);
}
}
async function addNewRule(_event, obj, ruleIndex = ticketworkflowconditions.length) {
const ticketworkflowContainer = document.getElementById('ticketworkflow-container');
// Create a whole wrapper
const wholeWrapper = document.createElement('div');
wholeWrapper.classList.add('whole-wrapper');
// Create the event dropdown set
const newDropdownSet = await createEventDropdownSet(obj, ruleIndex);
// Append the whole wrapper containing the dropdowns
wholeWrapper.appendChild(newDropdownSet);
// Append the whole wrapper to the main container
ticketworkflowContainer.appendChild(wholeWrapper);
// Initialize other dropdowns if needed
await initializeEventDropdown(newDropdownSet, obj, ruleIndex);
}
async function createEventDropdownSet(obj, ruleIndex) {
console.log("Creating Event Dropdown");
// Create whole-wrapper div
const wholeWrapper = document.createElement('div');
wholeWrapper.classList.add('whole-wrapper');
const eventValue = obj?.event?.eventValue;
const newDropdownSet = document.createElement('div');
newDropdownSet.classList.add('row', 'ticketworkflow-dropdown');
const selectedEvent = typeof eventValue === 'string' ? eventValue : '';
newDropdownSet.innerHTML = `
<div class="col-md-2 offset-md-2 event-name-container">
<fw-select
class="event-name-dropdown"
label="Select Event Name"
required="true"
placeholder="Your choice"
hint-text="Select singular option">
<fw-select-option value="TicketCreate" ${selectedEvent === "TicketCreate" ? "selected" : ""}>Ticket Create</fw-select-option>
<fw-select-option value="TicketUpdate" ${selectedEvent === "TicketUpdate" ? "selected" : ""}>Ticket Update</fw-select-option>
<fw-select-option value="TicketCreateandUpdate" ${selectedEvent === "TicketCreateandUpdate" ? "selected" : ""}>Ticket Create and Update</fw-select-option>
</fw-select>
</div>
`;
const eventSelect = newDropdownSet.querySelector('.event-name-dropdown');
if (selectedEvent) {
eventSelect.value = selectedEvent;
}
eventSelect.addEventListener('fwChange', async () => {
const newEvent = eventSelect.value;
//