|
| 1 | +import {getLocale} from "@symfony/ux-translator"; |
| 2 | +import AirDatepicker from 'air-datepicker'; |
| 3 | +import '../../styles/lib/air-datepicker-custom.css'; |
| 4 | +const datepickerLocale = await import('air-datepicker/locale/'+getLocale()+'.js'); |
| 5 | + |
| 6 | +const FORM_FILTER_SIDEBAR = document.getElementById('filter-sidebar'); |
| 7 | +const PERIOD_SELECT = document.getElementById('period'); |
| 8 | +const BTN_APPLY_PERIOD_FILTER = document.getElementById('apply-period-filter'); |
| 9 | + |
| 10 | +const datepicker = new AirDatepicker(document.getElementById('datepicker'), { |
| 11 | + range: true, |
| 12 | + multipleDates: true, |
| 13 | + locale: await datepickerLocale.default.default, |
| 14 | + onSelect: function ({ date, formattedDate }) { |
| 15 | + const customPeriod = document.querySelector('#period option[data-name=custom]'); |
| 16 | + customPeriod.classList.remove('d-none'); |
| 17 | + customPeriod.innerText = formattedDate.join(' - '); |
| 18 | + |
| 19 | + customPeriod.value = date.map(d => { |
| 20 | + return d.getFullYear() + '-' |
| 21 | + + (d.getMonth()+1).toString().padStart(2, '0') + '-' |
| 22 | + + d.getDate().toString().padStart(2, '0'); |
| 23 | + }).join(','); |
| 24 | + customPeriod.selected = true; |
| 25 | + |
| 26 | + if (date.length === 2) { |
| 27 | + customPeriod.selected = true; |
| 28 | + BTN_CLOSE_FILTER.click(); |
| 29 | + } |
| 30 | + }, |
| 31 | +}); |
| 32 | + |
| 33 | +PERIOD_SELECT.addEventListener('change', function () { |
| 34 | + datepicker.clear({silent: true}); |
| 35 | + const customPeriod = document.querySelector('#period option[data-name=custom]'); |
| 36 | + customPeriod.classList.add('d-none'); |
| 37 | + customPeriod.innerText = ''; |
| 38 | +}); |
| 39 | + |
| 40 | +BTN_APPLY_PERIOD_FILTER.addEventListener('click', function () { |
| 41 | + const period = PERIOD_SELECT.value; |
| 42 | + const url = new URL(window.location.href); |
| 43 | + |
| 44 | + url.searchParams.set('period', period); |
| 45 | + |
| 46 | + window.location.href = url.toString(); |
| 47 | +}); |
| 48 | + |
| 49 | +(function () { |
| 50 | + const searchParams = new URLSearchParams(window.location.search); |
| 51 | + |
| 52 | + new FormData(FORM_FILTER_SIDEBAR).forEach(function (value, key) { |
| 53 | + const element = FORM_FILTER_SIDEBAR.querySelector(`[name=${key}]`); |
| 54 | + if (element) { |
| 55 | + element.value = searchParams.get(key); |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + if ('' === PERIOD_SELECT.value) { |
| 60 | + const period = searchParams.get('period'); |
| 61 | + if (period) { |
| 62 | + const customPeriod = document.querySelector('#period option[data-name=custom]'); |
| 63 | + const periodInnerText = period.split(',').map(date => { |
| 64 | + const [year, month, day] = date.split('-'); |
| 65 | + return `${day.padStart(2, '0')}/${month.padStart(2, '0')}/${year}`; |
| 66 | + }); |
| 67 | + |
| 68 | + customPeriod.value = period; |
| 69 | + customPeriod.selected = 'selected'; |
| 70 | + customPeriod.innerText = periodInnerText.join(' - '); |
| 71 | + } |
| 72 | + } |
| 73 | +})(); |
0 commit comments