2023-04-06 13:15:40 +02:00
|
|
|
import { Sidebar } from './js/sidebar';
|
2023-04-05 19:33:31 +02:00
|
|
|
import './scss/app.scss'
|
2023-04-06 10:27:04 +02:00
|
|
|
|
|
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
|
|
initSearch();
|
2023-04-06 13:15:40 +02:00
|
|
|
initSidebar();
|
2023-04-06 10:27:04 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
function initSearch() {
|
|
|
|
const input = <HTMLInputElement>document.querySelector('#filter-js');
|
|
|
|
|
|
|
|
if(input) {
|
2023-04-06 10:45:33 +02:00
|
|
|
filterElements(input.value);
|
|
|
|
|
2023-04-06 10:27:04 +02:00
|
|
|
input.addEventListener('input', () => {
|
|
|
|
filterElements(input.value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function filterElements(input: string) {
|
|
|
|
const elements = document.querySelectorAll('form[data-filterable="true"]');
|
2023-04-06 10:45:33 +02:00
|
|
|
let resultWrapper = <HTMLElement>document.querySelector('#filter-result-js'),
|
|
|
|
amountShownElements = 0;
|
2023-04-06 10:27:04 +02:00
|
|
|
|
|
|
|
Array.prototype.forEach.call(elements, (element: HTMLElement) => {
|
|
|
|
// set both strings (input & dataset filter) to lowercase to not be case sensitive
|
|
|
|
let filterString = element.dataset.filter?.toLocaleLowerCase();
|
2023-04-06 10:45:33 +02:00
|
|
|
|
|
|
|
// bulk hide all elements
|
|
|
|
element.style.display = 'none';
|
2023-04-06 10:27:04 +02:00
|
|
|
|
2023-04-06 10:45:33 +02:00
|
|
|
// show if input matches
|
|
|
|
if(filterString?.includes(input.toLocaleLowerCase())) {
|
|
|
|
element.style.display = 'flex';
|
|
|
|
amountShownElements ++;
|
2023-04-06 10:27:04 +02:00
|
|
|
}
|
|
|
|
});
|
2023-04-06 10:45:33 +02:00
|
|
|
|
|
|
|
if(resultWrapper) {
|
|
|
|
resultWrapper.innerHTML = (amountShownElements === 0 ? 'Kein Ergebnis gefunden' : '<strong>' + amountShownElements + '</strong>' + (amountShownElements > 1 ? ' Ergebnisse' : ' Ergebnis') + ' gefunden');
|
|
|
|
}
|
2023-04-06 10:27:04 +02:00
|
|
|
}
|
2023-04-06 13:15:40 +02:00
|
|
|
|
|
|
|
function initSidebar() {
|
|
|
|
const sidebarTrigger = <NodeListOf<HTMLElement>>document.querySelectorAll('[data-trigger]');
|
|
|
|
|
|
|
|
if(sidebarTrigger) {
|
|
|
|
Array.prototype.forEach.call(sidebarTrigger, (triggerElement: HTMLElement) => {
|
|
|
|
if(triggerElement.dataset.trigger) {
|
|
|
|
const sidebar = new Sidebar(triggerElement.dataset.trigger);
|
|
|
|
|
|
|
|
triggerElement.addEventListener('click', () => {
|
2023-04-07 11:35:39 +02:00
|
|
|
if(triggerElement.dataset.trigger === 'addTrip') {
|
|
|
|
initTripSidebar(triggerElement);
|
|
|
|
}
|
|
|
|
|
2023-04-06 13:15:40 +02:00
|
|
|
sidebar.toggle();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2023-04-07 11:35:39 +02:00
|
|
|
|
|
|
|
function initTripSidebar(triggerElement: HTMLElement) {
|
|
|
|
const sidebarElement = <HTMLElement>document.querySelector('#addTrip');
|
|
|
|
if(sidebarElement && triggerElement.dataset.form && triggerElement.dataset.header) {
|
2023-04-07 11:45:12 +02:00
|
|
|
let form = <HTMLElement>document.querySelector(triggerElement.dataset.form);
|
|
|
|
let formElement = <HTMLElement>form.cloneNode(true);
|
2023-04-07 11:35:39 +02:00
|
|
|
let formContainerElement = <HTMLElement>sidebarElement.querySelector('.form-js');
|
|
|
|
if(formContainerElement) {
|
|
|
|
formContainerElement.innerHTML = '';
|
|
|
|
formContainerElement.append(formElement);
|
|
|
|
}
|
|
|
|
if(triggerElement.dataset.day) {
|
|
|
|
let hiddenElement = <HTMLInputElement>formElement.querySelector('.day-js');
|
|
|
|
if(hiddenElement) {
|
|
|
|
hiddenElement.value = triggerElement.dataset.day;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let headerElement = sidebarElement.querySelector('.header-js');
|
|
|
|
if(headerElement) {
|
|
|
|
headerElement.innerHTML = triggerElement.dataset.header;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|