mirror of
https://github.com/itflow-org/itflow
synced 2026-08-06 15:47:15 +00:00
Fixed Asset section in recurring ticket if global added project in add ticket
This commit is contained in:
@@ -1,42 +1,156 @@
|
||||
// Used to populate dynamic content in recurring_ticket_add_modal and ticket_add_modal_v2 based on selected client
|
||||
|
||||
// Not every modal that loads this script has every dropdown, and a modal opened
|
||||
// from a contact page has no client selector at all - the client arrives as a
|
||||
// hidden field. Everything below therefore checks that an element exists before
|
||||
// touching it, rather than assuming the full set is present.
|
||||
|
||||
// Client selected listener
|
||||
// We seem to have to use jQuery to listen for events, as the client input is a select2 component?
|
||||
|
||||
const clientSelectDropdown = document.getElementById("changeClientSelect"); // Define client selector
|
||||
|
||||
// // If the client selector is disabled, we must be on a client-specific page instead. Trigger the lists to update.
|
||||
if (clientSelectDropdown.disabled) {
|
||||
if (clientSelectDropdown) {
|
||||
|
||||
let client_id = $(clientSelectDropdown).find(':selected').val();
|
||||
// If the client selector is disabled, we must be on a client-specific page instead. Trigger the lists to update.
|
||||
if (clientSelectDropdown.disabled) {
|
||||
|
||||
let client_id = $(clientSelectDropdown).find(':selected').val();
|
||||
|
||||
populateLists(client_id);
|
||||
}
|
||||
|
||||
// Listener for client selection. Populate select lists when a client is selected
|
||||
$(clientSelectDropdown).on('select2:select', function (e) {
|
||||
let client_id = $(this).find(':selected').val();
|
||||
|
||||
// Update the dependent dropdown lists
|
||||
populateLists(client_id);
|
||||
|
||||
});
|
||||
|
||||
} else {
|
||||
|
||||
// No client selector - the modal was opened from a contact page, where the
|
||||
// client is fixed and arrives as a hidden field instead
|
||||
const clientIdHiddenField = document.getElementById("clientIdHidden");
|
||||
|
||||
if (clientIdHiddenField && clientIdHiddenField.value) {
|
||||
populateLists(clientIdHiddenField.value);
|
||||
}
|
||||
|
||||
populateLists(client_id);
|
||||
}
|
||||
|
||||
// Listener for client selection. Populate select lists when a client is selected
|
||||
$(clientSelectDropdown).on('select2:select', function (e) {
|
||||
let client_id = $(this).find(':selected').val();
|
||||
|
||||
// Update the contacts dropdown list
|
||||
populateLists(client_id);
|
||||
|
||||
});
|
||||
|
||||
// Populates dropdowns with dynamic content based on the client ID
|
||||
// Called the client select dropdown is used or if the client select is disabled
|
||||
// Called when the client select dropdown is used or if the client select is disabled
|
||||
function populateLists(client_id) {
|
||||
|
||||
populateContactsDropdown(client_id);
|
||||
|
||||
populateAssetsDropdown(client_id);
|
||||
populateAssetsDropdowns(client_id);
|
||||
|
||||
populateLocationsDropdown(client_id);
|
||||
|
||||
populateVendorsDropdown(client_id);
|
||||
|
||||
populateProjectsDropdown(client_id);
|
||||
}
|
||||
|
||||
// Empties a dropdown and adds its placeholder, returning the element - or null if
|
||||
// this modal doesn't have it. Pass null as the label for a multi-select, which has
|
||||
// no placeholder option of its own.
|
||||
function resetDropdown(id, placeholderLabel, placeholderValue) {
|
||||
|
||||
const dropdown = document.getElementById(id);
|
||||
|
||||
if (!dropdown) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// innerHTML rather than removing options one by one, which leaves empty optgroups behind
|
||||
dropdown.innerHTML = '';
|
||||
|
||||
// A multi-select keeps showing its old selections until select2 is told the value changed
|
||||
$(dropdown).val(null).trigger('change.select2');
|
||||
|
||||
if (placeholderLabel !== null) {
|
||||
dropdown[dropdown.length] = new Option(placeholderLabel, placeholderValue);
|
||||
}
|
||||
|
||||
return dropdown;
|
||||
}
|
||||
|
||||
// Redraws a select2 component after its options have been replaced
|
||||
function refreshDropdown(dropdown) {
|
||||
if (dropdown) {
|
||||
$(dropdown).trigger('change.select2');
|
||||
}
|
||||
}
|
||||
|
||||
// Re-applies the value the modal was opened with (e.g. ticket_add_v2.php?project_id=4),
|
||||
// which can only be selected once the options it refers to exist
|
||||
function applyPreselection(dropdown) {
|
||||
|
||||
// '0' is the "none selected" value every one of these dropdowns uses, and is
|
||||
// truthy as a string - so it has to be excluded explicitly
|
||||
if (!dropdown || !dropdown.dataset.selected || dropdown.dataset.selected === '0') {
|
||||
return;
|
||||
}
|
||||
|
||||
dropdown.value = dropdown.dataset.selected;
|
||||
}
|
||||
|
||||
// Adds an optgroup to a dropdown and returns it, so options can be appended into it
|
||||
function appendOptionGroup(dropdown, label) {
|
||||
|
||||
if (!dropdown) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const group = document.createElement("optgroup");
|
||||
group.label = label;
|
||||
dropdown.appendChild(group);
|
||||
|
||||
return group;
|
||||
}
|
||||
|
||||
// Adds an option to an optgroup
|
||||
function appendGroupedOption(group, label, value) {
|
||||
|
||||
if (!group) {
|
||||
return;
|
||||
}
|
||||
|
||||
group.appendChild(new Option(label, value));
|
||||
}
|
||||
|
||||
// Builds the asset label as "Name - Make Model - (Contact)", matching how assets read elsewhere
|
||||
function buildAssetLabel(asset) {
|
||||
|
||||
let label = asset.asset_name;
|
||||
|
||||
if (asset.asset_make) {
|
||||
label = label + " - " + asset.asset_make;
|
||||
|
||||
if (asset.asset_model) {
|
||||
label = label + " " + asset.asset_model;
|
||||
}
|
||||
}
|
||||
|
||||
if (asset.contact_name) {
|
||||
label = label + " - (" + asset.contact_name + ")";
|
||||
}
|
||||
|
||||
return label;
|
||||
}
|
||||
|
||||
// Populate client contacts
|
||||
function populateContactsDropdown(client_id) {
|
||||
|
||||
if (!document.getElementById("contactSelect")) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Send a GET request to ajax.php as ajax.php?get_client_contacts=true&client_id=NUM
|
||||
jQuery.get(
|
||||
"ajax.php",
|
||||
@@ -47,17 +161,10 @@ function populateContactsDropdown(client_id) {
|
||||
const response = JSON.parse(data);
|
||||
|
||||
// Access the data for contacts (multiple)
|
||||
const contacts = response.contacts;
|
||||
const contacts = response.contacts || [];
|
||||
|
||||
// Contacts dropdown
|
||||
const contactSelectDropdown = document.getElementById("contactSelect");
|
||||
|
||||
// Clear dropdown
|
||||
let i, L = contactSelectDropdown.options.length - 1;
|
||||
for (i = L; i >= 0; i--) {
|
||||
contactSelectDropdown.remove(i);
|
||||
}
|
||||
contactSelectDropdown[contactSelectDropdown.length] = new Option('- Contact -', '0');
|
||||
const contactSelectDropdown = resetDropdown("contactSelect", '- Contact -', '0');
|
||||
|
||||
// Populate dropdown
|
||||
contacts.forEach(contact => {
|
||||
@@ -70,12 +177,22 @@ function populateContactsDropdown(client_id) {
|
||||
contactSelectDropdown[contactSelectDropdown.length] = new Option(contact.contact_name + appendText, contact.contact_id);
|
||||
});
|
||||
|
||||
applyPreselection(contactSelectDropdown);
|
||||
|
||||
refreshDropdown(contactSelectDropdown);
|
||||
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// Populate client assets
|
||||
function populateAssetsDropdown(client_id) {
|
||||
// Populate client assets - feeds both the single asset picker and the additional assets
|
||||
// multi-select from one request, as both need the same list
|
||||
function populateAssetsDropdowns(client_id) {
|
||||
|
||||
if (!document.getElementById("assetSelect") && !document.getElementById("additionalAssetsSelect")) {
|
||||
return;
|
||||
}
|
||||
|
||||
jQuery.get(
|
||||
"ajax.php",
|
||||
{get_client_assets: 'true', client_id: client_id},
|
||||
@@ -85,34 +202,47 @@ function populateAssetsDropdown(client_id) {
|
||||
const response = JSON.parse(data);
|
||||
|
||||
// Access the data for assets (multiple)
|
||||
const assets = response.assets;
|
||||
const assets = response.assets || [];
|
||||
|
||||
// Assets dropdown
|
||||
const assetSelectDropdown = document.getElementById("assetSelect");
|
||||
const assetSelectDropdown = resetDropdown("assetSelect", '- None -', '0');
|
||||
const additionalAssetsDropdown = resetDropdown("additionalAssetsSelect", null, null);
|
||||
|
||||
// Clear dropdown
|
||||
let i, L = assetSelectDropdown.options.length - 1;
|
||||
for (i = L; i >= 0; i--) {
|
||||
assetSelectDropdown.remove(i);
|
||||
}
|
||||
assetSelectDropdown[assetSelectDropdown.length] = new Option('- Asset -', '0');
|
||||
// Assets arrive ordered by type, so a change of type starts a new group
|
||||
let currentType = null;
|
||||
let assetGroup = null;
|
||||
let additionalAssetGroup = null;
|
||||
|
||||
// Populate dropdown with asset name (and contact, if set)
|
||||
assets.forEach(asset => {
|
||||
let displayText = asset.asset_name;
|
||||
if (asset.contact_name !== null) {
|
||||
displayText = asset.asset_name + " - " + asset.contact_name;
|
||||
const assetType = asset.asset_type || 'Uncategorized';
|
||||
|
||||
if (assetType !== currentType) {
|
||||
currentType = assetType;
|
||||
assetGroup = appendOptionGroup(assetSelectDropdown, assetType);
|
||||
additionalAssetGroup = appendOptionGroup(additionalAssetsDropdown, assetType);
|
||||
}
|
||||
|
||||
assetSelectDropdown[assetSelectDropdown.length] = new Option(displayText, asset.asset_id);
|
||||
const assetLabel = buildAssetLabel(asset);
|
||||
|
||||
appendGroupedOption(assetGroup, assetLabel, asset.asset_id);
|
||||
appendGroupedOption(additionalAssetGroup, assetLabel, asset.asset_id);
|
||||
});
|
||||
|
||||
applyPreselection(assetSelectDropdown);
|
||||
|
||||
refreshDropdown(assetSelectDropdown);
|
||||
refreshDropdown(additionalAssetsDropdown);
|
||||
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// Populate client locations
|
||||
function populateLocationsDropdown(client_id) {
|
||||
|
||||
if (!document.getElementById("locationSelect")) {
|
||||
return;
|
||||
}
|
||||
|
||||
jQuery.get(
|
||||
"ajax.php",
|
||||
{get_client_locations: 'true', client_id: client_id},
|
||||
@@ -122,29 +252,31 @@ function populateLocationsDropdown(client_id) {
|
||||
const response = JSON.parse(data);
|
||||
|
||||
// Access the data for locations (multiple)
|
||||
const locations = response.locations;
|
||||
const locations = response.locations || [];
|
||||
|
||||
// Locations dropdown
|
||||
const locationSelectDropdown = document.getElementById("locationSelect");
|
||||
|
||||
// Clear dropdown
|
||||
let i, L = locationSelectDropdown.options.length - 1;
|
||||
for (i = L; i >= 0; i--) {
|
||||
locationSelectDropdown.remove(i);
|
||||
}
|
||||
locationSelectDropdown[locationSelectDropdown.length] = new Option('- Location -', '0');
|
||||
const locationSelectDropdown = resetDropdown("locationSelect", '- Location -', '0');
|
||||
|
||||
// Populate dropdown
|
||||
locations.forEach(location => {
|
||||
locationSelectDropdown[locationSelectDropdown.length] = new Option(location.location_name, location.location_id);
|
||||
});
|
||||
|
||||
applyPreselection(locationSelectDropdown);
|
||||
|
||||
refreshDropdown(locationSelectDropdown);
|
||||
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// Populate client vendors
|
||||
function populateVendorsDropdown(client_id) {
|
||||
|
||||
if (!document.getElementById("vendorSelect")) {
|
||||
return;
|
||||
}
|
||||
|
||||
jQuery.get(
|
||||
"ajax.php",
|
||||
{get_client_vendors: 'true', client_id: client_id},
|
||||
@@ -153,24 +285,55 @@ function populateVendorsDropdown(client_id) {
|
||||
// If we get a response from ajax.php, parse it as JSON
|
||||
const response = JSON.parse(data);
|
||||
|
||||
// Access the data for locations (multiple)
|
||||
const vendors = response.vendors;
|
||||
// Access the data for vendors (multiple)
|
||||
const vendors = response.vendors || [];
|
||||
|
||||
// Locations dropdown
|
||||
const vendorSelectDropdown = document.getElementById("vendorSelect");
|
||||
|
||||
// Clear dropdown
|
||||
let i, L = vendorSelectDropdown.options.length - 1;
|
||||
for (i = L; i >= 0; i--) {
|
||||
vendorSelectDropdown.remove(i);
|
||||
}
|
||||
vendorSelectDropdown[vendorSelectDropdown.length] = new Option('- Vendor -', '0');
|
||||
// Vendors dropdown
|
||||
const vendorSelectDropdown = resetDropdown("vendorSelect", '- Vendor -', '0');
|
||||
|
||||
// Populate dropdown
|
||||
vendors.forEach(vendor => {
|
||||
vendorSelectDropdown[vendorSelectDropdown.length] = new Option(vendor.vendor_name, vendor.vendor_id);
|
||||
});
|
||||
|
||||
applyPreselection(vendorSelectDropdown);
|
||||
|
||||
refreshDropdown(vendorSelectDropdown);
|
||||
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// Populate client projects
|
||||
function populateProjectsDropdown(client_id) {
|
||||
|
||||
if (!document.getElementById("projectSelect")) {
|
||||
return;
|
||||
}
|
||||
|
||||
jQuery.get(
|
||||
"ajax.php",
|
||||
{get_client_projects: 'true', client_id: client_id},
|
||||
function(data) {
|
||||
|
||||
// If we get a response from ajax.php, parse it as JSON
|
||||
const response = JSON.parse(data);
|
||||
|
||||
// Access the data for projects (multiple)
|
||||
const projects = response.projects || [];
|
||||
|
||||
// Projects dropdown
|
||||
const projectSelectDropdown = resetDropdown("projectSelect", '- Select Project -', '0');
|
||||
|
||||
// Populate dropdown
|
||||
projects.forEach(project => {
|
||||
projectSelectDropdown[projectSelectDropdown.length] = new Option(project.project_name, project.project_id);
|
||||
});
|
||||
|
||||
applyPreselection(projectSelectDropdown);
|
||||
|
||||
refreshDropdown(projectSelectDropdown);
|
||||
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user