UNPKG

11.8 kBJavaScriptView Raw
1var uiloosDateGallery=function(t){"use strict";class s{constructor(){this.t=[],this.i=0}h(t){this.i>0&&(this.t.push(t),this.t.length-1===this.i&&this.t.shift())}o(t=0){this.i=t}}class i{constructor(){this.D=[]}l(t){return this.D.push(t),()=>{this.u(t)}}u(t){this.D=this.D.filter((s=>t!==s))}m(){this.D.length=0}v(t,s){this.D.forEach((i=>i(t,s)))}}const e=["day","week","month","month-six-weeks","month-pad-to-week","year"];class h{constructor(t,s,i,e,h){this.canBeSelected=!0,this.dateGallery=t,this.date=s,this.events=i,this.isPadding=e,this.isSelected=h,this.isToday=t._(new Date,s),this.hasEvents=i.length>0,this.hasEventsWithOverlap=this.events.some((t=>t.isOverlapping)),t.g&&(this.canBeSelected=t.g(this))}select(){this.dateGallery.selectDate(this.date)}deselect(){this.dateGallery.deselectDate(this.date)}toggle(){this.dateGallery.toggleDateSelection(this.date)}}function n(t,s){const i=t.startDate<s.startDate?t:s,e=(i===t?s:t).startDate.getTime(),h=i.startDate.getTime(),n=i.endDate.getTime();return e>=h&&e<n}class r{constructor(t,s,i,e){this.overlappingEvents=[],this.isOverlapping=!1,this.spansMultipleDays=!1,this.dateGallery=t,this.data=s,this.startDate=i,this.endDate=e}p(){this.overlappingEvents.length=0,this.dateGallery.events.forEach((t=>{t!==this&&n(this,t)&&this.overlappingEvents.push(t)})),this.overlappingEvents.sort(((t,s)=>t.startDate.getTime()-s.startDate.getTime())),this.spansMultipleDays=!this.dateGallery._(this.startDate,this.endDate),this.isOverlapping=this.overlappingEvents.length>0}remove(){this.dateGallery.removeEvent(this)}move(t){this.dateGallery.moveEvent(this,t)}changeData(t){this.dateGallery.changeEventData(this,t)}}const o="uiloos > DateGallery >";class a extends Error{constructor(){super(`${o} invalid range, an events startDate lies after its endDate`),this.name="DateGalleryEventInvalidRangeError"}}class c extends Error{constructor(t){super(`${o} ${t} > "DateGalleryEvent" not found in events array`),this.name="DateGalleryEventNotFoundError"}}class D extends Error{constructor(){super(`${o} invalid firstDayOfWeek`),this.name="DateGalleryFirstDayOfWeekError"}}class l extends Error{constructor(t,s){super(`${o} ${t} > "${s}" is an or contains an invalid date`),this.name="DateGalleryInvalidDateError"}}class u extends Error{constructor(t){super(`${o} unknown mode: "${t}" provided`),this.name="DateGalleryModeError"}}class d extends Error{constructor(){super(`${o} numberOfFrames cannot be negative or zero`),this.name="DateGalleryNumberOfFramesError"}}class w extends Error{constructor(t){super(`${o} ${t} > selection limit reached`),this.name="DateGallerySelectionLimitReachedError"}}return t.DATE_GALLERY_MODES=e,t.DateGallery=class{constructor(t={},e){this.k=!1,this.isUTC=!1,this.frames=[],this.firstFrame={dates:[],events:[],anchorDate:new Date},this.numberOfFrames=1,this.maxSelectionLimit=!1,this.maxSelectionLimitBehavior="circular",this.selectedDates=[],this.g=void 0,this.events=[],this.mode="month-six-weeks",this.firstDayOfWeek=0,this.A=new Date,this.T=new s,this.history=this.T.t,this.G=new i,uiloosLicenseChecker.licenseChecker._checkLicense(),e&&this.subscribe(e),this.$(t,"constructor")}subscribe(t){return this.G.l(t)}unsubscribe(t){this.G.u(t)}unsubscribeAll(){this.G.m()}initialize(t){this.$(t,"initialize")}$(t,s){if(this.k=!0,this.isUTC=void 0!==t.isUTC&&t.isUTC,this.mode=t.mode?t.mode:"month-six-weeks",this.N(this.mode),this.firstDayOfWeek=t.firstDayOfWeek?t.firstDayOfWeek:0,this.firstDayOfWeek<0||this.firstDayOfWeek>6)throw new D;if(this.numberOfFrames=void 0!==t.numberOfFrames?t.numberOfFrames:1,this.numberOfFrames<=0)throw new d;this.maxSelectionLimit=void 0!==t.maxSelectionLimit&&t.maxSelectionLimit,this.maxSelectionLimitBehavior=void 0!==t.maxSelectionLimitBehavior?t.maxSelectionLimitBehavior:"circular",this.A=t.initialDate?this.S(t.initialDate,s,"initialDate"):new Date,this.C(this.A),this.F(),this.events.length=0,this.selectedDates.length=0,this.g=t.canSelect,t.selectedDates&&t.selectedDates.forEach((t=>{this.selectDate(this.S(t,s,"selectedDates"))})),t.events&&(t.events.forEach((t=>{this.L(t,s)})),this.events.forEach((t=>{t.p()}))),this.O(),this.T.t.length=0,this.T.o(t.keepHistoryFor),this.k=!1;const i={type:"INITIALIZED",time:new Date};this.v(i)}changeConfig(t){if(void 0===t.initialDate&&void 0===t.mode&&void 0===t.numberOfFrames)return;let s=!1,i=!1;if(void 0!==t.mode&&(this.N(t.mode),this.mode!==t.mode&&(this.mode=t.mode,s=!0,i=!0)),void 0!==t.initialDate){const e=this.S(t.initialDate,"changeConfig","initialDate");this.C(e),this._(e,this.A)||(this.A=e,s=!0,i=!0)}if(void 0!==t.numberOfFrames){if(t.numberOfFrames<=0)throw new d;this.numberOfFrames!==t.numberOfFrames&&(this.numberOfFrames=t.numberOfFrames,s=!0)}if(s){i&&this.F(),this.O();const t={type:"CONFIG_CHANGED",mode:this.mode,anchorDate:new Date(this.A),numberOfFrames:this.numberOfFrames,frames:this.frames,time:new Date};this.v(t)}}today(){this.changeConfig({initialDate:new Date})}O(t=!1){this.frames.length=0;const s=this.A;for(let t=0;t<this.numberOfFrames;t++){0!==t&&this.I(1);const s={dates:[],events:[],anchorDate:new Date(this.A)},i=new Date(this.A);if("day"===this.mode)s.dates.push(this.M(i));else if("week"===this.mode)this.R(i,7,s);else if("year"===this.mode){const t=this.V(i),e=t%4==0&&t%100!=0||t%400==0;this.R(i,e?366:365,s)}else if("month"===this.mode){const t=this.H(i);this.W(i,t,s)}else if("month-pad-to-week"===this.mode){const t=this.j(i),e=this.H(i);for(;this.H(t)!==e;)this.P(t,s);for(this.W(t,e,s);this.U(t)!==this.firstDayOfWeek;)this.P(t,s)}else if("month-six-weeks"===this.mode){const t=this.j(i);this.R(t,42,s)}this.frames.push(s);const e=new Date(s.dates[0].date),h=new Date(s.dates[s.dates.length-1].date);this.C(e),this.B(h,1),this.C(h),this.events.forEach((t=>{n({startDate:e,endDate:h},t)&&s.events.push(t)}))}if(this.A=s,this.firstFrame=this.frames[0],t){const t={type:"FRAME_CHANGED",frames:this.frames,time:new Date};this.v(t)}}next(){this.I(this.numberOfFrames),this.O(!0)}previous(){this.I(-this.numberOfFrames),this.O(!0)}I(t){const s=new Date(this.A);"day"===this.mode?this.B(s,1*t):"week"===this.mode?this.B(s,7*t):"year"===this.mode?this.isUTC?s.setUTCFullYear(s.getUTCFullYear()+1*t):s.setFullYear(s.getFullYear()+1*t):this.isUTC?s.setUTCMonth(s.getUTCMonth()+1*t):s.setMonth(s.getMonth()+1*t),this.A=s}selectDate(t){const s="selectDate",i=this.S(t,s,"date"),[e]=this.K(i,s);-1===e&&this.Y(i,s)}Y(t,s){const i=[],e=this.Z(t,s,i);if(!e)return;this.O();let h=i[0];h||(h=null);const n={type:"DATE_SELECTED",date:new Date(e),deselectedDate:h,time:new Date};this.v(n)}Z(t,s,i){const e=this.S(t,s,"date");if(this.C(e),this.g&&!this.g(this.M(e)))return null;if(!1!==this.maxSelectionLimit&&this.maxSelectionLimit===this.selectedDates.length){if("error"===this.maxSelectionLimitBehavior)throw new w(s);if("ignore"===this.maxSelectionLimitBehavior)return null;{const t=this.selectedDates.shift();t&&i.push(t)}}return this.selectedDates.push(e),e}deselectDate(t){const[s,i]=this.K(t,"deselectDate");-1!==s&&this.q(s,i)}q(t,s){this.selectedDates.splice(t,1),this.O();const i={type:"DATE_DESELECTED",date:s,time:new Date};this.v(i)}toggleDateSelection(t){const[s,i]=this.K(t,"toggleDateSelection");-1===s?this.Y(i,"toggleDateSelection"):this.q(s,i)}K(t,s){const i=this.S(t,s,"date");return[this.selectedDates.findIndex((t=>this._(t,i))),i]}deselectAll(){if(0===this.selectedDates.length)return;const t=[...this.selectedDates];this.selectedDates.length=0,this.O();const s={type:"DATE_DESELECTED_MULTIPLE",dates:t,time:new Date};this.v(s)}selectRange(t,s){const i="selectRange",e=this.S(t,i,"a"),h=this.S(s,i,"b"),n=h.getTime()>e.getTime()?e:h,r=e===n?h:e;this.C(n),this.B(r,1),this.C(r);const o=new Date(n),a=[...this.selectedDates],c=[],D=[];let l=null;try{for(;!this._(o,r);){if(-1===this.selectedDates.findIndex((t=>this._(t,o)))){const t=this.Z(o,i,D);t&&c.push(t)}this.B(o,1)}}catch(t){t instanceof w&&(l=t)}const u=c.filter((t=>!D.includes(t)));if(0===u.length){if(l)throw l;return}const d=a.filter((t=>!this.selectedDates.some((s=>this._(t,s)))));this.O();const f={type:"DATE_SELECTED_MULTIPLE",dates:u.map((t=>new Date(t))),deselectedDates:d.map((t=>new Date(t))),time:new Date};if(this.v(f),l)throw l}addEvent(t){const s=this.L(t,"addEvent");this.events.forEach((t=>{t.p()})),this.O();const i={type:"EVENT_ADDED",event:s,time:new Date};return this.v(i),s}L(t,s){const i=this.S(t.startDate,s,"event.startDate"),e=this.S(t.endDate,s,"event.endDate");if(i.getTime()>e.getTime())throw new a;const h=new r(this,t.data,i,e);return this.events.push(h),this.events.sort(((t,s)=>t.startDate.getTime()-s.startDate.getTime())),h}removeEvent(t){const s=this.events.indexOf(t);if(-1===s)return t;this.events.splice(s,1),this.events.forEach((t=>{t.p()})),this.O();const i={type:"EVENT_REMOVED",event:t,time:new Date};return this.v(i),t}moveEvent(t,s){const i=this.S(s.startDate,"moveEvent","range.startDate"),e=this.S(s.endDate,"moveEvent","range.endDate");if(i.getTime()>e.getTime())throw new a;if(i.getTime()===t.startDate.getTime()&&e.getTime()===t.endDate.getTime())return;if(-1===this.events.indexOf(t))throw new c("moveEvent");t.startDate=i,t.endDate=e,this.events.sort(((t,s)=>t.startDate.getTime()-s.startDate.getTime())),this.events.forEach((t=>{t.p()})),this.O();const h={type:"EVENT_MOVED",event:t,time:new Date};this.v(h)}changeEventData(t,s){if(-1===this.events.indexOf(t))throw new c("changeEventData");t.data=s;const i={type:"EVENT_DATA_CHANGED",event:t,data:s,time:new Date};this.v(i)}F(){"year"===this.mode?this.isUTC?(this.A.setUTCDate(1),this.A.setUTCMonth(0)):(this.A.setDate(1),this.A.setMonth(0)):"week"===this.mode?this.A=this.j(this.A):this.mode.startsWith("month")&&(this.isUTC?this.A.setUTCDate(1):this.A.setDate(1))}v(t){this.k||(this.T.h(t),this.G.v(this,t))}S(t,s,i){t instanceof Date&&this.J(t,s,i);const e=new Date(t);return this.J(e,s,i),e}J(t,s,i){if(!("[object Date]"===Object.prototype.toString.call(t)&&!isNaN(t.valueOf())))throw new l(s,i)}j(t){const s=new Date(t);for(;this.U(s)!==this.firstDayOfWeek;)this.B(s,-1);return s}R(t,s,i){for(let e=0;e<s;e++)this.P(t,i)}W(t,s,i){for(;this.H(t)===s;)this.P(t,i)}P(t,s){s.dates.push(this.M(t)),this.B(t,1)}M(t){const s=new Date(t);this.C(s);let i=!1;if("month-pad-to-week"===this.mode||"month-six-weeks"===this.mode){const t=this.H(this.A);i=this.H(s)!==t}return new h(this,s,this.events.filter((t=>{const i=s.getTime(),e=new Date(t.startDate),h=this.C(e),n=new Date(t.endDate);this.B(n,1);const r=this.C(n);return i>=h&&i<r})),i,this.selectedDates.some((t=>this._(t,s))))}isSameDay(t,s){const i="isSameDay";return this._(this.S(t,i,"a"),this.S(s,i,"b"))}_(t,s){return this.V(t)===this.V(s)&&this.H(t)===this.H(s)&&this.X(t)===this.X(s)}V(t){return this.isUTC?t.getUTCFullYear():t.getFullYear()}H(t){return this.isUTC?t.getUTCMonth():t.getMonth()}X(t){return this.isUTC?t.getUTCDate():t.getDate()}U(t){return this.isUTC?t.getUTCDay():t.getDay()}N(t){if(!e.includes(t))throw new u(t)}C(t){return this.isUTC?t.setUTCHours(0,0,0,0):t.setHours(0,0,0,0)}B(t,s){this.isUTC?t.setUTCDate(t.getUTCDate()+s):t.setDate(t.getDate()+s)}},t.DateGalleryDate=h,t.DateGalleryEvent=r,t.DateGalleryEventInvalidRangeError=a,t.DateGalleryEventNotFoundError=c,t.DateGalleryFirstDayOfWeekError=D,t.DateGalleryInvalidDateError=l,t.DateGalleryModeError=u,t.DateGalleryNumberOfFramesError=d,t.DateGallerySelectionLimitReachedError=w,t.createDateGallerySubscriber=function(t){return(s,i)=>{!function(t,s,i,e){const h="on"+s.type.toLowerCase().split("_").reduce(((t,s)=>{const i=s.split("");return i[0]=i[0].toUpperCase(),t+i.join("")}),""),n=e[h];n?n(s,i):e.debug&&console.warn(`uiloos > ${t} event '${s.type}' was fired but '${h}' method is not implemented, this might not be correct.`)}("createDateGallerySubscriber",i,s,t)}},t}({});