From c0366edbe13799c7725217ece461bf37e2731499 Mon Sep 17 00:00:00 2001 From: Marie Birner Date: Sat, 23 Sep 2023 21:05:08 +0200 Subject: [PATCH] [TASK] add working choices.js --- frontend/logbook.ts | 10 ----- frontend/main.ts | 57 +++++++++++++++++++++++++- templates/includes/forms/log.html.tera | 13 ++---- 3 files changed, 60 insertions(+), 20 deletions(-) diff --git a/frontend/logbook.ts b/frontend/logbook.ts index 8d848df..26c5a18 100644 --- a/frontend/logbook.ts +++ b/frontend/logbook.ts @@ -1,13 +1,3 @@ -import Choices from "choices.js"; - -const selects = document.querySelectorAll('select[multiple]'); - if(selects) { - Array.prototype.forEach.call(selects, (select: HTMLInputElement) => { - const choices = new Choices(select); - }); - } - - /*document.addEventListener('DOMContentLoaded', function() { setDistance('.set-distance-js'); }); diff --git a/frontend/main.ts b/frontend/main.ts index d78c41e..4c1f938 100644 --- a/frontend/main.ts +++ b/frontend/main.ts @@ -1,14 +1,62 @@ +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(); }); +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) { + const rowers = Number(select.dataset.seats) - 1; + choiceObjects['newrower'].config.maxItemCount = rowers; + choiceObjects['newrower'].removeActiveItems(rowers); + } + }); + }); + } +} + +function initChoices() { + const selects = document.querySelectorAll('select[data-init="true"]'); + if(selects) { + Array.prototype.forEach.call(selects, (select: HTMLInputElement) => { + initNewChoice(select); + }); + } +} + +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: Number(select.dataset.seats) - 1, + maxItemText: (maxItemCount) => { + return `Nur ${maxItemCount} Ruderer können hinzugefügt werden`; + }, + }); + + choiceObjects[select.id] = choice; +} + function initToggle() { // get filter btns & set object sessionStorage const btns = >document.querySelectorAll('.filter-trips-js'); @@ -191,6 +239,13 @@ function initTripSidebar(triggerElement: HTMLElement) { let body = document.querySelector(triggerElement.dataset.body); let bodyElement = body.cloneNode(true); let bodyContainerElement = sidebarElement.querySelector('.body-js'); + + const selects = bodyElement.querySelectorAll('select[multiple]'); + if(selects) { + Array.prototype.forEach.call(selects, (select: HTMLInputElement) => { + initNewChoice(select); + }); + } /* Quickfix duplicate ids checkboxes */ const checkboxes = >bodyElement.querySelectorAll('input[type="checkbox"]'); diff --git a/templates/includes/forms/log.html.tera b/templates/includes/forms/log.html.tera index fe1438b..3e37f7c 100644 --- a/templates/includes/forms/log.html.tera +++ b/templates/includes/forms/log.html.tera @@ -13,7 +13,7 @@ {{ amount_seats }}x {% for boat in grouped_boats %} -
+
{{ boat.name }} {% if boat.owner %} @@ -52,7 +52,7 @@ {% endif %} {% if not only_ones %} - {{ log::rower_select(id="newrower", selected=[], class="col-span-2") }} + {{ log::rower_select(id="newrower", selected=[], class="col-span-2", init=true) }} {% endif %}
@@ -103,9 +103,9 @@ {% endif %} {% endmacro boat_select %} -{% macro rower_select(id, selected, amount_seats='', class='') %} +{% macro rower_select(id, selected, amount_seats='', class='', init='false') %}
- {% for user in users %} {% set_global sel = false %} {% for rower in selected %} @@ -117,11 +117,6 @@ {% endfor %}
-
- - {{amount_seats}} - Ruderer auszuwählen -
{% endmacro rower_select %} {% macro show(log, state, allowed_to_close=false, only_ones) %}