@abbl/material-calendar
Version:
Calendar component build with React and Material-UI
103 lines • 4.2 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
import { eachDayOfInterval, endOfMonth, endOfWeek, startOfMonth, startOfWeek } from 'date-fns';
import EventStoragePresenceHelper, { DateRange } from './EventStoragePresenceHelper';
/**
* Class used to prefill days in instance of EventStorage interface
* with empty arrays.
*
* @remarks
* Take a note that preFillStorage method only works for date
* ranges that are defined in EventStoragePresenceHelper class.
*
* @internal
*/
var EventStorageFiller = /** @class */ (function () {
function EventStorageFiller() {
}
/**
* Fills closest date range with empty arrays.
*
* @param from
* @param till
* @param eventStorage instance of EventStorage interface.
*/
EventStorageFiller.preFillStorage = function (from, till, eventStorage) {
// Use PresenceHelper to determine DateRange that is the closest to the one provided by user.
var dateRange = EventStoragePresenceHelper.determineRange(from, till);
switch (dateRange) {
case DateRange.DAY:
return this.fillDay(from, eventStorage);
case DateRange.WEEK:
return this.fillWeek(from, eventStorage);
case DateRange.MONTH:
return this.fillMonth(from, eventStorage);
case DateRange.YEAR:
return this.fillYear(from, eventStorage);
default:
return eventStorage;
}
};
/**
* Fills a single day with empty array.
*
* @param dayDate day that will be filled with empty array.
* @param eventStorage instance of EventStorage interface.
*/
EventStorageFiller.fillDay = function (dayDate, eventStorage) {
var _a, _b;
var _c, _d, _e;
var eventStorageCopy = eventStorage;
var year = dayDate.getFullYear();
var month = dayDate.getMonth();
var day = dayDate.getDate();
if (((_d = (_c = eventStorageCopy[dayDate.getFullYear()]) === null || _c === void 0 ? void 0 : _c[dayDate.getMonth()]) === null || _d === void 0 ? void 0 : _d[dayDate.getDate()]) === undefined) {
eventStorageCopy[year] = __assign(__assign({}, eventStorageCopy[year]), (_a = {}, _a[month] = __assign(__assign({}, (_e = eventStorageCopy[year]) === null || _e === void 0 ? void 0 : _e[month]), (_b = {}, _b[day] = [], _b)), _a));
}
return eventStorageCopy;
};
/**
* Fills days in week with empty array.
*
* @param weekDay any day of target week.
* @param eventStorage instance of EventStorage interface.
*/
EventStorageFiller.fillWeek = function (weekDay, eventStorage) {
var weekDays = eachDayOfInterval({ start: startOfWeek(weekDay), end: endOfWeek(weekDay) });
for (var _i = 0, weekDays_1 = weekDays; _i < weekDays_1.length; _i++) {
var day = weekDays_1[_i];
this.fillDay(day, eventStorage);
}
return eventStorage;
};
/**
* Fills days in month with empty array.
*
* @param monthDay any day of target month
* @param eventStorage instance of EventStorage interface.
*/
EventStorageFiller.fillMonth = function (monthDay, eventStorage) {
var monthDays = eachDayOfInterval({ start: startOfMonth(monthDay), end: endOfMonth(monthDay) });
var storageCopy = eventStorage;
for (var _i = 0, monthDays_1 = monthDays; _i < monthDays_1.length; _i++) {
var day = monthDays_1[_i];
storageCopy = this.fillDay(day, storageCopy);
}
return storageCopy;
};
EventStorageFiller.fillYear = function (yearDay, eventStorage) {
throw new Error('Not implemented');
};
return EventStorageFiller;
}());
export default EventStorageFiller;
//# sourceMappingURL=EventStorageFiller.js.map