util.js 4.6 KB

1
  1. "use strict";exports.Calendar=class{constructor({date:t,selected:e,startDate:a,endDate:s,range:l}={}){this.date=this.getDate(new Date),this.selected=e||[],this.startDate=a,this.endDate=s,this.range=l,this.cleanMultipleStatus(),this.weeks={},this.lastHover=!1}setDate(t){this.selectDate=this.getDate(t),this._getWeek(this.selectDate.fullDate)}cleanMultipleStatus(){this.multipleStatus={before:"",after:"",data:[]}}resetSatrtDate(t){this.startDate=t}resetEndDate(t){this.endDate=t}getDate(t,e=0,a="day"){t||(t=new Date),"object"!=typeof t&&(t=t.replace(/-/g,"/"));const s=new Date(t);switch(a){case"day":s.setDate(s.getDate()+e);break;case"month":31===s.getDate()?s.setDate(s.getDate()+e):s.setMonth(s.getMonth()+e);break;case"year":s.setFullYear(s.getFullYear()+e)}const l=s.getFullYear(),i=s.getMonth()+1<10?"0"+(s.getMonth()+1):s.getMonth()+1,r=s.getDate()<10?"0"+s.getDate():s.getDate();return{fullDate:l+"-"+i+"-"+r,year:l,month:i,date:r,day:s.getDay()}}_getLastMonthDays(t,e){let a=[];for(let s=t;s>0;s--){const t=new Date(e.year,e.month-1,1-s).getDate();a.push({date:t,month:e.month-1,disable:!0})}return a}_currentMonthDys(t,e){let a=[],s=this.date.fullDate;for(let l=1;l<=t;l++){let t=e.year+"-"+(e.month,e.month+"-")+(l<10?"0"+l:l),i=s===t,r=this.selected&&this.selected.find((e=>{if(this.dateEqual(t,e.date))return e})),h=!0,u=!0;this.startDate&&(h=this.dateCompare(this.startDate,t)),this.endDate&&(u=this.dateCompare(t,this.endDate));let n=this.multipleStatus.data,o=!1,D=-1;this.range&&(n&&(D=n.findIndex((e=>this.dateEqual(e,t)))),-1!==D&&(o=!0));let p={fullDate:t,year:e.year,date:l,multiple:!!this.range&&o,beforeMultiple:this.isLogicBefore(t,this.multipleStatus.before,this.multipleStatus.after),afterMultiple:this.isLogicAfter(t,this.multipleStatus.before,this.multipleStatus.after),month:e.month,disable:!(h&&u),isDay:i,userChecked:!1};r&&(p.extraInfo=r),a.push(p)}return a}_getNextMonthDays(t,e){let a=[];for(let s=1;s<t+1;s++)a.push({date:s,month:Number(e.month)+1,disable:!0});return a}getInfo(t){t||(t=new Date);return this.canlender.find((e=>e.fullDate===this.getDate(t).fullDate))}dateCompare(t,e){return(t=new Date(t.replace("-","/").replace("-","/")))<=(e=new Date(e.replace("-","/").replace("-","/")))}dateEqual(t,e){return t=new Date(t.replace("-","/").replace("-","/")),e=new Date(e.replace("-","/").replace("-","/")),t.getTime()-e.getTime()==0}isLogicBefore(t,e,a){let s=e;return e&&a&&(s=this.dateCompare(e,a)?e:a),this.dateEqual(s,t)}isLogicAfter(t,e,a){let s=a;return e&&a&&(s=this.dateCompare(e,a)?a:e),this.dateEqual(s,t)}geDateAll(t,e){var a=[],s=t.split("-"),l=e.split("-"),i=new Date;i.setFullYear(s[0],s[1]-1,s[2]);var r=new Date;r.setFullYear(l[0],l[1]-1,l[2]);for(var h=i.getTime()-864e5,u=r.getTime()-864e5,n=h;n<=u;)n+=864e5,a.push(this.getDate(new Date(parseInt(n))).fullDate);return a}setMultiple(t){let{before:e,after:a}=this.multipleStatus;if(this.range){if(e&&a){if(!this.lastHover)return void(this.lastHover=!0);this.multipleStatus.before=t,this.multipleStatus.after="",this.multipleStatus.data=[],this.multipleStatus.fulldate="",this.lastHover=!1}else e?(this.multipleStatus.after=t,this.dateCompare(this.multipleStatus.before,this.multipleStatus.after)?this.multipleStatus.data=this.geDateAll(this.multipleStatus.before,this.multipleStatus.after):this.multipleStatus.data=this.geDateAll(this.multipleStatus.after,this.multipleStatus.before),this.lastHover=!0):(this.multipleStatus.before=t,this.lastHover=!1);this._getWeek(t)}}setHoverMultiple(t){let{before:e,after:a}=this.multipleStatus;this.range&&(this.lastHover||(e?(this.multipleStatus.after=t,this.dateCompare(this.multipleStatus.before,this.multipleStatus.after)?this.multipleStatus.data=this.geDateAll(this.multipleStatus.before,this.multipleStatus.after):this.multipleStatus.data=this.geDateAll(this.multipleStatus.after,this.multipleStatus.before)):this.multipleStatus.before=t,this._getWeek(t)))}setDefaultMultiple(t,e){this.multipleStatus.before=t,this.multipleStatus.after=e,t&&e&&(this.dateCompare(t,e)?(this.multipleStatus.data=this.geDateAll(t,e),this._getWeek(e)):(this.multipleStatus.data=this.geDateAll(e,t),this._getWeek(t)))}_getWeek(t){const{fullDate:e,year:a,month:s,date:l,day:i}=this.getDate(t);let r=new Date(a,s-1,1).getDay(),h=new Date(a,s,0).getDate(),u={lastMonthDays:this._getLastMonthDays(r,this.getDate(t)),currentMonthDys:this._currentMonthDys(h,this.getDate(t)),nextMonthDays:[],weeks:[]},n=[];const o=42-(u.lastMonthDays.length+u.currentMonthDys.length);u.nextMonthDays=this._getNextMonthDays(o,this.getDate(t)),n=n.concat(u.lastMonthDays,u.currentMonthDys,u.nextMonthDays);let D={};for(let p=0;p<n.length;p++)p%7==0&&(D[parseInt(p/7)]=new Array(7)),D[parseInt(p/7)][p%7]=n[p];this.canlender=n,this.weeks=D}};