162 lines
6.1 KiB
JavaScript

//
//Generated by: SM2Gen version:0.9(20Jan2025) Chameleon version:4.5.4 On Python:3.12.3 at 2025-04-05 11:17:38
//
$(document).ready(function() {
});
// Initialize multiselect with existing text values
function initMultiselect() {
const multiselect = document.getElementById('CountrySelect_select');
const textInput = document.getElementById('AccumCountryCodes_text');
// Split text input values and select corresponding options
textInput.value.split(',').forEach(value => {
const option = Array.from(multiselect.options).find(opt => opt.value === value.trim());
if (option) option.selected = true;
});
}
// Update text input when multiselect changes
function updateTextInput() {
const multiselect = document.getElementById('CountrySelect_select');
const textInput = document.getElementById('AccumCountryCodes_text');
// Get selected values from the multiselect as an array
const selectedValues = Array.from(multiselect.selectedOptions).map(option => option.value);
// Split the current value of the text input into an array of codes
const currentValues = textInput.value.split(',').map(code => code.trim()).filter(code => code !== '');
// Create a new set to manage the codes
const updatedValuesSet = new Set(currentValues);
// Add selected values to the set
selectedValues.forEach(value => updatedValuesSet.add(value));
// Remove values from the set that are no longer selected but were added via the multiselect
currentValues.forEach(code => {
if (!selectedValues.includes(code) && multiselect.querySelector(`option[value="${code}"]`)) {
updatedValuesSet.delete(code);
}
});
// Update the text input with the updated values, preserving manually added codes
textInput.value = Array.from(updatedValuesSet).join(',');
}
// Initialize on page load
document.addEventListener('DOMContentLoaded', () => {
initMultiselect();
// Add change listener to multiselect
document.getElementById('CountrySelect_select')
.addEventListener('click', updateTextInput);
});
/**
* Date Select Manager
*
* This script manages an HTML select element with dates and updates an HTML object
* based on the selected date. It sets the initial value to yesterday's date (or the latest date available),
* updates the object's data parameter, and refreshes the display.
*/
// Wait for DOM to be fully loaded before executing
document.addEventListener('DOMContentLoaded', function() {
// Initialize all date selects and their corresponding objects
initializeSelects();
});
function initializeSelects() {
// Find all select elements with IDs starting with "StatsDate_select"
const selects = document.querySelectorAll('select[id^="StatsDate_select"]');
selects.forEach(function(dateSelect) {
const selectId = dateSelect.id;
// Find the corresponding object element
// If the select has a numeric suffix, look for object with same suffix
const objectId = selectId === 'StatsDate_select' ?
'mailstats_object' :
'mailstats_object' + selectId.replace('StatsDate_select', '');
const mailstatsObject = document.getElementById(objectId);
const mailstatsfull = document.getElementById('mailstats-full-window');
// Check if elements exist before proceeding
if (!dateSelect) {
console.error(`Select element with ID "${selectId}" not found.`);
return; // Skip this pair if select doesn't exist
}
if (!mailstatsObject) {
console.error(`Object element with ID "${objectId}" not found.`);
// Continue anyway, as we can still set the select to the right date
}
// Function to update the object's data parameter and refresh it
function updateMailstatsObject(date) {
if (date && mailstatsObject) {
try {
// Create the full URL including the selected date
const currentDomain = window.location.origin;
const fullUrl = `${currentDomain}/mailstats/mailstats_for_${date}.html`;
// Update the data attribute with the full URL
mailstatsObject.setAttribute('data', fullUrl);
mailstatsfull.setAttribute('href', fullUrl);
// Force a refresh of the object
// [refresh code here]
console.log(`Updated object to show data from: ${fullUrl}`);
} catch (error) {
console.error(`Error updating object:`, error);
}
}
}
// Set the initial value of the select
const yesterdayDate = getYesterdayDate();
let dateFound = false;
// Find and select the option with yesterday's date
for (let i = 0; i < dateSelect.options.length; i++) {
if (dateSelect.options[i].value === yesterdayDate) {
dateSelect.selectedIndex = i;
dateFound = true;
break;
}
}
// If yesterday's date is not found, select the latest date (last option)
if (!dateFound && dateSelect.options.length > 0) {
dateSelect.selectedIndex = dateSelect.options.length - 1;
console.log(`Yesterday's date (${yesterdayDate}) not found in options for ${selectId}. Selected the latest date instead: ${dateSelect.value}`);
}
// Update the object with the initial date
if (dateSelect.value) {
updateMailstatsObject(dateSelect.value);
}
// Add event listener for changes to the select element
dateSelect.addEventListener('change', function() {
const selectedDate = this.value;
updateMailstatsObject(selectedDate);
});
});
}
// Function to get yesterday's date in YYYY-MM-DD format
function getYesterdayDate() {
const today = new Date();
const yesterday = new Date(today);
yesterday.setDate(yesterday.getDate() - 1);
const year = yesterday.getFullYear();
const month = String(yesterday.getMonth() + 1).padStart(2, '0');
const day = String(yesterday.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
}