YearsView.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import {hasProperty, pushUnique, createTagRepeat} from '../../lib/utils.js';
  2. import {dateValue, startOfYearPeriod} from '../../lib/date.js';
  3. import {parseHTML} from '../../lib/dom.js';
  4. import View from './View.js';
  5. function toTitleCase(word) {
  6. return [...word].reduce((str, ch, ix) => str += ix ? ch : ch.toUpperCase(), '');
  7. }
  8. // Class representing the years and decades view elements
  9. export default class YearsView extends View {
  10. constructor(picker, config) {
  11. super(picker, config);
  12. }
  13. init(options, onConstruction = true) {
  14. if (onConstruction) {
  15. this.navStep = this.step * 10;
  16. this.beforeShowOption = `beforeShow${toTitleCase(this.cellClass)}`;
  17. this.grid = this.element;
  18. this.element.classList.add(this.name, 'datepicker-grid');
  19. this.grid.appendChild(parseHTML(createTagRepeat('span', 12)));
  20. }
  21. super.init(options);
  22. }
  23. setOptions(options) {
  24. if (hasProperty(options, 'minDate')) {
  25. if (options.minDate === undefined) {
  26. this.minYear = this.minDate = undefined;
  27. } else {
  28. this.minYear = startOfYearPeriod(options.minDate, this.step);
  29. this.minDate = dateValue(this.minYear, 0, 1);
  30. }
  31. }
  32. if (hasProperty(options, 'maxDate')) {
  33. if (options.maxDate === undefined) {
  34. this.maxYear = this.maxDate = undefined;
  35. } else {
  36. this.maxYear = startOfYearPeriod(options.maxDate, this.step);
  37. this.maxDate = dateValue(this.maxYear, 11, 31);
  38. }
  39. }
  40. if (options[this.beforeShowOption] !== undefined) {
  41. const beforeShow = options[this.beforeShowOption];
  42. this.beforeShow = typeof beforeShow === 'function' ? beforeShow : undefined;
  43. }
  44. }
  45. // Update view's settings to reflect the viewDate set on the picker
  46. updateFocus() {
  47. const viewDate = new Date(this.picker.viewDate);
  48. const first = startOfYearPeriod(viewDate, this.navStep);
  49. const last = first + 9 * this.step;
  50. this.first = first;
  51. this.last = last;
  52. this.start = first - this.step;
  53. this.focused = startOfYearPeriod(viewDate, this.step);
  54. }
  55. // Update view's settings to reflect the selected dates
  56. updateSelection() {
  57. const {dates, rangepicker} = this.picker.datepicker;
  58. this.selected = dates.reduce((years, timeValue) => {
  59. return pushUnique(years, startOfYearPeriod(timeValue, this.step));
  60. }, []);
  61. if (rangepicker && rangepicker.dates) {
  62. this.range = rangepicker.dates.map(timeValue => {
  63. if (timeValue !== undefined) {
  64. return startOfYearPeriod(timeValue, this.step);
  65. }
  66. });
  67. }
  68. }
  69. // Update the entire view UI
  70. render() {
  71. // refresh disabled years on every render in order to clear the ones added
  72. // by beforeShow hook at previous render
  73. this.disabled = [];
  74. this.picker.setViewSwitchLabel(`${this.first}-${this.last}`);
  75. this.picker.setPrevBtnDisabled(this.first <= this.minYear);
  76. this.picker.setNextBtnDisabled(this.last >= this.maxYear);
  77. Array.from(this.grid.children).forEach((el, index) => {
  78. const classList = el.classList;
  79. const current = this.start + (index * this.step);
  80. const date = dateValue(current, 0, 1);
  81. el.className = `datepicker-cell ${this.cellClass}`;
  82. if (this.isMinView) {
  83. el.dataset.date = date;
  84. }
  85. el.textContent = el.dataset.year = current;
  86. if (index === 0) {
  87. classList.add('prev');
  88. } else if (index === 11) {
  89. classList.add('next');
  90. }
  91. if (current < this.minYear || current > this.maxYear) {
  92. classList.add('disabled');
  93. }
  94. if (this.range) {
  95. const [rangeStart, rangeEnd] = this.range;
  96. if (current > rangeStart && current < rangeEnd) {
  97. classList.add('range');
  98. }
  99. if (current === rangeStart) {
  100. classList.add('range-start');
  101. }
  102. if (current === rangeEnd) {
  103. classList.add('range-end');
  104. }
  105. }
  106. if (this.selected.includes(current)) {
  107. classList.add('selected');
  108. }
  109. if (current === this.focused) {
  110. classList.add('focused');
  111. }
  112. if (this.beforeShow) {
  113. this.performBeforeHook(el, current, date);
  114. }
  115. });
  116. }
  117. // Update the view UI by applying the changes of selected and focused items
  118. refresh() {
  119. const [rangeStart, rangeEnd] = this.range || [];
  120. this.grid
  121. .querySelectorAll('.range, .range-start, .range-end, .selected, .focused')
  122. .forEach((el) => {
  123. el.classList.remove('range', 'range-start', 'range-end', 'selected', 'focused');
  124. });
  125. Array.from(this.grid.children).forEach((el) => {
  126. const current = Number(el.textContent);
  127. const classList = el.classList;
  128. if (current > rangeStart && current < rangeEnd) {
  129. classList.add('range');
  130. }
  131. if (current === rangeStart) {
  132. classList.add('range-start');
  133. }
  134. if (current === rangeEnd) {
  135. classList.add('range-end');
  136. }
  137. if (this.selected.includes(current)) {
  138. classList.add('selected');
  139. }
  140. if (current === this.focused) {
  141. classList.add('focused');
  142. }
  143. });
  144. }
  145. // Update the view UI by applying the change of focused item
  146. refreshFocus() {
  147. const index = Math.round((this.focused - this.start) / this.step);
  148. this.grid.querySelectorAll('.focused').forEach((el) => {
  149. el.classList.remove('focused');
  150. });
  151. this.grid.children[index].classList.add('focused');
  152. }
  153. }