UNPKG

@opensig/opendesign

Version:

127 lines (126 loc) 4.6 kB
import { computed, toValue } from "vue"; import { stringToDateTimeNumber, pad } from "../_utils/time.mjs"; function buildTimeStepOptions(limit, step) { const list = []; for (let i = 0; i < limit; i += step) { list.push({ label: pad(i), value: i }); } return list; } function parseTimeBound(time) { if (!time) return null; const parsed = stringToDateTimeNumber(time, { timeOnly: true }); if (!parsed || parsed.hour === null || parsed.minute === null || parsed.second === null) return null; return parsed; } function addBoundaryDisabled(disabled, options, bounds) { for (const opt of options) { if (bounds.min !== null && opt.value < bounds.min) disabled.add(opt.value); if (bounds.max !== null && opt.value > bounds.max) disabled.add(opt.value); } } function addCustomDisabled(disabled, options, customValues) { const validValues = new Set(options.map((o) => o.value)); for (const v of customValues) { if (validValues.has(v)) disabled.add(v); } } function computeDisabledUnit({ options, bounds, customDisabled }) { const disabled = /* @__PURE__ */ new Set(); addBoundaryDisabled(disabled, options, bounds); addCustomDisabled(disabled, options, customDisabled); return [...disabled]; } function resolveMinuteBounds(hour, min, max) { const atMin = min !== null && hour === min.hour; const atMax = max !== null && hour === max.hour; return { min: atMin ? min.minute : null, max: atMax ? max.minute : null }; } function resolveSecondBounds({ hour, minute, min, max }) { const atMin = min !== null && hour === min.hour && minute === min.minute; const atMax = max !== null && hour === max.hour && minute === max.minute; return { min: atMin ? min.second : null, max: atMax ? max.second : null }; } function useTimeStepOptions(hourStep, minuteStep, secondStep) { const hourOptions = computed(() => buildTimeStepOptions(24, toValue(hourStep) || 1)); const minuteOptions = computed(() => buildTimeStepOptions(60, toValue(minuteStep) || 1)); const secondOptions = computed(() => buildTimeStepOptions(60, toValue(secondStep) || 1)); return { hourOptions, minuteOptions, secondOptions }; } function useTimeDisabledOptions({ hourOptions, minuteOptions, secondOptions, parsedMinTime, parsedMaxTime, disabledHours, disabledMinutes, disabledSeconds, selectedHour, selectedMinute }) { const disabledHourOptions = computed(() => { var _a; const min = parsedMinTime.value; const max = parsedMaxTime.value; return computeDisabledUnit({ options: hourOptions.value, bounds: { min: min ? min.hour : null, max: max ? max.hour : null }, customDisabled: ((_a = toValue(disabledHours)) == null ? void 0 : _a()) ?? [] }); }); const disabledMinuteOptions = computed(() => { var _a; const hour = toValue(selectedHour); if (hour === null) return []; return computeDisabledUnit({ options: minuteOptions.value, bounds: resolveMinuteBounds(hour, parsedMinTime.value, parsedMaxTime.value), customDisabled: ((_a = toValue(disabledMinutes)) == null ? void 0 : _a(hour)) ?? [] }); }); const disabledSecondOptions = computed(() => { var _a; const hour = toValue(selectedHour); const minute = toValue(selectedMinute); if (hour === null || minute === null) return []; return computeDisabledUnit({ options: secondOptions.value, bounds: resolveSecondBounds({ hour, minute, min: parsedMinTime.value, max: parsedMaxTime.value }), customDisabled: ((_a = toValue(disabledSeconds)) == null ? void 0 : _a(hour, minute)) ?? [] }); }); return { disabledHourOptions, disabledMinuteOptions, disabledSecondOptions }; } const useTimePickerOptions = ({ hourStep, minuteStep, secondStep, disabledHours, disabledMinutes, disabledSeconds, minTime, maxTime, selectedHour, selectedMinute }) => { const parsedMinTime = computed(() => parseTimeBound(toValue(minTime))); const parsedMaxTime = computed(() => parseTimeBound(toValue(maxTime))); const { hourOptions, minuteOptions, secondOptions } = useTimeStepOptions(hourStep, minuteStep, secondStep); const { disabledHourOptions, disabledMinuteOptions, disabledSecondOptions } = useTimeDisabledOptions({ hourOptions, minuteOptions, secondOptions, parsedMinTime, parsedMaxTime, disabledHours, disabledMinutes, disabledSeconds, selectedHour, selectedMinute }); return { hourOptions, minuteOptions, secondOptions, disabledHourOptions, disabledMinuteOptions, disabledSecondOptions }; }; export { useTimePickerOptions };