UNPKG

dk-plus

Version:
154 lines (153 loc) 4.7 kB
import { defineComponent as E, ref as v, computed as u, watch as P, nextTick as q } from "vue"; const I = (o) => String(o).padStart(2, "0"), S = (o, a, l) => Math.max(a, Math.min(l, o)), y = (o) => Array.from({ length: o }, (a, l) => l), $ = (o) => { const a = o instanceof Date && !Number.isNaN(o.getTime()) ? o : new Date(); return new Date(a.getFullYear(), a.getMonth(), a.getDate(), a.getHours(), a.getMinutes(), a.getSeconds()); }, z = E({ name: "DkTimePickerPanel", props: { modelValue: { type: Date, default: void 0 }, disabled: { type: Boolean, default: !1 }, arrowControl: { type: Boolean, default: !1 }, showSeconds: { type: Boolean, default: !0 }, disabledHours: { type: Function, default: void 0 }, disabledMinutes: { type: Function, default: void 0 }, disabledSeconds: { type: Function, default: void 0 } }, emits: { "update:modelValue": (o) => o instanceof Date }, setup(o, { emit: a }) { const l = v(), M = v(), h = v(), k = u(() => y(24)), B = u(() => y(60)), N = u(() => y(60)), g = (e) => { const n = o.disabledHours, t = typeof n == "function" ? n() : []; return Array.isArray(t) ? t.includes(e) : !1; }, d = (e, n) => { const t = o.disabledMinutes, s = typeof t == "function" ? t(e) : []; return Array.isArray(s) ? s.includes(n) : !1; }, f = (e, n, t) => { const s = o.disabledSeconds, c = typeof s == "function" ? s(e, n) : []; return Array.isArray(c) ? c.includes(t) : !1; }, r = u(() => $(o.modelValue)), D = u(() => r.value.getHours()), w = u(() => r.value.getMinutes()), H = u(() => r.value.getSeconds()), x = (e, n) => { if (!d(e, n)) return n; for (let t = 0; t < 60; t++) { const s = (n + t) % 60; if (!d(e, s)) return s; } return n; }, b = (e, n, t) => { if (!f(e, n, t)) return t; for (let s = 0; s < 60; s++) { const c = (t + s) % 60; if (!f(e, n, c)) return c; } return t; }, m = (e, n, t) => { if (o.disabled || g(e) || d(e, n) || o.showSeconds && f(e, n, t)) return; const s = r.value, c = new Date(s.getFullYear(), s.getMonth(), s.getDate(), e, n, o.showSeconds ? t : 0); a("update:modelValue", c); }, A = (e) => { const n = r.value, t = S(e, 0, 23), s = x(t, n.getMinutes()), c = b(t, s, n.getSeconds()); m(t, s, c); }, T = (e) => { const n = r.value, t = n.getHours(), s = S(e, 0, 59), c = x(t, s), i = b(t, c, n.getSeconds()); m(t, c, i); }, V = (e) => { const n = r.value, t = n.getHours(), s = n.getMinutes(), c = S(e, 0, 59), i = b(t, s, c); m(t, s, i); }, C = (e) => { const n = r.value; for (let t = 1; t <= 24; t++) { const s = (n.getHours() + e * t + 24) % 24; if (!g(s)) { A(s); return; } } }, F = (e) => { const n = r.value, t = n.getHours(); for (let s = 1; s <= 60; s++) { const c = (n.getMinutes() + e * s + 60) % 60; if (!d(t, c)) { T(c); return; } } }, Y = (e) => { const n = r.value, t = n.getHours(), s = n.getMinutes(); for (let c = 1; c <= 60; c++) { const i = (n.getSeconds() + e * c + 60) % 60; if (!f(t, s, i)) { V(i); return; } } }, R = (e) => { o.disabled || o.arrowControl || (e.preventDefault(), C(e.deltaY > 0 ? 1 : -1)); }, W = (e) => { o.disabled || o.arrowControl || (e.preventDefault(), F(e.deltaY > 0 ? 1 : -1)); }, _ = (e) => { o.disabled || o.arrowControl || (e.preventDefault(), Y(e.deltaY > 0 ? 1 : -1)); }; return P(() => o.modelValue, async () => { await q(); const e = (n, t) => { if (!n) return; const s = n.querySelector(`[data-idx="${t}"]`); s == null || s.scrollIntoView({ block: "center" }); }; e(l.value, D.value), e(M.value, w.value), o.showSeconds && e(h.value, H.value); }, { immediate: !0 }), { pad2: I, hours: k, minutes: B, seconds: N, selectedHour: D, selectedMinute: w, selectedSecond: H, hoursRef: l, minutesRef: M, secondsRef: h, isHourDisabled: g, isMinuteDisabled: d, isSecondDisabled: f, setHour: A, setMinute: T, setSecond: V, stepHour: C, stepMinute: F, stepSecond: Y, onWheelHours: R, onWheelMinutes: W, onWheelSeconds: _ }; } }); export { z as default };