test/static/js/ol/map/map-timeline.js
2025-06-04 10:00:54 +07:00

73 lines
2.3 KiB
JavaScript

class TimeLine {
constructor(options) {
this.mapCore = options.mapCore
this.target = options.target
this.updateFunction = options.updateFunction
this.timeline = this.initTimeLine()
this.addTimelineEventListeners()
}
initTimeLine() {
const items = new vis.DataSet([]);
moment.locale('ru')
const options = {
locale: "ru",
min: '2025-05-30T12:00:00',
max: moment().add(12, 'hours'),
start: moment(),
zoomMin: 1000 * 60 * 60 * 24,
zoomMax: 1000 * 60 * 60 * 24,
moveable: true,
zoomable: true,
showCurrentTime: true,
selectable: false,
orientation: {axis: "top"},
format: {
minorLabels: {
minute: 'HH:mm',
hour: 'HH:mm',
second: 'HH:mm:ss'
},
majorLabels: function (date) {
return date.format("DD.MM.YY");
}
}
};
return new vis.Timeline(this.target, items, options);
}
addTimelineEventListeners() {
this.timeline.on('rangechanged', (props) => {
const center = new Date((props.start.getTime() + props.end.getTime()) / 2);
const currentDate = new Date()
const dateDif = currentDate - center
const iso = center.toISOString();
if (dateDif > 5000) {
globalTimeQuery = "?time=" + iso
} else {
globalTimeQuery = ""
}
document.getElementById("current-time").textContent = "Время на карте: " + center.toLocaleDateString() + " " + center.toLocaleTimeString()
this.updateFunction(globalTimeQuery);
});
setInterval(() => {
const newMax = moment().add(12, 'hours').toDate();
this.timeline.setOptions({
max: newMax
});
const window = this.timeline.getWindow();
if (window.end > (newMax - (60000 * 5))) {
const duration = window.end - window.start;
this.timeline.setWindow(new Date(newMax - duration), newMax);
}
}, 3000);
}
}