datepicker.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. import { TAPi18n } from '/imports/i18n';
  3. import moment from 'moment/min/moment-with-locales';
  4. import { Utils } from './utils';
  5. // Helper function to replace HH with H for 24 hours format
  6. function adjustedTimeFormat() {
  7. return moment.localeData().longDateFormat('LT');
  8. }
  9. export class DatePicker extends BlazeComponent {
  10. template() {
  11. return 'datepicker';
  12. }
  13. onCreated(defaultTime = '1970-01-01 08:00:00') {
  14. this.error = new ReactiveVar('');
  15. this.card = this.data();
  16. this.date = new ReactiveVar(moment.invalid());
  17. this.defaultTime = defaultTime;
  18. }
  19. startDayOfWeek() {
  20. const currentUser = ReactiveCache.getCurrentUser();
  21. if (currentUser) {
  22. return currentUser.getStartDayOfWeek();
  23. } else {
  24. return 1;
  25. }
  26. }
  27. onRendered() {
  28. const $picker = this.$('.js-datepicker')
  29. .datepicker({
  30. todayHighlight: true,
  31. todayBtn: 'linked',
  32. language: TAPi18n.getLanguage(),
  33. weekStart: this.startDayOfWeek(),
  34. calendarWeeks: true,
  35. beforeParse: (value) => Utils.normalizeDigits(value),
  36. })
  37. .on(
  38. 'changeDate',
  39. function(evt) {
  40. const normalizedDate = moment(evt.date).format('L');
  41. this.find('#date').value = normalizedDate;
  42. this.error.set('');
  43. this._handleTimeInput(evt);
  44. }.bind(this),
  45. );
  46. if (this.date.get().isValid()) {
  47. $picker.datepicker('update', this.date.get().toDate());
  48. }
  49. }
  50. _handleTimeInput(evt) {
  51. const timeInput = this.find('#time');
  52. timeInput.focus();
  53. if (!timeInput.value && this.defaultTime) {
  54. const currentHour = evt.date.getHours();
  55. const defaultMoment = moment(
  56. currentHour > 0 ? evt.date : this.defaultTime,
  57. );
  58. timeInput.value = defaultMoment.format('LT');
  59. }
  60. }
  61. showDate() {
  62. if (this.date.get().isValid()) return this.date.get().format('L');
  63. return '';
  64. }
  65. showTime() {
  66. if (this.date.get().isValid()) return this.date.get().format('LT');
  67. return '';
  68. }
  69. dateFormat() {
  70. return moment.localeData().longDateFormat('L');
  71. }
  72. timeFormat() {
  73. return moment.localeData().longDateFormat('LT');
  74. }
  75. events() {
  76. return [{
  77. 'keyup .js-date-field'() {
  78. const rawValue = this.find('#date').value;
  79. const normalizedValue = Utils.normalizeDigits(rawValue);
  80. const dateMoment = moment(normalizedValue, 'L', true);
  81. if (dateMoment.isValid()) {
  82. this.error.set('');
  83. this.$('.js-datepicker').datepicker('update', dateMoment.toDate());
  84. }
  85. },
  86. 'keyup .js-time-field'() {
  87. const rawValue = this.find('#time').value;
  88. const normalizedValue = Utils.normalizeDigits(rawValue);
  89. const timeMoment = moment(normalizedValue, adjustedTimeFormat(), true);
  90. if (timeMoment.isValid()) {
  91. this.error.set('');
  92. }
  93. },
  94. 'submit .edit-date'(evt) {
  95. evt.preventDefault();
  96. const dateValue = Utils.normalizeDigits(evt.target.date.value);
  97. const timeValue = Utils.normalizeDigits(evt.target.time.value) ||
  98. moment(new Date().setHours(12, 0, 0)).format('LT');
  99. const dateString = `${dateValue} ${timeValue}`;
  100. const format = `L ${adjustedTimeFormat()}`;
  101. const newDate = moment(dateString, format, true);
  102. if (!newDate.isValid()) {
  103. this._handleDateTimeError(evt, dateValue, timeValue);
  104. return;
  105. }
  106. this._storeDate(newDate.toDate());
  107. Popup.back();
  108. },
  109. 'click .js-delete-date'(evt) {
  110. evt.preventDefault();
  111. this._deleteDate();
  112. Popup.back();
  113. }
  114. }];
  115. }
  116. _handleDateTimeError(evt, dateValue, timeValue) {
  117. const dateMoment = moment(dateValue, 'L', true);
  118. const timeMoment = moment(timeValue, adjustedTimeFormat(), true);
  119. if (!timeMoment.isValid()) {
  120. this.error.set('invalid-time');
  121. evt.target.time.focus();
  122. } else if (!dateMoment.isValid()) {
  123. this.error.set('invalid-date');
  124. evt.target.date.focus();
  125. } else {
  126. this.error.set('invalid');
  127. }
  128. }
  129. }