UNPKG

big-scheduler

Version:

Big scheduler is a powerful and intuitive scheduler and resource planning solution built with React. Seamlessly integrate this modern browser-compatible component into your applications to effectively manage time, appointments, and resources. With drag-a

412 lines (411 loc) 22.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = _interopRequireWildcard(require("react")); var _propTypes = require("prop-types"); var _AddMore = _interopRequireDefault(require("./AddMore")); var _Summary = _interopRequireDefault(require("./Summary")); var _SelectedArea = _interopRequireDefault(require("./SelectedArea")); var _default2 = require("../config/default"); var _utility = require("../helper/utility"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; } function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _extends() { _extends = Object.assign ? Object.assign.bind() : 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; }; return _extends.apply(this, arguments); } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } var ResourceEvents = /*#__PURE__*/function (_Component) { function ResourceEvents(props) { var _this; _classCallCheck(this, ResourceEvents); _this = _callSuper(this, ResourceEvents, [props]); _defineProperty(_this, "supportTouchHelper", function () { var evType = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "add"; var ev = evType === "add" ? _this.eventContainer.addEventListener : _this.eventContainer.removeEventListener; if (_this.supportTouch) { // ev('touchstart', this.initDrag, false); } else { ev("mousedown", _this.initDrag, false); } }); _defineProperty(_this, "initDrag", function (ev) { var isSelecting = _this.state.isSelecting; if (isSelecting) return; if ((ev.srcElement || ev.target) !== _this.eventContainer) return; ev.stopPropagation(); var resourceEvents = _this.props.resourceEvents; if (resourceEvents.groupOnly) return; var _this$dragHelper = _this.dragHelper(ev, "init"), _this$dragHelper2 = _slicedToArray(_this$dragHelper, 2), clientX = _this$dragHelper2[0], toReturn = _this$dragHelper2[1]; if (toReturn) { return; } var schedulerData = _this.props.schedulerData; var cellWidth = schedulerData.getContentCellWidth(); var pos = (0, _utility.getPos)(_this.eventContainer); var startX = clientX - pos.x; var leftIndex = Math.floor(startX / cellWidth); var left = leftIndex * cellWidth; var rightIndex = Math.ceil(startX / cellWidth); var width = (rightIndex - leftIndex) * cellWidth; _this.setState({ startX: startX, left: left, leftIndex: leftIndex, width: width, rightIndex: rightIndex, isSelecting: true }); if (_this.supportTouch) { document.documentElement.addEventListener("touchmove", _this.doDrag, false); document.documentElement.addEventListener("touchend", _this.stopDrag, false); document.documentElement.addEventListener("touchcancel", _this.cancelDrag, false); } else { document.documentElement.addEventListener("mousemove", _this.doDrag, false); document.documentElement.addEventListener("mouseup", _this.stopDrag, false); } document.onselectstart = function () { return false; }; document.ondragstart = function () { return false; }; }); _defineProperty(_this, "doDrag", function (ev) { ev.stopPropagation(); var _this$dragHelper3 = _this.dragHelper(ev, "do"), _this$dragHelper4 = _slicedToArray(_this$dragHelper3, 2), clientX = _this$dragHelper4[0], toReturn = _this$dragHelper4[1]; if (toReturn) { return; } var startX = _this.state.startX; var schedulerData = _this.props.schedulerData; var headers = schedulerData.headers; var cellWidth = schedulerData.getContentCellWidth(); var pos = (0, _utility.getPos)(_this.eventContainer); var currentX = clientX - pos.x; var leftIndex = Math.floor(Math.min(startX, currentX) / cellWidth); leftIndex = leftIndex < 0 ? 0 : leftIndex; var left = leftIndex * cellWidth; var rightIndex = Math.ceil(Math.max(startX, currentX) / cellWidth); rightIndex = rightIndex > headers.length ? headers.length : rightIndex; var width = (rightIndex - leftIndex) * cellWidth; _this.setState({ leftIndex: leftIndex, left: left, rightIndex: rightIndex, width: width, isSelecting: true }); }); _defineProperty(_this, "dragHelper", function (ev, dragType) { var clientX = 0; if (_this.supportTouch) { if (ev.changedTouches.length === 0) return [clientX, true]; var touch = ev.changedTouches[0]; clientX = touch.pageX; } else if (dragType === "init") { if (ev.buttons !== undefined && ev.buttons !== 1) return [clientX, true]; clientX = ev.clientX; } else { clientX = ev.clientX; } return [clientX, false]; }); _defineProperty(_this, "stopDrag", function (ev) { ev.stopPropagation(); var _this$props = _this.props, schedulerData = _this$props.schedulerData, newEvent = _this$props.newEvent, resourceEvents = _this$props.resourceEvents; var headers = schedulerData.headers, events = schedulerData.events, config = schedulerData.config, cellUnit = schedulerData.cellUnit, localeDayjs = schedulerData.localeDayjs; var _this$state = _this.state, leftIndex = _this$state.leftIndex, rightIndex = _this$state.rightIndex; if (_this.supportTouch) { document.documentElement.removeEventListener("touchmove", _this.doDrag, false); document.documentElement.removeEventListener("touchend", _this.stopDrag, false); document.documentElement.removeEventListener("touchcancel", _this.cancelDrag, false); } else { document.documentElement.removeEventListener("mousemove", _this.doDrag, false); document.documentElement.removeEventListener("mouseup", _this.stopDrag, false); } document.onselectstart = null; document.ondragstart = null; var startTime = headers[leftIndex].time; var endTime = resourceEvents.headerItems[rightIndex - 1].end; if (cellUnit !== _default2.CellUnit.Hour) { endTime = localeDayjs(new Date(resourceEvents.headerItems[rightIndex - 1].start)).hour(23).minute(59).second(59).format(_default2.DATETIME_FORMAT); } var slotId = resourceEvents.slotId; var slotName = resourceEvents.slotName; _this.setState({ startX: 0, leftIndex: 0, left: 0, rightIndex: 0, width: 0, isSelecting: false }); var hasConflict = false; if (config.checkConflict) { var start = localeDayjs(new Date(startTime)); var end = localeDayjs(endTime); events.forEach(function (e) { if (schedulerData._getEventSlotId(e) === slotId) { var eStart = localeDayjs(e.start); var eEnd = localeDayjs(e.end); if (start >= eStart && start < eEnd || end > eStart && end <= eEnd || eStart >= start && eStart < end || eEnd > start && eEnd <= end) hasConflict = true; } }); } if (hasConflict) { var conflictOccurred = _this.props.conflictOccurred; if (conflictOccurred !== undefined) { conflictOccurred(schedulerData, "New", { id: undefined, start: startTime, end: endTime, slotId: slotId, slotName: slotName, title: undefined }, _default2.DnDTypes.EVENT, slotId, slotName, startTime, endTime); } else { console.log("Conflict occurred, set conflictOccurred func in Scheduler to handle it"); } } else if (newEvent !== undefined) newEvent(schedulerData, slotId, slotName, startTime, endTime); }); _defineProperty(_this, "cancelDrag", function (ev) { ev.stopPropagation(); var isSelecting = _this.state.isSelecting; if (isSelecting) { document.documentElement.removeEventListener("touchmove", _this.doDrag, false); document.documentElement.removeEventListener("touchend", _this.stopDrag, false); document.documentElement.removeEventListener("touchcancel", _this.cancelDrag, false); document.onselectstart = null; document.ondragstart = null; _this.setState({ startX: 0, leftIndex: 0, left: 0, rightIndex: 0, width: 0, isSelecting: false }); } }); _defineProperty(_this, "onAddMoreClick", function (headerItem) { var _this$props2 = _this.props, onSetAddMoreState = _this$props2.onSetAddMoreState, resourceEvents = _this$props2.resourceEvents, schedulerData = _this$props2.schedulerData; if (onSetAddMoreState) { var config = schedulerData.config; var cellWidth = schedulerData.getContentCellWidth(); var index = resourceEvents.headerItems.indexOf(headerItem); if (index !== -1) { var left = index * (cellWidth - 1); var pos = (0, _utility.getPos)(_this.eventContainer); left += pos.x; var top = pos.y; var height = (headerItem.count + 1) * config.eventItemLineHeight + 20; onSetAddMoreState({ headerItem: headerItem, left: left, top: top, height: height }); } } }); _defineProperty(_this, "eventContainerRef", function (element) { _this.eventContainer = element; }); _this.state = { isSelecting: false, left: 0, width: 0 }; _this.supportTouch = false; // 'ontouchstart' in window; return _this; } _inherits(ResourceEvents, _Component); return _createClass(ResourceEvents, [{ key: "componentDidMount", value: function componentDidMount() { var schedulerData = this.props.schedulerData; var config = schedulerData.config; this.supportTouch = "ontouchstart" in window; if (config.creatable === true) { this.supportTouchHelper(); } } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { if (prevProps !== this.props) { this.supportTouchHelper("remove"); if (this.props.schedulerData.config.creatable) { this.supportTouchHelper(); } } } }, { key: "render", value: function render() { var _this2 = this; var _this$props3 = this.props, resourceEvents = _this$props3.resourceEvents, schedulerData = _this$props3.schedulerData, connectDropTarget = _this$props3.connectDropTarget, dndSource = _this$props3.dndSource; var cellUnit = schedulerData.cellUnit, startDate = schedulerData.startDate, endDate = schedulerData.endDate, config = schedulerData.config, localeDayjs = schedulerData.localeDayjs; var _this$state2 = this.state, isSelecting = _this$state2.isSelecting, left = _this$state2.left, width = _this$state2.width; var cellWidth = schedulerData.getContentCellWidth(); var cellMaxEvents = schedulerData.getCellMaxEvents(); var rowWidth = schedulerData.getContentTableWidth(); var DnDEventItem = dndSource.getDragSource(); var selectedArea = isSelecting ? /*#__PURE__*/_react["default"].createElement(_SelectedArea["default"], _extends({}, this.props, { left: left, width: width })) : /*#__PURE__*/_react["default"].createElement("div", null); var eventList = []; resourceEvents.headerItems.forEach(function (headerItem, index) { if (headerItem.count > 0 || headerItem.summary !== undefined) { var isTop = config.summaryPos === _default2.SummaryPos.TopRight || config.summaryPos === _default2.SummaryPos.Top || config.summaryPos === _default2.SummaryPos.TopLeft; var marginTop = resourceEvents.hasSummary && isTop ? 1 + config.eventItemLineHeight : 1; var renderEventsMaxIndex = headerItem.addMore === 0 ? cellMaxEvents : headerItem.addMoreIndex; headerItem.events.forEach(function (evt, idx) { if (idx < renderEventsMaxIndex && evt !== undefined && evt.render) { var durationStart = localeDayjs(new Date(startDate)); var durationEnd = localeDayjs(endDate); if (cellUnit === _default2.CellUnit.Hour) { durationStart = localeDayjs(new Date(startDate)).add(config.dayStartFrom, "hours"); durationEnd = localeDayjs(endDate).add(config.dayStopTo + 1, "hours"); } var eventStart = localeDayjs(evt.eventItem.start); var eventEnd = localeDayjs(evt.eventItem.end); var isStart = eventStart >= durationStart; var isEnd = eventEnd <= durationEnd; var _left = index * cellWidth + (index > 0 ? 2 : 3); var _width = evt.span * cellWidth - (index > 0 ? 5 : 6) > 0 ? evt.span * cellWidth - (index > 0 ? 5 : 6) : 0; var top = marginTop + idx * config.eventItemLineHeight; var eventItem = /*#__PURE__*/_react["default"].createElement(DnDEventItem, _extends({}, _this2.props, { key: evt.eventItem.id, eventItem: evt.eventItem, isStart: isStart, isEnd: isEnd, isInPopover: false, left: _left, width: _width, top: top, leftIndex: index, rightIndex: index + evt.span })); eventList.push(eventItem); } }); if (headerItem.addMore > 0) { var _left2 = index * cellWidth + (index > 0 ? 2 : 3); var _width2 = cellWidth - (index > 0 ? 5 : 6); var top = marginTop + headerItem.addMoreIndex * config.eventItemLineHeight; var addMoreItem = /*#__PURE__*/_react["default"].createElement(_AddMore["default"], _extends({}, _this2.props, { key: headerItem.time, headerItem: headerItem, number: headerItem.addMore, left: _left2, width: _width2, top: top, clickAction: _this2.onAddMoreClick })); eventList.push(addMoreItem); } if (headerItem.summary !== undefined) { var _top = isTop ? 1 : resourceEvents.rowHeight - config.eventItemLineHeight + 1; var _left3 = index * cellWidth + (index > 0 ? 2 : 3); var _width3 = cellWidth - (index > 0 ? 5 : 6); var key = "".concat(resourceEvents.slotId, "_").concat(headerItem.time); var summary = /*#__PURE__*/_react["default"].createElement(_Summary["default"], { key: key, schedulerData: schedulerData, summary: headerItem.summary, left: _left3, width: _width3, top: _top }); eventList.push(summary); } } }); var eventContainer = /*#__PURE__*/_react["default"].createElement("div", { ref: this.eventContainerRef, className: "event-container", style: { height: resourceEvents.rowHeight } }, selectedArea, eventList); return /*#__PURE__*/_react["default"].createElement("tr", null, /*#__PURE__*/_react["default"].createElement("td", { style: { width: rowWidth } }, config.dragAndDropEnabled ? connectDropTarget(eventContainer) : eventContainer)); } }]); }(_react.Component); _defineProperty(ResourceEvents, "propTypes", { resourceEvents: _propTypes.PropTypes.object.isRequired, schedulerData: _propTypes.PropTypes.object.isRequired, dndSource: _propTypes.PropTypes.object.isRequired, onSetAddMoreState: _propTypes.PropTypes.func, updateEventStart: _propTypes.PropTypes.func, updateEventEnd: _propTypes.PropTypes.func, moveEvent: _propTypes.PropTypes.func, movingEvent: _propTypes.PropTypes.func, conflictOccurred: _propTypes.PropTypes.func, subtitleGetter: _propTypes.PropTypes.func, eventItemClick: _propTypes.PropTypes.func, eventContextMenuOpen: _propTypes.PropTypes.func, viewEventClick: _propTypes.PropTypes.func, viewEventText: _propTypes.PropTypes.string, viewEvent2Click: _propTypes.PropTypes.func, viewEvent2Text: _propTypes.PropTypes.string, newEvent: _propTypes.PropTypes.func, eventItemTemplateResolver: _propTypes.PropTypes.func }); var _default = exports["default"] = ResourceEvents; //# sourceMappingURL=ResourceEvents.js.map