View.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import {pushUnique} from '../../lib/utils.js';
  2. import {parseHTML, replaceChildNodes} from '../../lib/dom.js';
  3. // Base class of the view classes
  4. export default class View {
  5. constructor(picker, config) {
  6. Object.assign(this, config, {
  7. picker,
  8. element: parseHTML(`<div class="datepicker-view"></div>`).firstChild,
  9. selected: [],
  10. });
  11. this.init(this.picker.datepicker.config);
  12. }
  13. init(options) {
  14. if (options.pickLevel !== undefined) {
  15. this.isMinView = this.id === options.pickLevel;
  16. }
  17. this.setOptions(options);
  18. this.updateFocus();
  19. this.updateSelection();
  20. }
  21. // Execute beforeShow() callback and apply the result to the element
  22. // args:
  23. // - current - current value on the iteration on view rendering
  24. // - timeValue - time value of the date to pass to beforeShow()
  25. performBeforeHook(el, current, timeValue) {
  26. let result = this.beforeShow(new Date(timeValue));
  27. switch (typeof result) {
  28. case 'boolean':
  29. result = {enabled: result};
  30. break;
  31. case 'string':
  32. result = {classes: result};
  33. }
  34. if (result) {
  35. if (result.enabled === false) {
  36. el.classList.add('disabled');
  37. pushUnique(this.disabled, current);
  38. }
  39. if (result.classes) {
  40. const extraClasses = result.classes.split(/\s+/);
  41. el.classList.add(...extraClasses);
  42. if (extraClasses.includes('disabled')) {
  43. pushUnique(this.disabled, current);
  44. }
  45. }
  46. if (result.content) {
  47. replaceChildNodes(el, result.content);
  48. }
  49. }
  50. }
  51. }