import Choices from "choices.js"; import { Sidebar } from './js/sidebar'; import './scss/app.scss' export interface choiceMap { [details: string]: Choices; } let choiceObjects: choiceMap = {}; document.addEventListener('DOMContentLoaded', function() { initSearch(); initSidebar(); initToggle(); replaceStrings(); initChoices(); initBoatActions(); selectBoatChange(); addRelationMagic(document.querySelector('body')); reloadPage(); setCurrentdate(document.querySelector('#departure')); }); function setCurrentdate(input: HTMLInputElement) { if(input) { const now = new Date(); const formattedDateTime = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}T${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}`; input.value = formattedDateTime; } } function selectBoatChange() { const boatSelect = document.querySelector('#boat_id'); if(boatSelect) { const boatChoice = new Choices(boatSelect, { loadingText: 'Wird geladen...', noResultsText: 'Keine Ergebnisse gefunden', noChoicesText: 'Keine Ergebnisse gefunden', itemSelectText: 'Zum Auswählen klicken', } as any); boatSelect.addEventListener('addItem', function(e) { const amount_seats = e.detail.customProperties.amount_seats; setMaxAmountRowers("newrower", amount_seats); const inputElement = document.getElementById("departure") as HTMLInputElement; const now = new Date(); const formattedDateTime = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}T${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}`; inputElement.value = formattedDateTime; },false); choiceObjects[boatSelect.id] = boatChoice; choiceObjects["boat_id"] = boatChoice; } } function reloadPage() { let pageTitle = document.title; let attentionMessage = 'Riemen- und Dollenbruch'; document.addEventListener('visibilitychange', function() { let isPageActive = !document.hidden; if(!isPageActive){ document.title = attentionMessage; } else { document.title = pageTitle; location.reload(); } }); } function setMaxAmountRowers(name: string, rowers: number) { console.log("called fct: name="+name+"; rowers="+rowers); if(choiceObjects[name]) { choiceObjects[name].removeActiveItems(); //let curSelection = choiceObjects[name].getValue(true); //let amount_to_delete = (curSelection).length - rowers; //if (amount_to_delete > 0){ // let to_delete = (curSelection).slice(-amount_to_delete); // for (let del of to_delete) { // choiceObjects[name].removeActiveItemsByValue(del); // } //} let input = document.querySelector('#'+name); if(input) { choiceObjects[name].config.maxItemCount = rowers; if (rowers === 0) { choiceObjects[name].disable() input.parentElement?.parentElement?.parentElement?.classList.add('hidden'); input.parentElement?.parentElement?.parentElement?.classList.add('md:block'); input.parentElement?.parentElement?.parentElement?.classList.add('opacity-50'); } else{ choiceObjects[name].enable(); input.parentElement?.parentElement?.parentElement?.classList.remove('hidden'); input.parentElement?.parentElement?.parentElement?.classList.remove('md:block'); input.parentElement?.parentElement?.parentElement?.classList.remove('opacity-50'); } } //let only_steering = document.querySelector('#shipmaster_only_steering'); //if(only_steering) { // if(isShipmasterSteering == 'true') { // only_steering.removeAttribute('disabled'); // only_steering.setAttribute('checked', 'true'); // only_steering.parentElement?.parentElement?.parentElement?.classList.remove('hidden'); // only_steering.parentElement?.parentElement?.parentElement?.classList.remove('md:block'); // only_steering.parentElement?.parentElement?.parentElement?.classList.remove('opacity-50'); // } else { // only_steering.setAttribute('disabled', 'disabled'); // only_steering.removeAttribute('checked'); // only_steering.parentElement?.parentElement?.parentElement?.classList.add('hidden'); // only_steering.parentElement?.parentElement?.parentElement?.classList.add('md:block'); // only_steering.parentElement?.parentElement?.parentElement?.classList.add('opacity-50'); // } //} } console.log(choiceObjects["newrower"].config.maxItemCount); } function initBoatActions() { const boatSelects = document.querySelectorAll('.boats-js[data-onclick="true"]'); if(boatSelects) { Array.prototype.forEach.call(boatSelects, (select: HTMLInputElement) => { select.addEventListener('click', function() { if(select.dataset.seats) { if (select.dataset.id) { choiceObjects['boat_id'].setChoiceByValue(select.dataset.id); } window.scrollTo(0, 0); } }); }); } } function initChoices() { const selects = document.querySelectorAll('select[data-init="true"]'); if(selects) { Array.prototype.forEach.call(selects, (select: HTMLInputElement) => { initNewChoice(select); }); } } interface ChoiceEvent extends Event{ detail: { value: string; label: string, customProperties: { is_cox: boolean, steers: boolean, cox_on_boat: boolean, amount_seats: number, } }; } function initNewChoice(select: HTMLInputElement) { const choice = new Choices(select, { removeItemButton: true, loadingText: 'Wird geladen...', noResultsText: 'Keine Ergebnisse gefunden', noChoicesText: 'Keine Ergebnisse gefunden', itemSelectText: 'Zum Auswählen klicken', placeholderValue: 'Ruderer auswählen', maxItemCount: -1, //TODO maxItemText: (maxItemCount) => { return `Nur ${maxItemCount} Ruderer können hinzugefügt werden`; }, callbackOnCreateTemplates: function () { return { option: ({ label, value, customProperties, active, disabled, }: any) => { const opt: HTMLOptionElement = Choices.defaults.templates.option.call( this, { label, value, customProperties, active, disabled } ); // We get the original