2022-12-23 18:26:16 +01:00
|
|
|
import Choices from 'choices.js';
|
2022-11-17 18:49:25 +01:00
|
|
|
import { formatDay, formatTime } from './date';
|
2022-11-29 19:19:30 +01:00
|
|
|
import { ready } from './ready';
|
2018-12-06 22:45:40 +01:00
|
|
|
|
2016-08-21 20:14:09 +02:00
|
|
|
/**
|
2017-12-27 13:36:38 +01:00
|
|
|
* Sets all checkboxes to the wanted state
|
|
|
|
*/
|
2023-04-01 15:14:32 +02:00
|
|
|
ready(() => {
|
|
|
|
document.querySelectorAll('button.checkbox-selection').forEach((buttonElement) => {
|
|
|
|
buttonElement.addEventListener('click', () => {
|
|
|
|
document.querySelectorAll(`#${buttonElement.dataset.id} input[type="checkbox"]`).forEach((checkboxElement) => {
|
|
|
|
/**
|
|
|
|
* @type {boolean|int[]}
|
|
|
|
*/
|
|
|
|
const value = JSON.parse(buttonElement.dataset.value);
|
|
|
|
if (typeof value === 'boolean') {
|
|
|
|
checkboxElement.checked = value;
|
|
|
|
} else {
|
|
|
|
checkboxElement.checked = value.includes(Number(checkboxElement.value));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2022-11-26 14:50:59 +01:00
|
|
|
});
|
2023-04-01 15:14:32 +02:00
|
|
|
});
|
2014-12-06 21:39:59 +01:00
|
|
|
|
2023-01-02 18:54:32 +01:00
|
|
|
ready(() => {
|
|
|
|
/**
|
|
|
|
* @param {HTMLElement} element
|
|
|
|
*/
|
|
|
|
const triggerChange = (element) => {
|
|
|
|
const changeEvent = new Event('change');
|
|
|
|
element.dispatchEvent(changeEvent);
|
2023-01-03 20:20:46 +01:00
|
|
|
};
|
2017-11-29 13:23:38 +01:00
|
|
|
|
2022-12-23 18:26:16 +01:00
|
|
|
/**
|
|
|
|
* Sets a select value and triggers a change.
|
|
|
|
* If the select has a Choices.js instances, it uses this instead to set the value.
|
|
|
|
*
|
|
|
|
* @param {HTMLSelectElement} element
|
|
|
|
* @param {*} value
|
|
|
|
*/
|
|
|
|
const setSelectValue = (element, value) => {
|
|
|
|
if (element.choices) {
|
|
|
|
element.choices.setChoiceByValue(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
element.value = value;
|
|
|
|
triggerChange(element);
|
|
|
|
};
|
|
|
|
|
2023-01-02 18:54:32 +01:00
|
|
|
/**
|
|
|
|
* Sets the values of the input fields with the IDs to from/to:
|
|
|
|
* - date portion of from → start_day
|
|
|
|
* - time portion of from → start_time
|
|
|
|
* - date portion of to → end_day
|
|
|
|
* - time portion of to → end_time
|
|
|
|
*
|
|
|
|
* @param {Date} from
|
|
|
|
* @param {Date} to
|
|
|
|
*/
|
|
|
|
const setInput = (from, to) => {
|
|
|
|
const fromDay = document.getElementById('start_day');
|
|
|
|
const fromTime = document.getElementById('start_time');
|
|
|
|
const toDay = document.getElementById('end_day');
|
|
|
|
const toTime = document.getElementById('end_time');
|
|
|
|
|
|
|
|
if (!fromDay || !fromTime || !toDay || !toTime) {
|
|
|
|
console.warn('cannot set input date because of missing field');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-23 18:26:16 +01:00
|
|
|
setSelectValue(fromDay, formatDay(from));
|
2023-01-02 18:54:32 +01:00
|
|
|
fromTime.value = formatTime(from);
|
|
|
|
|
2022-12-23 18:26:16 +01:00
|
|
|
setSelectValue(toDay, formatDay(to));
|
2023-01-02 18:54:32 +01:00
|
|
|
toTime.value = formatTime(to);
|
|
|
|
};
|
2017-11-29 13:23:38 +01:00
|
|
|
|
2023-01-02 18:54:32 +01:00
|
|
|
/**
|
|
|
|
* @param {MouseEvent} event
|
|
|
|
*/
|
|
|
|
const onClickDate = (event) => {
|
|
|
|
const days = Number(event.currentTarget.dataset.days);
|
2017-11-29 13:23:38 +01:00
|
|
|
|
2023-01-02 18:54:32 +01:00
|
|
|
const from = new Date();
|
|
|
|
from.setHours(0, 0, 0, 0);
|
2017-11-29 13:23:38 +01:00
|
|
|
|
2023-01-02 18:54:32 +01:00
|
|
|
// add days, Date handles the overflow
|
|
|
|
from.setDate(from.getDate() + days);
|
2017-11-29 13:23:38 +01:00
|
|
|
|
2023-01-02 18:54:32 +01:00
|
|
|
const to = new Date(from);
|
|
|
|
to.setHours(23, 59);
|
2017-11-29 13:23:38 +01:00
|
|
|
|
2023-01-02 18:54:32 +01:00
|
|
|
setInput(from, to);
|
|
|
|
};
|
2017-11-29 13:23:38 +01:00
|
|
|
|
2023-01-02 18:54:32 +01:00
|
|
|
/**
|
|
|
|
* @param {MouseEvent} event
|
|
|
|
*/
|
|
|
|
const onClickTime = (event) => {
|
|
|
|
const hours = Number(event.currentTarget.dataset.hours);
|
2017-11-29 13:23:38 +01:00
|
|
|
|
2023-01-02 18:54:32 +01:00
|
|
|
const from = new Date();
|
|
|
|
const to = new Date(from);
|
2017-11-29 13:23:38 +01:00
|
|
|
|
2023-01-02 18:54:32 +01:00
|
|
|
// add hours, Date handles the overflow
|
|
|
|
to.setHours(to.getHours() + hours);
|
2017-11-29 13:23:38 +01:00
|
|
|
|
2023-01-02 18:54:32 +01:00
|
|
|
if (to < from) {
|
|
|
|
setInput(to, from);
|
2023-01-03 20:20:46 +01:00
|
|
|
} else {
|
2023-01-02 18:54:32 +01:00
|
|
|
setInput(from, to);
|
|
|
|
}
|
|
|
|
};
|
2017-11-29 13:23:38 +01:00
|
|
|
|
2023-01-02 18:54:32 +01:00
|
|
|
document.querySelectorAll('.set-date').forEach((element) => {
|
|
|
|
element.addEventListener('click', onClickDate);
|
|
|
|
});
|
|
|
|
document.querySelectorAll('.set-time').forEach((element) => {
|
|
|
|
element.addEventListener('click', onClickTime);
|
|
|
|
});
|
|
|
|
});
|
2017-11-29 13:23:38 +01:00
|
|
|
|
2022-11-29 21:47:26 +01:00
|
|
|
ready(() => {
|
2022-11-26 14:50:59 +01:00
|
|
|
/**
|
2022-11-29 21:47:26 +01:00
|
|
|
* Disable every submit button after clicking (to prevent double-clicking)
|
|
|
|
*/
|
|
|
|
document.querySelectorAll('form').forEach((formElement) => {
|
|
|
|
formElement.addEventListener('submit', () => {
|
|
|
|
document.querySelectorAll('input[type="submit"],button[type="submit"]').forEach((element) => {
|
|
|
|
element.readOnly = true;
|
|
|
|
element.classList.add('disabled');
|
|
|
|
});
|
|
|
|
});
|
2022-11-26 14:50:59 +01:00
|
|
|
});
|
2014-12-06 21:39:59 +01:00
|
|
|
});
|
2018-12-04 21:03:32 +01:00
|
|
|
|
2022-12-23 18:31:26 +01:00
|
|
|
/**
|
|
|
|
* {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/disabled#overview}
|
|
|
|
*/
|
|
|
|
const DISABLE_ELEMENTS = [
|
2022-12-23 19:15:03 +01:00
|
|
|
'button',
|
|
|
|
'command',
|
|
|
|
'fieldset',
|
|
|
|
'input',
|
|
|
|
'keygen',
|
|
|
|
'optgroup',
|
|
|
|
'option',
|
|
|
|
'select',
|
|
|
|
'textarea',
|
2022-12-23 18:31:26 +01:00
|
|
|
];
|
|
|
|
ready(() => {
|
|
|
|
// get all input-radio's and add for each an onChange event listener
|
|
|
|
document.querySelectorAll('input[type="radio"]').forEach((radioElement) => {
|
2023-11-05 00:46:48 +01:00
|
|
|
// build selector and get all corresponding elements for this input-radio
|
2022-12-23 19:15:03 +01:00
|
|
|
const selector = DISABLE_ELEMENTS.map(
|
|
|
|
(tagName) => `${tagName}[data-radio-name="${radioElement.name}"][data-radio-value]`
|
|
|
|
).join(',');
|
2022-12-23 18:31:26 +01:00
|
|
|
const elements = Array.from(document.querySelectorAll(selector));
|
|
|
|
|
|
|
|
// set all states one time on init for each of the corresponding elements
|
|
|
|
elements.forEach((element) => {
|
|
|
|
// each radio button updates only his elements
|
|
|
|
if (element.dataset.radioValue === radioElement.value) {
|
|
|
|
element.disabled = !radioElement.checked;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// add an onChange event listener that update the disabled state for all corresponding elements
|
|
|
|
radioElement.addEventListener('change', () => {
|
|
|
|
elements.forEach((element) => {
|
|
|
|
element.disabled = element.dataset.radioValue !== radioElement.value;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-10 21:24:21 +01:00
|
|
|
ready(() => {
|
2023-05-11 17:52:01 +02:00
|
|
|
const addClickHandler = (selector, onClick) => {
|
|
|
|
document.querySelectorAll(selector).forEach((element) => {
|
|
|
|
const inputElement = document.getElementById(element.dataset.inputId);
|
|
|
|
|
|
|
|
if (!inputElement || !inputElement.stepUp || !inputElement.stepDown) return;
|
|
|
|
|
|
|
|
if (inputElement.disabled || inputElement.readOnly) {
|
|
|
|
// The input element is disabled or read-only → disable the +/- button as well.
|
|
|
|
// Note that changing the "disabled" or "readonly" attributes during runtime is not yet supported.
|
|
|
|
element.setAttribute('disabled', 'disabled');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
element.addEventListener('click', () => onClick(inputElement));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
addClickHandler('.spinner-up', (inputElement) => {
|
|
|
|
inputElement.stepUp();
|
2022-12-10 21:24:21 +01:00
|
|
|
});
|
2023-05-11 17:52:01 +02:00
|
|
|
|
|
|
|
addClickHandler('.spinner-down', (inputElement) => {
|
|
|
|
inputElement.stepDown();
|
2022-12-10 21:24:21 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-10 02:24:20 +01:00
|
|
|
/**
|
2018-12-05 18:43:51 +01:00
|
|
|
* Button to set current time in time input fields.
|
|
|
|
*/
|
2022-11-29 21:47:26 +01:00
|
|
|
ready(() => {
|
|
|
|
document.querySelectorAll('.input-group.time').forEach((element) => {
|
|
|
|
const button = element.querySelector('button');
|
|
|
|
if (!button) return;
|
|
|
|
|
|
|
|
button.addEventListener('click', () => {
|
2022-11-26 14:50:59 +01:00
|
|
|
const now = new Date();
|
2022-11-29 21:47:26 +01:00
|
|
|
const input = element.querySelector('input');
|
|
|
|
if (!input) return;
|
|
|
|
|
|
|
|
input.value = formatTime(now);
|
|
|
|
const daySelector = document.getElementById(input.id.replace('time', 'day'));
|
|
|
|
if (!daySelector) return;
|
|
|
|
|
|
|
|
const dayElements = daySelector.querySelectorAll('option');
|
2022-11-26 14:50:59 +01:00
|
|
|
const yyyyMMDD = formatDay(now);
|
2022-11-29 21:47:26 +01:00
|
|
|
dayElements.forEach((dayElement) => {
|
|
|
|
if (dayElement.value === yyyyMMDD) {
|
|
|
|
daySelector.value = dayElement.value;
|
2022-11-26 14:50:59 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2018-12-05 18:43:51 +01:00
|
|
|
});
|
2022-11-26 14:50:59 +01:00
|
|
|
});
|
2018-12-05 18:43:51 +01:00
|
|
|
});
|
2019-09-06 03:45:56 +02:00
|
|
|
|
2023-11-05 00:46:48 +01:00
|
|
|
/**
|
|
|
|
* Init select dropdown choices
|
|
|
|
*/
|
2022-11-29 21:47:26 +01:00
|
|
|
ready(() => {
|
2022-12-23 18:26:16 +01:00
|
|
|
document.querySelectorAll('select').forEach((element) => {
|
|
|
|
element.choices = new Choices(element, {
|
2023-08-14 10:52:02 +02:00
|
|
|
allowHTML: true,
|
2023-11-08 17:27:15 +01:00
|
|
|
shouldSort: false,
|
|
|
|
shouldSortItems: false,
|
2022-12-23 18:26:16 +01:00
|
|
|
classNames: {
|
|
|
|
containerInner: 'choices__inner form-control',
|
|
|
|
},
|
|
|
|
fuseOptions: {
|
|
|
|
distance: 0,
|
|
|
|
ignoreLocation: true,
|
|
|
|
includeScore: true,
|
|
|
|
threshold: 0,
|
|
|
|
},
|
|
|
|
itemSelectText: '',
|
|
|
|
// do not use Number.MAX_SAFE_INTEGER here, because otherwise the script gets stuck
|
|
|
|
searchResultLimit: 9999,
|
|
|
|
});
|
2022-11-26 14:50:59 +01:00
|
|
|
});
|
2022-12-22 18:28:51 +01:00
|
|
|
});
|
2020-10-20 16:07:34 +02:00
|
|
|
|
2023-01-22 19:16:33 +01:00
|
|
|
/**
|
|
|
|
* Init Bootstrap Popover
|
|
|
|
*/
|
|
|
|
ready(() => {
|
|
|
|
document.querySelectorAll('[data-bs-toggle="popover"]').forEach((element) => new bootstrap.Popover(element));
|
|
|
|
});
|
|
|
|
|
2023-02-26 11:27:41 +01:00
|
|
|
/**
|
|
|
|
* Init Bootstrap Tooltips
|
|
|
|
*/
|
|
|
|
ready(() => {
|
|
|
|
document.querySelectorAll('[data-bs-toggle="tooltip"]').forEach((element) => new bootstrap.Tooltip(element));
|
|
|
|
});
|
|
|
|
|
2023-11-05 00:46:48 +01:00
|
|
|
/**
|
|
|
|
* Init Bootstrap Modals
|
|
|
|
*/
|
|
|
|
ready(() => {
|
|
|
|
document.querySelectorAll('.modal').forEach((element) => new bootstrap.Modal(element));
|
|
|
|
});
|
|
|
|
|
2023-11-05 01:27:12 +01:00
|
|
|
/**
|
|
|
|
* Show confirmation modal before submitting form
|
|
|
|
*
|
|
|
|
* Uses the buttons data attributes to show in the modal:
|
|
|
|
* - data-confirm_title: Optional title of the modal
|
|
|
|
* - data-confirm_submit: Body of the modal
|
|
|
|
*
|
|
|
|
* The class, title and content of the requesting button gets copied for confirmation
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
ready(() => {
|
|
|
|
document.querySelectorAll('[data-confirm_submit_title], [data-confirm_submit_text]').forEach((element) => {
|
|
|
|
let modalOpen = false;
|
|
|
|
let oldType = element.type;
|
|
|
|
if (element.type !== 'submit') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
element.type = 'button';
|
|
|
|
element.addEventListener('click', (event) => {
|
|
|
|
if (modalOpen) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
document.getElementById('confirmation-modal')?.remove();
|
|
|
|
document.body.insertAdjacentHTML(
|
|
|
|
'beforeend',
|
|
|
|
`
|
|
|
|
<div class="modal" tabindex="-1" id="confirmation-modal">
|
|
|
|
<div class="modal-dialog">
|
|
|
|
<div class="modal-content ${document.body.dataset.theme_type === 'light' ? 'bg-white' : 'bg-dark'}">
|
|
|
|
<div class="modal-header">
|
|
|
|
<h5 class="modal-title">${element.dataset.confirm_submit_title ?? ''}</h5>
|
|
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
|
|
</div>
|
|
|
|
<div class="modal-body${element.dataset.confirm_submit_text ? '' : ' d-none'}">
|
|
|
|
<p>${element.dataset.confirm_submit_text ?? ''}</p>
|
|
|
|
</div>
|
|
|
|
<div class="modal-footer">
|
|
|
|
<button type="button" class="${element.className}"
|
2023-11-05 16:29:07 +01:00
|
|
|
title="${element.title}" data-submit="">
|
|
|
|
${element.dataset.confirm_button_text ?? element.innerHTML}
|
|
|
|
</button>
|
2023-11-05 01:27:12 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`
|
|
|
|
);
|
|
|
|
|
|
|
|
let modal = document.getElementById('confirmation-modal');
|
|
|
|
modal.addEventListener('hide.bs.modal', () => {
|
|
|
|
modalOpen = false;
|
|
|
|
});
|
|
|
|
modal.querySelector('[data-submit]').addEventListener('click', (event) => {
|
|
|
|
element.type = oldType;
|
|
|
|
element.click();
|
|
|
|
});
|
|
|
|
|
|
|
|
modalOpen = true;
|
|
|
|
let bootstrapModal = new bootstrap.Modal(modal);
|
|
|
|
bootstrapModal.show();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-11-15 18:47:30 +01:00
|
|
|
/**
|
|
|
|
* Show oauth buttons on welcome title click
|
|
|
|
*/
|
2022-11-29 21:47:26 +01:00
|
|
|
ready(() => {
|
|
|
|
[
|
2022-12-10 02:24:20 +01:00
|
|
|
['welcome-title', '.btn-group .btn.d-none'],
|
2022-11-29 21:47:26 +01:00
|
|
|
['settings-title', '.user-settings .nav-item'],
|
|
|
|
['oauth-settings-title', 'table tr.d-none'],
|
|
|
|
].forEach(([id, selector]) => {
|
|
|
|
document.getElementById(id)?.addEventListener('click', () => {
|
|
|
|
document.querySelectorAll(selector).forEach((element) => {
|
|
|
|
element.classList.remove('d-none');
|
|
|
|
});
|
|
|
|
});
|
2022-11-26 14:50:59 +01:00
|
|
|
});
|
2020-11-15 18:47:30 +01:00
|
|
|
});
|
|
|
|
|
2019-09-06 03:45:56 +02:00
|
|
|
/**
|
|
|
|
* Set the filter selects to latest state
|
|
|
|
*
|
|
|
|
* Uses DOMContentLoaded to prevent flickering
|
|
|
|
*/
|
2022-11-29 19:19:30 +01:00
|
|
|
ready(() => {
|
2023-04-01 15:14:32 +02:00
|
|
|
const collapseElement = document.getElementById('collapseShiftsFilterSelect');
|
|
|
|
if (collapseElement) {
|
|
|
|
if (localStorage.getItem('collapseShiftsFilterSelect') === 'hidden.bs.collapse') {
|
|
|
|
collapseElement.classList.remove('show');
|
|
|
|
}
|
2019-09-06 03:45:56 +02:00
|
|
|
|
2023-04-01 15:14:32 +02:00
|
|
|
/**
|
|
|
|
* @param {Event} event
|
|
|
|
*/
|
|
|
|
const onChange = (event) => {
|
|
|
|
localStorage.setItem('collapseShiftsFilterSelect', event.type);
|
|
|
|
};
|
2022-10-22 16:14:21 +02:00
|
|
|
|
2023-04-01 15:14:32 +02:00
|
|
|
collapseElement.addEventListener('hidden.bs.collapse', onChange);
|
|
|
|
collapseElement.addEventListener('shown.bs.collapse', onChange);
|
2022-11-26 14:50:59 +01:00
|
|
|
}
|
2023-04-01 15:14:32 +02:00
|
|
|
});
|
2019-09-06 03:45:56 +02:00
|
|
|
|
2023-04-01 15:14:32 +02:00
|
|
|
/**
|
|
|
|
* Show/hide checkboxes for User Driver-Licenses
|
|
|
|
*/
|
|
|
|
ready(() => {
|
|
|
|
const checkboxElement = document.getElementById('wants_to_drive');
|
|
|
|
const drivingLicenseElement = document.getElementById('driving_license');
|
2019-09-06 03:45:56 +02:00
|
|
|
|
2023-04-01 15:14:32 +02:00
|
|
|
if (checkboxElement && drivingLicenseElement) {
|
|
|
|
drivingLicenseElement.hidden = !checkboxElement.checked;
|
2022-11-29 21:47:26 +01:00
|
|
|
|
2023-04-01 15:14:32 +02:00
|
|
|
checkboxElement.addEventListener('click', () => {
|
|
|
|
drivingLicenseElement.hidden = !checkboxElement.checked;
|
|
|
|
});
|
|
|
|
}
|
2019-09-06 03:45:56 +02:00
|
|
|
});
|
2023-04-16 19:33:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Prevent scrolling on # links in menu
|
|
|
|
*/
|
|
|
|
ready(() => {
|
|
|
|
const elements = document.querySelectorAll('.navbar a[href="#"]');
|
|
|
|
elements.forEach((a) => {
|
|
|
|
a.addEventListener('click', (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|