UNPKG

dk-plus

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