test/static/js/ol/map/map-coefficients-manager.js

84 lines
3.1 KiB
JavaScript

class CoefficientsManager {
constructor(options = {}) {
this.items = options.items || [];
this.updateInterval = options.updateInterval || 5000;
this.csrfToken = options.csrfToken || document.querySelector('[name=csrfmiddlewaretoken]').value;
this._init();
if (this.updateInterval > 0) {
this.startAutoUpdate();
}
}
_init() {
this.items.forEach(item => {
const container = document.querySelector(`.coefficient-item[data-type="${item}"]`);
if (container) {
container.addEventListener('click', (e) => {
if (e.target.closest('.clickable-area')) {
const menu = container.querySelector('.dropdown-menu');
menu.style.display = menu.style.display === 'block' ? 'none' : 'block';
}
});
}
});
}
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);
}
}
}