test/static/js/ol/map/map-time-slider.js

67 lines
2.2 KiB
JavaScript

async function fetchEarthquakes(date) {
const formattedDate = date.toISOString().split('T')[0]; // Формат YYYY-MM-DD
const response = await fetch(`/seismicproc/earthquakes/?date=${formattedDate}`)
return await response.json()
}
class TimeSlider {
constructor(options = {}) {
this.sliderId = options.sliderId || 'time-slider';
this.dateDisplayId = options.dateDisplayId || 'slider-date';
this.onChange = options.onChange || (() => {
});
this.formatDate = options.formatDate || this._defaultFormatDate;
this.slider = document.getElementById(this.sliderId);
this.dateDisplay = document.getElementById(this.dateDisplayId);
this._init();
}
_init() {
this.slider.addEventListener('input', (e) => {
const date = this.calculateDateFromSlider(parseInt(e.target.value));
this.dateDisplay.textContent = this.formatDate(date);
this.onChange(date);
});
}
setRange(startDate, endDate, initialDate = null) {
this.startDate = startDate;
this.endDate = endDate;
const totalDays = (endDate - startDate) / (24 * 60 * 60 * 1000);
this.slider.min = 0;
this.slider.max = 100;
this.slider.step = 100 / totalDays;
if (initialDate) {
this.setValue(initialDate);
}
}
setValue(date) {
const value = this._calculateSliderValueFromDate(date);
this.slider.value = value;
this.dateDisplay.textContent = this.formatDate(date);
}
calculateDateFromSlider(value) {
const fraction = value / 100;
const timeDiff = this.endDate - this.startDate;
return new Date(this.startDate.getTime() + fraction * timeDiff);
}
_calculateSliderValueFromDate(date) {
const timeDiff = date - this.startDate;
const totalTime = this.endDate - this.startDate;
return (timeDiff / totalTime) * 100;
}
_defaultFormatDate(date) {
const day = String(date.getDate()).padStart(2, '0');
const month = String(date.getMonth() + 1).padStart(2, '0');
const year = date.getFullYear();
return `${day}.${month}.${year}`;
}
}