skipper-organisms
Version:
A vanilla JavaScript library for skipperhospitality management
2 lines (1 loc) • 14.9 kB
JavaScript
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const b=function(t,e,i){let n,a;for(n in e)a=t[n]!==void 0,a&&typeof e[n]=="object"&&e[n]!==null&&e[n].nodeName===void 0?h(e[n])?i&&(t[n]=new Date(e[n].getTime())):Array.isArray(e[n])?i&&(t[n]=e[n].slice(0)):t[n]=b({},e[n],i):(i||!a)&&(t[n]=e[n]);return t},h=function(t){return/Date/.test(Object.prototype.toString.call(t))&&!isNaN(t.getTime())},z=function(t){return t%4===0&&t%100!==0||t%400===0},S=t=>t.offsetParent===null,G=function(t,e,i){return e.getTime()>i.getTime()&&i.getTime()>t.getTime()},k=function(t,e,i,n){window.addEventListener?t.addEventListener(e,i,!!n):t.attachEvent("on"+e,i)},K=function(t,e,i,n){window.addEventListener?t.removeEventListener(e,i,!!n):t.detachEvent("on"+e,i)},x=function(t,e){u(t,e)||(t.className=t.className===""?e:t.className+" "+e)},E=function(t,e){t.className=Q((" "+t.className+" ").replace(" "+e+" "," "))},u=function(t,e){return(" "+t.className+" ").indexOf(" "+e+" ")!==-1},Q=function(t){return t.trim?t.trim():t.replace(/^\s+|\s+$/g,"")},C=function(t,e){return[31,z(t)?29:28,31,30,31,30,31,31,30,31,30,31][e]},D=function(t){h(t)&&t.setHours(0,0,0,0)},I=function(t,e){return t.getTime()===e.getTime()};const R=window.setTimeout,l={checkIn:null,checkOut:null,checkInLabel:null,checkOutLabel:null,priceList:null,bound:void 0,container:null,position:"bottom left",reposition:!0,showPrice:!1,numberOfMonths:2,startDate:null,endDate:null,format:"YYYY-MM-DD",toString:null,parse:null,minDate:null,maxDate:null,minYear:0,maxYear:9999,minMonth:void 0,maxMonth:void 0,dpickerConst:{previousMonth:"Previous Month",nextMonth:"Next Month",months:["January","February","March","April","May","June","July","August","September","October","November","December"],monthsShort:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],weekdays:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],weekdaysShort:["Su","Mo","Tu","We","Th","Fr","Sa"]},onSelect:null,onOpen:null,onClose:null},y=function(t,e,i){let n;document.createEvent?(n=document.createEvent("HTMLEvents"),n.initEvent(e,!0,!1),n=b(n,i),t.dispatchEvent(n)):document.createEventObject&&(n=document.createEventObject(),n=b(n,i),t.fireEvent("on"+e,n))},N=function(t){return t.month<0&&(t.year-=Math.ceil(Math.abs(t.month)/12),t.month+=12),t.month>11&&(t.year+=Math.floor(Math.abs(t.month)/12),t.month-=12),t},Y=function(t,e,i){for(;e>=7;)e-=7;return i?t.dpickerConst.weekdaysShort[e]:t.dpickerConst.weekdays[e]},P=function(t,e){return e.getDate()+"<small>"+t.dpickerConst.monthsShort[e.getMonth()]+"</small>"},U=function(t){let e=[],i="false";if(t.isEmpty)return'<td class="is-empty"></td>';t.isToday&&e.push("is-today"),(t.isFirst||t.isEnd)&&(e.push("is-selected"),i="true"),t.isInBetween&&(e.push("is-inBetween"),i="true"),t.isDisabled&&e.push("is-disabled");let n=t.showPrice?`<div class="day-box"><div class="day-date">${t.day}</div>${t.price?'<div class="day-price">$'+t.price+"</div>":""}</div>`:`${t.day}`;return'<td data-day="'+t.day+'" class="'+e.join(" ")+'" aria-selected="'+i+'"><button class="daypicker-button daypicker-day" type="button" data-daypicker-year="'+t.year+'" data-daypicker-month="'+t.month+'" data-daypicker-day="'+t.day+'">'+n+"</button></td>"},Z=function(t){return`<tr class="daypicker-row">${t.join("")}</tr>`},tt=function(t){return"<tbody>"+t.join("")+"</tbody>"},et=function(t){let e,i=[];for(t.showWeekNumber&&i.push("<th></th>"),e=0;e<7;e++)i.push('<th scope="col"><span title="'+Y(t,e)+'">'+Y(t,e,!0)+"</span></th>");return`<thead><tr class="days-name">${i.join("")}</tr></thead>`},it=function(t,e,i,n,a){let s=t._o,r=!0,o=!0,d=`<div id="${a}" class="daypicker-title" role="heading" aria-live="assertive">`;return d+=`<div class="daypicker-label"> ${s.dpickerConst.months[n]} ${i} </div>`,(s.minYear>i||s.minMonth>=n)&&(r=!1),e===0&&r&&(d+=`<button class="daypicker-prev" type="button">${s.dpickerConst.previousMonth}</button>`),(s.maxYear<i||s.maxMonth<=n)&&(o=!1),screen.width>768?e===t._o.numberOfMonths-1&&o&&(d+=`<button class="daypicker-next" type="button">${s.dpickerConst.nextMonth}</button>`):e===0&&o&&(d+=`<button class="daypicker-next" type="button">${s.dpickerConst.nextMonth}</button>`),d+="</div>"},nt=function(t,e,i){return'<table cellpadding="0" cellspacing="0" class="daypicker-table" role="grid" aria-labelledby="'+i+'">'+et(t)+tt(e)+"</table>"};let H=function(t){let e=this,i=e.config(t);this.goToNextMonth=function(){e.nextMonth()},this.goToPrevMonth=function(){e.prevMonth()},this.selectStartDate=s=>{e.setStartDate(s)},this.selectEndDate=s=>{e.setEndDate(s)},this.getCurrentStartDate=()=>this._d.start&&h(this._d.start)?this.format(i.format,"start"):"",this.getCurrentEndDate=()=>this._d.end&&h(this._d.end)?this.format(i.format,"end"):"",this.getCurrentDateRange=()=>this._o.checkIn.value+" - "+this._o.checkOut.value,this.setDateRange=(s,r)=>{e.setStartDate(s),e.setEndDate(r)},this.setPriceList=s=>{i.priceList={},s.length>0&&s.forEach(r=>{let o=new Date(r.date),d=o.getFullYear()+"_"+o.getMonth()+"_"+o.getDate();i.priceList[d]=r.price})};function n(s){e.setDate(new Date(s.getAttribute("data-daypicker-year"),s.getAttribute("data-daypicker-month"),s.getAttribute("data-daypicker-day"))),!!i.bound&&!!e._d&&!!e._d.start&&!!e._d.end&&R(function(){e.hide()},500)}e._onMouseDown=function(s){if(!e._view)return;s=s||window.event;let r=s.target||s.srcElement;if(!!r){if(u(r,"daypicker-button")&&!u(r,"is-empty"))n(r);else if(u(r,"day-box")){const o=r.parentNode;n(o)}else if(u(r,"day-date")||u(r,"day-price")){const o=r.parentNode.parentNode;n(o)}else u(r,"daypicker-prev")?e.prevMonth():u(r,"daypicker-next")&&e.nextMonth();if(s.preventDefault)s.preventDefault();else return s.returnValue=!1,!1}},e._parseFieldValue=function(){return i.parse?i.parse(i.checkIn.value,i.format):new Date(Date.parse(i.checkIn.value))},e._onInputClick=function(){e.show()},e._onClick=function(s){s=s||window.event;let r=s.target||s.srcElement,o=r;if(!!r){do if(u(o,"daypicker-range")||o===i.trigger)return;while(o=o.parentNode);e._view&&r!==i.checkIn&&r!==i.checkOut&&e.hide()}},e.el=document.createElement("div"),e.el.className="daypicker-range",k(e.el,"mousedown",e._onMouseDown,!0),k(e.el,"touchend",e._onMouseDown,!0),i.checkIn&&(i.container&&screen.width<768?i.container.appendChild(e.el):i.bound?document.body.appendChild(e.el):i.checkIn.parentNode.insertBefore(e.el,i.checkIn.nextSibling));let a=new Date;h(i.minDate)&&i.minDate.getTime()>a.getTime()?a=i.minDate:h(i.maxDate)&&i.maxDate.getTime()<a.getTime()&&(a=i.maxDate),e.gotoDate(a),i.bound&&(this.hide(),e.el.className+=" is-bound",k(i.checkIn,"click",e._onInputClick),k(i.checkOut,"click",e._onInputClick))};H.prototype={config:function(t){this._o||(this._o=b({},l,!0));let e=b(this._o,t,!0);return e.checkIn=e.checkIn&&e.checkIn.nodeName?e.checkIn:null,e.bound=!!(e.bound!==void 0?0:e.checkIn),e.trigger=e.trigger&&e.trigger.nodeName?e.trigger:e.checkIn,h(e.minDate)||(e.minDate=!1),h(e.maxDate)||(e.maxDate=!1),e.minDate&&e.maxDate&&e.maxDate<e.minDate&&(e.maxDate=e.minDate=!1),e.minDate&&this.setMinDate(e.minDate),e.maxDate&&this.setMaxDate(e.maxDate),h(this._o.startDate)&&this.setStartDate(this._o.startDate),h(this._o.endDate)&&this.setEndDate(this._o.endDate),e},toString:function(t){return t=t||this._o.format,h(this._d.start)?this._o.toString?this._o.toString(this._d.start,t):this._d&&this._d.start&&this._d.end?this.format(t,"start")+" - "+this.format(t,"end"):this._d&&this._d.start?this.format(t,"start"):"":""},getStartDate:function(t){return t=t||this._o.format,h(this._d.start)&&this._d&&this._d.start?this.format(t,"start"):""},getStartDateLabel:function(){return this._d&&this._d.start?P(this._o,this._d.start):""},getEndDate:function(t){return t=t||this._o.format,h(this._d.end)&&this._d&&this._d.end?this.format(t,"end"):""},getEndDateLabel:function(){return this._d&&this._d.end?P(this._o,this._d.end):""},format:function(t,e){let i={"M+":this._d[e].getMonth()+1,"d+":this._d[e].getDate(),"D+":this._d[e].getDate(),"h+":this._d[e].getHours(),"m+":this._d[e].getMinutes(),"s+":this._d[e].getSeconds(),"q+":Math.floor((this._d.start.getMonth()+3)/3),S:this._d.start.getMilliseconds()};/((Y+)|(y+))/.test(t)&&(t=t.replace(RegExp.$1,(this._d[e].getFullYear()+"").substr(4-RegExp.$1.length)));for(let n in i)new RegExp("("+n+")").test(t)&&(t=t.replace(RegExp.$1,RegExp.$1.length==1?i[n]:("00"+i[n]).substr((""+i[n]).length)));return t},getDate:function(){return h(this._d)?new Date(this._d.getTime()):null},setDate:function(t,e){if(!t)return this._d=null,this._o.checkIn&&(this._o.checkIn.value="",y(this._o.checkIn,"change",{firedBy:this})),this.draw();if(typeof t=="string"&&(t=new Date(Date.parse(t))),!h(t))return;let i=this._o.minDate,n=this._o.maxDate;h(i)&&t<i?t=i:h(n)&&t>n&&(t=n),this._d&&this._d.start&&this._d.end&&(this._d=null);let a=!this._d||!this._d.start?"start":"end";if(a==="start"&&(this._d={}),this._d[a]=new Date(t.getTime()),D(this._d[a]),this.gotoDate(this._d[[a]]),a=="end"&&this._d.start.getTime()>this._d.end.getTime()){let s=this._d.start;this._d.start=this._d.end,this._d.end=s}this._o.checkIn&&(this._o.checkIn.value=this.getStartDate(),this._o.checkInLabel&&(this._o.checkInLabel.innerHTML=this.getStartDateLabel()),y(this._o.checkIn,"change",{firedBy:this})),this._o.checkOut&&(this._o.checkOut.value=this.getEndDate(),this._o.checkOutLabel&&(this._o.checkOutLabel.innerHTML=this.getEndDateLabel()),y(this._o.checkOut,"change",{firedBy:this})),!e&&typeof this._o.onSelect=="function"&&this._o.onSelect.call(this,this.getDate())},setStartDate:function(t){if(!t||(typeof t=="string"&&(t=new Date(Date.parse(t))),!h(t)))return;let e=this._o.minDate;h(e)&&t<e&&(t=e);let i="start";this._d||(this._d={}),this._d[i]=new Date(t.getTime()),D(this._d[i]),this.gotoDate(this._d[[i]]),this._o.checkIn&&(this._o.checkIn.value=this.getStartDate(),this._o.checkInLabel&&(this._o.checkInLabel.innerHTML=this.getStartDateLabel()),y(this._o.checkIn,"change",{firedBy:this}))},setEndDate:function(t){if(!t||(typeof t=="string"&&(t=new Date(Date.parse(t))),!h(t)))return;let e=this._o.maxDate;h(e)&&t>e&&(t=e);let i="end";this._d||(this._d={}),this._d[i]=new Date(t.getTime()),D(this._d[i]),this.gotoDate(this._d[[i]]),this._o.checkOut&&(this._o.checkOut.value=this.getEndDate(),this._o.checkOutLabel&&(this._o.checkOutLabel.innerHTML=this.getEndDateLabel()),y(this._o.checkOut,"change",{firedBy:this}))},gotoDate:function(t){let e=!0;if(!!h(t)){if(this.calendars){let i=new Date(this.calendars[0].year,this.calendars[0].month,1),n=new Date(this.calendars[this.calendars.length-1].year,this.calendars[this.calendars.length-1].month,1),a=t.getTime();n.setMonth(n.getMonth()+1),n.setDate(n.getDate()-1),e=a<i.getTime()||n.getTime()<a}e&&(this.calendars=[{month:t.getMonth(),year:t.getFullYear()}]),this.adjustCalendars()}},adjustCalendars:function(){this.calendars[0]=N(this.calendars[0]);for(let t=1;t<this._o.numberOfMonths;t++)this.calendars[t]=N({month:this.calendars[0].month+t,year:this.calendars[0].year});this.draw()},nextMonth:function(){this.calendars[0].month++,this.adjustCalendars()},prevMonth:function(){this.calendars[0].month--,this.adjustCalendars()},setMinDate:function(t){t instanceof Date?(D(t),this._o.minDate=t,this._o.minYear=t.getFullYear(),this._o.minMonth=t.getMonth()):(this._o.minDate=l.minDate,this._o.minYear=l.minYear,this._o.minMonth=l.minMonth,this._o.startRange=l.startRange),this.draw()},setMaxDate:function(t){t instanceof Date?(D(t),this._o.maxDate=t,this._o.maxYear=t.getFullYear(),this._o.maxMonth=t.getMonth()):(this._o.maxDate=l.maxDate,this._o.maxYear=l.maxYear,this._o.maxMonth=l.maxMonth,this._o.endRange=l.endRange),this.draw()},draw:function(t){if(!this._view&&!t)return;let e=this._o,i="",n;for(let a=0;a<e.numberOfMonths;a++)n="daypicker-title-"+Math.random().toString(36).replace(/[^a-z]+/g,"").substr(0,2),i+='<div class="daypicker-lendar">'+it(this,a,this.calendars[a].year,this.calendars[a].month,n)+this.render(this.calendars[a].year,this.calendars[a].month,n)+"</div>";this.el.innerHTML=i,e.bound&&e.checkIn.type!=="hidden"&&R(function(){e.trigger.focus()},200)},adjustPosition:function(){let t,e,i,n,a,s,r,o,d,c,g,f;if(t=this._o.trigger,e=t,i=this.el.offsetWidth,n=this.el.offsetHeight,a=window.innerWidth||document.documentElement.clientWidth,s=window.innerHeight||document.documentElement.clientHeight,r=window.pageYOffset||document.body.scrollTop||document.documentElement.scrollTop,g=!0,f=!0,!S(this._o.checkIn)&&typeof this._o.checkIn.getBoundingClientRect=="function")c=this._o.checkIn.getBoundingClientRect(),o=c.left+window.pageXOffset,d=c.bottom+window.pageYOffset;else if(!S(this._o.checkInLabel)&&typeof this._o.checkInLabel.getBoundingClientRect=="function")c=this._o.checkInLabel.getBoundingClientRect(),o=c.left+window.pageXOffset,d=c.bottom+window.pageYOffset;else for(o=e.offsetLeft,d=e.offsetTop+e.offsetHeight;e=e.offsetParent;)o+=e.offsetLeft,d+=e.offsetTop;(this._o.reposition&&o+i>a||this._o.position.indexOf("right")>-1&&o-i+t.offsetWidth>0)&&(o=o-i+t.offsetWidth,g=!1),(this._o.reposition&&d+n>s+r||this._o.position.indexOf("top")>-1&&d-n-t.offsetHeight>0)&&(d=d-n-t.offsetHeight,f=!1),o<0&&(o=0),d<0&&(d=0),this.el.style.left=o+"px",this.el.style.top=d+"px",x(this.el,g?"left-aligned":"right-aligned"),x(this.el,f?"bottom-aligned":"top-aligned"),E(this.el,g?"right-aligned":"left-aligned"),E(this.el,f?"top-aligned":"bottom-aligned")},render:function(t,e,i){let n=this._o,a=new Date,s=C(t,e),r=new Date(t,e,1).getDay(),o=[],d=[];D(a);let c=e===0?11:e-1,g=e===11?0:e+1,f=e===0?t-1:t,$=e===11?t+1:t,j=C(f,c),v=s+r,B=v%7;v+=7-B;for(let _=0,T=0;_<v;_++){let m=new Date(t,e,1+(_-r)),A=!!this._d&&!!this._d.start&&h(this._d.start)?I(m,this._d.start):!1,F=!!this._d&&!!this._d.end&&h(this._d.end)?I(m,this._d.end):!1,W=!!this._d&&!!this._d.start&&!!this._d.end?G(this._d.start,this._d.end,m):!1,J=I(m,a),L=_<r||_>=s+r,p=1+(_-r),w=e,M=t,V=n.minDate&&m<n.minDate||n.maxDate&&m>n.maxDate;L&&(_<r?(p=j+p,w=c,M=f):(p=p-s,w=g,M=$));let O=M+"_"+w+"_"+p,X=n.priceList[O]?n.priceList[O]:null,q={day:p,month:w,year:M,isFirst:A,isEnd:F,isInBetween:W,isToday:J,isEmpty:L,isDisabled:V,showPrice:n.showPrice,price:X,showDaysInNextAndPreviousMonths:n.showDaysInNextAndPreviousMonths};d.push(U(q)),++T===7&&(o.push(Z(d)),d=[],T=0)}return nt(n,o,i)},isVisible:function(){return this._view},show:function(){this.isVisible()||(this._view=!0,this.draw(),E(this.el,"is-hidden"),this._o.bound&&(k(document,"click",this._onClick),this.adjustPosition()),typeof this._o.onOpen=="function"&&this._o.onOpen.call(this))},hide:function(){let t=this._view;t!==!1&&(this._o.bound&&K(document,"click",this._onClick),this.el.style.position="static",this.el.style.left="auto",this.el.style.top="auto",x(this.el,"is-hidden"),this._view=!1,t!==void 0&&typeof this._o.onClose=="function"&&this._o.onClose.call(this))}};exports.DayRangePicker=H;