scanfile/server/web/assets/js/datepicker.min.js

1 line
2.9 KiB
JavaScript

(()=>{(function(){function D(e){if(!e||typeof e!="string")return null;let a=e.match(/^(\d{4})-(\d{2})-(\d{2})$/);if(!a)return null;let t=parseInt(a[1],10),n=parseInt(a[2],10)-1,i=parseInt(a[3],10),l=new Date(Date.UTC(t,n,i));return l.getUTCFullYear()===t&&l.getUTCMonth()===n&&l.getUTCDate()===i?l:null}function f(e,a,t){if(!e||isNaN(e.getTime()))return"";let n={timeZone:"UTC"};switch(a){case"locale-short":n.dateStyle="short";break;case"locale-long":n.dateStyle="long";break;case"locale-full":n.dateStyle="full";break;case"locale-medium":default:n.dateStyle="medium";break}try{return new Intl.DateTimeFormat(t,n).format(e)}catch(i){console.error(`Error formatting date with Intl (locale: ${t}, format: ${a}, timezone: UTC):`,i);try{let l={dateStyle:"medium",timeZone:"UTC"};return new Intl.DateTimeFormat(t,l).format(e)}catch(l){console.error(`Error formatting date with fallback Intl (locale: ${t}, timezone: UTC):`,l);let s=e.getUTCFullYear(),o=(e.getUTCMonth()+1).toString().padStart(2,"0"),r=e.getUTCDate().toString().padStart(2,"0");return`${s}-${o}-${r}`}}}function p(e){if(!e||e.hasAttribute("data-initialized"))return;e.setAttribute("data-initialized","true");let a=e.id,t=e.querySelector("[data-datepicker-display]"),n=a+"-calendar-instance",i=document.getElementById(n),l=n+"-hidden",s=document.getElementById(l),o=i,r=s;if(!i||!s){let d=e.getAttribute("aria-controls"),c=d?document.getElementById(d):null;if(c&&(o||(o=c.querySelector("[data-calendar-container]")),!r)){let u=c.querySelector("[data-calendar-wrapper]");r=u?u.querySelector("[data-calendar-hidden-input]"):null}}if(!t||!o||!r){console.error("DatePicker init error: Missing required elements.",{datePickerID:a,displaySpan:t,calendar:o,hiddenInput:r});return}let I=e.dataset.displayFormat||"locale-medium",C=e.dataset.localeTag||"en-US",m=e.dataset.placeholder||"Select a date",h=d=>{if(!d.detail||!d.detail.date||!(d.detail.date instanceof Date))return;let c=d.detail.date,u=f(c,I,C);t.textContent=u,t.classList.remove("text-muted-foreground");let S=e.closest("[data-popover]")?.querySelector("[data-popover-trigger]");S instanceof HTMLElement?S.click():e.click()},T=()=>{if(r&&r.value){let d=D(r.value);if(d){let c=f(d,I,C);t.textContent.trim()!==c&&(t.textContent=c,t.classList.remove("text-muted-foreground"))}else t.textContent=m,t.classList.add("text-muted-foreground")}else t.textContent=m,t.classList.add("text-muted-foreground")};o.addEventListener("calendar-date-selected",h),T(),e._datePickerInitialized=!0,e._datePickerCleanup=()=>{o&&o.removeEventListener("calendar-date-selected",h)};let k=e.closest("form");k&&k.addEventListener("reset",()=>{r&&(r.value=""),t.textContent=m,t.classList.add("text-muted-foreground")})}function y(e=document){e instanceof Element&&e.matches('[data-datepicker="true"]')&&p(e),e.querySelectorAll('[data-datepicker="true"]:not([data-initialized])').forEach(a=>{p(a)})}window.templUI=window.templUI||{},window.templUI.datePicker={init:y},document.addEventListener("DOMContentLoaded",()=>y())})();})();