UNPKG

@pisell/pisellos

Version:

一个可扩展的前端模块化SDK框架,支持插件系统

605 lines (603 loc) 18.5 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/modules/Schedule/utils.ts var utils_exports = {}; __export(utils_exports, { calcCalendarDataByDesignation: () => calcCalendarDataByDesignation, calcCalendarDataBySchedule: () => calcCalendarDataBySchedule, calcCalendarDataByScheduleResult: () => calcCalendarDataByScheduleResult, calcCalendarDataBySchedules: () => calcCalendarDataBySchedules, calcCalendarDataByStandard: () => calcCalendarDataByStandard, calcCalendarDataByTimeSlots: () => calcCalendarDataByTimeSlots, calcMinTimeMaxTimeBySchedules: () => calcMinTimeMaxTimeBySchedules, calcScheduleDateRange: () => calcScheduleDateRange, formatScheduleData: () => formatScheduleData, formatScheduleResult: () => formatScheduleResult, getAllSortedDateRanges: () => getAllSortedDateRanges, getDaysByStartEnd: () => getDaysByStartEnd, isAllDay: () => isAllDay }); module.exports = __toCommonJS(utils_exports); var import_dayjs = __toESM(require("dayjs")); var calcCalendarDataBySchedule = (values, others, type) => { if (type === "time-slots") { return calcCalendarDataByTimeSlots(values, others); } if (type === "designation") { return calcCalendarDataByDesignation(values, others); } return calcCalendarDataByStandard(values, others); }; var getDaysByStartEnd = (startDate, endDate) => { let start = (0, import_dayjs.default)(startDate); const end = (0, import_dayjs.default)(endDate); const daysArray = []; while (start.isBefore(end, "day") || start.isSame(end, "day")) { daysArray.push(start.format("YYYY-MM-DD")); start = start.add(1, "day"); } return daysArray; }; var mergeDateTime = (date, time) => { const dateStr = `${(0, import_dayjs.default)(date).format("YYYY-MM-DD")}${time ? (0, import_dayjs.default)(time).format(" HH:MM") : ""}`; return (0, import_dayjs.default)(dateStr); }; var getDataRange = ({ start, end, type, time_slot }) => { if (type === "time-slots") { let dateRange = []; const dateStr = getDaysByStartEnd(start, end); dateStr.forEach((str) => { time_slot == null ? void 0 : time_slot.forEach((time) => { dateRange.push({ start: `${str} ${time.start_time}`, end: `${str} ${time.end_time}` }); }); }); return dateRange; } return [ { start: start.format("YYYY-MM-DD HH:mm:ss"), end: end.format("YYYY-MM-DD HH:mm:ss") } ]; }; var getDaysByRepeatDay = (params, isGetRange) => { const { start, end, deadline, frequency, type, time_slot, excludedDays } = params; const scheduleDiff = end.diff(start, "second"); const excludedDaysMap = new Map(excludedDays.map((day) => [day, true])); let startTmp = getMaxStart(start); const daysArr = []; while (startTmp.isBefore(deadline, "day") || startTmp.isSame(deadline, "day")) { const e = startTmp.add(scheduleDiff, "second"); if (isGetRange) { if (excludedDaysMap.has(startTmp.format("YYYY-MM-DD"))) { startTmp = startTmp.add(frequency, "day"); continue; } const arr = getDataRange({ start: startTmp, end: e, type, time_slot }); daysArr.push(...arr); } else { daysArr.push(...getDaysByStartEnd(startTmp, e)); } startTmp = startTmp.add(frequency, "day"); } return Array.from(new Set(daysArr)); }; var getDaysByRepeatWeek = (params, isGetRange) => { const { start, end, deadline, frequency, frequency_date, type, time_slot, excludedDays } = params; const scheduleDiff = end.diff(start, "second"); const excludedDaysMap = new Map(excludedDays.map((day) => [day, true])); let startTmp = getMaxStart(start); const daysArr = []; while (startTmp.isBefore(deadline, "day") || startTmp.isSame(deadline, "day")) { for (let i = 0; i < frequency_date.length; i++) { const item = frequency_date[i]; const _start = startTmp.day(item); const _end = _start.add(scheduleDiff, "second"); if (isGetRange) { if (excludedDaysMap.has(_start.format("YYYY-MM-DD"))) { startTmp = startTmp.day(1).add(frequency, "day"); continue; } const arr = getDataRange({ start: _start, end: _end, type, time_slot }); daysArr.push(...arr); } else { daysArr.push(...getDaysByStartEnd(_start, _end)); } } startTmp = startTmp.day(1).add(frequency, "week"); } return Array.from(new Set(daysArr)); }; var formatDayColor = ({ days, color, excludedDays }) => { const excludedDaysSet = new Set(excludedDays); return days.map((item) => ({ date: item, color: [color], isExcluded: excludedDaysSet.has(item) })); }; var getDaysByStartEndArr = (arr = []) => { const daysArr = []; arr.forEach((item) => { if (item && item.start && item.end) { daysArr.push(...getDaysByStartEnd(item.start, item.end)); } }); return Array.from(new Set(daysArr)); }; var processDateRange = ({ repeat_type, start, end, color, endRadio, end_date, excluded_date, included_date, frequency, excludedStatus, includeStatus, frequency_date, type, time_slot }, isGetRange) => { let days = []; const excludedDays = getDaysByStartEndArr(excluded_date); const includedDays = getDaysByStartEndArr(included_date); if (repeat_type === "0") { if (isGetRange) { if (type === "time-slots") { days = (time_slot == null ? void 0 : time_slot.map((item) => ({ start: start.format(`YYYY-MM-DD ${item.start_time}`), end: end.format(`YYYY-MM-DD ${item.end_time}`) }))) || []; } else { days = [ { start: start.format("YYYY-MM-DD HH:mm:ss"), end: end.format("YYYY-MM-DD HH:mm:ss") } ]; } } else { days = getDaysByStartEnd(start, end); } } else { let endDate = (0, import_dayjs.default)(end_date); if (endRadio === 1) { endDate = isGetRange ? (0, import_dayjs.default)().add(12, "month") : (0, import_dayjs.default)().add(18, "month"); } if (repeat_type === "1") { days = getDaysByRepeatDay( { start, end, deadline: endDate, frequency, type, time_slot, excludedDays }, isGetRange ); } else if (repeat_type === "2") { days = getDaysByRepeatWeek( { start, end, deadline: endDate, frequency, frequency_date, type, time_slot, excludedDays }, isGetRange ); } } if (isGetRange) { if (includedDays.length) { for (let i = 0; i < includedDays.length; i++) { const includedDay = includedDays[i]; days.push( ...getDataRange({ start: (0, import_dayjs.default)(includedDay), end: (0, import_dayjs.default)(includedDay), type, time_slot }) ); } } return days; } if (includeStatus) { days = days.concat(includedDays); } return formatDayColor({ days, color, excludedDays: excludedStatus ? excludedDays : [] }); }; var calcCalendarDataByStandard = (values, others, isGetRange) => { const { color, excluded_date, included_date, end_date, designation, repeat_type, frequency = 1, frequency_date = [], time_slot, start_time, end_time } = values; const { endRadio, isAllDay: isAllDay2, includeStatus, excludedStatus } = others; if (Array.isArray(designation)) { return []; } const start = designation ? mergeDateTime( designation.start_date, isAllDay2 ? designation.start_time : "" ) : (0, import_dayjs.default)(start_time); const end = designation ? mergeDateTime(designation.end_date, isAllDay2 ? designation.end_time : "") : (0, import_dayjs.default)(end_time); return processDateRange( { repeat_type, start, end, color, endRadio, end_date, excluded_date, excludedStatus, frequency, frequency_date, included_date, includeStatus, time_slot, type: "standard" }, isGetRange ); }; var getMaxStart = (start_time) => { const valueStart = (0, import_dayjs.default)(start_time); const currentYearStart = (0, import_dayjs.default)().startOf("year").set("hour", valueStart.get("hour")).set("minute", valueStart.get("minutes")).set("second", valueStart.get("second")); const start = valueStart.isBefore(currentYearStart) ? currentYearStart : valueStart; return start; }; var calcCalendarDataByTimeSlots = (values, others, isGetRange) => { const { color, excluded_date, included_date, end_date, repeat_type, frequency = 1, frequency_date = [], start_time, time_slot } = values; const { endRadio, excludedStatus, includeStatus } = others; if (!start_time) return []; const start = (0, import_dayjs.default)(start_time); const end = (0, import_dayjs.default)(start_time); return processDateRange( { repeat_type, start, end, color, endRadio, end_date, excluded_date, excludedStatus, frequency, frequency_date, included_date, includeStatus, time_slot, type: "time-slots" }, isGetRange ); }; var calcCalendarDataByDesignation = (values, others, isGetRange) => { const { designation, color } = values; const days = []; if (Array.isArray(designation)) { designation.forEach((item) => { const start = mergeDateTime(item.start_date, item.start_time); const end = mergeDateTime(item.end_date, item.end_time); if (isGetRange) { days.push({ start: `${item.start_date} ${item.start_time}`, end: `${item.end_date} ${item.end_time}` }); } else { days.push(...getDaysByStartEnd(start, end)); } }); } if (isGetRange) { return days; } return formatDayColor({ days, color, excludedDays: [] }); }; var isAllDay = (value) => { return (0, import_dayjs.default)(value.start_time).format("HH:mm:ss") === "00:00:00" && (0, import_dayjs.default)(value.end_time).format("HH:mm:ss") === "23:59:59"; }; var repeatTypeMap = { none: "0", daily: "1", weekly: "2" }; var endTypeMap = { never: 1, date: 2 }; var calcCalendarDataByScheduleResult = (schedule) => { const { scheduleFormData, scheduleFormOtherValue } = formatScheduleResult(schedule); return calcCalendarDataBySchedule( scheduleFormData, scheduleFormOtherValue, schedule.type ); }; var formatScheduleResult = (schedule) => { const { color, name, repeat_type, designation, repeat_rule, start_time, end_time, is_all, type, time_slot } = schedule; const { frequency, end, frequency_date, included_date, excluded_date } = repeat_rule || {}; return { scheduleFormData: { color, name, repeat_type: repeatTypeMap[repeat_type], designation, frequency, frequency_date, end_time, start_time, excluded_date, included_date, time_slot, end_date: end == null ? void 0 : end.end_date }, scheduleFormOtherValue: { endRadio: endTypeMap[(end == null ? void 0 : end.type) || "never"], isAllDay: !!is_all, type, excludedStatus: !!(excluded_date == null ? void 0 : excluded_date.length), includeStatus: !!(included_date == null ? void 0 : included_date.length) } }; }; var calcScheduleDateRange = (schedule) => { const { scheduleFormData, scheduleFormOtherValue } = formatScheduleResult(schedule); if (schedule.type === "time-slots") { return calcCalendarDataByTimeSlots( scheduleFormData, scheduleFormOtherValue, true ); } if (schedule.type === "designation") { return calcCalendarDataByDesignation( scheduleFormData, scheduleFormOtherValue, true ); } return calcCalendarDataByStandard( scheduleFormData, scheduleFormOtherValue, true ); }; var calcCalendarDataBySchedules = (schedules) => { const allSchedules = []; schedules.forEach((item) => { allSchedules.push(...calcCalendarDataByScheduleResult(item)); }); const dateMap = allSchedules.reduce((pre, cur) => { var _a, _b; return { ...pre, [cur.date]: { ...cur, color: [...((_a = pre[cur.date]) == null ? void 0 : _a.color) || [], ...cur.color], isExcluded: ((_b = pre[cur.date]) == null ? void 0 : _b.isExcluded) || cur.isExcluded } }; }, {}); return Object.values(dateMap); }; var calcMinTimeMaxTimeBySchedules = (schedules, scheduleAllMap, selectDate) => { return schedules.reduce((pre, cur) => { var _a; let dateRange = ((_a = scheduleAllMap == null ? void 0 : scheduleAllMap[cur.id]) == null ? void 0 : _a.dateRange) || calcScheduleDateRange(cur); let dateRangeFormat = []; if (selectDate) { const dateRangeTmp = []; const dateRangeFormatTmp = []; for (let i = 0; i < dateRange.length; i++) { const range = dateRange[i]; const start = (0, import_dayjs.default)(range.start); const end = (0, import_dayjs.default)(range.end); const isSameStart = range.start.includes(selectDate); const isSameEnd = range.end.includes(selectDate); if (isSameStart || isSameEnd) { dateRangeTmp.push(range); if (isSameStart && !isSameEnd) { dateRangeFormatTmp.push({ start: range.start, end: end.format("YYYY-MM-DD 23:59:59") }); } else if (!isSameStart && isSameEnd) { dateRangeFormatTmp.push({ start: start.format("YYYY-MM-DD 00:00:00"), end: range.end }); } else { dateRangeFormatTmp.push(range); } } } dateRange = dateRangeTmp; dateRangeFormat = dateRangeFormatTmp; } let minTime = null; let maxTime = null; if (dateRange.length) { minTime = (0, import_dayjs.default)(dateRange[0].start); maxTime = (0, import_dayjs.default)(dateRange[dateRange.length - 1].end); } return { ...pre, [cur.id]: { minTime, maxTime, minTimeStr: (minTime == null ? void 0 : minTime.format("YYYY-MM-DD HH:mm:ss")) || null, maxTimeStr: (maxTime == null ? void 0 : maxTime.format("YYYY-MM-DD HH:mm:ss")) || null, dateRange, dateRangeFormat } }; }, {}); }; var formatScheduleData = (relation, table_type, item_type, isExcluded) => { const _relation = relation || []; if (table_type && item_type && isExcluded) { return _relation.find( (item) => (item == null ? void 0 : item.relation_table_type) === table_type && (item == null ? void 0 : item.item_type) !== item_type ); } if (table_type && item_type) { return _relation.find( (item) => (item == null ? void 0 : item.relation_table_type) === table_type && (item == null ? void 0 : item.item_type) === item_type ); } if (table_type) { return _relation.some( (item) => (item == null ? void 0 : item.relation_table_type) === table_type ); } if (item_type) { return _relation.find((item) => (item == null ? void 0 : item.item_type) === item_type); } }; function getAllSortedDateRanges(schedules) { const allDateRanges = []; Object.values(schedules).forEach((schedule) => { if (schedule.dateRange && Array.isArray(schedule.dateRange)) { allDateRanges.push(...schedule.dateRange); } }); if (allDateRanges.length === 0) { console.log("No dateRanges found in schedules:", schedules); return []; } let sortedDateRanges = allDateRanges.sort((a, b) => { const startTimeCompare = (0, import_dayjs.default)(a.start).valueOf() - (0, import_dayjs.default)(b.start).valueOf(); if (startTimeCompare !== 0) return startTimeCompare; const durationA = (0, import_dayjs.default)(a.end).valueOf() - (0, import_dayjs.default)(a.start).valueOf(); const durationB = (0, import_dayjs.default)(b.end).valueOf() - (0, import_dayjs.default)(b.start).valueOf(); return durationA - durationB; }); const uniqueDateRanges = sortedDateRanges.filter( (range, index, self) => index === self.findIndex((t) => (0, import_dayjs.default)(t.start).isSame((0, import_dayjs.default)(range.start)) && (0, import_dayjs.default)(t.end).isSame((0, import_dayjs.default)(range.end))) ); return uniqueDateRanges; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { calcCalendarDataByDesignation, calcCalendarDataBySchedule, calcCalendarDataByScheduleResult, calcCalendarDataBySchedules, calcCalendarDataByStandard, calcCalendarDataByTimeSlots, calcMinTimeMaxTimeBySchedules, calcScheduleDateRange, formatScheduleData, formatScheduleResult, getAllSortedDateRanges, getDaysByStartEnd, isAllDay });