react-big-scheduler-stch
Version:
A scheduler and resource planning component built for React and made for modern browsers (A react-big-scheduler fork with additional features and fixes).
1,085 lines (995 loc) • 53.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = undefined;
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
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; }; }();
var _dayjs = require("dayjs");
var _dayjs2 = _interopRequireDefault(_dayjs);
var _quarterOfYear = require("dayjs/plugin/quarterOfYear");
var _quarterOfYear2 = _interopRequireDefault(_quarterOfYear);
var _utc = require("dayjs/plugin/utc");
var _utc2 = _interopRequireDefault(_utc);
var _weekday = require("dayjs/plugin/weekday");
var _weekday2 = _interopRequireDefault(_weekday);
var _rrule = require("rrule");
var _config = require("./config");
var _config2 = _interopRequireDefault(_config);
var _behaviors = require("./behaviors");
var _behaviors2 = _interopRequireDefault(_behaviors);
var _index = require("./index");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var SchedulerData = function () {
function SchedulerData() {
var date = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : (0, _dayjs2.default)();
var viewType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _index.ViewType.Week;
var showAgenda = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
var isEventPerspective = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
var newConfig = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : undefined;
var newBehaviors = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : undefined;
_classCallCheck(this, SchedulerData);
this.resources = [];
this.events = [];
this.eventGroups = [];
this.eventGroupsAutoGenerated = true;
this.viewType = viewType;
this.cellUnit = viewType === _index.ViewType.Day ? _index.CellUnit.Hour : _index.CellUnit.Day;
this.showAgenda = showAgenda;
this.isEventPerspective = isEventPerspective;
this.resizing = false;
this.scrollToSpecialDayjs = false;
this.documentWidth = 0;
this._shouldReloadViewType = false;
this.calendarPopoverLocale = undefined;
_dayjs2.default.extend(_quarterOfYear2.default);
_dayjs2.default.extend(_weekday2.default);
_dayjs2.default.extend(_utc2.default);
this.localeDayjs = _dayjs2.default;
this.config = newConfig == undefined ? _config2.default : _extends({}, _config2.default, newConfig);
this._validateMinuteStep(this.config.minuteStep);
this.behaviors = newBehaviors == undefined ? _behaviors2.default : _extends({}, _behaviors2.default, newBehaviors);
this._resolveDate(0, date);
this._createHeaders();
this._createRenderData();
}
_createClass(SchedulerData, [{
key: "setSchedulerLocale",
value: function setSchedulerLocale(preset, object) {
if (!preset) return;
var l = preset;
if (typeof preset === 'string') {
l = require("dayjs/locale/" + preset + ".js");
if (!!object) {
Object.entries(object).forEach(function (_ref) {
var _ref2 = _slicedToArray(_ref, 2),
key = _ref2[0],
value = _ref2[1];
return l[key] = value;
});
}
}
this.localeDayjs.locale(l);
this._shouldReloadViewType = true;
this.setViewType(this.viewType, this.showAgenda, this.isEventPerspective);
}
}, {
key: "setCalendarPopoverLocale",
value: function setCalendarPopoverLocale(lang) {
if (!!lang && typeof lang === 'string') {
this.calendarPopoverLocale = require("antd/locale/" + lang + ".js");
}
}
}, {
key: "setResources",
value: function setResources(resources) {
this._validateResource(resources);
this.resources = Array.from(new Set(resources));
this._createRenderData();
this.setScrollToSpecialDayjs(true);
}
}, {
key: "setEventGroupsAutoGenerated",
value: function setEventGroupsAutoGenerated(autoGenerated) {
this.eventGroupsAutoGenerated = autoGenerated;
}
//optional
}, {
key: "setEventGroups",
value: function setEventGroups(eventGroups) {
this._validateEventGroups(eventGroups);
this.eventGroups = Array.from(new Set(eventGroups));
this.eventGroupsAutoGenerated = false;
this._createRenderData();
this.setScrollToSpecialDayjs(true);
}
}, {
key: "setMinuteStep",
value: function setMinuteStep(minuteStep) {
if (this.config.minuteStep !== minuteStep) {
this._validateMinuteStep(minuteStep);
this.config.minuteStep = minuteStep;
this._createHeaders();
this._createRenderData();
}
}
}, {
key: "setBesidesWidth",
value: function setBesidesWidth(besidesWidth) {
if (besidesWidth >= 0) {
this.config.besidesWidth = besidesWidth;
}
}
}, {
key: "getMinuteStepsInHour",
value: function getMinuteStepsInHour() {
return 60 / this.config.minuteStep;
}
}, {
key: "addResource",
value: function addResource(resource) {
var existedResources = this.resources.filter(function (x) {
return x.id === resource.id;
});
if (existedResources.length === 0) {
this.resources.push(resource);
this._createRenderData();
}
}
}, {
key: "addEventGroup",
value: function addEventGroup(eventGroup) {
var existedEventGroups = this.eventGroups.filter(function (x) {
return x.id === eventGroup.id;
});
if (existedEventGroups.length === 0) {
this.eventGroups.push(eventGroup);
this._createRenderData();
}
}
}, {
key: "removeEventGroupById",
value: function removeEventGroupById(eventGroupId) {
var index = -1;
this.eventGroups.forEach(function (item, idx) {
if (item.id === eventGroupId) index = idx;
});
if (index !== -1) this.eventGroups.splice(index, 1);
}
}, {
key: "containsEventGroupId",
value: function containsEventGroupId(eventGroupId) {
var index = -1;
this.eventGroups.forEach(function (item, idx) {
if (item.id === eventGroupId) index = idx;
});
return index !== -1;
}
}, {
key: "setEvents",
value: function setEvents(events) {
this._validateEvents(events);
this.events = Array.from(events);
if (this.eventGroupsAutoGenerated) this._generateEventGroups();
if (this.config.recurringEventsEnabled) this._handleRecurringEvents();
this._createRenderData();
}
}, {
key: "setScrollToSpecialDayjs",
value: function setScrollToSpecialDayjs(scrollToSpecialDayjs) {
if (this.config.scrollToSpecialDayjsEnabled) this.scrollToSpecialDayjs = scrollToSpecialDayjs;
}
}, {
key: "prev",
value: function prev() {
this._resolveDate(-1);
this.events = [];
this._createHeaders();
this._createRenderData();
}
}, {
key: "next",
value: function next() {
this._resolveDate(1);
this.events = [];
this._createHeaders();
this._createRenderData();
}
}, {
key: "setDate",
value: function setDate() {
var date = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : (0, _dayjs2.default)(new Date());
this._resolveDate(0, date);
this.events = [];
this._createHeaders();
this._createRenderData();
}
}, {
key: "setViewType",
value: function setViewType() {
var viewType = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _index.ViewType.Week;
var showAgenda = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var isEventPerspective = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
this.showAgenda = showAgenda;
this.isEventPerspective = isEventPerspective;
this.cellUnit = _index.CellUnit.Day;
if (this.viewType !== viewType || this._shouldReloadViewType) {
var date = this.startDate;
if (viewType === _index.ViewType.Custom || viewType === _index.ViewType.Custom1 || viewType === _index.ViewType.Custom2) {
this.viewType = viewType;
this._resolveDate(0, date);
} else {
if (this.viewType < viewType) {
if (viewType === _index.ViewType.Week) {
this.startDate = this.localeDayjs(new Date(date)).startOf('week');
this.endDate = this.localeDayjs(new Date(this.startDate)).endOf('week');
} else if (viewType === _index.ViewType.Month) {
this.startDate = this.localeDayjs(new Date(date)).startOf('month');
this.endDate = this.localeDayjs(new Date(this.startDate)).endOf('month');
} else if (viewType === _index.ViewType.Quarter) {
this.startDate = this.localeDayjs(new Date(date)).startOf('quarter');
this.endDate = this.localeDayjs(new Date(this.startDate)).endOf('quarter');
} else if (viewType === _index.ViewType.Year) {
this.startDate = this.localeDayjs(new Date(date)).startOf('year');
this.endDate = this.localeDayjs(new Date(this.startDate)).endOf('year');
}
} else {
var start = this.localeDayjs(new Date(this.startDate));
var end = this.localeDayjs(new Date(this.endDate)).add(1, 'days');
if (this.selectDate !== undefined) {
var selectDate = this.localeDayjs(new Date(this.selectDate));
if (selectDate >= start && selectDate < end) {
date = this.selectDate;
}
}
var now = this.localeDayjs();
if (now >= start && now < end) {
date = now.startOf('day');
}
if (viewType === _index.ViewType.Day) {
this.startDate = date;
this.endDate = this.startDate;
this.cellUnit = _index.CellUnit.Hour;
} else if (viewType === _index.ViewType.Week) {
this.startDate = this.localeDayjs(new Date(date)).startOf('week');
this.endDate = this.localeDayjs(new Date(this.startDate)).endOf('week');
} else if (viewType === _index.ViewType.Month) {
this.startDate = this.localeDayjs(new Date(date)).startOf('month');
this.endDate = this.localeDayjs(new Date(this.startDate)).endOf('month');
} else if (viewType === _index.ViewType.Quarter) {
this.startDate = this.localeDayjs(new Date(date)).startOf('quarter');
this.endDate = this.localeDayjs(new Date(this.startDate)).endOf('quarter');
}
}
this.viewType = viewType;
}
this._shouldReloadViewType = false;
this.events = [];
this._createHeaders();
this._createRenderData();
this.setScrollToSpecialDayjs(true);
}
}
}, {
key: "setSchedulerMaxHeight",
value: function setSchedulerMaxHeight(newSchedulerMaxHeight) {
this.config.schedulerMaxHeight = newSchedulerMaxHeight;
}
}, {
key: "isSchedulerResponsive",
value: function isSchedulerResponsive() {
return !!this.config.schedulerWidth.endsWith && this.config.schedulerWidth.endsWith("%");
}
}, {
key: "toggleExpandStatus",
value: function toggleExpandStatus(slotId) {
var slotEntered = false;
var slotIndent = -1;
var isExpanded = false;
var expandedMap = new Map();
this.renderData.forEach(function (item) {
if (slotEntered === false) {
if (item.slotId === slotId && item.hasChildren) {
slotEntered = true;
isExpanded = !item.expanded;
item.expanded = isExpanded;
slotIndent = item.indent;
expandedMap.set(item.indent, {
expanded: item.expanded,
render: item.render
});
}
} else {
if (item.indent > slotIndent) {
var expandStatus = expandedMap.get(item.indent - 1);
item.render = expandStatus.expanded && expandStatus.render;
if (item.hasChildren) {
expandedMap.set(item.indent, {
expanded: item.expanded,
render: item.render
});
}
} else {
slotEntered = false;
}
}
});
}
}, {
key: "isResourceViewResponsive",
value: function isResourceViewResponsive() {
var resourceTableWidth = this.getResourceTableConfigWidth();
return !!resourceTableWidth.endsWith && resourceTableWidth.endsWith("%");
}
}, {
key: "isContentViewResponsive",
value: function isContentViewResponsive() {
var contentCellWidth = this.getContentCellConfigWidth();
return !!contentCellWidth.endsWith && contentCellWidth.endsWith("%");
}
}, {
key: "getSchedulerWidth",
value: function getSchedulerWidth() {
var baseWidth = this.documentWidth - this.config.besidesWidth > 0 ? this.documentWidth - this.config.besidesWidth : 0;
return this.isSchedulerResponsive() ? parseInt(baseWidth * Number(this.config.schedulerWidth.slice(0, -1)) / 100) : this.config.schedulerWidth;
}
}, {
key: "getResourceTableWidth",
value: function getResourceTableWidth() {
var resourceTableConfigWidth = this.getResourceTableConfigWidth();
var schedulerWidth = this.getSchedulerWidth();
var resourceTableWidth = this.isResourceViewResponsive() ? parseInt(schedulerWidth * Number(resourceTableConfigWidth.slice(0, -1)) / 100) : resourceTableConfigWidth;
if (this.isSchedulerResponsive() && this.getContentTableWidth() + resourceTableWidth < schedulerWidth) resourceTableWidth = schedulerWidth - this.getContentTableWidth();
return resourceTableWidth;
}
}, {
key: "getContentCellWidth",
value: function getContentCellWidth() {
var contentCellConfigWidth = this.getContentCellConfigWidth();
var schedulerWidth = this.getSchedulerWidth();
return this.isContentViewResponsive() ? parseInt(schedulerWidth * Number(contentCellConfigWidth.slice(0, -1)) / 100) : contentCellConfigWidth;
}
}, {
key: "getContentTableWidth",
value: function getContentTableWidth() {
return this.headers.length * this.getContentCellWidth();
}
}, {
key: "getScrollToSpecialDayjs",
value: function getScrollToSpecialDayjs() {
if (this.config.scrollToSpecialDayjsEnabled) return this.scrollToSpecialDayjs;
return false;
}
}, {
key: "getSlots",
value: function getSlots() {
return this.isEventPerspective ? this.eventGroups : this.resources;
}
}, {
key: "getSlotById",
value: function getSlotById(slotId) {
var slots = this.getSlots();
var slot = undefined;
slots.forEach(function (item) {
if (item.id === slotId) slot = item;
});
return slot;
}
}, {
key: "getResourceById",
value: function getResourceById(resourceId) {
var resource = undefined;
this.resources.forEach(function (item) {
if (item.id === resourceId) resource = item;
});
return resource;
}
}, {
key: "getTableHeaderHeight",
value: function getTableHeaderHeight() {
return this.config.tableHeaderHeight;
}
}, {
key: "getSchedulerContentDesiredHeight",
value: function getSchedulerContentDesiredHeight() {
var height = 0;
this.renderData.forEach(function (item) {
if (item.render) height += item.rowHeight;
});
return height;
}
}, {
key: "getCellMaxEvents",
value: function getCellMaxEvents() {
return this.viewType === _index.ViewType.Week ? this.config.weekMaxEvents : this.viewType === _index.ViewType.Day ? this.config.dayMaxEvents : this.viewType === _index.ViewType.Month ? this.config.monthMaxEvents : this.viewType === _index.ViewType.Year ? this.config.yearMaxEvents : this.viewType === _index.ViewType.Quarter ? this.config.quarterMaxEvents : this.config.customMaxEvents;
}
}, {
key: "getCalendarPopoverLocale",
value: function getCalendarPopoverLocale() {
return this.calendarPopoverLocale;
}
}, {
key: "getSelectedDate",
value: function getSelectedDate() {
return this.selectDate.format(_index.DATE_FORMAT);
}
}, {
key: "getViewStartDate",
value: function getViewStartDate() {
return this.startDate;
}
}, {
key: "getViewEndDate",
value: function getViewEndDate() {
return this.endDate;
}
}, {
key: "getViewDates",
value: function getViewDates() {
return {
startDate: this.startDate,
endDate: this.endDate
};
}
}, {
key: "getDateLabel",
value: function getDateLabel() {
var start = this.localeDayjs(new Date(this.startDate));
var end = this.localeDayjs(new Date(this.endDate));
var dateLabel = start.format('LL');
if (start != end) dateLabel = start.format('LL') + "-" + end.format('LL');
if (!!this.behaviors.getDateLabelFunc) dateLabel = this.behaviors.getDateLabelFunc(this, this.viewType, this.startDate, this.endDate);
return dateLabel;
}
}, {
key: "addEvent",
value: function addEvent(newEvent) {
this._attachEvent(newEvent);
if (this.eventGroupsAutoGenerated) this._generateEventGroups();
this._createRenderData();
}
}, {
key: "updateEventStart",
value: function updateEventStart(event, newStart) {
this._detachEvent(event);
event.start = newStart;
this._attachEvent(event);
this._createRenderData();
}
}, {
key: "updateEventEnd",
value: function updateEventEnd(event, newEnd) {
event.end = newEnd;
this._createRenderData();
}
}, {
key: "moveEvent",
value: function moveEvent(event, newSlotId, newSlotName, newStart, newEnd) {
this._detachEvent(event);
if (this.isEventPerspective) {
event.groupId = newSlotId;
event.groupName = newSlotName;
} else event.resourceId = newSlotId;
event.end = newEnd;
event.start = newStart;
this._attachEvent(event);
this._createRenderData();
}
}, {
key: "isEventInTimeWindow",
value: function isEventInTimeWindow(eventStart, eventEnd, windowStart, windowEnd) {
return eventStart < windowEnd && eventEnd > windowStart;
}
}, {
key: "removeEvent",
value: function removeEvent(event) {
var index = this.events.indexOf(event);
if (index !== -1) {
this.events.splice(index, 1);
this._createRenderData();
}
}
}, {
key: "removeEventById",
value: function removeEventById(eventId) {
var index = -1;
this.events.forEach(function (item, idx) {
if (item.id === eventId) index = idx;
});
if (index !== -1) {
this.events.splice(index, 1);
this._createRenderData();
}
}
}, {
key: "getResourceTableConfigWidth",
value: function getResourceTableConfigWidth() {
if (this.showAgenda) return this.config.agendaResourceTableWidth;
return this.viewType === _index.ViewType.Week ? this.config.weekResourceTableWidth : this.viewType === _index.ViewType.Day ? this.config.dayResourceTableWidth : this.viewType === _index.ViewType.Month ? this.config.monthResourceTableWidth : this.viewType === _index.ViewType.Year ? this.config.yearResourceTableWidth : this.viewType === _index.ViewType.Quarter ? this.config.quarterResourceTableWidth : this.config.customResourceTableWidth;
}
}, {
key: "getContentCellConfigWidth",
value: function getContentCellConfigWidth() {
return this.viewType === _index.ViewType.Week ? this.config.weekCellWidth : this.viewType === _index.ViewType.Day ? this.config.dayCellWidth : this.viewType === _index.ViewType.Month ? this.config.monthCellWidth : this.viewType === _index.ViewType.Year ? this.config.yearCellWidth : this.viewType === _index.ViewType.Quarter ? this.config.quarterCellWidth : this.config.customCellWidth;
}
}, {
key: "_setDocumentWidth",
value: function _setDocumentWidth(documentWidth) {
if (documentWidth >= 0) {
this.documentWidth = documentWidth;
}
}
}, {
key: "_detachEvent",
value: function _detachEvent(event) {
var index = this.events.indexOf(event);
if (index !== -1) this.events.splice(index, 1);
}
}, {
key: "_attachEvent",
value: function _attachEvent(event) {
var _this = this;
var pos = 0;
var eventStart = this.localeDayjs(new Date(event.start));
this.events.forEach(function (item, index) {
var start = _this.localeDayjs(new Date(item.start));
if (eventStart >= start) pos = index + 1;
});
this.events.splice(pos, 0, event);
}
}, {
key: "_handleRecurringEvents",
value: function _handleRecurringEvents() {
var _this2 = this;
var recurringEvents = this.events.filter(function (x) {
return !!x.rrule;
});
recurringEvents.forEach(function (item) {
_this2._detachEvent(item);
});
recurringEvents.forEach(function (item) {
var windowStart = _this2.startDate,
windowEnd = _this2.endDate.add(1, 'days'),
oldStart = _this2.localeDayjs(new Date(item.start)),
oldEnd = _this2.localeDayjs(new Date(item.end)),
rule = (0, _rrule.rrulestr)(item.rrule),
oldDtstart = undefined,
oldUntil = rule.origOptions.until || windowEnd.toDate();
if (!!rule.origOptions.dtstart) {
oldDtstart = _this2.localeDayjs(new Date(rule.origOptions.dtstart));
}
//rule.origOptions.dtstart = oldStart.toDate();
if (windowEnd < oldUntil) {
rule.origOptions.until = windowEnd.toDate();
}
//reload
rule = (0, _rrule.rrulestr)(rule.toString());
if (item.exdates || item.exrule) {
var rruleSet = new _rrule.RRuleSet();
rruleSet.rrule(rule);
if (item.exrule) {
rruleSet.exrule((0, _rrule.rrulestr)(item.exrule));
}
if (item.exdates) {
item.exdates.forEach(function (exdate) {
rruleSet.exdate(_this2.localeDayjs(exdate).toDate());
});
}
rule = rruleSet;
}
var all = rule.between(new Date(windowStart), new Date(windowEnd));
all.forEach(function (time, index) {
var newEvent = _extends({}, item, {
recurringEventId: item.id,
recurringEventStart: item.start,
recurringEventEnd: item.end,
id: item.id + "-" + index,
start: rule.origOptions.tzid ? _this2.localeDayjs.utc(time).utcOffset(_this2.localeDayjs(new Date().utcOffset)(), true).format(_index.DATETIME_FORMAT) : _this2.localeDayjs(new Date(time)).format(_index.DATETIME_FORMAT),
end: rule.origOptions.tzid ? _this2.localeDayjs.utc(time).utcOffset(_this2.localeDayjs(new Date().utcOffset)(), true).add(oldEnd.diff(oldStart), 'ms').add(_this2.localeDayjs(new Date(oldUntil)).utcOffset() - _this2.localeDayjs(new Date(item.start)).utcOffset(), "m").format(_index.DATETIME_FORMAT) : _this2.localeDayjs(new Date(time)).add(oldEnd.diff(oldStart), 'ms').format(_index.DATETIME_FORMAT)
});
var eventStart = _this2.localeDayjs(newEvent.start),
eventEnd = _this2.localeDayjs(newEvent.end);
if (_this2.isEventInTimeWindow(eventStart, eventEnd, windowStart, windowEnd) && (!oldDtstart || eventStart >= oldDtstart)) {
_this2._attachEvent(newEvent);
}
});
});
}
}, {
key: "_resolveDate",
value: function _resolveDate(num) {
var date = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
if (date != undefined) this.selectDate = this.localeDayjs(date);
if (this.viewType === _index.ViewType.Week) {
this.startDate = date != undefined ? this.localeDayjs(date).startOf('week') : this.localeDayjs(this.startDate).add(num, 'weeks');
this.endDate = this.localeDayjs(this.startDate).endOf('week');
} else if (this.viewType === _index.ViewType.Day) {
this.startDate = date != undefined ? this.selectDate : this.localeDayjs(this.startDate).add(num, 'days');
this.endDate = this.startDate;
} else if (this.viewType === _index.ViewType.Month) {
this.startDate = date != undefined ? this.localeDayjs(date).startOf('month') : this.localeDayjs(this.startDate).add(num, 'months');
this.endDate = this.localeDayjs(this.startDate).endOf('month');
} else if (this.viewType === _index.ViewType.Quarter) {
this.startDate = date != undefined ? this.localeDayjs(date).startOf('quarter') : this.localeDayjs(this.startDate).add(num, 'quarters');
this.endDate = this.localeDayjs(this.startDate).endOf('quarter');
} else if (this.viewType === _index.ViewType.Year) {
this.startDate = date != undefined ? this.localeDayjs(date).startOf('year') : this.localeDayjs(this.startDate).add(num, 'years');
this.endDate = this.localeDayjs(this.startDate).endOf('year');
} else if (this.viewType === _index.ViewType.Custom || this.viewType === _index.ViewType.Custom1 || this.viewType === _index.ViewType.Custom2) {
if (this.behaviors.getCustomDateFunc != undefined) {
var customDate = this.behaviors.getCustomDateFunc(this, num, date);
this.startDate = this.localeDayjs(customDate.startDate);
this.endDate = this.localeDayjs(customDate.endDate);
if (!!customDate.cellUnit) this.cellUnit = customDate.cellUnit;
} else {
throw new Error('This is custom view type, set behaviors.getCustomDateFunc func to resolve the time window(startDate and endDate) yourself');
}
}
}
}, {
key: "_createHeaders",
value: function _createHeaders() {
var headers = [],
start = this.localeDayjs(new Date(this.startDate)),
end = this.localeDayjs(new Date(this.endDate)),
header = start;
if (this.showAgenda) {
headers.push({ time: header.format(_index.DATETIME_FORMAT), nonWorkingTime: false });
} else {
if (this.cellUnit === _index.CellUnit.Hour) {
start = start.add(this.config.dayStartFrom, 'hours');
end = end.add(this.config.dayStopTo, 'hours');
header = start;
while (header >= start && header <= end) {
var minuteSteps = this.getMinuteStepsInHour();
for (var i = 0; i < minuteSteps; i++) {
var hour = header.hour();
if (hour >= this.config.dayStartFrom && hour <= this.config.dayStopTo) {
var time = header.format(_index.DATETIME_FORMAT);
var nonWorkingTime = this.behaviors.isNonWorkingTimeFunc(this, time);
headers.push({ time: time, nonWorkingTime: nonWorkingTime });
}
header = header.add(this.config.minuteStep, 'minutes');
}
}
} else {
while (header >= start && header <= end) {
var _time = header.format(_index.DATETIME_FORMAT);
var dayOfWeek = header.weekday();
if (this.config.displayWeekend || dayOfWeek !== 0 && dayOfWeek !== 6) {
var _nonWorkingTime = this.behaviors.isNonWorkingTimeFunc(this, _time);
headers.push({ time: _time, nonWorkingTime: _nonWorkingTime });
}
header = header.add(1, 'days');
}
}
}
this.headers = headers;
}
}, {
key: "_createInitHeaderEvents",
value: function _createInitHeaderEvents(header) {
var start = this.localeDayjs(new Date(header.time)),
startValue = start.format(_index.DATETIME_FORMAT);
var endValue = this.showAgenda ? this.viewType === _index.ViewType.Week ? start.add(1, 'weeks').format(_index.DATETIME_FORMAT) : this.viewType === _index.ViewType.Day ? start.add(1, 'days').format(_index.DATETIME_FORMAT) : this.viewType === _index.ViewType.Month ? start.add(1, 'months').format(_index.DATETIME_FORMAT) : this.viewType === _index.ViewType.Year ? start.add(1, 'years').format(_index.DATETIME_FORMAT) : this.viewType === _index.ViewType.Quarter ? start.add(1, 'quarters').format(_index.DATETIME_FORMAT) : this.localeDayjs(new Date(this.endDate)).add(1, 'days').format(_index.DATETIME_FORMAT) : this.cellUnit === _index.CellUnit.Hour ? start.add(this.config.minuteStep, 'minutes').format(_index.DATETIME_FORMAT) : start.add(1, 'days').format(_index.DATETIME_FORMAT);
return {
time: header.time,
nonWorkingTime: header.nonWorkingTime,
start: startValue,
end: endValue,
count: 0,
addMore: 0,
addMoreIndex: 0,
events: [,,,]
};
}
}, {
key: "_createHeaderEvent",
value: function _createHeaderEvent(render, span, eventItem) {
return {
render: render,
span: span,
eventItem: eventItem
};
}
}, {
key: "_getEventSlotId",
value: function _getEventSlotId(event) {
return this.isEventPerspective ? this._getEventGroupId(event) : event.resourceId;
}
}, {
key: "_getEventGroupId",
value: function _getEventGroupId(event) {
return !!event.groupId ? event.groupId.toString() : event.id.toString();
}
}, {
key: "_getEventGroupName",
value: function _getEventGroupName(event) {
return !!event.groupName ? event.groupName : event.title;
}
}, {
key: "_generateEventGroups",
value: function _generateEventGroups() {
var _this3 = this;
var eventGroups = [];
var set = new Set();
this.events.forEach(function (item) {
var groupId = _this3._getEventGroupId(item);
var groupName = _this3._getEventGroupName(item);
if (!set.has(groupId)) {
eventGroups.push({
id: groupId,
name: groupName,
state: item
});
set.add(groupId);
}
});
this.eventGroups = eventGroups;
}
}, {
key: "_createInitRenderData",
value: function _createInitRenderData(isEventPerspective, eventGroups, resources, headers) {
var _this4 = this;
var slots = isEventPerspective ? eventGroups : resources;
var slotTree = [],
slotMap = new Map();
slots.forEach(function (slot) {
var headerEvents = headers.map(function (header) {
return _this4._createInitHeaderEvents(header);
});
var slotRenderData = {
slotId: slot.id,
slotName: slot.name,
parentId: slot.parentId,
groupOnly: slot.groupOnly,
hasSummary: false,
rowMaxCount: 0,
rowHeight: _this4.config.nonAgendaSlotMinHeight !== 0 ? _this4.config.nonAgendaSlotMinHeight : _this4.config.eventItemLineHeight + 2,
headerItems: headerEvents,
indent: 0,
hasChildren: false,
expanded: true,
render: true
};
var id = slot.id;
var value = undefined;
if (slotMap.has(id)) {
value = slotMap.get(id);
value.data = slotRenderData;
} else {
value = {
data: slotRenderData,
children: []
};
slotMap.set(id, value);
}
var parentId = slot.parentId;
if (!parentId || parentId === id) {
slotTree.push(value);
} else {
var parentValue = undefined;
if (slotMap.has(parentId)) {
parentValue = slotMap.get(parentId);
} else {
parentValue = {
data: undefined,
children: []
};
slotMap.set(parentId, parentValue);
}
parentValue.children.push(value);
}
});
var slotStack = [];
var i = void 0;
for (i = slotTree.length - 1; i >= 0; i--) {
slotStack.push(slotTree[i]);
}
var initRenderData = [];
var currentNode = undefined;
while (slotStack.length > 0) {
currentNode = slotStack.pop();
if (currentNode.data.indent > 0) {
currentNode.data.render = this.config.defaultExpanded;
}
if (currentNode.children.length > 0) {
currentNode.data.hasChildren = true;
currentNode.data.expanded = this.config.defaultExpanded;
}
initRenderData.push(currentNode.data);
for (i = currentNode.children.length - 1; i >= 0; i--) {
currentNode.children[i].data.indent = currentNode.data.indent + 1;
slotStack.push(currentNode.children[i]);
}
}
return initRenderData;
}
}, {
key: "_getSpan",
value: function _getSpan(startTime, endTime, headers) {
if (this.showAgenda) return 1;
function startOfWeek(date) {
var day = date.getDay();
var diff = date.getDate() - day;
return new Date(date.getFullYear(), date.getMonth(), diff);
}
var timeBetween = function timeBetween(date1, date2, timeIn) {
if (timeIn === 'days' || timeIn === 'day') {
if (date1.getDate() === date2.getDate() && date1.getMonth() === date2.getMonth()) {
return 1;
}
}
var one = void 0;
switch (timeIn) {
case 'days':
case 'day':
one = 1000 * 60 * 60 * 24;
break;
case 'minutes':
case 'minute':
one = 1000 * 60;
break;
default:
return 0;
}
var date1_ms = date1.getTime();
var date2_ms = date2.getTime();
var diff = (date2_ms - date1_ms) / one;
return diff < 0 ? 0 : diff;
};
var eventStart = new Date(startTime),
eventEnd = new Date(endTime),
span = 0,
windowStart = new Date(this.startDate),
windowEnd = new Date(this.endDate);
windowStart.setHours(0, 0, 0, 0);
windowEnd.setHours(23, 59, 59);
if (this.viewType === _index.ViewType.Day) {
if (headers.length > 0) {
var day = new Date(headers[0].time);
if (day.getDate() > eventStart.getDate() && day.getDate() < eventEnd.getDate()) {
span = 1440 / this.config.minuteStep;
} else if (day.getDate() > eventStart.getDate() && day.getDate() === eventEnd.getDate()) {
span = Math.ceil(timeBetween(day, eventEnd, 'minutes') / this.config.minuteStep);
} else if (day.getDate() === eventStart.getDate() && day.getDate() < eventEnd.getDate()) {
day.setHours(23, 59, 59);
span = Math.ceil(timeBetween(eventStart, day, 'minutes') / this.config.minuteStep);
} else if (day.getDate() === eventStart.getDate() && day.getDate() === eventEnd.getDate() || eventEnd.getDate() === eventStart.getDate()) {
span = Math.ceil(timeBetween(eventStart, eventEnd, 'minutes') / this.config.minuteStep);
}
}
} else if (this.viewType === _index.ViewType.Week || this.viewType === _index.ViewType.Month || this.viewType === _index.ViewType.Quarter || this.viewType === _index.ViewType.Year) {
var startDate = windowStart < eventStart ? eventStart : windowStart;
var endDate = windowEnd > eventEnd ? eventEnd : windowEnd;
span = Math.ceil(timeBetween(startDate, endDate, 'days'));
} else {
if (this.cellUnit === _index.CellUnit.Day) {
eventEnd.setHours(23, 59, 59);
eventStart.setHours(0, 0, 0, 0);
}
var timeIn = this.cellUnit === _index.CellUnit.Day ? 'days' : 'minutes';
var dividedBy = this.cellUnit === _index.CellUnit.Day ? 1 : this.config.minuteStep;
if (windowStart >= eventStart && eventEnd <= windowEnd) {
span = Math.ceil(timeBetween(windowStart, eventEnd, timeIn) / dividedBy);
} else if (windowStart > eventStart && eventEnd > windowEnd) {
span = Math.ceil(timeBetween(windowStart, windowEnd, timeIn) / dividedBy);
} else if (windowStart <= eventStart && eventEnd >= windowEnd) {
span = Math.ceil(timeBetween(eventStart, windowEnd, timeIn) / dividedBy);
} else {
span = Math.ceil(timeBetween(eventStart, eventEnd, timeIn) / dividedBy);
}
}
return span;
}
}, {
key: "_validateResource",
value: function _validateResource(resources) {
if (Object.prototype.toString.call(resources) !== "[object Array]") {
throw new Error('Resources should be Array object');
}
resources.forEach(function (item, index) {
if (item == undefined) {
console.error("Resource undefined: " + index);
throw new Error("Resource undefined: " + index);
}
if (item.id == undefined || item.name == undefined) {
console.error('Resource property missed', index, item);
throw new Error("Resource property undefined: " + index);
}
});
}
}, {
key: "_validateEventGroups",
value: function _validateEventGroups(eventGroups) {
if (Object.prototype.toString.call(eventGroups) !== "[object Array]") {
throw new Error('Event groups should be Array object');
}
eventGroups.forEach(function (item, index) {
if (item == undefined) {
console.error("Event group undefined: " + index);
throw new Error("Event group undefined: " + index);
}
if (item.id == undefined || item.name == undefined) {
console.error('Event group property missed', index, item);
throw new Error("Event group property undefined: " + index);
}
});
}
}, {
key: "_validateEvents",
value: function _validateEvents(events) {
if (Object.prototype.toString.call(events) !== "[object Array]") {
throw new Error('Events should be Array object');
}
events.forEach(function (e, index) {
if (e == undefined) {
console.error("Event undefined: " + index);
throw new Error("Event undefined: " + index);
}
if (e.id == undefined || e.resourceId == undefined || e.title == undefined || e.start == undefined || e.end == undefined) {
console.error('Event property missed', index, e);
throw new Error("Event property undefined: " + index);
}
});
}
}, {
key: "_validateMinuteStep",
value: function _validateMinuteStep(minuteStep) {
if (60 % minuteStep !== 0) {
console.error('Minute step is not set properly - 60 minutes must be divisible without remainder by this number');
throw new Error('Minute step is not set properly - 60 minutes must be divisible without remainder by this number');
}
}
}, {
key: "_compare",
value: function _compare(event1, event2) {
var start1 = this.localeDayjs(event1.start),
start2 = this.localeDayjs(event2.start);
if (start1 !== start2) return start1 < start2 ? -1 : 1;
var end1 = this.localeDayjs(event1.end),
end2 = this.localeDayjs(event2.end);
if (end1 !== end2) return end1 < end2 ? -1 : 1;
return event1.id < event2.id ? -1 : 1;
}
}, {
key: "_createRenderData",
value: function _createRenderData() {
var _this5 = this;
var initRenderData = this._createInitRenderData(this.isEventPerspective, this.eventGroups, this.resources, this.headers);
//this.events.sort(this._compare);
var cellMaxEventsCount = this.getCellMaxEvents();
var cellMaxEventsCountValue = 30;
this.events.forEach(function (item) {
var resourceEventsList = initRenderData.filter(function (x) {
return x.slotId === _this5._getEventSlotId(item);
});
if (resourceEventsList.length > 0) {
var resourceEvents = resourceEventsList[0];
var span = _this5._getSpan(item.start, item.end, _this5.headers);
var eventStart = new Date(item.start),
eventEnd = new Date(item.end);
var pos = -1;
resourceEvents.headerItems.forEach(function (header, index) {
var headerStart = new Date(header.start),
headerEnd = new Date(header.end);
if (headerEnd > eventStart && headerStart < eventEnd) {
header.count = header.count + 1;
if (header.count > resourceEvents.rowMaxCount) {
resourceEvents.rowMaxCount = header.count;
var rowsCount = cellMaxEventsCount <= cellMaxEventsCountValue && resourceEvents.rowMaxCount > cellMaxEventsCount ? cellMaxEventsCount : resourceEvents.rowMaxCount;
var newRowHeight = rowsCount * _this5.config.eventItemLineHeight + (_this5.config.creatable && _this5.config.checkConflict === false ? 20 : 2);
if (newRowHeight > resourceEvents.rowHeight) resourceEvents.rowHeight = newRowHeight;
}
if (pos === -1) {
var tmp = 0;
while (header.events[tmp] !== undefined) {
tmp++;
}pos = tmp;
}
var render = headerStart <= eventStart || index === 0;
if (render === false) {
var previousHeader = resourceEvents.headerItems[index - 1];
var previousHeaderStart = new Date(previousHeader.start),