functions.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import {limitToRange} from '../lib/utils.js';
  2. import {addMonths, addYears} from '../lib/date.js';
  3. export function triggerDatepickerEvent(datepicker, type) {
  4. const detail = {
  5. date: datepicker.getDate(),
  6. viewDate: new Date(datepicker.picker.viewDate),
  7. viewId: datepicker.picker.currentView.id,
  8. datepicker,
  9. };
  10. datepicker.element.dispatchEvent(new CustomEvent(type, {detail}));
  11. }
  12. // direction: -1 (to previous), 1 (to next)
  13. export function goToPrevOrNext(datepicker, direction) {
  14. const {minDate, maxDate} = datepicker.config;
  15. const {currentView, viewDate} = datepicker.picker;
  16. let newViewDate;
  17. switch (currentView.id) {
  18. case 0:
  19. newViewDate = addMonths(viewDate, direction);
  20. break;
  21. case 1:
  22. newViewDate = addYears(viewDate, direction);
  23. break;
  24. default:
  25. newViewDate = addYears(viewDate, direction * currentView.navStep);
  26. }
  27. newViewDate = limitToRange(newViewDate, minDate, maxDate);
  28. datepicker.picker.changeFocus(newViewDate).render();
  29. }
  30. export function switchView(datepicker) {
  31. const viewId = datepicker.picker.currentView.id;
  32. if (viewId === datepicker.config.maxView) {
  33. return;
  34. }
  35. datepicker.picker.changeView(viewId + 1).render();
  36. }
  37. export function unfocus(datepicker) {
  38. if (datepicker.config.updateOnBlur) {
  39. datepicker.update({autohide: true});
  40. } else {
  41. datepicker.refresh('input');
  42. datepicker.hide();
  43. }
  44. }