forked from qodesmith/datepicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatepicker.min.js
1 lines (1 loc) · 6.39 KB
/
datepicker.min.js
1
!function(e,t){var n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};return"object"===("undefined"==typeof exports?"undefined":n(exports))?module.exports=t():"function"==typeof define&&define.amd?define(function(){return t()}):e.datepicker=t()}(this,function(){"use strict";function e(e,o){var a=document.querySelector(e);o=t(o||n(),a,e);var i=document.createElement("div"),s=o.startDate||new Date((new Date).toLocaleDateString()),d=o.dateSelected,l="body"===e||"html"===e,h={el:a,nonInput:"INPUT"!==a.nodeName,noPosition:l,position:!l&&o.position,startDate:s,dateSelected:d,minDate:o.minDate,maxDate:o.maxDate,noWeekends:!!o.noWeekends,calendar:i,currentMonth:(s||d).getMonth(),currentMonthName:m[(s||d).getMonth()],currentYear:(s||d).getFullYear(),setDate:u,remove:f,onSelect:o.onSelect,onShow:o.onShow,onHide:o.onHide,onMonthChange:o.onMonthChange};d&&c(a,h),i.classList.add("datepicker"),i.classList.add("hidden"),D.push(a),r(s||d,h),p(i,h),window.addEventListener("click",v.bind(h)),window.addEventListener("focusin",v.bind(h));var w=a.parentElement;return"static"===getComputedStyle(w).position&&(h.parent=w,w.style.position="relative"),w.appendChild(i),h}function t(e,t,n){if(!t)throw new Error('"'+n+"\" doesn't reference any element on the page.");if(D.includes(t))throw new Error("A datepicker already exists on that element.");var r=e.position,a=e.maxDate,i=e.minDate,s=e.dateSelected;if(r){var c=["tr","tl","br","bl"].some(function(e){return r===e}),d='"options.position" needs to be one of the following: tl, tr, bl, or br.';if(!c)throw new Error(d);e.position=o(r)}else e.position=o("bl");if(["startDate","dateSelected","minDate","maxDate"].forEach(function(t){if(e[t]){if(!h(e[t])||isNaN(+e[t]))throw new TypeError('"options.'+t+'" needs to be a valid JavaScript Date object.');e[t]=new Date(e[t].toLocaleDateString())}}),e.startDate=e.startDate||e.dateSelected,a<i)throw new Error('"maxDate" in options is less than "minDate".');if(s){if(i>s)throw new Error('"dateSelected" in options is less than "minDate".');if(a<s)throw new Error('"dateSelected" in options is greater than "maxDate".')}return["onSelect","onShow","onHide","onMonthChange"].forEach(function(t){e[t]="function"==typeof e[t]&&e[t]}),e}function n(){return{startDate:new Date((new Date).toLocaleDateString()),position:"bl"}}function o(e){var t={};return t[g[e[0]]]=1,t[g[e[1]]]=1,t}function r(e,t){var n=a(e,t),o=i(e,t);t.calendar.innerHTML=n+o}function a(e){return['<div class="controls">','<div class="arrow left"></div>','<div class="month-year">','<span class="month">'+m[e.getMonth()]+"</span>",'<span class="year">'+e.getFullYear()+"</span>","</div>",'<div class="arrow right"></div>',"</div>"].join("")}function i(e,t){var n=t.minDate,o=t.maxDate,r=t.dateSelected,a=t.currentYear,i=t.currentMonth,s=t.noWeekends,c=new Date,d=c.toJSON().slice(0,7)===e.toJSON().slice(0,7),l=new Date(new Date(e).setDate(1)),u=l.getDay();l.setMonth(l.getMonth()+1),l.setDate(0);var h=l.getDate(),f=[],p=7*((u+h)/7|0);p+=(u+h)%7?7:0;for(var v=1;v<=p;v++){var m=w[v%7],g=v-u,D="",S='<span class="num">'+g+"</span>",b=new Date(a,i,g),y=g<1||g>h;if(y)D="empty",S="";else{var M=n&&b<n||o&&b>o,L="Sat"===m||"Sun"===m,E=d&&!M&&g===c.getDate();M=M||s&&L,D=M?"disabled":E?"current":""}+b!==+r||y||(D+=" active"),f.push('<div class="square num '+m+" "+D+'">'+S+"</div>")}var x=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"].map(function(e){return'<div class="square day">'+e+"</div>"}).concat(f);if(x.length%7!==0){var N="Calendar not constructed properly. The # of squares should be a multiple of 7.";throw new Error(N)}return x.unshift('<div class="squares">'),x.push("</div>"),x.join("")}function s(e,t){var n=t.currentMonth,o=t.currentYear,r=t.calendar,a=t.el,i=r.querySelector(".active"),s=e.textContent;t.dateSelected=new Date(o,n,s),i&&i.classList.remove("active"),e.classList.add("active"),c(a,t),t.calendar.classList.add("hidden"),t.onSelect&&t.onSelect(t)}function c(e,t){t.nonInput||(e.value=t.dateSelected.toDateString())}function d(e,t){t.currentMonth+=e.contains("right")?1:-1,12===t.currentMonth?(t.currentMonth=0,t.currentYear++):t.currentMonth===-1&&(t.currentMonth=11,t.currentYear--);var n=new Date(t.currentYear,t.currentMonth,1);r(n,t),t.currentMonthName=m[t.currentMonth],t.onMonthChange&&t.onMonthChange(t)}function l(e){if(!e.noPosition){var t=e.el,n=e.calendar,o=e.position,r=e.parent,a=o.top,i=o.right,s=r.getBoundingClientRect(),c=t.getBoundingClientRect(),d=n.getBoundingClientRect(),l=c.top-s.top+r.scrollTop,u="\n top:"+(l-(a?d.height:c.height*-1))+"px;\n left:"+(c.left-s.left+(i?c.width-d.width:0))+"px;\n ";n.setAttribute("style",u)}}function u(e){if(!h(e))throw new TypeError("`setDate` needs a JavaScript Date object.");e=new Date(e.toLocaleDateString()),this.currentYear=e.getFullYear(),this.currentMonth=e.getMonth(),this.currentMonthName=m[e.getMonth()],this.dateSelected=e,c(this.el,this),r(e,this)}function h(e){return"[object Date]"==={}.toString.call(e)}function f(){window.removeEventListener("click",v),window.removeEventListener("focusin",v),this.calendar.remove(),this.observer.disconnect(),this.calendar.hasOwnProperty("parentStyle")&&(this.parent.style.position="");var e=D.indexOf(this.el);e>-1&&D.splice(e,1)}function p(e,t){t.observer=new MutationObserver(function(e,n){e[0].oldValue.includes("hidden")?(l(t),t.onShow&&t.onShow(t)):t.onHide&&t.onHide(t)}),t.observer.observe(e,{attributes:1,attributeFilter:["class"],attributeOldValue:1})}function v(e){function t(e,t){var n=e.target.classList;n.contains("num")?!function(){var n="SPAN"===e.target.nodeName?e.target.parentNode:e.target,o=["disabled","active","empty"].some(function(e){return n.classList.contains(e)});!o&&s(n,t)}():n.contains("arrow")&&d(n,t)}if(!e.path){var n=e.target;for(e.path=[];n!==document;)e.path.push(n),n=n.parentNode}var o=this.calendar.classList,r=o.contains("hidden");return"focusin"===e.type?e.target===this.el&&o.remove("hidden"):void(this.noPosition?e.path.includes(this.calendar)?t(e,this):o.toggle("hidden"):r?e.target===this.el&&o.remove("hidden"):e.path.includes(this.calendar)?t(e,this):e.target!==this.el&&o.add("hidden"))}var m={0:"January",1:"February",2:"March",3:"April",4:"May",5:"June",6:"July",7:"August",8:"September",9:"October",10:"November",11:"December"},w={1:"Sun",2:"Mon",3:"Tue",4:"Wed",5:"Thu",6:"Fri",0:"Sat"},g={t:"top",r:"right",b:"bottom",l:"left"},D=[];return e});