@maihcx/super-date 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
package/dist/super-date.esm.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*! SuperDate v0.3.
|
|
1
|
+
/*! SuperDate v0.3.1 | MIT License */
|
|
2
2
|
/**
|
|
3
3
|
* format.ts — format string parsing, token metadata, and date/time read/write helpers.
|
|
4
4
|
*/
|
|
@@ -1076,7 +1076,7 @@ class SuperDateRegistry {
|
|
|
1076
1076
|
*/
|
|
1077
1077
|
/** Singleton registry — the default export used in most projects. */
|
|
1078
1078
|
const SuperDate = new SuperDateRegistry();
|
|
1079
|
-
SuperDate.version = "0.3.
|
|
1079
|
+
SuperDate.version = "0.3.1";
|
|
1080
1080
|
SuperDate.name = "SuperDate";
|
|
1081
1081
|
|
|
1082
1082
|
export { SuperDate as default };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
/*! SuperDate v0.3.
|
|
2
|
-
function t(t){const e=t.type;return"time"===e||"datetime-local"===e?e:"date"}function e(t){v.lastIndex=0;const e=[];let s,i=0;for(;null!==(s=v.exec(t));){if(s.index>i){const n=t.slice(i,s.index);e.push({token:null,text:n,start:i,end:s.index})}e.push({token:s[0],text:s[0],start:s.index,end:s.index+s[0].length}),i=s.index+s[0].length}if(i<t.length){const s=t.slice(i);e.push({token:null,text:s,start:i,end:t.length})}return e}function s(t){return H[t].t}function i(t){return H[t].maxValue}function n(t){return H[t].minValue}function h(t){return H[t].placeholder}function r(t,e){if(!e)return"";switch(t){case"dd":return y(e.getDate(),2);case"d":return e.getDate()+"";case"MM":return y(e.getMonth()+1,2);case"M":return e.getMonth()+1+"";case"yyyy":return y(e.getFullYear(),4);case"yy":return y(e.getFullYear()%100,2);case"HH":return y(e.getHours(),2);case"H":return e.getHours()+"";case"hh":return y(f(e.getHours()),2);case"h":return f(e.getHours())+"";case"mm":return y(e.getMinutes(),2);case"ss":return y(e.getSeconds(),2)}}function a(t,e){switch(t){case"dd":case"d":return e.getDate();case"MM":case"M":return e.getMonth()+1;case"yyyy":return e.getFullYear();case"yy":return e.getFullYear()%100;case"HH":case"H":return e.getHours();case"hh":case"h":return f(e.getHours());case"mm":return e.getMinutes();case"ss":return e.getSeconds()}}function o(e){if(!e.value)return null;const s=t(e);return"date"===s?u(e.value):"time"===s?(t=>{const e=t.split(":");if(e.length<2)return null;const[s,i,n=0]=e.map(Number);if(isNaN(s)||isNaN(i))return null;const h=new Date;return new Date(h.getFullYear(),h.getMonth(),h.getDate(),s,i,n)})(e.value):(t=>{const e=t.indexOf("T");if(-1===e)return null;const s=u(t.slice(0,e));if(!s)return null;const i=t.slice(e+1).split(":").map(Number),[n=0,h=0,r=0]=i;return new Date(s.getFullYear(),s.getMonth(),s.getDate(),n,h,r)})(e.value)}function c(e,s){const i=t(e);e.value="date"===i?l(s):"time"===i?d(s):(t=>`${l(t)}T${d(t)}`)(s),e.dispatchEvent(new Event("input",{bubbles:!0})),e.dispatchEvent(new Event("change",{bubbles:!0}))}function u(t){const e=t.split("-");if(3!==e.length)return null;const[s,i,n]=e.map(Number);return isNaN(s)||isNaN(i)||isNaN(n)?null:new Date(s,i-1,n)}function l(t){return`${t.getFullYear()}-${y(t.getMonth()+1,2)}-${y(t.getDate(),2)}`}function d(t){return`${y(t.getHours(),2)}:${y(t.getMinutes(),2)}:${y(t.getSeconds(),2)}`}function m(t,i,n){const h=e(i);let r=0;const a=new Date;let o=a.getDate(),c=a.getMonth()+1,u=a.getFullYear(),l=0,d=0,m=0;for(const e of h){if(r>=t.length)break;if(!e.token){t.slice(r,r+e.text.length)===e.text&&(r+=e.text.length);continue}const i=s(e.token);let n="";for(let e=0;e<i&&r<t.length&&/\d/.test(t[r]);e++,r++)n+=t[r];if(!n)return null;const h=parseInt(n,10);switch(e.token){case"dd":case"d":o=h;break;case"MM":case"M":c=h;break;case"yyyy":u=h;break;case"yy":u=2e3+h;break;case"HH":case"H":l=h;break;case"hh":case"h":l=p(h,l);break;case"mm":d=h;break;case"ss":m=h}}return"date"!==n&&"datetime-local"!==n||!(c<1||c>12||o<1||o>31)?"time"===n?l<0||l>23||d<0||d>59?null:new Date(a.getFullYear(),a.getMonth(),a.getDate(),l,d,m):new Date(u,c-1,o,l,d,m):null}function y(t,e){return(t+"").padStart(e,"0")}function f(t){return 0===t?12:t>12?t-12:t}function p(t,e){const s=e>=12;return 12===t?s?12:0:s?t+12:t}function b(t){t.forEach(t=>t.classList.remove("active"))}function x(t={}){return{format:t.format??"dd/MM/yyyy",timeFormat:t.timeFormat??"HH:mm",dateTimeDelimiter:t.dateTimeDelimiter??" ",locale:t.locale??("undefined"!=typeof navigator?navigator.language:"en")}}function M(t,e){t[$]=new N(t,e.format,e.timeFormat,e.dateTimeDelimiter)}function k(t){return I.has(t.type)&&!t[$]&&!(t=>t.hasAttribute(E))(t)}function w(t){for(const e of T)t instanceof HTMLInputElement&&t.matches(e.selector)&&k(t)&&M(t,e.options),t.querySelectorAll(e.selector).forEach(t=>{k(t)&&M(t,e.options)})}const v=/yyyy|yy|YYYY|YY|MM|M|HH|H|hh|h|mm|ss|dd|DD|d|D/g,H={i:{t:2,maxValue:31,minValue:1,placeholder:"dd"},d:{t:2,maxValue:31,minValue:1,placeholder:"d"},h:{t:2,maxValue:12,minValue:1,placeholder:"mm"},o:{t:2,maxValue:12,minValue:1,placeholder:"m"},u:{t:4,maxValue:9999,minValue:1,placeholder:"yyyy"},l:{t:2,maxValue:99,minValue:0,placeholder:"yy"},m:{t:2,maxValue:23,minValue:0,placeholder:"HH"},p:{t:2,maxValue:23,minValue:0,placeholder:"H"},M:{t:2,maxValue:12,minValue:1,placeholder:"hh"},k:{t:2,maxValue:12,minValue:1,placeholder:"h"},mm:{t:2,maxValue:59,minValue:0,placeholder:"mm"},v:{t:2,maxValue:59,minValue:0,placeholder:"ss"}},D=["yyyy-MM-dd","dd/MM/yyyy","MM/dd/yyyy","d.M.yyyy","MM-dd-yyyy"],g=["HH:mm:ss","HH:mm","H:mm"],V=["yyyy-MM-dd HH:mm:ss","yyyy-MM-dd HH:mm","dd/MM/yyyy HH:mm"],$="__superdate__";class N{constructor(s,i,n="HH:mm",h=" "){this.H=-1,this.D="",this.V=-1,this.$=-1,this.N=!1,this.S=!1,this.input=s,this.kind=t(s);const r=s.dataset.dateFormat??i,a=s.dataset.timeFormat??n;this.format="datetime-local"===this.kind?((t,e,s)=>`${t}${s}${e}`)(r,a,s.dataset.dateTimeDelimiter??h):"time"===this.kind?a:r,this.segments=e(this.format);const o=((t,e,s,i,n="date")=>{const h=document.createElement("div");h.className="superdate-wrapper",h.style.width=t.style.width||"",t.parentNode.insertBefore(h,t),h.appendChild(t),t.classList.add("superdate-input"),t.style.display="block",t.style.boxSizing="border-box";const r=document.createElement("div");r.className="superdate-overlay";const a=[];e.forEach((t,e)=>{const i=document.createElement("span");i.className="superdate-seg",t.token&&(i.dataset.token=t.token,i.dataset.idx=e+"",i.setAttribute("tabindex","-1"),i.addEventListener("click",t=>{t.stopPropagation(),s(e)})),r.appendChild(i),a.push(i)});const o=document.createElement("span");return o.className="superdate-icon",o.innerHTML="time"===n?'<svg width="16" height="16" viewBox="0 0 24 24" fill="none"\n stroke="currentColor" stroke-width="2" stroke-linecap="round"\n stroke-linejoin="round" aria-hidden="true">\n <circle cx="12" cy="12" r="10"/>\n <polyline points="12 6 12 12 16 14"/>\n</svg>':'<svg width="16" height="16" viewBox="0 0 24 24" fill="none"\n stroke="currentColor" stroke-width="2" stroke-linecap="round"\n stroke-linejoin="round" aria-hidden="true">\n <rect x="3" y="4" width="18" height="18" rx="2"/>\n <line x1="16" y1="2" x2="16" y2="6"/>\n <line x1="8" y1="2" x2="8" y2="6"/>\n <line x1="3" y1="10" x2="21" y2="10"/>\n</svg>',o.addEventListener("click",t=>{t.stopPropagation(),i()}),r.appendChild(o),h.appendChild(r),{T:h,I:r,A:a}})(s,this.segments,t=>this.B(t),()=>this.input.showPicker?.(),this.kind);this.T=o.T,this.I=o.I,this.A=o.A,this.Y(),this.C=t=>this.F(t),this._=t=>this.L(t),this.j=()=>this.Y(),this.O=t=>this.U(t),this.W=t=>this.K(t),this.P=t=>this.R(t),this.q=t=>this.G(t),this.J=t=>this.X(t),this.Z=t=>this.tt(t),this.et=t=>this.st(t),this.it=new MutationObserver(()=>{this.S||this.Y()}),this.nt()}Y(){var t,e,s;t=this.segments,e=this.A,s=o(this.input),t.forEach((t,i)=>{const n=e[i];if(!t.token)return void(n.textContent=t.text);const a=r(t.token,s);a?(n.textContent=a,n.classList.remove("empty")):(n.textContent=h(t.token),n.classList.add("empty"))})}ht(){return this.segments.findIndex(t=>null!==t.token)}rt(){for(let t=this.segments.length-1;t>=0;t--)if(this.segments[t].token)return t;return-1}ot(t,e=1){let s=t+e;for(;s>=0&&s<this.segments.length;){if(this.segments[s].token)return s;s+=e}return-1}ct(t){const e=this.segments[t].token;if(!e)return;let i=this.ut(t);const n=s(e),h=y(a(e,new Date),n);"0"===i?(this.D="",this.lt(e,h,!0)):""!==i&&(n<=2?i.length<n&&(i=i.padStart(n,"0"),this.D="",this.lt(e,i,!0)):(this.D="",i=`${h.substring(0,n-i.length)}${i}`,this.lt(e,i,!0)))}B(t){t<0||t>=this.segments.length||this.segments[t].token&&(this.dt(),this.D="",this.H=t,((t,e)=>{this.A.forEach((t,s)=>t.classList.toggle("active",s===e))})(0,t),this.input.focus({yt:!0}))}ft(){if(-1!==this.H){const t=this.segments[this.H].token;if(t){this.ct(this.H);const e=o(this.input);e&&a(t,e)<n(t)&&this.bt(t,n(t))}}this.H=-1,this.D="",this.dt(),b(this.A)}xt(){return-1!==this.V}Mt(t,e){const s=Math.min(t,e),i=Math.max(t,e);this.A.forEach((t,e)=>{t.classList.toggle("active",null!==this.segments[e].token&&e>=s&&e<=i)})}kt(t){this.$=t,this.Mt(this.V,this.$)}wt(){const t=this.ht(),e=this.rt();-1!==t&&(this.H=-1,this.V=t,this.$=e,this.Mt(t,e))}dt(){-1!==this.H&&this.ct(this.H),this.V=-1,this.$=-1,b(this.A)}vt(){const t=o(this.input);if(!t||!this.xt())return;const e=Math.min(this.V,this.$),s=Math.max(this.V,this.$);let i="";for(let n=e;n<=s;n++){const e=this.segments[n];e&&(i+=e.token?r(e.token,t):e.text)}navigator.clipboard.writeText(i).catch(()=>{const t=document.createElement("textarea");t.value=i,t.style.cssText="position:fixed;opacity:0;pointer-events:none",document.body.appendChild(t),t.select(),document.execCommand("copy"),document.body.removeChild(t)})}Ht(){this.segments.every((t,e)=>!t.token||""===this.ut(e))&&(this.input.value="",this.input.dispatchEvent(new Event("input",{bubbles:!0})),this.input.dispatchEvent(new Event("change",{bubbles:!0})),this.Y(),this.H=-1)}Dt(t){const e=t.target.closest("[data-idx]");if(!e)return-1;const s=parseInt(e.dataset.idx??"-1",10);return this.segments[s]?.token?s:-1}X(t){if(t.detail>=2)return;const e=this.Dt(t);-1!==e&&(this.V=e,this.$=e)}tt(t){if(-1===this.V||!(1&t.buttons))return;const e=this.Dt(t);-1!==e&&e!==this.$&&(-1!==this.H&&(this.H=-1,this.D=""),this.kt(e))}st(t){if(-1!==this.V&&this.V===this.$){const t=this.V;this.V=-1,this.$=-1,this.B(t)}else this.V!==this.$&&(this.N=!0,this.input.focus({yt:!0}))}G(t){t.preventDefault(),this.H=-1,this.D="",this.wt(),this.input.focus({yt:!0})}U(t){const e=t.relatedTarget;e&&this.T.contains(e)||this.ft()}K(t){this.B(0)}R(t){if(this.N)return void(this.N=!1);const e=t.target;e!==this.T&&e!==this.input&&e!==this.I||(this.dt(),this.B(this.ht()))}F(t){const e=t.ctrlKey||t.metaKey;if(e&&"a"===t.key)return t.preventDefault(),void this.wt();if(e&&"c"===t.key)return void(this.xt()&&(t.preventDefault(),this.vt()));if("Escape"===t.key)return void(this.xt()?this.dt():this.ft());if("Backspace"===t.key||"Delete"===t.key){t.preventDefault(),this.D="";let e=-1;if(this.xt()){e=this.V;for(let t=e;t<=this.$;t++)this.segments[t]?.token&&this.gt(t);this.dt()}else-1!==this.H&&(""===this.ut(this.H)&&(e=this.H),this.gt());if(e>0){let t=e-1;for(;t>=0;){if(this.segments[t]?.token){this.B(t);break}t--}}else 0==e&&this.B(e);return void this.Ht()}if(-1===this.H)return;const s=this.segments[this.H].token;switch(t.key){case"Tab":this.Vt(t);break;case"ArrowRight":{t.preventDefault();const e=this.ot(this.H,1);-1!==e&&this.B(e);break}case"ArrowLeft":{t.preventDefault();const e=this.ot(this.H,-1);-1!==e&&this.B(e);break}case"ArrowUp":t.preventDefault(),this.$t(s,1);break;case"ArrowDown":t.preventDefault(),this.$t(s,-1);break;default:/^\d$/.test(t.key)&&(t.preventDefault(),this.lt(s,t.key))}}Vt(t){const e=this.ot(this.H,t.shiftKey?-1:1);-1!==e?(t.preventDefault(),this.B(e)):this.ft()}lt(t,e,h=!1){this.D+=e;const r=parseInt(this.D,10),a=i(t),o=s(t);if(this.D.length>=o){const e=n(t);if(this.bt(t,Math.max(e,Math.min(a,r))),this.D="",!h){const t=this.ot(this.H,1);-1!==t&&setTimeout(()=>this.B(t),0)}}else this.gt()}gt(t=this.H){const e=this.A[t],s=this.segments[t].token;this.D?(e.textContent=this.D,e.classList.remove("empty")):(e.textContent=h(s),e.classList.add("empty"))}ut(t){if(this.D)return this.D;const e=this.A[t];return e.classList.contains("empty")?"":e.textContent??""}$t(t,e){const s=a(t,o(this.input)??new Date),h=n(t),r=i(t);let c=s+e;c<h&&(c=r),c>r&&(c=h),this.bt(t,c)}bt(t,e){const s=((t,e,s)=>{const i=t??new Date;let n=i.getFullYear(),h=i.getMonth()+1,r=i.getDate(),a=i.getHours(),o=i.getMinutes(),c=i.getSeconds();switch(e){case"dd":case"d":r=s;break;case"MM":case"M":h=s;break;case"yyyy":n=s;break;case"yy":n=2e3+s;break;case"HH":case"H":a=s;break;case"hh":case"h":a=p(s,a);break;case"mm":o=s;break;case"ss":c=s}const u=new Date(n,h,0).getDate();return r=Math.max(1,Math.min(r,u)),new Date(n,h-1,r,a,o,c)})(o(this.input),t,e);c(this.input,s),this.Y()}L(t){t.preventDefault();const e=((t,e,s="date")=>{const i=[e,...("time"===s?g:"datetime-local"===s?V:D).filter(t=>t!==e)];for(const e of i){const i=m(t,e,s);if(i)return i}return null})((t.clipboardData?.getData("text/plain")??"").trim(),this.format,this.kind);e&&(c(this.input,e),this.Y())}nt(){this.input.addEventListener("keydown",this.C),this.input.addEventListener("paste",this._),this.input.addEventListener("change",this.j),this.input.addEventListener("blur",this.O),this.input.addEventListener("focus",this.W),this.T.addEventListener("click",this.P),this.T.addEventListener("dblclick",this.q),this.I.addEventListener("mousedown",this.J),this.I.addEventListener("mousemove",this.Z),this.I.addEventListener("mouseup",this.et),this.it.observe(this.input,{attributes:!0,attributeFilter:["value","min","max"]})}update(){this.Y()}destroy(){this.S||(this.S=!0,this.it.disconnect(),this.input.removeEventListener("keydown",this.C),this.input.removeEventListener("paste",this._),this.input.removeEventListener("change",this.j),this.input.removeEventListener("blur",this.O),this.T.removeEventListener("click",this.P),this.T.removeEventListener("dblclick",this.q),this.I.removeEventListener("mousedown",this.J),this.I.removeEventListener("mousemove",this.Z),this.I.removeEventListener("mouseup",this.et),this.input.classList.remove("superdate-input"),this.input.style.display="",this.input.style.width="",this.input.style.color="",this.T.replaceWith(this.input))}}let S=null,T=[];const E="data-superdate-destroyed",I=new Set(["date","time","datetime-local"]),SuperDate=new class{constructor(){this.version="",this.name=""}bind(t,e={}){const s=x(e);return T.push({selector:t,options:s}),(t=>{document.querySelectorAll(t).forEach(t=>{t.removeAttribute(E)})})(t),((t,e)=>{document.querySelectorAll(t).forEach(t=>{k(t)&&M(t,e)})})(t,s),S||(S=new MutationObserver(t=>{for(const e of t)for(const t of Array.from(e.addedNodes))t instanceof Element&&w(t)}),S.observe(document.body,{childList:!0,subtree:!0})),this}init(t,e={}){if(t[$])return t[$];t.removeAttribute(E);const s=x(e),i=new N(t,s.format,s.timeFormat,s.dateTimeDelimiter);return t[$]=i,i}destroy(t){t[$]&&(t[$].destroy(),delete t[$]),(t=>{t.setAttribute(E,"")})(t)}};SuperDate.version="0.3.0",SuperDate.name="SuperDate";export{SuperDate as default};
|
|
1
|
+
/*! SuperDate v0.3.1 | MIT License */
|
|
2
|
+
function t(t){const e=t.type;return"time"===e||"datetime-local"===e?e:"date"}function e(t){v.lastIndex=0;const e=[];let s,i=0;for(;null!==(s=v.exec(t));){if(s.index>i){const n=t.slice(i,s.index);e.push({token:null,text:n,start:i,end:s.index})}e.push({token:s[0],text:s[0],start:s.index,end:s.index+s[0].length}),i=s.index+s[0].length}if(i<t.length){const s=t.slice(i);e.push({token:null,text:s,start:i,end:t.length})}return e}function s(t){return H[t].t}function i(t){return H[t].maxValue}function n(t){return H[t].minValue}function h(t){return H[t].placeholder}function r(t,e){if(!e)return"";switch(t){case"dd":return y(e.getDate(),2);case"d":return e.getDate()+"";case"MM":return y(e.getMonth()+1,2);case"M":return e.getMonth()+1+"";case"yyyy":return y(e.getFullYear(),4);case"yy":return y(e.getFullYear()%100,2);case"HH":return y(e.getHours(),2);case"H":return e.getHours()+"";case"hh":return y(f(e.getHours()),2);case"h":return f(e.getHours())+"";case"mm":return y(e.getMinutes(),2);case"ss":return y(e.getSeconds(),2)}}function a(t,e){switch(t){case"dd":case"d":return e.getDate();case"MM":case"M":return e.getMonth()+1;case"yyyy":return e.getFullYear();case"yy":return e.getFullYear()%100;case"HH":case"H":return e.getHours();case"hh":case"h":return f(e.getHours());case"mm":return e.getMinutes();case"ss":return e.getSeconds()}}function o(e){if(!e.value)return null;const s=t(e);return"date"===s?u(e.value):"time"===s?(t=>{const e=t.split(":");if(e.length<2)return null;const[s,i,n=0]=e.map(Number);if(isNaN(s)||isNaN(i))return null;const h=new Date;return new Date(h.getFullYear(),h.getMonth(),h.getDate(),s,i,n)})(e.value):(t=>{const e=t.indexOf("T");if(-1===e)return null;const s=u(t.slice(0,e));if(!s)return null;const i=t.slice(e+1).split(":").map(Number),[n=0,h=0,r=0]=i;return new Date(s.getFullYear(),s.getMonth(),s.getDate(),n,h,r)})(e.value)}function c(e,s){const i=t(e);e.value="date"===i?l(s):"time"===i?d(s):(t=>`${l(t)}T${d(t)}`)(s),e.dispatchEvent(new Event("input",{bubbles:!0})),e.dispatchEvent(new Event("change",{bubbles:!0}))}function u(t){const e=t.split("-");if(3!==e.length)return null;const[s,i,n]=e.map(Number);return isNaN(s)||isNaN(i)||isNaN(n)?null:new Date(s,i-1,n)}function l(t){return`${t.getFullYear()}-${y(t.getMonth()+1,2)}-${y(t.getDate(),2)}`}function d(t){return`${y(t.getHours(),2)}:${y(t.getMinutes(),2)}:${y(t.getSeconds(),2)}`}function m(t,i,n){const h=e(i);let r=0;const a=new Date;let o=a.getDate(),c=a.getMonth()+1,u=a.getFullYear(),l=0,d=0,m=0;for(const e of h){if(r>=t.length)break;if(!e.token){t.slice(r,r+e.text.length)===e.text&&(r+=e.text.length);continue}const i=s(e.token);let n="";for(let e=0;e<i&&r<t.length&&/\d/.test(t[r]);e++,r++)n+=t[r];if(!n)return null;const h=parseInt(n,10);switch(e.token){case"dd":case"d":o=h;break;case"MM":case"M":c=h;break;case"yyyy":u=h;break;case"yy":u=2e3+h;break;case"HH":case"H":l=h;break;case"hh":case"h":l=p(h,l);break;case"mm":d=h;break;case"ss":m=h}}return"date"!==n&&"datetime-local"!==n||!(c<1||c>12||o<1||o>31)?"time"===n?l<0||l>23||d<0||d>59?null:new Date(a.getFullYear(),a.getMonth(),a.getDate(),l,d,m):new Date(u,c-1,o,l,d,m):null}function y(t,e){return(t+"").padStart(e,"0")}function f(t){return 0===t?12:t>12?t-12:t}function p(t,e){const s=e>=12;return 12===t?s?12:0:s?t+12:t}function b(t){t.forEach(t=>t.classList.remove("active"))}function x(t={}){return{format:t.format??"dd/MM/yyyy",timeFormat:t.timeFormat??"HH:mm",dateTimeDelimiter:t.dateTimeDelimiter??" ",locale:t.locale??("undefined"!=typeof navigator?navigator.language:"en")}}function M(t,e){t[$]=new N(t,e.format,e.timeFormat,e.dateTimeDelimiter)}function k(t){return I.has(t.type)&&!t[$]&&!(t=>t.hasAttribute(E))(t)}function w(t){for(const e of T)t instanceof HTMLInputElement&&t.matches(e.selector)&&k(t)&&M(t,e.options),t.querySelectorAll(e.selector).forEach(t=>{k(t)&&M(t,e.options)})}const v=/yyyy|yy|YYYY|YY|MM|M|HH|H|hh|h|mm|ss|dd|DD|d|D/g,H={dd:{t:2,maxValue:31,minValue:1,placeholder:"dd"},d:{t:2,maxValue:31,minValue:1,placeholder:"d"},MM:{t:2,maxValue:12,minValue:1,placeholder:"mm"},M:{t:2,maxValue:12,minValue:1,placeholder:"m"},yyyy:{t:4,maxValue:9999,minValue:1,placeholder:"yyyy"},yy:{t:2,maxValue:99,minValue:0,placeholder:"yy"},HH:{t:2,maxValue:23,minValue:0,placeholder:"HH"},H:{t:2,maxValue:23,minValue:0,placeholder:"H"},hh:{t:2,maxValue:12,minValue:1,placeholder:"hh"},h:{t:2,maxValue:12,minValue:1,placeholder:"h"},mm:{t:2,maxValue:59,minValue:0,placeholder:"mm"},ss:{t:2,maxValue:59,minValue:0,placeholder:"ss"}},D=["yyyy-MM-dd","dd/MM/yyyy","MM/dd/yyyy","d.M.yyyy","MM-dd-yyyy"],g=["HH:mm:ss","HH:mm","H:mm"],V=["yyyy-MM-dd HH:mm:ss","yyyy-MM-dd HH:mm","dd/MM/yyyy HH:mm"],$="__superdate__";class N{constructor(s,i,n="HH:mm",h=" "){this.i=-1,this.o="",this.u=-1,this.l=-1,this.m=!1,this.p=!1,this.input=s,this.kind=t(s);const r=s.dataset.dateFormat??i,a=s.dataset.timeFormat??n;this.format="datetime-local"===this.kind?((t,e,s)=>`${t}${s}${e}`)(r,a,s.dataset.dateTimeDelimiter??h):"time"===this.kind?a:r,this.segments=e(this.format);const o=((t,e,s,i,n="date")=>{const h=document.createElement("div");h.className="superdate-wrapper",h.style.width=t.style.width||"",t.parentNode.insertBefore(h,t),h.appendChild(t),t.classList.add("superdate-input"),t.style.display="block",t.style.boxSizing="border-box";const r=document.createElement("div");r.className="superdate-overlay";const a=[];e.forEach((t,e)=>{const i=document.createElement("span");i.className="superdate-seg",t.token&&(i.dataset.token=t.token,i.dataset.idx=e+"",i.setAttribute("tabindex","-1"),i.addEventListener("click",t=>{t.stopPropagation(),s(e)})),r.appendChild(i),a.push(i)});const o=document.createElement("span");return o.className="superdate-icon",o.innerHTML="time"===n?'<svg width="16" height="16" viewBox="0 0 24 24" fill="none"\n stroke="currentColor" stroke-width="2" stroke-linecap="round"\n stroke-linejoin="round" aria-hidden="true">\n <circle cx="12" cy="12" r="10"/>\n <polyline points="12 6 12 12 16 14"/>\n</svg>':'<svg width="16" height="16" viewBox="0 0 24 24" fill="none"\n stroke="currentColor" stroke-width="2" stroke-linecap="round"\n stroke-linejoin="round" aria-hidden="true">\n <rect x="3" y="4" width="18" height="18" rx="2"/>\n <line x1="16" y1="2" x2="16" y2="6"/>\n <line x1="8" y1="2" x2="8" y2="6"/>\n <line x1="3" y1="10" x2="21" y2="10"/>\n</svg>',o.addEventListener("click",t=>{t.stopPropagation(),i()}),r.appendChild(o),h.appendChild(r),{k:h,v:r,D:a}})(s,this.segments,t=>this.V(t),()=>this.input.showPicker?.(),this.kind);this.k=o.k,this.v=o.v,this.D=o.D,this.$(),this.N=t=>this.S(t),this.T=t=>this.I(t),this.A=()=>this.$(),this.B=t=>this.Y(t),this.C=t=>this.F(t),this._=t=>this.L(t),this.j=t=>this.O(t),this.U=t=>this.W(t),this.K=t=>this.P(t),this.R=t=>this.q(t),this.G=new MutationObserver(()=>{this.p||this.$()}),this.J()}$(){var t,e,s;t=this.segments,e=this.D,s=o(this.input),t.forEach((t,i)=>{const n=e[i];if(!t.token)return void(n.textContent=t.text);const a=r(t.token,s);a?(n.textContent=a,n.classList.remove("empty")):(n.textContent=h(t.token),n.classList.add("empty"))})}X(){return this.segments.findIndex(t=>null!==t.token)}Z(){for(let t=this.segments.length-1;t>=0;t--)if(this.segments[t].token)return t;return-1}tt(t,e=1){let s=t+e;for(;s>=0&&s<this.segments.length;){if(this.segments[s].token)return s;s+=e}return-1}et(t){const e=this.segments[t].token;if(!e)return;let i=this.st(t);const n=s(e),h=y(a(e,new Date),n);"0"===i?(this.o="",this.it(e,h,!0)):""!==i&&(n<=2?i.length<n&&(i=i.padStart(n,"0"),this.o="",this.it(e,i,!0)):(this.o="",i=`${h.substring(0,n-i.length)}${i}`,this.it(e,i,!0)))}V(t){t<0||t>=this.segments.length||this.segments[t].token&&(this.nt(),this.o="",this.i=t,((t,e)=>{this.D.forEach((t,s)=>t.classList.toggle("active",s===e))})(0,t),this.input.focus({ht:!0}))}rt(){if(-1!==this.i){const t=this.segments[this.i].token;if(t){this.et(this.i);const e=o(this.input);e&&a(t,e)<n(t)&&this.ot(t,n(t))}}this.i=-1,this.o="",this.nt(),b(this.D)}ct(){return-1!==this.u}ut(t,e){const s=Math.min(t,e),i=Math.max(t,e);this.D.forEach((t,e)=>{t.classList.toggle("active",null!==this.segments[e].token&&e>=s&&e<=i)})}lt(t){this.l=t,this.ut(this.u,this.l)}dt(){const t=this.X(),e=this.Z();-1!==t&&(this.i=-1,this.u=t,this.l=e,this.ut(t,e))}nt(){-1!==this.i&&this.et(this.i),this.u=-1,this.l=-1,b(this.D)}yt(){const t=o(this.input);if(!t||!this.ct())return;const e=Math.min(this.u,this.l),s=Math.max(this.u,this.l);let i="";for(let n=e;n<=s;n++){const e=this.segments[n];e&&(i+=e.token?r(e.token,t):e.text)}navigator.clipboard.writeText(i).catch(()=>{const t=document.createElement("textarea");t.value=i,t.style.cssText="position:fixed;opacity:0;pointer-events:none",document.body.appendChild(t),t.select(),document.execCommand("copy"),document.body.removeChild(t)})}ft(){this.segments.every((t,e)=>!t.token||""===this.st(e))&&(this.input.value="",this.input.dispatchEvent(new Event("input",{bubbles:!0})),this.input.dispatchEvent(new Event("change",{bubbles:!0})),this.$(),this.i=-1)}bt(t){const e=t.target.closest("[data-idx]");if(!e)return-1;const s=parseInt(e.dataset.idx??"-1",10);return this.segments[s]?.token?s:-1}W(t){if(t.detail>=2)return;const e=this.bt(t);-1!==e&&(this.u=e,this.l=e)}P(t){if(-1===this.u||!(1&t.buttons))return;const e=this.bt(t);-1!==e&&e!==this.l&&(-1!==this.i&&(this.i=-1,this.o=""),this.lt(e))}q(t){if(-1!==this.u&&this.u===this.l){const t=this.u;this.u=-1,this.l=-1,this.V(t)}else this.u!==this.l&&(this.m=!0,this.input.focus({ht:!0}))}O(t){t.preventDefault(),this.i=-1,this.o="",this.dt(),this.input.focus({ht:!0})}Y(t){const e=t.relatedTarget;e&&this.k.contains(e)||this.rt()}F(t){this.V(0)}L(t){if(this.m)return void(this.m=!1);const e=t.target;e!==this.k&&e!==this.input&&e!==this.v||(this.nt(),this.V(this.X()))}S(t){const e=t.ctrlKey||t.metaKey;if(e&&"a"===t.key)return t.preventDefault(),void this.dt();if(e&&"c"===t.key)return void(this.ct()&&(t.preventDefault(),this.yt()));if("Escape"===t.key)return void(this.ct()?this.nt():this.rt());if("Backspace"===t.key||"Delete"===t.key){t.preventDefault(),this.o="";let e=-1;if(this.ct()){e=this.u;for(let t=e;t<=this.l;t++)this.segments[t]?.token&&this.xt(t);this.nt()}else-1!==this.i&&(""===this.st(this.i)&&(e=this.i),this.xt());if(e>0){let t=e-1;for(;t>=0;){if(this.segments[t]?.token){this.V(t);break}t--}}else 0==e&&this.V(e);return void this.ft()}if(-1===this.i)return;const s=this.segments[this.i].token;switch(t.key){case"Tab":this.Mt(t);break;case"ArrowRight":{t.preventDefault();const e=this.tt(this.i,1);-1!==e&&this.V(e);break}case"ArrowLeft":{t.preventDefault();const e=this.tt(this.i,-1);-1!==e&&this.V(e);break}case"ArrowUp":t.preventDefault(),this.kt(s,1);break;case"ArrowDown":t.preventDefault(),this.kt(s,-1);break;default:/^\d$/.test(t.key)&&(t.preventDefault(),this.it(s,t.key))}}Mt(t){const e=this.tt(this.i,t.shiftKey?-1:1);-1!==e?(t.preventDefault(),this.V(e)):this.rt()}it(t,e,h=!1){this.o+=e;const r=parseInt(this.o,10),a=i(t),o=s(t);if(this.o.length>=o){const e=n(t);if(this.ot(t,Math.max(e,Math.min(a,r))),this.o="",!h){const t=this.tt(this.i,1);-1!==t&&setTimeout(()=>this.V(t),0)}}else this.xt()}xt(t=this.i){const e=this.D[t],s=this.segments[t].token;this.o?(e.textContent=this.o,e.classList.remove("empty")):(e.textContent=h(s),e.classList.add("empty"))}st(t){if(this.o)return this.o;const e=this.D[t];return e.classList.contains("empty")?"":e.textContent??""}kt(t,e){const s=a(t,o(this.input)??new Date),h=n(t),r=i(t);let c=s+e;c<h&&(c=r),c>r&&(c=h),this.ot(t,c)}ot(t,e){const s=((t,e,s)=>{const i=t??new Date;let n=i.getFullYear(),h=i.getMonth()+1,r=i.getDate(),a=i.getHours(),o=i.getMinutes(),c=i.getSeconds();switch(e){case"dd":case"d":r=s;break;case"MM":case"M":h=s;break;case"yyyy":n=s;break;case"yy":n=2e3+s;break;case"HH":case"H":a=s;break;case"hh":case"h":a=p(s,a);break;case"mm":o=s;break;case"ss":c=s}const u=new Date(n,h,0).getDate();return r=Math.max(1,Math.min(r,u)),new Date(n,h-1,r,a,o,c)})(o(this.input),t,e);c(this.input,s),this.$()}I(t){t.preventDefault();const e=((t,e,s="date")=>{const i=[e,...("time"===s?g:"datetime-local"===s?V:D).filter(t=>t!==e)];for(const e of i){const i=m(t,e,s);if(i)return i}return null})((t.clipboardData?.getData("text/plain")??"").trim(),this.format,this.kind);e&&(c(this.input,e),this.$())}J(){this.input.addEventListener("keydown",this.N),this.input.addEventListener("paste",this.T),this.input.addEventListener("change",this.A),this.input.addEventListener("blur",this.B),this.input.addEventListener("focus",this.C),this.k.addEventListener("click",this._),this.k.addEventListener("dblclick",this.j),this.v.addEventListener("mousedown",this.U),this.v.addEventListener("mousemove",this.K),this.v.addEventListener("mouseup",this.R),this.G.observe(this.input,{attributes:!0,attributeFilter:["value","min","max"]})}update(){this.$()}destroy(){this.p||(this.p=!0,this.G.disconnect(),this.input.removeEventListener("keydown",this.N),this.input.removeEventListener("paste",this.T),this.input.removeEventListener("change",this.A),this.input.removeEventListener("blur",this.B),this.k.removeEventListener("click",this._),this.k.removeEventListener("dblclick",this.j),this.v.removeEventListener("mousedown",this.U),this.v.removeEventListener("mousemove",this.K),this.v.removeEventListener("mouseup",this.R),this.input.classList.remove("superdate-input"),this.input.style.display="",this.input.style.width="",this.input.style.color="",this.k.replaceWith(this.input))}}let S=null,T=[];const E="data-superdate-destroyed",I=new Set(["date","time","datetime-local"]),SuperDate=new class{constructor(){this.version="",this.name=""}bind(t,e={}){const s=x(e);return T.push({selector:t,options:s}),(t=>{document.querySelectorAll(t).forEach(t=>{t.removeAttribute(E)})})(t),((t,e)=>{document.querySelectorAll(t).forEach(t=>{k(t)&&M(t,e)})})(t,s),S||(S=new MutationObserver(t=>{for(const e of t)for(const t of Array.from(e.addedNodes))t instanceof Element&&w(t)}),S.observe(document.body,{childList:!0,subtree:!0})),this}init(t,e={}){if(t[$])return t[$];t.removeAttribute(E);const s=x(e),i=new N(t,s.format,s.timeFormat,s.dateTimeDelimiter);return t[$]=i,i}destroy(t){t[$]&&(t[$].destroy(),delete t[$]),(t=>{t.setAttribute(E,"")})(t)}};SuperDate.version="0.3.1",SuperDate.name="SuperDate";export{SuperDate as default};
|
|
Binary file
|
package/dist/super-date.min.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
/*! SuperDate v0.3.
|
|
2
|
-
var t,e;t=this,e=function(){"use strict";function t(t){const e=t.type;return"time"===e||"datetime-local"===e?e:"date"}function e(t){v.lastIndex=0;const e=[];let s,i=0;for(;null!==(s=v.exec(t));){if(s.index>i){const n=t.slice(i,s.index);e.push({token:null,text:n,start:i,end:s.index})}e.push({token:s[0],text:s[0],start:s.index,end:s.index+s[0].length}),i=s.index+s[0].length}if(i<t.length){const s=t.slice(i);e.push({token:null,text:s,start:i,end:t.length})}return e}function s(t){return g[t].t}function i(t){return g[t].maxValue}function n(t){return g[t].minValue}function h(t){return g[t].placeholder}function r(t,e){if(!e)return"";switch(t){case"dd":return y(e.getDate(),2);case"d":return e.getDate()+"";case"MM":return y(e.getMonth()+1,2);case"M":return e.getMonth()+1+"";case"yyyy":return y(e.getFullYear(),4);case"yy":return y(e.getFullYear()%100,2);case"HH":return y(e.getHours(),2);case"H":return e.getHours()+"";case"hh":return y(f(e.getHours()),2);case"h":return f(e.getHours())+"";case"mm":return y(e.getMinutes(),2);case"ss":return y(e.getSeconds(),2)}}function a(t,e){switch(t){case"dd":case"d":return e.getDate();case"MM":case"M":return e.getMonth()+1;case"yyyy":return e.getFullYear();case"yy":return e.getFullYear()%100;case"HH":case"H":return e.getHours();case"hh":case"h":return f(e.getHours());case"mm":return e.getMinutes();case"ss":return e.getSeconds()}}function o(e){if(!e.value)return null;const s=t(e);return"date"===s?u(e.value):"time"===s?(t=>{const e=t.split(":");if(e.length<2)return null;const[s,i,n=0]=e.map(Number);if(isNaN(s)||isNaN(i))return null;const h=new Date;return new Date(h.getFullYear(),h.getMonth(),h.getDate(),s,i,n)})(e.value):(t=>{const e=t.indexOf("T");if(-1===e)return null;const s=u(t.slice(0,e));if(!s)return null;const i=t.slice(e+1).split(":").map(Number),[n=0,h=0,r=0]=i;return new Date(s.getFullYear(),s.getMonth(),s.getDate(),n,h,r)})(e.value)}function c(e,s){const i=t(e);e.value="date"===i?l(s):"time"===i?d(s):(t=>`${l(t)}T${d(t)}`)(s),e.dispatchEvent(new Event("input",{bubbles:!0})),e.dispatchEvent(new Event("change",{bubbles:!0}))}function u(t){const e=t.split("-");if(3!==e.length)return null;const[s,i,n]=e.map(Number);return isNaN(s)||isNaN(i)||isNaN(n)?null:new Date(s,i-1,n)}function l(t){return`${t.getFullYear()}-${y(t.getMonth()+1,2)}-${y(t.getDate(),2)}`}function d(t){return`${y(t.getHours(),2)}:${y(t.getMinutes(),2)}:${y(t.getSeconds(),2)}`}function m(t,i,n){const h=e(i);let r=0;const a=new Date;let o=a.getDate(),c=a.getMonth()+1,u=a.getFullYear(),l=0,d=0,m=0;for(const e of h){if(r>=t.length)break;if(!e.token){t.slice(r,r+e.text.length)===e.text&&(r+=e.text.length);continue}const i=s(e.token);let n="";for(let e=0;e<i&&r<t.length&&/\d/.test(t[r]);e++,r++)n+=t[r];if(!n)return null;const h=parseInt(n,10);switch(e.token){case"dd":case"d":o=h;break;case"MM":case"M":c=h;break;case"yyyy":u=h;break;case"yy":u=2e3+h;break;case"HH":case"H":l=h;break;case"hh":case"h":l=p(h,l);break;case"mm":d=h;break;case"ss":m=h}}return"date"!==n&&"datetime-local"!==n||!(c<1||c>12||o<1||o>31)?"time"===n?l<0||l>23||d<0||d>59?null:new Date(a.getFullYear(),a.getMonth(),a.getDate(),l,d,m):new Date(u,c-1,o,l,d,m):null}function y(t,e){return(t+"").padStart(e,"0")}function f(t){return 0===t?12:t>12?t-12:t}function p(t,e){const s=e>=12;return 12===t?s?12:0:s?t+12:t}function b(t){t.forEach(t=>t.classList.remove("active"))}function x(t={}){return{format:t.format??"dd/MM/yyyy",timeFormat:t.timeFormat??"HH:mm",dateTimeDelimiter:t.dateTimeDelimiter??" ",locale:t.locale??("undefined"!=typeof navigator?navigator.language:"en")}}function M(t,e){t[T]=new $(t,e.format,e.timeFormat,e.dateTimeDelimiter)}function k(t){return I.has(t.type)&&!t[T]&&!(t=>t.hasAttribute(E))(t)}function w(t){for(const e of S)t instanceof HTMLInputElement&&t.matches(e.selector)&&k(t)&&M(t,e.options),t.querySelectorAll(e.selector).forEach(t=>{k(t)&&M(t,e.options)})}const v=/yyyy|yy|YYYY|YY|MM|M|HH|H|hh|h|mm|ss|dd|DD|d|D/g,g={i:{t:2,maxValue:31,minValue:1,placeholder:"dd"},d:{t:2,maxValue:31,minValue:1,placeholder:"d"},h:{t:2,maxValue:12,minValue:1,placeholder:"mm"},o:{t:2,maxValue:12,minValue:1,placeholder:"m"},u:{t:4,maxValue:9999,minValue:1,placeholder:"yyyy"},l:{t:2,maxValue:99,minValue:0,placeholder:"yy"},m:{t:2,maxValue:23,minValue:0,placeholder:"HH"},p:{t:2,maxValue:23,minValue:0,placeholder:"H"},M:{t:2,maxValue:12,minValue:1,placeholder:"hh"},k:{t:2,maxValue:12,minValue:1,placeholder:"h"},mm:{t:2,maxValue:59,minValue:0,placeholder:"mm"},v:{t:2,maxValue:59,minValue:0,placeholder:"ss"}},H=["yyyy-MM-dd","dd/MM/yyyy","MM/dd/yyyy","d.M.yyyy","MM-dd-yyyy"],D=["HH:mm:ss","HH:mm","H:mm"],V=["yyyy-MM-dd HH:mm:ss","yyyy-MM-dd HH:mm","dd/MM/yyyy HH:mm"],T="__superdate__";class ${constructor(s,i,n="HH:mm",h=" "){this.H=-1,this.D="",this.V=-1,this.T=-1,this.$=!1,this.N=!1,this.input=s,this.kind=t(s);const r=s.dataset.dateFormat??i,a=s.dataset.timeFormat??n;this.format="datetime-local"===this.kind?((t,e,s)=>`${t}${s}${e}`)(r,a,s.dataset.dateTimeDelimiter??h):"time"===this.kind?a:r,this.segments=e(this.format);const o=((t,e,s,i,n="date")=>{const h=document.createElement("div");h.className="superdate-wrapper",h.style.width=t.style.width||"",t.parentNode.insertBefore(h,t),h.appendChild(t),t.classList.add("superdate-input"),t.style.display="block",t.style.boxSizing="border-box";const r=document.createElement("div");r.className="superdate-overlay";const a=[];e.forEach((t,e)=>{const i=document.createElement("span");i.className="superdate-seg",t.token&&(i.dataset.token=t.token,i.dataset.idx=e+"",i.setAttribute("tabindex","-1"),i.addEventListener("click",t=>{t.stopPropagation(),s(e)})),r.appendChild(i),a.push(i)});const o=document.createElement("span");return o.className="superdate-icon",o.innerHTML="time"===n?'<svg width="16" height="16" viewBox="0 0 24 24" fill="none"\n stroke="currentColor" stroke-width="2" stroke-linecap="round"\n stroke-linejoin="round" aria-hidden="true">\n <circle cx="12" cy="12" r="10"/>\n <polyline points="12 6 12 12 16 14"/>\n</svg>':'<svg width="16" height="16" viewBox="0 0 24 24" fill="none"\n stroke="currentColor" stroke-width="2" stroke-linecap="round"\n stroke-linejoin="round" aria-hidden="true">\n <rect x="3" y="4" width="18" height="18" rx="2"/>\n <line x1="16" y1="2" x2="16" y2="6"/>\n <line x1="8" y1="2" x2="8" y2="6"/>\n <line x1="3" y1="10" x2="21" y2="10"/>\n</svg>',o.addEventListener("click",t=>{t.stopPropagation(),i()}),r.appendChild(o),h.appendChild(r),{S:h,I:r,A:a}})(s,this.segments,t=>this.B(t),()=>this.input.showPicker?.(),this.kind);this.S=o.S,this.I=o.I,this.A=o.A,this.Y(),this.C=t=>this.F(t),this._=t=>this.j(t),this.L=()=>this.Y(),this.O=t=>this.U(t),this.W=t=>this.K(t),this.P=t=>this.R(t),this.q=t=>this.G(t),this.J=t=>this.X(t),this.Z=t=>this.tt(t),this.et=t=>this.st(t),this.it=new MutationObserver(()=>{this.N||this.Y()}),this.nt()}Y(){var t,e,s;t=this.segments,e=this.A,s=o(this.input),t.forEach((t,i)=>{const n=e[i];if(!t.token)return void(n.textContent=t.text);const a=r(t.token,s);a?(n.textContent=a,n.classList.remove("empty")):(n.textContent=h(t.token),n.classList.add("empty"))})}ht(){return this.segments.findIndex(t=>null!==t.token)}rt(){for(let t=this.segments.length-1;t>=0;t--)if(this.segments[t].token)return t;return-1}ot(t,e=1){let s=t+e;for(;s>=0&&s<this.segments.length;){if(this.segments[s].token)return s;s+=e}return-1}ct(t){const e=this.segments[t].token;if(!e)return;let i=this.ut(t);const n=s(e),h=y(a(e,new Date),n);"0"===i?(this.D="",this.lt(e,h,!0)):""!==i&&(n<=2?i.length<n&&(i=i.padStart(n,"0"),this.D="",this.lt(e,i,!0)):(this.D="",i=`${h.substring(0,n-i.length)}${i}`,this.lt(e,i,!0)))}B(t){t<0||t>=this.segments.length||this.segments[t].token&&(this.dt(),this.D="",this.H=t,((t,e)=>{this.A.forEach((t,s)=>t.classList.toggle("active",s===e))})(0,t),this.input.focus({yt:!0}))}ft(){if(-1!==this.H){const t=this.segments[this.H].token;if(t){this.ct(this.H);const e=o(this.input);e&&a(t,e)<n(t)&&this.bt(t,n(t))}}this.H=-1,this.D="",this.dt(),b(this.A)}xt(){return-1!==this.V}Mt(t,e){const s=Math.min(t,e),i=Math.max(t,e);this.A.forEach((t,e)=>{t.classList.toggle("active",null!==this.segments[e].token&&e>=s&&e<=i)})}kt(t){this.T=t,this.Mt(this.V,this.T)}wt(){const t=this.ht(),e=this.rt();-1!==t&&(this.H=-1,this.V=t,this.T=e,this.Mt(t,e))}dt(){-1!==this.H&&this.ct(this.H),this.V=-1,this.T=-1,b(this.A)}vt(){const t=o(this.input);if(!t||!this.xt())return;const e=Math.min(this.V,this.T),s=Math.max(this.V,this.T);let i="";for(let n=e;n<=s;n++){const e=this.segments[n];e&&(i+=e.token?r(e.token,t):e.text)}navigator.clipboard.writeText(i).catch(()=>{const t=document.createElement("textarea");t.value=i,t.style.cssText="position:fixed;opacity:0;pointer-events:none",document.body.appendChild(t),t.select(),document.execCommand("copy"),document.body.removeChild(t)})}gt(){this.segments.every((t,e)=>!t.token||""===this.ut(e))&&(this.input.value="",this.input.dispatchEvent(new Event("input",{bubbles:!0})),this.input.dispatchEvent(new Event("change",{bubbles:!0})),this.Y(),this.H=-1)}Ht(t){const e=t.target.closest("[data-idx]");if(!e)return-1;const s=parseInt(e.dataset.idx??"-1",10);return this.segments[s]?.token?s:-1}X(t){if(t.detail>=2)return;const e=this.Ht(t);-1!==e&&(this.V=e,this.T=e)}tt(t){if(-1===this.V||!(1&t.buttons))return;const e=this.Ht(t);-1!==e&&e!==this.T&&(-1!==this.H&&(this.H=-1,this.D=""),this.kt(e))}st(t){if(-1!==this.V&&this.V===this.T){const t=this.V;this.V=-1,this.T=-1,this.B(t)}else this.V!==this.T&&(this.$=!0,this.input.focus({yt:!0}))}G(t){t.preventDefault(),this.H=-1,this.D="",this.wt(),this.input.focus({yt:!0})}U(t){const e=t.relatedTarget;e&&this.S.contains(e)||this.ft()}K(t){this.B(0)}R(t){if(this.$)return void(this.$=!1);const e=t.target;e!==this.S&&e!==this.input&&e!==this.I||(this.dt(),this.B(this.ht()))}F(t){const e=t.ctrlKey||t.metaKey;if(e&&"a"===t.key)return t.preventDefault(),void this.wt();if(e&&"c"===t.key)return void(this.xt()&&(t.preventDefault(),this.vt()));if("Escape"===t.key)return void(this.xt()?this.dt():this.ft());if("Backspace"===t.key||"Delete"===t.key){t.preventDefault(),this.D="";let e=-1;if(this.xt()){e=this.V;for(let t=e;t<=this.T;t++)this.segments[t]?.token&&this.Dt(t);this.dt()}else-1!==this.H&&(""===this.ut(this.H)&&(e=this.H),this.Dt());if(e>0){let t=e-1;for(;t>=0;){if(this.segments[t]?.token){this.B(t);break}t--}}else 0==e&&this.B(e);return void this.gt()}if(-1===this.H)return;const s=this.segments[this.H].token;switch(t.key){case"Tab":this.Vt(t);break;case"ArrowRight":{t.preventDefault();const e=this.ot(this.H,1);-1!==e&&this.B(e);break}case"ArrowLeft":{t.preventDefault();const e=this.ot(this.H,-1);-1!==e&&this.B(e);break}case"ArrowUp":t.preventDefault(),this.Tt(s,1);break;case"ArrowDown":t.preventDefault(),this.Tt(s,-1);break;default:/^\d$/.test(t.key)&&(t.preventDefault(),this.lt(s,t.key))}}Vt(t){const e=this.ot(this.H,t.shiftKey?-1:1);-1!==e?(t.preventDefault(),this.B(e)):this.ft()}lt(t,e,h=!1){this.D+=e;const r=parseInt(this.D,10),a=i(t),o=s(t);if(this.D.length>=o){const e=n(t);if(this.bt(t,Math.max(e,Math.min(a,r))),this.D="",!h){const t=this.ot(this.H,1);-1!==t&&setTimeout(()=>this.B(t),0)}}else this.Dt()}Dt(t=this.H){const e=this.A[t],s=this.segments[t].token;this.D?(e.textContent=this.D,e.classList.remove("empty")):(e.textContent=h(s),e.classList.add("empty"))}ut(t){if(this.D)return this.D;const e=this.A[t];return e.classList.contains("empty")?"":e.textContent??""}Tt(t,e){const s=a(t,o(this.input)??new Date),h=n(t),r=i(t);let c=s+e;c<h&&(c=r),c>r&&(c=h),this.bt(t,c)}bt(t,e){const s=((t,e,s)=>{const i=t??new Date;let n=i.getFullYear(),h=i.getMonth()+1,r=i.getDate(),a=i.getHours(),o=i.getMinutes(),c=i.getSeconds();switch(e){case"dd":case"d":r=s;break;case"MM":case"M":h=s;break;case"yyyy":n=s;break;case"yy":n=2e3+s;break;case"HH":case"H":a=s;break;case"hh":case"h":a=p(s,a);break;case"mm":o=s;break;case"ss":c=s}const u=new Date(n,h,0).getDate();return r=Math.max(1,Math.min(r,u)),new Date(n,h-1,r,a,o,c)})(o(this.input),t,e);c(this.input,s),this.Y()}j(t){t.preventDefault();const e=((t,e,s="date")=>{const i=[e,...("time"===s?D:"datetime-local"===s?V:H).filter(t=>t!==e)];for(const n of i){const e=m(t,n,s);if(e)return e}return null})((t.clipboardData?.getData("text/plain")??"").trim(),this.format,this.kind);e&&(c(this.input,e),this.Y())}nt(){this.input.addEventListener("keydown",this.C),this.input.addEventListener("paste",this._),this.input.addEventListener("change",this.L),this.input.addEventListener("blur",this.O),this.input.addEventListener("focus",this.W),this.S.addEventListener("click",this.P),this.S.addEventListener("dblclick",this.q),this.I.addEventListener("mousedown",this.J),this.I.addEventListener("mousemove",this.Z),this.I.addEventListener("mouseup",this.et),this.it.observe(this.input,{attributes:!0,attributeFilter:["value","min","max"]})}update(){this.Y()}destroy(){this.N||(this.N=!0,this.it.disconnect(),this.input.removeEventListener("keydown",this.C),this.input.removeEventListener("paste",this._),this.input.removeEventListener("change",this.L),this.input.removeEventListener("blur",this.O),this.S.removeEventListener("click",this.P),this.S.removeEventListener("dblclick",this.q),this.I.removeEventListener("mousedown",this.J),this.I.removeEventListener("mousemove",this.Z),this.I.removeEventListener("mouseup",this.et),this.input.classList.remove("superdate-input"),this.input.style.display="",this.input.style.width="",this.input.style.color="",this.S.replaceWith(this.input))}}let N=null,S=[];const E="data-superdate-destroyed",I=new Set(["date","time","datetime-local"]);void 0===globalThis.GLOBAL_SDATE&&(globalThis.GLOBAL_SDATE=new class{constructor(){this.version="",this.name=""}bind(t,e={}){const s=x(e);return S.push({selector:t,options:s}),(t=>{document.querySelectorAll(t).forEach(t=>{t.removeAttribute(E)})})(t),((t,e)=>{document.querySelectorAll(t).forEach(t=>{k(t)&&M(t,e)})})(t,s),N||(N=new MutationObserver(t=>{for(const e of t)for(const t of Array.from(e.addedNodes))t instanceof Element&&w(t)}),N.observe(document.body,{childList:!0,subtree:!0})),this}init(t,e={}){if(t[T])return t[T];t.removeAttribute(E);const s=x(e),i=new $(t,s.format,s.timeFormat,s.dateTimeDelimiter);return t[T]=i,i}destroy(t){t[T]&&(t[T].destroy(),delete t[T]),(t=>{t.setAttribute(E,"")})(t)}});var SuperDate=globalThis.GLOBAL_SDATE;return SuperDate.version="0.3.0",SuperDate.name="SuperDate",SuperDate},"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).SuperDate=e();
|
|
1
|
+
/*! SuperDate v0.3.1 | MIT License */
|
|
2
|
+
var t,e;t=this,e=function(){"use strict";function t(t){const e=t.type;return"time"===e||"datetime-local"===e?e:"date"}function e(t){v.lastIndex=0;const e=[];let s,i=0;for(;null!==(s=v.exec(t));){if(s.index>i){const n=t.slice(i,s.index);e.push({token:null,text:n,start:i,end:s.index})}e.push({token:s[0],text:s[0],start:s.index,end:s.index+s[0].length}),i=s.index+s[0].length}if(i<t.length){const s=t.slice(i);e.push({token:null,text:s,start:i,end:t.length})}return e}function s(t){return g[t].t}function i(t){return g[t].maxValue}function n(t){return g[t].minValue}function h(t){return g[t].placeholder}function r(t,e){if(!e)return"";switch(t){case"dd":return y(e.getDate(),2);case"d":return e.getDate()+"";case"MM":return y(e.getMonth()+1,2);case"M":return e.getMonth()+1+"";case"yyyy":return y(e.getFullYear(),4);case"yy":return y(e.getFullYear()%100,2);case"HH":return y(e.getHours(),2);case"H":return e.getHours()+"";case"hh":return y(f(e.getHours()),2);case"h":return f(e.getHours())+"";case"mm":return y(e.getMinutes(),2);case"ss":return y(e.getSeconds(),2)}}function a(t,e){switch(t){case"dd":case"d":return e.getDate();case"MM":case"M":return e.getMonth()+1;case"yyyy":return e.getFullYear();case"yy":return e.getFullYear()%100;case"HH":case"H":return e.getHours();case"hh":case"h":return f(e.getHours());case"mm":return e.getMinutes();case"ss":return e.getSeconds()}}function o(e){if(!e.value)return null;const s=t(e);return"date"===s?u(e.value):"time"===s?(t=>{const e=t.split(":");if(e.length<2)return null;const[s,i,n=0]=e.map(Number);if(isNaN(s)||isNaN(i))return null;const h=new Date;return new Date(h.getFullYear(),h.getMonth(),h.getDate(),s,i,n)})(e.value):(t=>{const e=t.indexOf("T");if(-1===e)return null;const s=u(t.slice(0,e));if(!s)return null;const i=t.slice(e+1).split(":").map(Number),[n=0,h=0,r=0]=i;return new Date(s.getFullYear(),s.getMonth(),s.getDate(),n,h,r)})(e.value)}function c(e,s){const i=t(e);e.value="date"===i?l(s):"time"===i?d(s):(t=>`${l(t)}T${d(t)}`)(s),e.dispatchEvent(new Event("input",{bubbles:!0})),e.dispatchEvent(new Event("change",{bubbles:!0}))}function u(t){const e=t.split("-");if(3!==e.length)return null;const[s,i,n]=e.map(Number);return isNaN(s)||isNaN(i)||isNaN(n)?null:new Date(s,i-1,n)}function l(t){return`${t.getFullYear()}-${y(t.getMonth()+1,2)}-${y(t.getDate(),2)}`}function d(t){return`${y(t.getHours(),2)}:${y(t.getMinutes(),2)}:${y(t.getSeconds(),2)}`}function m(t,i,n){const h=e(i);let r=0;const a=new Date;let o=a.getDate(),c=a.getMonth()+1,u=a.getFullYear(),l=0,d=0,m=0;for(const e of h){if(r>=t.length)break;if(!e.token){t.slice(r,r+e.text.length)===e.text&&(r+=e.text.length);continue}const i=s(e.token);let n="";for(let e=0;e<i&&r<t.length&&/\d/.test(t[r]);e++,r++)n+=t[r];if(!n)return null;const h=parseInt(n,10);switch(e.token){case"dd":case"d":o=h;break;case"MM":case"M":c=h;break;case"yyyy":u=h;break;case"yy":u=2e3+h;break;case"HH":case"H":l=h;break;case"hh":case"h":l=p(h,l);break;case"mm":d=h;break;case"ss":m=h}}return"date"!==n&&"datetime-local"!==n||!(c<1||c>12||o<1||o>31)?"time"===n?l<0||l>23||d<0||d>59?null:new Date(a.getFullYear(),a.getMonth(),a.getDate(),l,d,m):new Date(u,c-1,o,l,d,m):null}function y(t,e){return(t+"").padStart(e,"0")}function f(t){return 0===t?12:t>12?t-12:t}function p(t,e){const s=e>=12;return 12===t?s?12:0:s?t+12:t}function b(t){t.forEach(t=>t.classList.remove("active"))}function x(t={}){return{format:t.format??"dd/MM/yyyy",timeFormat:t.timeFormat??"HH:mm",dateTimeDelimiter:t.dateTimeDelimiter??" ",locale:t.locale??("undefined"!=typeof navigator?navigator.language:"en")}}function M(t,e){t[T]=new $(t,e.format,e.timeFormat,e.dateTimeDelimiter)}function k(t){return I.has(t.type)&&!t[T]&&!(t=>t.hasAttribute(E))(t)}function w(t){for(const e of S)t instanceof HTMLInputElement&&t.matches(e.selector)&&k(t)&&M(t,e.options),t.querySelectorAll(e.selector).forEach(t=>{k(t)&&M(t,e.options)})}const v=/yyyy|yy|YYYY|YY|MM|M|HH|H|hh|h|mm|ss|dd|DD|d|D/g,g={dd:{t:2,maxValue:31,minValue:1,placeholder:"dd"},d:{t:2,maxValue:31,minValue:1,placeholder:"d"},MM:{t:2,maxValue:12,minValue:1,placeholder:"mm"},M:{t:2,maxValue:12,minValue:1,placeholder:"m"},yyyy:{t:4,maxValue:9999,minValue:1,placeholder:"yyyy"},yy:{t:2,maxValue:99,minValue:0,placeholder:"yy"},HH:{t:2,maxValue:23,minValue:0,placeholder:"HH"},H:{t:2,maxValue:23,minValue:0,placeholder:"H"},hh:{t:2,maxValue:12,minValue:1,placeholder:"hh"},h:{t:2,maxValue:12,minValue:1,placeholder:"h"},mm:{t:2,maxValue:59,minValue:0,placeholder:"mm"},ss:{t:2,maxValue:59,minValue:0,placeholder:"ss"}},H=["yyyy-MM-dd","dd/MM/yyyy","MM/dd/yyyy","d.M.yyyy","MM-dd-yyyy"],D=["HH:mm:ss","HH:mm","H:mm"],V=["yyyy-MM-dd HH:mm:ss","yyyy-MM-dd HH:mm","dd/MM/yyyy HH:mm"],T="__superdate__";class ${constructor(s,i,n="HH:mm",h=" "){this.i=-1,this.o="",this.u=-1,this.l=-1,this.m=!1,this.p=!1,this.input=s,this.kind=t(s);const r=s.dataset.dateFormat??i,a=s.dataset.timeFormat??n;this.format="datetime-local"===this.kind?((t,e,s)=>`${t}${s}${e}`)(r,a,s.dataset.dateTimeDelimiter??h):"time"===this.kind?a:r,this.segments=e(this.format);const o=((t,e,s,i,n="date")=>{const h=document.createElement("div");h.className="superdate-wrapper",h.style.width=t.style.width||"",t.parentNode.insertBefore(h,t),h.appendChild(t),t.classList.add("superdate-input"),t.style.display="block",t.style.boxSizing="border-box";const r=document.createElement("div");r.className="superdate-overlay";const a=[];e.forEach((t,e)=>{const i=document.createElement("span");i.className="superdate-seg",t.token&&(i.dataset.token=t.token,i.dataset.idx=e+"",i.setAttribute("tabindex","-1"),i.addEventListener("click",t=>{t.stopPropagation(),s(e)})),r.appendChild(i),a.push(i)});const o=document.createElement("span");return o.className="superdate-icon",o.innerHTML="time"===n?'<svg width="16" height="16" viewBox="0 0 24 24" fill="none"\n stroke="currentColor" stroke-width="2" stroke-linecap="round"\n stroke-linejoin="round" aria-hidden="true">\n <circle cx="12" cy="12" r="10"/>\n <polyline points="12 6 12 12 16 14"/>\n</svg>':'<svg width="16" height="16" viewBox="0 0 24 24" fill="none"\n stroke="currentColor" stroke-width="2" stroke-linecap="round"\n stroke-linejoin="round" aria-hidden="true">\n <rect x="3" y="4" width="18" height="18" rx="2"/>\n <line x1="16" y1="2" x2="16" y2="6"/>\n <line x1="8" y1="2" x2="8" y2="6"/>\n <line x1="3" y1="10" x2="21" y2="10"/>\n</svg>',o.addEventListener("click",t=>{t.stopPropagation(),i()}),r.appendChild(o),h.appendChild(r),{k:h,v:r,D:a}})(s,this.segments,t=>this.V(t),()=>this.input.showPicker?.(),this.kind);this.k=o.k,this.v=o.v,this.D=o.D,this.T(),this.$=t=>this.N(t),this.S=t=>this.I(t),this.A=()=>this.T(),this.B=t=>this.Y(t),this.C=t=>this.F(t),this._=t=>this.j(t),this.L=t=>this.O(t),this.U=t=>this.W(t),this.K=t=>this.P(t),this.R=t=>this.q(t),this.G=new MutationObserver(()=>{this.p||this.T()}),this.J()}T(){var t,e,s;t=this.segments,e=this.D,s=o(this.input),t.forEach((t,i)=>{const n=e[i];if(!t.token)return void(n.textContent=t.text);const a=r(t.token,s);a?(n.textContent=a,n.classList.remove("empty")):(n.textContent=h(t.token),n.classList.add("empty"))})}X(){return this.segments.findIndex(t=>null!==t.token)}Z(){for(let t=this.segments.length-1;t>=0;t--)if(this.segments[t].token)return t;return-1}tt(t,e=1){let s=t+e;for(;s>=0&&s<this.segments.length;){if(this.segments[s].token)return s;s+=e}return-1}et(t){const e=this.segments[t].token;if(!e)return;let i=this.st(t);const n=s(e),h=y(a(e,new Date),n);"0"===i?(this.o="",this.it(e,h,!0)):""!==i&&(n<=2?i.length<n&&(i=i.padStart(n,"0"),this.o="",this.it(e,i,!0)):(this.o="",i=`${h.substring(0,n-i.length)}${i}`,this.it(e,i,!0)))}V(t){t<0||t>=this.segments.length||this.segments[t].token&&(this.nt(),this.o="",this.i=t,((t,e)=>{this.D.forEach((t,s)=>t.classList.toggle("active",s===e))})(0,t),this.input.focus({ht:!0}))}rt(){if(-1!==this.i){const t=this.segments[this.i].token;if(t){this.et(this.i);const e=o(this.input);e&&a(t,e)<n(t)&&this.ot(t,n(t))}}this.i=-1,this.o="",this.nt(),b(this.D)}ct(){return-1!==this.u}ut(t,e){const s=Math.min(t,e),i=Math.max(t,e);this.D.forEach((t,e)=>{t.classList.toggle("active",null!==this.segments[e].token&&e>=s&&e<=i)})}lt(t){this.l=t,this.ut(this.u,this.l)}dt(){const t=this.X(),e=this.Z();-1!==t&&(this.i=-1,this.u=t,this.l=e,this.ut(t,e))}nt(){-1!==this.i&&this.et(this.i),this.u=-1,this.l=-1,b(this.D)}yt(){const t=o(this.input);if(!t||!this.ct())return;const e=Math.min(this.u,this.l),s=Math.max(this.u,this.l);let i="";for(let n=e;n<=s;n++){const e=this.segments[n];e&&(i+=e.token?r(e.token,t):e.text)}navigator.clipboard.writeText(i).catch(()=>{const t=document.createElement("textarea");t.value=i,t.style.cssText="position:fixed;opacity:0;pointer-events:none",document.body.appendChild(t),t.select(),document.execCommand("copy"),document.body.removeChild(t)})}ft(){this.segments.every((t,e)=>!t.token||""===this.st(e))&&(this.input.value="",this.input.dispatchEvent(new Event("input",{bubbles:!0})),this.input.dispatchEvent(new Event("change",{bubbles:!0})),this.T(),this.i=-1)}bt(t){const e=t.target.closest("[data-idx]");if(!e)return-1;const s=parseInt(e.dataset.idx??"-1",10);return this.segments[s]?.token?s:-1}W(t){if(t.detail>=2)return;const e=this.bt(t);-1!==e&&(this.u=e,this.l=e)}P(t){if(-1===this.u||!(1&t.buttons))return;const e=this.bt(t);-1!==e&&e!==this.l&&(-1!==this.i&&(this.i=-1,this.o=""),this.lt(e))}q(t){if(-1!==this.u&&this.u===this.l){const t=this.u;this.u=-1,this.l=-1,this.V(t)}else this.u!==this.l&&(this.m=!0,this.input.focus({ht:!0}))}O(t){t.preventDefault(),this.i=-1,this.o="",this.dt(),this.input.focus({ht:!0})}Y(t){const e=t.relatedTarget;e&&this.k.contains(e)||this.rt()}F(t){this.V(0)}j(t){if(this.m)return void(this.m=!1);const e=t.target;e!==this.k&&e!==this.input&&e!==this.v||(this.nt(),this.V(this.X()))}N(t){const e=t.ctrlKey||t.metaKey;if(e&&"a"===t.key)return t.preventDefault(),void this.dt();if(e&&"c"===t.key)return void(this.ct()&&(t.preventDefault(),this.yt()));if("Escape"===t.key)return void(this.ct()?this.nt():this.rt());if("Backspace"===t.key||"Delete"===t.key){t.preventDefault(),this.o="";let e=-1;if(this.ct()){e=this.u;for(let t=e;t<=this.l;t++)this.segments[t]?.token&&this.xt(t);this.nt()}else-1!==this.i&&(""===this.st(this.i)&&(e=this.i),this.xt());if(e>0){let t=e-1;for(;t>=0;){if(this.segments[t]?.token){this.V(t);break}t--}}else 0==e&&this.V(e);return void this.ft()}if(-1===this.i)return;const s=this.segments[this.i].token;switch(t.key){case"Tab":this.Mt(t);break;case"ArrowRight":{t.preventDefault();const e=this.tt(this.i,1);-1!==e&&this.V(e);break}case"ArrowLeft":{t.preventDefault();const e=this.tt(this.i,-1);-1!==e&&this.V(e);break}case"ArrowUp":t.preventDefault(),this.kt(s,1);break;case"ArrowDown":t.preventDefault(),this.kt(s,-1);break;default:/^\d$/.test(t.key)&&(t.preventDefault(),this.it(s,t.key))}}Mt(t){const e=this.tt(this.i,t.shiftKey?-1:1);-1!==e?(t.preventDefault(),this.V(e)):this.rt()}it(t,e,h=!1){this.o+=e;const r=parseInt(this.o,10),a=i(t),o=s(t);if(this.o.length>=o){const e=n(t);if(this.ot(t,Math.max(e,Math.min(a,r))),this.o="",!h){const t=this.tt(this.i,1);-1!==t&&setTimeout(()=>this.V(t),0)}}else this.xt()}xt(t=this.i){const e=this.D[t],s=this.segments[t].token;this.o?(e.textContent=this.o,e.classList.remove("empty")):(e.textContent=h(s),e.classList.add("empty"))}st(t){if(this.o)return this.o;const e=this.D[t];return e.classList.contains("empty")?"":e.textContent??""}kt(t,e){const s=a(t,o(this.input)??new Date),h=n(t),r=i(t);let c=s+e;c<h&&(c=r),c>r&&(c=h),this.ot(t,c)}ot(t,e){const s=((t,e,s)=>{const i=t??new Date;let n=i.getFullYear(),h=i.getMonth()+1,r=i.getDate(),a=i.getHours(),o=i.getMinutes(),c=i.getSeconds();switch(e){case"dd":case"d":r=s;break;case"MM":case"M":h=s;break;case"yyyy":n=s;break;case"yy":n=2e3+s;break;case"HH":case"H":a=s;break;case"hh":case"h":a=p(s,a);break;case"mm":o=s;break;case"ss":c=s}const u=new Date(n,h,0).getDate();return r=Math.max(1,Math.min(r,u)),new Date(n,h-1,r,a,o,c)})(o(this.input),t,e);c(this.input,s),this.T()}I(t){t.preventDefault();const e=((t,e,s="date")=>{const i=[e,...("time"===s?D:"datetime-local"===s?V:H).filter(t=>t!==e)];for(const n of i){const e=m(t,n,s);if(e)return e}return null})((t.clipboardData?.getData("text/plain")??"").trim(),this.format,this.kind);e&&(c(this.input,e),this.T())}J(){this.input.addEventListener("keydown",this.$),this.input.addEventListener("paste",this.S),this.input.addEventListener("change",this.A),this.input.addEventListener("blur",this.B),this.input.addEventListener("focus",this.C),this.k.addEventListener("click",this._),this.k.addEventListener("dblclick",this.L),this.v.addEventListener("mousedown",this.U),this.v.addEventListener("mousemove",this.K),this.v.addEventListener("mouseup",this.R),this.G.observe(this.input,{attributes:!0,attributeFilter:["value","min","max"]})}update(){this.T()}destroy(){this.p||(this.p=!0,this.G.disconnect(),this.input.removeEventListener("keydown",this.$),this.input.removeEventListener("paste",this.S),this.input.removeEventListener("change",this.A),this.input.removeEventListener("blur",this.B),this.k.removeEventListener("click",this._),this.k.removeEventListener("dblclick",this.L),this.v.removeEventListener("mousedown",this.U),this.v.removeEventListener("mousemove",this.K),this.v.removeEventListener("mouseup",this.R),this.input.classList.remove("superdate-input"),this.input.style.display="",this.input.style.width="",this.input.style.color="",this.k.replaceWith(this.input))}}let N=null,S=[];const E="data-superdate-destroyed",I=new Set(["date","time","datetime-local"]);void 0===globalThis.GLOBAL_SDATE&&(globalThis.GLOBAL_SDATE=new class{constructor(){this.version="",this.name=""}bind(t,e={}){const s=x(e);return S.push({selector:t,options:s}),(t=>{document.querySelectorAll(t).forEach(t=>{t.removeAttribute(E)})})(t),((t,e)=>{document.querySelectorAll(t).forEach(t=>{k(t)&&M(t,e)})})(t,s),N||(N=new MutationObserver(t=>{for(const e of t)for(const t of Array.from(e.addedNodes))t instanceof Element&&w(t)}),N.observe(document.body,{childList:!0,subtree:!0})),this}init(t,e={}){if(t[T])return t[T];t.removeAttribute(E);const s=x(e),i=new $(t,s.format,s.timeFormat,s.dateTimeDelimiter);return t[T]=i,i}destroy(t){t[T]&&(t[T].destroy(),delete t[T]),(t=>{t.setAttribute(E,"")})(t)}});var SuperDate=globalThis.GLOBAL_SDATE;return SuperDate.version="0.3.1",SuperDate.name="SuperDate",SuperDate},"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).SuperDate=e();
|
|
Binary file
|
package/dist/super-date.umd.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*! SuperDate v0.3.
|
|
1
|
+
/*! SuperDate v0.3.1 | MIT License */
|
|
2
2
|
(function (global, factory) {
|
|
3
3
|
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
|
4
4
|
typeof define === 'function' && define.amd ? define(factory) :
|
|
@@ -1085,7 +1085,7 @@
|
|
|
1085
1085
|
globalThis.GLOBAL_SDATE = new SuperDateRegistry();
|
|
1086
1086
|
}
|
|
1087
1087
|
var SuperDate = globalThis.GLOBAL_SDATE;
|
|
1088
|
-
SuperDate.version = "0.3.
|
|
1088
|
+
SuperDate.version = "0.3.1";
|
|
1089
1089
|
SuperDate.name = "SuperDate";
|
|
1090
1090
|
|
|
1091
1091
|
return SuperDate;
|