Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.75 KB | None | 0 0
  1. import Ember from 'ember';
  2.  
  3. const {
  4. Component,
  5. computed,
  6. getProperties,
  7. get,
  8. set,
  9. setProperties,
  10. isPresent,
  11. isBlank
  12. } = Ember;
  13.  
  14. import calendar, {
  15. isDate,
  16. isSameDay,
  17. isSameWeek,
  18. isSameMonth,
  19. getDateISO,
  20. getNextMonth,
  21. getPreviousMonth,
  22. WEEK_DAYS,
  23. CALENDAR_MONTHS,
  24. CALENDAR_MONTHS_SHORT
  25. } from '../utils/date-picker';
  26.  
  27. export default Component.extend({
  28.  
  29. date: '',
  30.  
  31. multiple: true,
  32.  
  33. currentSelection: 'endDate',
  34.  
  35. classNames: ['zero-component'],
  36.  
  37. init() {
  38. this._super(...arguments);
  39.  
  40. if (isBlank(get(this, 'startDate'))) {
  41. let currentDate = new Date();
  42. let startDateOfWeek = currentDate.getDate() - currentDate.getDay();
  43. let endDateOfWeek = startDateOfWeek + 6;
  44. set(this, 'startDate', new Date(`${currentDate.getFullYear()}-${currentDate.getMonth()+1}-${startDateOfWeek}`));
  45. set(this, 'endDate', new Date(`${currentDate.getFullYear()}-${currentDate.getMonth()+1}-${endDateOfWeek}`));
  46. }
  47.  
  48. let dateProp = get(this, 'startDate');
  49.  
  50. let isDateObject = isDate(dateProp);
  51.  
  52. let date = isDateObject ? dateProp : new Date();
  53.  
  54. console.log('here', date);
  55.  
  56. this.setProperties({
  57. current: isDateObject ? dateProp : null,
  58. month: +date.getMonth() + 1,
  59. year: date.getFullYear()
  60. });
  61. },
  62.  
  63. nextMonthName: computed('month', {
  64. get() {
  65. return CALENDAR_MONTHS_SHORT[get(this, 'month')]
  66. }
  67. }),
  68.  
  69. currentMonthName: computed('month', {
  70. get() {
  71. // month starts from 1
  72. return CALENDAR_MONTHS_SHORT[get(this, 'month') - 1]
  73. }
  74. }),
  75.  
  76. weekDays: computed({
  77. get() {
  78. return Object.keys(WEEK_DAYS).map(key => WEEK_DAYS[key]);
  79. }
  80. }),
  81.  
  82. getCalendarDates: computed('month', 'year', 'startDate', 'endDate', {
  83. get() {
  84. const { current, month, year } = getProperties(this, 'current', 'month', 'year');
  85. const { startDate, endDate } = getProperties(this, 'startDate', 'endDate');
  86.  
  87. const calendarMonth = month || +current.getMonth() + 1;
  88. const calendarYear = year || current.getFullYear();
  89.  
  90. console.log('calendarMonth', calendarMonth);
  91.  
  92. return calendar(calendarMonth, calendarYear, true).map(date => {
  93. const _date = new Date(date.join('-'));
  94. console.log('thanks', _date, _date.getMonth() + 1, calendarMonth)
  95. return {
  96. _date,
  97. date,
  98. isStartWeek: isSameWeek(_date, startDate),
  99. isEndWeek: isSameWeek(_date, endDate),
  100. isSameMonth: calendarMonth === _date.getMonth() + 1,
  101. isNextMonth: calendarMonth + 1 === _date.getMonth() + 1,
  102. isPreviousMonth: calendarMonth - 1 === _date.getMonth() + 1,
  103. isWithinRange: _date > startDate && _date < endDate,
  104. dateNumber: _date.getDate()
  105. };
  106. });
  107. }
  108. }),
  109.  
  110. weeks: computed('getCalendarDates', 'startDate', 'endDate', {
  111. get() {
  112. let getCalendarDates = get(this, 'getCalendarDates');
  113.  
  114. return [...new Array(6)].map((x, index) => {
  115. return getCalendarDates.slice(index * 7, 7 + index * 7);
  116. });
  117. }
  118. }),
  119.  
  120. getCalendarDatesNextMonth: computed('month', 'year', 'startDate', 'endDate', {
  121. get() {
  122. const { current, month, year } = getProperties(this, 'current', 'month', 'year');
  123. const { startDate, endDate } = getProperties(this, 'startDate', 'endDate');
  124.  
  125. let calendarMonth;
  126. let calendarYear;
  127.  
  128. if (month === 11) {
  129. calendarMonth = 1;
  130. calendarYear = year + 1;
  131. } else {
  132. calendarMonth = month + 1;
  133. calendarYear = year;
  134. }
  135.  
  136. return calendar(calendarMonth, calendarYear).map(date => {
  137. const _date = new Date(date.join('-'));
  138. return {
  139. _date,
  140. date,
  141. isStartWeek: isSameWeek(_date, startDate),
  142. isEndWeek: isSameWeek(_date, endDate),
  143. isSameMonth: calendarMonth === _date.getMonth() + 1,
  144. isNextMonth: calendarMonth + 1 === _date.getMonth() + 1,
  145. isPreviousMonth: calendarMonth - 1 === _date.getMonth() + 1,
  146. isWithinRange: _date > startDate && _date < endDate,
  147. dateNumber: _date.getDate()
  148. };
  149. });
  150. }
  151. }),
  152.  
  153. nextWeeks: computed('getCalendarDates', 'startDate', 'endDate', {
  154. get() {
  155. let getCalendarDates = get(this, 'getCalendarDatesNextMonth');
  156.  
  157. return [...new Array(6)].map((x, index) => {
  158. return getCalendarDates.slice(index * 7, 7 + index * 7);
  159. });
  160. }
  161. }),
  162.  
  163. actions: {
  164. selectWeek(week) {
  165. let { startDate, endDate, multiple } = getProperties(this, 'startDate', 'endDate', 'multiple');
  166. if (multiple && isPresent(startDate) && isPresent(endDate)) {
  167. let selectedStartDate = week[0]._date;
  168. let selectedEndDate = week[6]._date;
  169. let currentSelection = get(this, 'currentSelection');
  170.  
  171. if (currentSelection === 'startDate') {
  172. set(this, 'startDate', selectedStartDate);
  173. if (startDate.getFullYear() === selectedEndDate.getFullYear() && startDate.getMonth() === selectedEndDate.getMonth() && startDate.getDate() + 6 === selectedEndDate.getDate()) {
  174. set(this, 'endDate', selectedEndDate);
  175. } else if (endDate < selectedStartDate) {
  176. set(this, 'endDate', selectedEndDate);
  177. }
  178. set(this, 'currentSelection', 'endDate');
  179. } else {
  180. set(this, 'endDate', selectedEndDate);
  181. if (endDate.getFullYear() === selectedStartDate.getFullYear() && endDate.getMonth() === selectedStartDate.getMonth() && endDate.getDate() + 6 === selectedStartDate.getDate()) {
  182. set(this, 'startDate', selectedStartDate);
  183. set(this, 'currentSelection', 'endDate');
  184. } else if (startDate > selectedEndDate) {
  185. set(this, 'startDate', selectedStartDate);
  186. set(this, 'currentSelection', 'endDate');
  187. } else {
  188. set(this, 'currentSelection', 'startDate');
  189. }
  190. }
  191. } else {
  192. this.setProperties({
  193. startDate: week[0]._date,
  194. endDate: week[6]._date
  195. });
  196. }
  197. if (!multiple) {
  198. this.apply(getProperties(this, 'startDate', 'endDate'));
  199. this.close();
  200. }
  201. },
  202.  
  203. previous() {
  204. let { month, year } = getProperties(this, 'month', 'year');
  205. if (month === 1) {
  206. setProperties(this, {
  207. year: --year,
  208. month: 12
  209. });
  210. } else {
  211. set(this, 'month', --month);
  212. }
  213. },
  214.  
  215. next() {
  216. let { month, year } = getProperties(this, 'month', 'year');
  217. if (month === 12) {
  218. setProperties(this, {
  219. year: ++year,
  220. month: 1
  221. });
  222. } else {
  223. set(this, 'month', ++month);
  224. }
  225. },
  226.  
  227. apply() {
  228. this.apply(getProperties(this, 'startDate', 'endDate'));
  229. },
  230.  
  231. cancel() {
  232. this.close();
  233. }
  234. }
  235. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement