UNPKG

@josmangarsal/pragmatic-scheduler

Version:
84 lines (83 loc) 3.5 kB
"use strict"; 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); }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.useSchedulerViewConfig = void 0; var react_1 = require("react"); var defaults_1 = require("../constants/defaults"); var useSchedulerViewConfig = function (_a) { var _b = _a.daysToDisplay, daysToDisplay = _b === void 0 ? 3 : _b, _c = _a.interval, interval = _c === void 0 ? 2 : _c, _d = _a.prevDays, prevDays = _d === void 0 ? 0 : _d; var _e = __read((0, react_1.useState)(0), 2), calculatedDivisionWidth = _e[0], setCalculatedDivisionWidth = _e[1]; (0, react_1.useEffect)(function () { // Get calculated value from custom event from TimelineView var handleEvent = function (event) { var customEvent = event; if (customEvent === null || customEvent === void 0 ? void 0 : customEvent.detail) { setCalculatedDivisionWidth(customEvent.detail.calculatedDivisionWidth); } }; window.addEventListener('timelineview:resize:calculatedDivisionWidth', handleEvent); return function () { window.removeEventListener('timelineview:resize:calculatedDivisionWidth', handleEvent); }; }, []); (0, react_1.useEffect)(function () { setCalculatedDivisionWidth(0); // Reset on view change }, [interval, daysToDisplay, prevDays]); var config = (0, react_1.useMemo)(function () { var currentConfig = __assign({}, defaults_1.defaultConfig); currentConfig.daysToDisplay = daysToDisplay; if (currentConfig.daysToDisplay >= 6) { currentConfig.divisionParts = 1; } switch (interval) { case 0.25: // 15 minutes // currentConfig.divisionWidth = 160 / 2; break; case 24: // 1 day currentConfig.divisionWidth = 160 * 2; break; case 24 * 7: // 1 week currentConfig.divisionWidth = 160 * 4; break; default: } if (calculatedDivisionWidth) { currentConfig.divisionWidth = calculatedDivisionWidth; } currentConfig.previousDaysToDisplay = prevDays; currentConfig.daysToDisplay += currentConfig.previousDaysToDisplay; currentConfig.unAssignedRows = 0; return currentConfig; }, [calculatedDivisionWidth, daysToDisplay, interval, prevDays]); return { currentInterval: interval, config: config, }; }; exports.useSchedulerViewConfig = useSchedulerViewConfig;