cardDate.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // Edit start & due dates
  2. const EditCardDate = BlazeComponent.extendComponent({
  3. template() {
  4. return 'editCardDate';
  5. },
  6. onCreated() {
  7. this.error = new ReactiveVar('');
  8. this.card = this.data();
  9. this.date = new ReactiveVar(moment.invalid());
  10. },
  11. onRendered() {
  12. const $picker = this.$('.js-datepicker').datepicker({
  13. todayHighlight: true,
  14. todayBtn: 'linked',
  15. language: TAPi18n.getLanguage(),
  16. }).on('changeDate', function(evt) {
  17. this.find('#date').value = moment(evt.date).format('L');
  18. this.error.set('');
  19. this.find('#time').focus();
  20. }.bind(this));
  21. if (this.date.get().isValid()) {
  22. $picker.datepicker('update', this.date.get().toDate());
  23. }
  24. },
  25. showDate() {
  26. if (this.date.get().isValid())
  27. return this.date.get().format('L');
  28. return '';
  29. },
  30. showTime() {
  31. if (this.date.get().isValid())
  32. return this.date.get().format('LT');
  33. return '';
  34. },
  35. dateFormat() {
  36. return moment.localeData().longDateFormat('L');
  37. },
  38. timeFormat() {
  39. return moment.localeData().longDateFormat('LT');
  40. },
  41. events() {
  42. return [{
  43. 'keyup .js-date-field'() {
  44. // parse for localized date format in strict mode
  45. const dateMoment = moment(this.find('#date').value, 'L', true);
  46. if (dateMoment.isValid()) {
  47. this.error.set('');
  48. this.$('.js-datepicker').datepicker('update', dateMoment.toDate());
  49. }
  50. },
  51. 'keyup .js-time-field'() {
  52. // parse for localized time format in strict mode
  53. const dateMoment = moment(this.find('#time').value, 'LT', true);
  54. if (dateMoment.isValid()) {
  55. this.error.set('');
  56. }
  57. },
  58. 'submit .edit-date'(evt) {
  59. evt.preventDefault();
  60. // if no time was given, init with 12:00
  61. const time = evt.target.time.value || moment(new Date().setHours(12, 0, 0)).format('LT');
  62. const dateString = `${evt.target.date.value} ${time}`;
  63. const newDate = moment(dateString, 'L LT', true);
  64. if (newDate.isValid()) {
  65. this._storeDate(newDate.toDate());
  66. Popup.close();
  67. }
  68. else {
  69. this.error.set('invalid-date');
  70. evt.target.date.focus();
  71. }
  72. },
  73. 'click .js-delete-date'(evt) {
  74. evt.preventDefault();
  75. this._deleteDate();
  76. Popup.close();
  77. },
  78. }];
  79. },
  80. });
  81. // editCardStartDatePopup
  82. (class extends EditCardDate {
  83. onCreated() {
  84. super.onCreated();
  85. this.data().startAt && this.date.set(moment(this.data().startAt));
  86. }
  87. _storeDate(date) {
  88. this.card.setStart(date);
  89. }
  90. _deleteDate() {
  91. this.card.unsetStart();
  92. }
  93. }).register('editCardStartDatePopup');
  94. // editCardDueDatePopup
  95. (class extends EditCardDate {
  96. onCreated() {
  97. super.onCreated();
  98. this.data().dueAt && this.date.set(moment(this.data().dueAt));
  99. }
  100. onRendered() {
  101. super.onRendered();
  102. if (moment.isDate(this.card.startAt)) {
  103. this.$('.js-datepicker').datepicker('setStartDate', this.card.startAt);
  104. }
  105. }
  106. _storeDate(date) {
  107. this.card.setDue(date);
  108. }
  109. _deleteDate() {
  110. this.card.unsetDue();
  111. }
  112. }).register('editCardDueDatePopup');
  113. // Display start & due dates
  114. const CardDate = BlazeComponent.extendComponent({
  115. template() {
  116. return 'dateBadge';
  117. },
  118. onCreated() {
  119. const self = this;
  120. self.date = ReactiveVar();
  121. self.now = ReactiveVar(moment());
  122. window.setInterval(() => {
  123. self.now.set(moment());
  124. }, 60000);
  125. },
  126. showDate() {
  127. // this will start working once mquandalle:moment
  128. // is updated to at least moment.js 2.10.5
  129. // until then, the date is displayed in the "L" format
  130. return this.date.get().calendar(null, {
  131. sameElse: 'llll',
  132. });
  133. },
  134. showISODate() {
  135. return this.date.get().toISOString();
  136. },
  137. });
  138. class CardStartDate extends CardDate {
  139. onCreated() {
  140. super.onCreated();
  141. const self = this;
  142. self.autorun(() => {
  143. self.date.set(moment(self.data().startAt));
  144. });
  145. }
  146. classes() {
  147. if (this.date.get().isBefore(this.now.get(), 'minute') &&
  148. this.now.get().isBefore(this.data().dueAt)) {
  149. return 'current';
  150. }
  151. return '';
  152. }
  153. showTitle() {
  154. return `${TAPi18n.__('card-start-on')} ${this.date.get().format('LLLL')}`;
  155. }
  156. events() {
  157. return super.events().concat({
  158. 'click .js-edit-date': Popup.open('editCardStartDate'),
  159. });
  160. }
  161. }
  162. CardStartDate.register('cardStartDate');
  163. class CardDueDate extends CardDate {
  164. onCreated() {
  165. super.onCreated();
  166. const self = this;
  167. self.autorun(() => {
  168. self.date.set(moment(self.data().dueAt));
  169. });
  170. }
  171. classes() {
  172. if (this.now.get().diff(this.date.get(), 'days') >= 2)
  173. return 'long-overdue';
  174. else if (this.now.get().diff(this.date.get(), 'minute') >= 0)
  175. return 'due';
  176. else if (this.now.get().diff(this.date.get(), 'days') >= -1)
  177. return 'almost-due';
  178. return '';
  179. }
  180. showTitle() {
  181. return `${TAPi18n.__('card-due-on')} ${this.date.get().format('LLLL')}`;
  182. }
  183. events() {
  184. return super.events().concat({
  185. 'click .js-edit-date': Popup.open('editCardDueDate'),
  186. });
  187. }
  188. }
  189. CardDueDate.register('cardDueDate');
  190. (class extends CardStartDate {
  191. showDate() {
  192. return this.date.get().format('l');
  193. }
  194. }).register('minicardStartDate');
  195. (class extends CardDueDate {
  196. showDate() {
  197. return this.date.get().format('l');
  198. }
  199. }).register('minicardDueDate');