Реализовал поле ввода даты и времени в таймлайне
На главной странице добавлена возможность выбирать дату и время с помощью поля ввода (необходимо кликнуть на подчеркнутую текущую дату)
This commit is contained in:
parent
4be7847240
commit
b905f49022
@ -286,7 +286,6 @@
|
||||
if (globalTimeQuery !== "" && prevTimeQuery === globalTimeQuery) return
|
||||
if (globalTimeQuery !== "") {
|
||||
loading = true
|
||||
console.log(loaderWrapper.classList.contains("d-none"))
|
||||
if (loaderWrapper.classList.contains("d-none")) {
|
||||
loaderWrapper.classList.remove("d-none")
|
||||
}
|
||||
|
||||
@ -4,6 +4,8 @@ class TimeLine {
|
||||
this.target = options.target
|
||||
this.updateFunction = options.updateFunction
|
||||
|
||||
this.disposePopover = this.disposePopover.bind(this);
|
||||
|
||||
this.timeline = this.initTimeLine()
|
||||
this.addTimelineEventListeners()
|
||||
}
|
||||
@ -25,11 +27,8 @@ class TimeLine {
|
||||
orientation: {axis: "top"},
|
||||
format: {
|
||||
minorLabels: {
|
||||
minute: 'HH:mm',
|
||||
hour: 'HH:mm',
|
||||
second: 'HH:mm:ss'
|
||||
},
|
||||
majorLabels: function (date) {
|
||||
minute: 'HH:mm', hour: 'HH:mm', second: 'HH:mm:ss'
|
||||
}, majorLabels: function (date) {
|
||||
return date.format("DD.MM.YY");
|
||||
}
|
||||
}
|
||||
@ -37,6 +36,14 @@ class TimeLine {
|
||||
return new vis.Timeline(this.target, items, options);
|
||||
}
|
||||
|
||||
disposePopover(e) {
|
||||
if (e.target !== this.currentTimeLink &&
|
||||
!this.currentTimeLink.contains(e.target) &&
|
||||
!document.querySelector('.popover')?.contains(e.target)) {
|
||||
this.popover.hide();
|
||||
}
|
||||
}
|
||||
|
||||
addTimelineEventListeners() {
|
||||
this.timeline.on('rangechanged', (props) => {
|
||||
const center = new Date((props.start.getTime() + props.end.getTime()) / 2);
|
||||
@ -48,14 +55,12 @@ class TimeLine {
|
||||
globalTimeQuery = "?time=" + iso
|
||||
} else {
|
||||
globalTimeQuery = ""
|
||||
|
||||
}
|
||||
document.getElementById("current-time").textContent = "Время на карте: " + center.toLocaleDateString() + " " + center.toLocaleTimeString()
|
||||
|
||||
this.updateCurrentTimeDisplay(center);
|
||||
this.updateFunction(globalTimeQuery);
|
||||
});
|
||||
|
||||
|
||||
setInterval(() => {
|
||||
const newMax = moment().add(12, 'hours').toDate();
|
||||
this.timeline.setOptions({
|
||||
@ -69,4 +74,117 @@ class TimeLine {
|
||||
}
|
||||
}, 3000);
|
||||
}
|
||||
}
|
||||
|
||||
updateCurrentTimeDisplay(center) {
|
||||
const currentTime = document.getElementById("current-time");
|
||||
|
||||
// Очистка currentTime
|
||||
while (currentTime.firstChild) {
|
||||
currentTime.removeChild(currentTime.firstChild);
|
||||
}
|
||||
|
||||
const currentTimeSpan = document.createElement("span");
|
||||
currentTimeSpan.textContent = "Время на карте: ";
|
||||
|
||||
const currentTimeLink = document.createElement("a");
|
||||
currentTimeLink.textContent = center.toLocaleDateString() + " " + center.toLocaleTimeString();
|
||||
currentTimeLink.style.zIndex = "33";
|
||||
currentTimeLink.style.textDecoration = "underline";
|
||||
currentTimeLink.style.cursor = "pointer";
|
||||
currentTimeLink.id = "currentTimeLink"
|
||||
this.currentTimeLink = currentTimeLink
|
||||
|
||||
// Создаем форму для выбора времени
|
||||
const form = document.createElement('form');
|
||||
form.id = 'timelineDatetimeSelectForm';
|
||||
form.className = 'needs-validation';
|
||||
form.noValidate = true;
|
||||
|
||||
const formGroup = document.createElement('div');
|
||||
formGroup.className = 'mb-3';
|
||||
|
||||
const label = document.createElement('label');
|
||||
label.htmlFor = 'timelineDatetimeInput';
|
||||
label.className = 'form-label';
|
||||
label.textContent = 'Выбор даты и времени';
|
||||
|
||||
const input = document.createElement('input');
|
||||
input.type = 'datetime-local';
|
||||
input.id = 'timelineDatetimeInput';
|
||||
input.className = 'form-control';
|
||||
|
||||
// Устанавливаем текущее значение и максимальную дату
|
||||
const setInputValues = () => {
|
||||
const now = new Date();
|
||||
const year = now.getFullYear();
|
||||
const month = String(now.getMonth() + 1).padStart(2, '0');
|
||||
const day = String(now.getDate()).padStart(2, '0');
|
||||
const hours = String(now.getHours()).padStart(2, '0');
|
||||
const minutes = String(now.getMinutes()).padStart(2, '0');
|
||||
const maxDateTime = `${year}-${month}-${day}T${hours}:${minutes}`;
|
||||
|
||||
input.max = maxDateTime;
|
||||
};
|
||||
|
||||
const inputContainer = document.createElement('div');
|
||||
inputContainer.id = 'datetimeInputContainer';
|
||||
inputContainer.appendChild(input);
|
||||
|
||||
const resetBtn = document.createElement('button');
|
||||
resetBtn.type = 'reset';
|
||||
resetBtn.className = 'btn btn-outline-secondary btn-sm me-2';
|
||||
resetBtn.textContent = 'Сбросить';
|
||||
|
||||
const submitBtn = document.createElement('button');
|
||||
submitBtn.type = 'submit';
|
||||
submitBtn.className = 'btn btn-primary btn-sm';
|
||||
submitBtn.textContent = 'Перейти';
|
||||
|
||||
formGroup.appendChild(label);
|
||||
formGroup.appendChild(inputContainer);
|
||||
form.appendChild(formGroup);
|
||||
form.appendChild(resetBtn);
|
||||
form.appendChild(submitBtn);
|
||||
|
||||
form.addEventListener("submit", (e) => {
|
||||
e.preventDefault();
|
||||
const selectedDate = new Date(input.value);
|
||||
if (isNaN(selectedDate.getTime())) return;
|
||||
|
||||
// Обновляем таймлайн
|
||||
const duration = this.timeline.getWindow().end - this.timeline.getWindow().start;
|
||||
const start = new Date(selectedDate.getTime() - duration / 2);
|
||||
const end = new Date(selectedDate.getTime() + duration / 2);
|
||||
|
||||
this.timeline.setWindow(start, end);
|
||||
|
||||
// Обновляем URL
|
||||
globalTimeQuery = "?time=" + selectedDate.toISOString();
|
||||
this.updateFunction(globalTimeQuery);
|
||||
|
||||
// Закрываем popover
|
||||
popover.hide();
|
||||
});
|
||||
|
||||
const popover = new bootstrap.Popover(currentTimeLink, {
|
||||
trigger: 'manual', html: true, content: form, placement: 'top', sanitize: false
|
||||
});
|
||||
this.popover = popover
|
||||
// Обновляем значения поля ввода при каждом показе popover
|
||||
currentTimeLink.addEventListener('click', (e) => {
|
||||
e.preventDefault();
|
||||
setInputValues();
|
||||
|
||||
if (currentTimeLink.getAttribute('aria-describedby')) {
|
||||
popover.hide();
|
||||
} else {
|
||||
popover.show();
|
||||
}
|
||||
});
|
||||
|
||||
document.addEventListener('click', (e) => this.disposePopover(e, currentTimeLink, popover));
|
||||
|
||||
currentTime.appendChild(currentTimeSpan);
|
||||
currentTime.appendChild(currentTimeLink);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user