test/static/js/ol/map/map-coefficients-manager.js
2025-06-03 17:13:14 +07:00

135 lines
4.8 KiB
JavaScript

function toggleDropdown(event, id) {
event.stopPropagation();
const dropdown = document.getElementById(`dropdown-${id}`);
const isVisible = dropdown.classList.contains('show');
// Сначала скрываем все dropdown
document.querySelectorAll('.custom-dropdown-menu').forEach(menu => {
menu.classList.remove('show');
});
// Затем показываем/скрываем нужный
if (!isVisible) {
dropdown.classList.add('show');
positionDropdown(dropdown);
}
}
function positionDropdown(dropdown) {
const container = dropdown.parentElement;
const containerRect = container.getBoundingClientRect();
const dropdownRect = dropdown.getBoundingClientRect();
const windowWidth = window.innerWidth;
const windowHeight = window.innerHeight;
// Сбрасываем стили позиционирования
dropdown.style.left = '';
dropdown.style.right = '';
dropdown.style.top = '';
dropdown.style.bottom = '';
dropdown.style.margin = '2px 0 0';
// Позиционируем по умолчанию (снизу)
let left = 0;
let top = containerRect.height;
// Проверяем, выходит ли dropdown за правую границу
if (containerRect.left + dropdownRect.width > windowWidth) {
dropdown.style.right = '0';
} else {
dropdown.style.left = '0';
}
// Проверяем, выходит ли dropdown за нижнюю границу
if (containerRect.bottom + dropdownRect.height > windowHeight) {
dropdown.style.bottom = '100%';
dropdown.style.top = 'auto';
dropdown.style.margin = '0 0 2px';
} else {
dropdown.style.top = '100%';
dropdown.style.bottom = 'auto';
}
}
// Закрытие dropdown при клике вне области
document.addEventListener('click', function () {
document.querySelectorAll('.custom-dropdown-menu').forEach(menu => {
menu.classList.remove('show');
});
});
// Предотвращаем закрытие при клике внутри dropdown
document.querySelectorAll('.custom-dropdown-menu').forEach(menu => {
menu.addEventListener('click', function (e) {
e.stopPropagation();
});
});
class CoefficientsManager {
constructor(options = {}) {
this.items = options.items || [];
this.updateInterval = options.updateInterval || 5000;
this.csrfToken = options.csrfToken || document.querySelector('[name=csrfmiddlewaretoken]').value;
if (this.updateInterval > 0) {
this.startAutoUpdate();
}
}
async update() {
try {
const response = await fetch("/main/coefficients/", {
headers: {
'X-CSRFToken': this.csrfToken
}
});
const data = await response.json();
this.items.forEach(item => {
const itemData = data[item];
if (!itemData) return;
const valueElement = document.getElementById(item);
if (valueElement) {
valueElement.textContent = itemData.Value + "%";
// Удаляем все классы перед добавлением нового
valueElement.className = 'badge coefficient-value';
if (itemData.Value >= 75) {
valueElement.classList.add('bg-success');
} else if (itemData.Value >= 50) {
valueElement.classList.add('bg-warning');
} else if (itemData.Value >= 25) {
valueElement.classList.add('bg-orange');
} else {
valueElement.classList.add('bg-danger');
}
}
const dropdownList = document.querySelector(`#dropdown-list-${item}`);
if (dropdownList) {
if (itemData.BadObjects && itemData.BadObjects.length > 0) {
dropdownList.innerHTML = itemData.BadObjects.map(obj => `
<li><a class="dropdown-item" href="${obj.url}">${obj.name}</a></li>
`).join('');
} else {
dropdownList.innerHTML = '<span style="text-align: center; width: 100%">Нет данных.</span>';
}
}
});
} catch (error) {
console.error("Ошибка при обновлении коэффициентов:", error);
}
}
startAutoUpdate() {
this.update();
this.intervalId = setInterval(() => this.update(), this.updateInterval);
}
stopAutoUpdate() {
if (this.intervalId) {
clearInterval(this.intervalId);
}
}
}