2
0

datepicker.js 4.3 KB

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