if i want another combo with same event, i will click addnewrule and choose ticketcreate, then choosing field and choices. it should add as new combination . now the json should view like this.
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-2', 'field-name-container');
container.appendChild(fieldNameContainer);
}
if (!choicesContainer) {
choicesContainer = document.createElement('div');
choicesContainer.classList.add('col-md-2', 'choices-container');
container.appendChild(choicesContainer);
}
function updateIsEnabledState(selectedEventValue, isChecked) {
if (!selectedEventValue) {
console.error("Event is not selected.");
return;
}
if (!ticketworkflowconditions[selectedEventValue]) {
ticketworkflowconditions[selectedEventValue] = { combinations: [{ isEnabled: isChecked, fields: {} }] };
}
let combination = ticketworkflowconditions[selectedEventValue].combinations[0];
combination.isEnabled = isChecked;
console.log(`Toggle for ${selectedEventValue} set to ${isChecked ? 'enabled' : 'disabled'}.`);
console.log(`Updated ticketworkflowconditions after toggle change: ${JSON.stringify(ticketworkflowconditions, null, 2)}`);
}
}
async function fieldschoosingvalues(selectedEventValue, fieldNameContainer, choicesContainer, buttonContainer, container, selectedFieldName = null, selectedChoices = null) {
if (!selectedEventValue) {
console.error("fieldschoosingvalues called with an undefined selectedEventValue.");
return;
}
try {
let statusResp = await makeApiCall(apiKeyInput1.value, domainInput1.value);
if (statusResp && statusResp.ticket_fields) {
let fields = statusResp.ticket_fields.filter(field =>
field.field_type.startsWith('default_') &&
field.choices &&
field.choices.length > 0 &&
field.required_for_agents
);
console.log(`Starting fieldschoosingvalues with selectedEventValue: ${selectedEventValue}`);
console.log(`Mapping field name: ${selectedFieldName}`);
console.log(`Mapping choice value: ${selectedChoices}`);
if (selectedFieldName) {
const fieldContainer = document.createElement('div');
fieldContainer.classList.add('field-container');
fieldNameContainer.appendChild(fieldContainer);
const choicesDiv = document.createElement('div');
choicesDiv.classList.add('choices-container');
choicesContainer.appendChild(choicesDiv);
await getFieldnamesfrom1stapi(fieldContainer, fields, choicesDiv, selectedEventValue, buttonContainer, container, selectedFieldName, selectedChoices);
} else {
await getFieldnamesfrom1stapi(fieldNameContainer, fields, choicesContainer, selectedEventValue, buttonContainer, container);
}
} else {
console.error('No fields found or data not available');
}
} catch (error) {
console.error("Error fetching data for Dropdown:", error);
}
}
async function getFieldnamesfrom1stapi(fieldNameContainer, fields, choicesContainer, selectedEventValue, buttonContainer, container, selectedFieldName = null, selectedChoices = null) {
if (!selectedEventValue) {
showErrorToast("Select an event before proceeding.");
return;
}
if (selectedChoices && selectedChoices.includes(String(choice.id))) {
option.setAttribute('selected', 'selected');
}
});
choicesContainer.appendChild(choicesDropdown);
// Check if the button has already been added
if (!buttonContainer.querySelector('.btn-primary')) {
const addButton = document.createElement('button');
addButton.textContent = 'Add';
addButton.classList.add('btn', 'btn-primary', 'mt-2');
buttonContainer.appendChild(addButton);
The JavaScript code you posted appears to be part of a web application intended for dynamic creation and management of dropdown menus for ticket event types and their associated rules. The purpose seems to be allowing users to specify combinations of fields and values for different ticket events, which then get stored as JSON configurations - similar to the examples you've shown at the beginning.
Let's break down the main parts of this code to understand what it's doing and how it might be extended or modified to meet your needs:
Main Components:
createEventDropdownSet(eventName):
This function is responsible for creating a dropdown set for a specific event, including an event name dropdown and adding event listeners.
It initializes ticketworkflowconditions with the default structure if the event does not exist.
This function sets up the UI for a single event with its configurations.
It initializes various UI containers and manages their dynamic layout and event bindings.
Updates the isEnabled state based on a toggle switch.
fieldschoosingvalues(...):
Responsible for selecting fields and their choices based on collected data from an API call.
Ensures the correct field and choice selection based on previous user actions or API data.
Calls methods to get field names and set up choice selectors.
getFieldnamesfrom1stapi(...):
Populates the field selector according to the API response.
Adds event listeners to update choices when a new field is selected.
showingitschoices(...):
Displays choices related to the selected field.
Updates the configurations when a new choice is selected.
Adds an "Add" button to allow adding more rules/choices.
createAdditionalDropdowns(container):
Allows users to create additional sets of dropdowns.
Manages the additional UI elements for creating more combinations.
Understanding the Code:
The code deals heavily with dynamically creating DOM elements and using event listeners to manage state.
It saves the configuration using a ticketworkflowconditions object, which mirrors the JSON structure demonstrated at the beginning.
The UI is built using specific HTML elements, and interaction is managed via event listeners and state updates.
Building Upon the Code:
Adding a New Rule: When a user wants to add a new combination, they would presumably click an "Add New Rule" button, triggering createAdditionalDropdowns, which in turn calls fieldschoosingvalues for a new set.
Field & Choice Logic: The logic to add fields and choices involves fetching data from an API and constructing the dropdown options based on that data.
Toggle & Delete: Provides UI controls to enable/disable a rule (via a toggle) or remove an event entirely.
This framework enables users to create and manage complex configurations by selecting various ticket events, each with its own rules about status, priority, etc. If you have more specific actions you'd like to implement or other modifications needed, feel free to specify!