evc
Version:
A simple javascript event calendar
1 lines • 8.18 kB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.EVC=e():t.EVC=e()}(self,(()=>(()=>{"use strict";var t={d:(e,n)=>{for(var a in n)t.o(n,a)&&!t.o(e,a)&&Object.defineProperty(e,a,{enumerable:!0,get:n[a]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};t.r(e),t.d(e,{CalendarEvent:()=>h,EventCalendar:()=>N});const n={randomUUID:"undefined"!=typeof crypto&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};let a;const i=new Uint8Array(16);function r(){if(!a&&(a="undefined"!=typeof crypto&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!a))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return a(i)}const o=[];for(let t=0;t<256;++t)o.push((t+256).toString(16).slice(1));const s=function(t,e,a){if(n.randomUUID&&!e&&!t)return n.randomUUID();const i=(t=t||{}).random||(t.rng||r)();if(i[6]=15&i[6]|64,i[8]=63&i[8]|128,e){a=a||0;for(let t=0;t<16;++t)e[a+t]=i[t];return e}return function(t,e=0){return o[t[e+0]]+o[t[e+1]]+o[t[e+2]]+o[t[e+3]]+"-"+o[t[e+4]]+o[t[e+5]]+"-"+o[t[e+6]]+o[t[e+7]]+"-"+o[t[e+8]]+o[t[e+9]]+"-"+o[t[e+10]]+o[t[e+11]]+o[t[e+12]]+o[t[e+13]]+o[t[e+14]]+o[t[e+15]]}(i)};function u(t,e){for(var n=0;n<e.length;n++){var a=e[n];a.enumerable=a.enumerable||!1,a.configurable=!0,"value"in a&&(a.writable=!0),Object.defineProperty(t,a.key,a)}}var h=function(){function t(e){var n=e.id,a=void 0===n?null:n,i=e.title,r=e.start,o=e.end,u=void 0===o?null:o,h=e.themeColor,l=void 0===h?"#03a9f4":h,c=e.props,f=void 0===c?{}:c;if(function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.id=a||s(),this.title=i,u&&void 0!==u.weekDay&&Number.isInteger(u.weekDay)&&(void 0===r.weekDay||!Number.isInteger(r.weekDay)))throw"End day of weekly event is given but no start day is given";this.start=r,this.end=null!=u?u:{},Object.assign(this.end,null!=u?u:this.start),this.validateHexColor(l)&&(this.themeColor=l),this.props=f}var e,n;return e=t,(n=[{key:"validateHexColor",value:function(t){if(null!==t.match(/^#([0-9a-f]{3}){1,2}$/i))return!0;throw"Invalid hex color "+t}}])&&u(e.prototype,n),Object.defineProperty(e,"prototype",{writable:!1}),t}();function l(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function c(t,e){for(var n=0;n<e.length;n++){var a=e[n];a.enumerable=a.enumerable||!1,a.configurable=!0,"value"in a&&(a.writable=!0),Object.defineProperty(t,a.key,a)}}function f(t,e,n){!function(t,e){if(e.has(t))throw new TypeError("Cannot initialize the same private elements twice on an object")}(t,e),e.set(t,n)}function y(t,e,n){return function(t,e,n){if(e.set)e.set.call(t,n);else{if(!e.writable)throw new TypeError("attempted to set read only private field");e.value=n}}(t,v(t,e,"set"),n),n}function d(t,e){return function(t,e){return e.get?e.get.call(t):e.value}(t,v(t,e,"get"))}function v(t,e,n){if(!e.has(t))throw new TypeError("attempted to "+n+" private field on non-instance");return e.get(t)}var p=new WeakMap,m=new WeakMap,g=new WeakMap,w=new WeakMap,D=new WeakMap,b=new WeakMap,E=new WeakMap,k=new WeakMap,C=new WeakMap,N=function(){function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},n=e.events,a=void 0===n?[]:n;if(l(this,t),f(this,p,{writable:!0,value:new Date}),f(this,m,{writable:!0,value:void 0}),f(this,g,{writable:!0,value:void 0}),f(this,w,{writable:!0,value:void 0}),f(this,D,{writable:!0,value:[]}),f(this,b,{writable:!0,value:[new h({title:"Today",start:{day:d(this,p).getDate(),month:d(this,p).getMonth()+1,year:d(this,p).getFullYear()},themeColor:"#8a4af3"})]}),f(this,E,{writable:!0,value:null}),f(this,k,{writable:!0,value:[]}),f(this,C,{writable:!0,value:[]}),!this.validateNoDuplicateEvents(a))throw"Duplicate Events Not Allowed";this.events=a,this.setup()}var e,n;return e=t,n=[{key:"month",get:function(){return d(this,g)},set:function(t){y(this,g,t),this.month>11?(y(this,g,0),this.year++):this.month<0&&(y(this,g,11),this.year--),this.dispatchCustomEvent("monthChanged",this.month),this.updateDays()}},{key:"year",get:function(){return d(this,w)},set:function(t){y(this,w,t),this.dispatchCustomEvent("yearChanged",this.year),this.updateDays()}},{key:"events",get:function(){return d(this,D)},set:function(t){t.map((function(t){return new h(t)})),y(this,D,t.map((function(t){return t instanceof h?t:new h(t)}))),this.updateDays(),this.dispatchCustomEvent("eventsChanged",this.events)}},{key:"selectedDay",get:function(){return d(this,E)},set:function(t){y(this,E,t),this.dispatchCustomEvent("selectedDayChanged",{day:this.selectedDay,events:this.getEventsForDay(t)}),this.updateDays()}},{key:"days",get:function(){var t=this;return d(this,m).map((function(e){return{day:e,events:t.getEventsForDay(e)}}))},set:function(t){y(this,m,t),this.dispatchCustomEvent("daysChanged",this.days)}},{key:"customEventStore",get:function(){return d(this,k)},set:function(t){y(this,k,[])}},{key:"setup",value:function(){this.events=this.events.concat(d(this,b)),this.month=d(this,p).getMonth(),this.year=d(this,p).getFullYear(),this.days=this.getDaysInMonth(this.month,this.year)}},{key:"on",value:function(t,e){var n=this;d(this,k).push({customEventName:t,callback:e});var a=d(this,C).filter((function(e){return e.customEventName===t}));return a.length>0&&(a.forEach((function(t){n.dispatchCustomEvent(t.customEventName,t.data)})),y(this,C,d(this,C).filter((function(e){return e.customEventName!==t})))),this}},{key:"updateDays",value:function(){void 0!==this.month&&Number.isInteger(this.month)&&void 0!==this.year&&Number.isInteger(this.year)&&(this.days=this.getDaysInMonth(this.month,this.year))}},{key:"addEvent",value:function(t){if(t instanceof h||(t=new h(t)),!this.validateEventDoesntExistsAlready(t))throw"Duplicate Events Not Allowed";this.events=this.events.concat(t)}},{key:"removeEvent",value:function(t){t instanceof h||(t=new h(t)),this.events=this.events.filter((function(e){return e.id!==t.id&&JSON.stringify(e)!==JSON.stringify(t)}))}},{key:"dispatchCustomEvent",value:function(t,e){var n=this;d(this,k).length>0?d(this,k).forEach((function(a){a.customEventName===t&&a.callback(e,n)})):d(this,C).push({customEventName:t,data:e})}},{key:"getEventsForDay",value:function(t){var e=this,n=[];return this.events.forEach((function(a){e.checkDayHasEvent(t,a)&&n.push(a)})),n}},{key:"checkDayHasEvent",value:function(t,e){var n=function(){for(var t=arguments.length,e=new Array(t),n=0;n<t;n++)e[n]=arguments[n];return e.every((function(t){return void 0!==t&&Number.isInteger(t)}))},a=!1===n(e.start.day)?1:e.start.day,i=!1===n(e.start.month)?n(e.start.year)?1:t.getMonth()+1:e.start.month,r=!1===n(e.start.year)?t.getFullYear():e.start.year,o=!1===n(e.start.weekDay)?null:e.start.weekDay,s=!1===n(e.end.day)?a:e.end.day,u=!1===n(e.end.month)?i:e.end.month,h=!1===n(e.end.year)?r:e.end.year,l=!1===n(e.end.weekDay)?o:e.end.weekDay,c=new Date(r,i-1,a),f=new Date(h,u-1,s);return null===o?!(!this.matchSameDate(t,c)||!this.matchSameDate(t,f))||t.getTime()>=c.getTime()&&t.getTime()<=f.getTime():null!==o&&t.getDay()===o&&null===l||null!==o&&null!==l&&t.getDay()>=o&&t.getDay()<=l||void 0}},{key:"validateEventDoesntExistsAlready",value:function(t){var e;return 0===(null!==(e=this.events)&&void 0!==e?e:[]).filter((function(e){return JSON.stringify(e)===JSON.stringify(t)})).length}},{key:"validateNoDuplicateEvents",value:function(t){var e=!1;return t.forEach((function(n){!0!==e&&t.filter((function(t){return JSON.stringify(t)===JSON.stringify(n)})).length>1&&(e=!0)})),!e}},{key:"getDaysInMonth",value:function(t,e){for(var n=new Date(e,t,1),a=[];n.getMonth()===t;)a.push(new Date(n)),n.setDate(n.getDate()+1);return a}},{key:"matchSameDate",value:function(t,e){return t&&e&&t.getFullYear()===e.getFullYear()&&t.getDate()===e.getDate()&&t.getMonth()===e.getMonth()}}],n&&c(e.prototype,n),Object.defineProperty(e,"prototype",{writable:!1}),t}();return e})()));