wix-style-react
Version:
wix-style-react
71 lines (59 loc) • 2.6 kB
JavaScript
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
import addMonths from 'date-fns/add_months';
import startOfMonth from 'date-fns/start_of_month';
import endOfMonth from 'date-fns/end_of_month';
import endOfDay from 'date-fns/end_of_day';
import isWithinRange from 'date-fns/is_within_range';
import isAfter from 'date-fns/is_after';
import isBefore from 'date-fns/is_before';
import differenceInCalendarDays from 'date-fns/difference_in_calendar_days';
/**
* Date utils for a multi-month calendar view.
*/
export var CalendarView = function () {
/**
* @param {*} firstMonthDate a date that represents the first displayed month in the calendar view. (Does not have to be the 1st of that month)
* @param {*} numOfMonths number of months that are displayed in the view
*/
function CalendarView(firstMonthDate, numOfMonths) {
_classCallCheck(this, CalendarView);
this.startDate = startOfMonth(firstMonthDate);
this.endDate = endOfDay(endOfMonth(addMonths(this.startDate, numOfMonths - 1)));
}
/**
* Is the given date contained in the calendar view
*
* @param {*} date
* @returns
* @memberof CalendarView
*/
_createClass(CalendarView, [{
key: 'isContained',
value: function isContained(date) {
return isWithinRange(date, this.startDate, this.endDate);
}
}, {
key: 'isRangeFits',
value: function isRangeFits(from, to) {
return differenceInCalendarDays(to, from) <= differenceInCalendarDays(this.endDate, this.startDate);
}
/**
* Is the given date after the view's end date
*/
}, {
key: 'isAfterView',
value: function isAfterView(date) {
return isAfter(date, this.endDate);
}
/**
* Is the given date before the view's start date
*/
}, {
key: 'isBeforeView',
value: function isBeforeView(date) {
return isBefore(date, this.startDate);
}
}]);
return CalendarView;
}();