Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Ember from 'ember';
- const {
- Component,
- computed,
- getProperties,
- get,
- set,
- setProperties,
- isPresent,
- isBlank
- } = Ember;
- import calendar, {
- isDate,
- isSameDay,
- isSameWeek,
- isSameMonth,
- getDateISO,
- getNextMonth,
- getPreviousMonth,
- WEEK_DAYS,
- CALENDAR_MONTHS,
- CALENDAR_MONTHS_SHORT
- } from '../utils/date-picker';
- export default Component.extend({
- date: '',
- multiple: true,
- currentSelection: 'endDate',
- classNames: ['zero-component'],
- init() {
- this._super(...arguments);
- if (isBlank(get(this, 'startDate'))) {
- let currentDate = new Date();
- let startDateOfWeek = currentDate.getDate() - currentDate.getDay();
- let endDateOfWeek = startDateOfWeek + 6;
- set(this, 'startDate', new Date(`${currentDate.getFullYear()}-${currentDate.getMonth()+1}-${startDateOfWeek}`));
- set(this, 'endDate', new Date(`${currentDate.getFullYear()}-${currentDate.getMonth()+1}-${endDateOfWeek}`));
- }
- let dateProp = get(this, 'startDate');
- let isDateObject = isDate(dateProp);
- let date = isDateObject ? dateProp : new Date();
- console.log('here', date);
- this.setProperties({
- current: isDateObject ? dateProp : null,
- month: +date.getMonth() + 1,
- year: date.getFullYear()
- });
- },
- nextMonthName: computed('month', {
- get() {
- return CALENDAR_MONTHS_SHORT[get(this, 'month')]
- }
- }),
- currentMonthName: computed('month', {
- get() {
- // month starts from 1
- return CALENDAR_MONTHS_SHORT[get(this, 'month') - 1]
- }
- }),
- weekDays: computed({
- get() {
- return Object.keys(WEEK_DAYS).map(key => WEEK_DAYS[key]);
- }
- }),
- getCalendarDates: computed('month', 'year', 'startDate', 'endDate', {
- get() {
- const { current, month, year } = getProperties(this, 'current', 'month', 'year');
- const { startDate, endDate } = getProperties(this, 'startDate', 'endDate');
- const calendarMonth = month || +current.getMonth() + 1;
- const calendarYear = year || current.getFullYear();
- console.log('calendarMonth', calendarMonth);
- return calendar(calendarMonth, calendarYear, true).map(date => {
- const _date = new Date(date.join('-'));
- console.log('thanks', _date, _date.getMonth() + 1, calendarMonth)
- return {
- _date,
- date,
- isStartWeek: isSameWeek(_date, startDate),
- isEndWeek: isSameWeek(_date, endDate),
- isSameMonth: calendarMonth === _date.getMonth() + 1,
- isNextMonth: calendarMonth + 1 === _date.getMonth() + 1,
- isPreviousMonth: calendarMonth - 1 === _date.getMonth() + 1,
- isWithinRange: _date > startDate && _date < endDate,
- dateNumber: _date.getDate()
- };
- });
- }
- }),
- weeks: computed('getCalendarDates', 'startDate', 'endDate', {
- get() {
- let getCalendarDates = get(this, 'getCalendarDates');
- return [...new Array(6)].map((x, index) => {
- return getCalendarDates.slice(index * 7, 7 + index * 7);
- });
- }
- }),
- getCalendarDatesNextMonth: computed('month', 'year', 'startDate', 'endDate', {
- get() {
- const { current, month, year } = getProperties(this, 'current', 'month', 'year');
- const { startDate, endDate } = getProperties(this, 'startDate', 'endDate');
- let calendarMonth;
- let calendarYear;
- if (month === 11) {
- calendarMonth = 1;
- calendarYear = year + 1;
- } else {
- calendarMonth = month + 1;
- calendarYear = year;
- }
- return calendar(calendarMonth, calendarYear).map(date => {
- const _date = new Date(date.join('-'));
- return {
- _date,
- date,
- isStartWeek: isSameWeek(_date, startDate),
- isEndWeek: isSameWeek(_date, endDate),
- isSameMonth: calendarMonth === _date.getMonth() + 1,
- isNextMonth: calendarMonth + 1 === _date.getMonth() + 1,
- isPreviousMonth: calendarMonth - 1 === _date.getMonth() + 1,
- isWithinRange: _date > startDate && _date < endDate,
- dateNumber: _date.getDate()
- };
- });
- }
- }),
- nextWeeks: computed('getCalendarDates', 'startDate', 'endDate', {
- get() {
- let getCalendarDates = get(this, 'getCalendarDatesNextMonth');
- return [...new Array(6)].map((x, index) => {
- return getCalendarDates.slice(index * 7, 7 + index * 7);
- });
- }
- }),
- actions: {
- selectWeek(week) {
- let { startDate, endDate, multiple } = getProperties(this, 'startDate', 'endDate', 'multiple');
- if (multiple && isPresent(startDate) && isPresent(endDate)) {
- let selectedStartDate = week[0]._date;
- let selectedEndDate = week[6]._date;
- let currentSelection = get(this, 'currentSelection');
- if (currentSelection === 'startDate') {
- set(this, 'startDate', selectedStartDate);
- if (startDate.getFullYear() === selectedEndDate.getFullYear() && startDate.getMonth() === selectedEndDate.getMonth() && startDate.getDate() + 6 === selectedEndDate.getDate()) {
- set(this, 'endDate', selectedEndDate);
- } else if (endDate < selectedStartDate) {
- set(this, 'endDate', selectedEndDate);
- }
- set(this, 'currentSelection', 'endDate');
- } else {
- set(this, 'endDate', selectedEndDate);
- if (endDate.getFullYear() === selectedStartDate.getFullYear() && endDate.getMonth() === selectedStartDate.getMonth() && endDate.getDate() + 6 === selectedStartDate.getDate()) {
- set(this, 'startDate', selectedStartDate);
- set(this, 'currentSelection', 'endDate');
- } else if (startDate > selectedEndDate) {
- set(this, 'startDate', selectedStartDate);
- set(this, 'currentSelection', 'endDate');
- } else {
- set(this, 'currentSelection', 'startDate');
- }
- }
- } else {
- this.setProperties({
- startDate: week[0]._date,
- endDate: week[6]._date
- });
- }
- if (!multiple) {
- this.apply(getProperties(this, 'startDate', 'endDate'));
- this.close();
- }
- },
- previous() {
- let { month, year } = getProperties(this, 'month', 'year');
- if (month === 1) {
- setProperties(this, {
- year: --year,
- month: 12
- });
- } else {
- set(this, 'month', --month);
- }
- },
- next() {
- let { month, year } = getProperties(this, 'month', 'year');
- if (month === 12) {
- setProperties(this, {
- year: ++year,
- month: 1
- });
- } else {
- set(this, 'month', ++month);
- }
- },
- apply() {
- this.apply(getProperties(this, 'startDate', 'endDate'));
- },
- cancel() {
- this.close();
- }
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement