main.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import flatpickr from 'flatpickr';
  2. const initSwitcher = (gridMode, listMode, content) => {
  3. const activeClass = 'active';
  4. const displayList = 'display_list';
  5. const displayGrid = 'display_grid';
  6. const toggleSwitch = (btn, className) => {
  7. btn.classList.toggle(activeClass);
  8. content.classList.toggle(className);
  9. };
  10. let active = listMode;
  11. const eventListener = (oldBtn, oldClass, newBtn, newClass) => {
  12. return () => {
  13. if(active !== newBtn) {
  14. active = newBtn;
  15. // remove old state
  16. toggleSwitch(oldBtn, oldClass);
  17. // add new state
  18. toggleSwitch(newBtn, newClass);
  19. }
  20. };
  21. };
  22. gridMode.addEventListener('click', eventListener(listMode, displayList, gridMode, displayGrid));
  23. listMode.addEventListener('click', eventListener(gridMode, displayGrid, listMode, displayList));
  24. };
  25. initSwitcher(
  26. document.getElementById('mode-switcher-grid'),
  27. document.getElementById('mode-switcher-list'),
  28. document.getElementById('content')
  29. );
  30. const initDatepicker = (id) => {
  31. flatpickr(id, {
  32. dateFormat: "Y-m-d",
  33. wrap: true,
  34. weekNumbers: true
  35. });
  36. };
  37. initDatepicker('#from-date');
  38. initDatepicker('#to-date');