datebook
Version:
Generates URLs and ICS file content for adding events to popular calendar apps.
3 lines (2 loc) • 8.6 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).datebook={})}(this,(function(t){"use strict";var e="YYYYMMDD",n="ThhmmssZ",r="YYYYMMDDThhmmssZ",a="YYYYMMDDThhmmss",o="YYYY-MM-DD",i="Thh:mm:ssZ",s={YAHOO:"https://calendar.yahoo.com/",GOOGLE:"https://calendar.google.com/calendar/render",OUTLOOK:"https://outlook.{{host}}.com/calendar/0/deeplink/compose"},c=function(t){return void 0===t&&(t=""),"0".concat(parseInt(t.toString(),10)).slice(-2)},u=function(t,e){void 0===t&&(t=new Date);var n={YYYY:t.getUTCFullYear(),MM:c(t.getUTCMonth()+1),DD:c(t.getUTCDate()),hh:c(t.getUTCHours()),mm:c(t.getUTCMinutes()),ss:c(t.getUTCSeconds())};return Object.keys(n).reduce((function(t,e){return t.replace(e,n[e].toString())}),e)},d=function(t,e){var n=Math.floor((e-t)/1e3),r=Math.floor(n/3600),a=n/3600%1*60;return"".concat(c(r)).concat(c(a))},l=function(t,e){var n=Math.floor((e-t)/1e3);return Math.floor(n/3600)},f=u,p=function(t,e){void 0===t&&(t=new Date);var n={YYYY:t.getFullYear(),MM:c(t.getMonth()+1),DD:c(t.getDate()),hh:c(t.getHours()),mm:c(t.getMinutes()),ss:c(t.getSeconds())};return Object.keys(n).reduce((function(t,e){return t.replace(e,n[e].toString())}),e)},m=function(){return u(new Date,r)},h=function(t,e){var n=864e5*e,r=new Date;return r.setTime(t.getTime()+n),r},T=function(){function t(t){var e=this;this.isAllDay=!1,this.description="",this.title="",this.location="",this.start=new Date,this.end=new Date,this.params={},this.attendees=[],this.setText=function(t){e.description=t.description||"",e.title=t.title||"",e.location=t.location||""},this.setTimestamps=function(t){e.isAllDay=!t.end,e.start=t.start,e.end=t.end||h(e.start,1),e.recurrence=t.recurrence},this.setParam=function(t,n){return e.params[t]=n,e},this.setText(t),this.setTimestamps(t),this.setAttendees(t)}return t.prototype.setAttendees=function(t){this.attendees=Array.isArray(t.attendees)?t.attendees:[]},t}(),P=function(t,e){return P=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])},P(t,e)};function y(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function n(){this.constructor=t}P(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}var A=function(){return A=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var a in e=arguments[n])Object.prototype.hasOwnProperty.call(e,a)&&(t[a]=e[a]);return t},A.apply(this,arguments)};function v(t,e,n){if(n||2===arguments.length)for(var r,a=0,o=e.length;a<o;a++)!r&&a in e||(r||(r=Array.prototype.slice.call(e,0,a)),r[a]=e[a]);return t.concat(r||Array.prototype.slice.call(e))}var D=function(t,e,n){void 0===e&&(e=";"),void 0===n&&(n=function(t){return t});var r=[];for(var a in t)t.hasOwnProperty(a)&&void 0!==t[a]&&r.push("".concat(a,"=").concat(n(t[a])));return r.join(e)},g=D,E=function(t){var e=Object.keys(t).filter((function(e){return null!==t[e]})).reduce((function(e,n){var r;return A(A({},e),((r={})[n]=t[n],r))}),{});return D(e,"&",encodeURIComponent)},O=function(t){return D(t,";")},M=function(t){return t.map((function(t){var e=t.email,n=t.name;return n?"".concat(n," <").concat(e,">"):e}))},Y=function(t){return void 0===t&&(t=""),t.replace(/\\/g,"\\\\").replace(/\n/g,"\\n").replace(/[,;]/g,"\\$&")},R=function(){return Math.random().toString(36).substr(2)},S=function(){return"undefined"!=typeof window?window.location.host:"datebook"},I=function(t){var e,n,a={FREQ:t.frequency,INTERVAL:null===(e=t.interval)||void 0===e?void 0:e.toString(),COUNT:null===(n=t.count)||void 0===n?void 0:n.toString(),WKST:t.weekstart,BYDAY:t.weekdays,BYMONTHDAY:t.monthdays};return t.end&&(a.UNTIL=f(t.end,r)),O(a)},N=function(t){function r(r){var a=t.call(this,r)||this;return a.setInitialParams=function(){var t=e;a.isAllDay||(t+=n);var r=[f(a.start,t),f(a.end,t)].join("/");a.setParam("action","TEMPLATE").setParam("dates",r).setParam("text",a.title).setParam("details",a.description).setParam("location",a.location).setParam("allday",a.isAllDay.toString()),a.recurrence&&a.setParam("recur","RRULE:".concat(I(a.recurrence))),a.attendees.length>0&&a.setParam("add",M(a.attendees).join(","))},a.render=function(){var t=s.GOOGLE,e=E(a.params);return"".concat(t,"?").concat(e)},a.setInitialParams(),a}return y(r,t),r}(T),U=function(t){function n(n){var r=t.call(this,n)||this;return r.setInitialParams=function(){r.setParam("v","60").setParam("title",r.title).setParam("desc",r.description).setParam("in_loc",r.location),r.setTimeParams(),r.attendees.length>0&&r.setParam("inv_list",M(r.attendees).join(","))},r.setTimeParams=function(){r.isAllDay?r.setParam("dur","allday").setParam("st",p(r.start,e)):(r.setParam("st",p(r.start,a)),l(r.start.getTime(),r.end.getTime())>99?r.setParam("et",p(r.end,a)):r.setParam("dur",d(r.start.getTime(),r.end.getTime())))},r.render=function(){var t=s.YAHOO,e=E(r.params);return"".concat(t,"?").concat(e)},r.setInitialParams(),r}return y(n,t),n}(T),C=function(t){function e(e){var n=t.call(this,e)||this;return n.baseUrl=s.OUTLOOK,n.setInitialParams=function(){var t=o;n.isAllDay||(t+=i),n.setParam("rru","addevent").setParam("path","/calendar/action/compose").setParam("startdt",f(n.start,t)).setParam("enddt",f(n.end,t)).setParam("subject",n.title).setParam("body",n.description).setParam("location",n.location).setParam("allday",n.isAllDay.toString()),n.attendees.length>0&&n.setParam("to",M(n.attendees).join(","))},n.setHost=function(t){return["live","office"].includes(t)&&(n.baseUrl=s.OUTLOOK.replace("{{host}}",t)),n},n.render=function(){var t=E(n.params);return"".concat(n.baseUrl,"?").concat(t)},n.setInitialParams(),n.setHost("live"),n}return y(e,t),e}(T),L=function(t){function n(n){var a=t.call(this,n)||this;return a.additionalEvents=[],a.properties=[],a.meta={},a.setInitialParams=function(){a.setMeta("UID",R()).setMeta("DTSTAMP",m()).addProperty("CLASS","PUBLIC").addProperty("DESCRIPTION",Y(a.description)).addProperty("LOCATION",Y(a.location)).addProperty("SUMMARY",Y(a.title)).addProperty("TRANSP","TRANSPARENT"),a.isAllDay?a.addProperty("DTSTART;VALUE=DATE",p(a.start,e)).addProperty("DTEND;VALUE=DATE",p(h(a.start,1),e)):a.addProperty("DTSTART",f(a.start,r)).addProperty("DTEND",f(a.end,r)),a.recurrence&&a.addProperty("RRULE",I(a.recurrence)),a.attendees.length>0&&a.attendees.forEach((function(t){var e=t.email,n=t.name,r=t.icsOptions,o=void 0===r?{}:r,i=a.getAttendeeParams(o,n),s="MAILTO:".concat(e);a.addProperty(i,s)}))},a.getAttendeeParams=function(t,e){var n={};e&&(n.CN=e),t.delegatedFrom&&(n["DELEGATED-FROM"]=t.delegatedFrom),t.partStat&&(n.PARTSTAT=t.partStat),t.role&&(n.ROLE=t.role),t.sentBy&&(n["SENT-BY"]=t.sentBy),n.RSVP=t.rsvp?"TRUE":"FALSE";var r=g(n,";");return"ATTENDEE;".concat(r)},a.getAlarmDuration=function(t){var e=["".concat(t.weeks,"W"),"".concat(t.days,"D"),"".concat(t.hours,"H"),"".concat(t.minutes,"M"),"".concat(t.seconds,"S")].filter((function(t){return/^[0-9]+[A-Z]$/.exec(t)}));return e.unshift(t.after?"PT":"-PT"),e.join("")},a.getMeta=function(){return Object.keys(a.meta).map((function(t){return"".concat(t,":").concat(a.meta[t])}))},a.setMeta=function(t,e){return a.meta[t]=e,a},a.addEvent=function(t){return a.additionalEvents.push(t),a},a.addProperty=function(t,e){if("object"==typeof e){for(var n in a.properties.push("BEGIN:".concat(t)),e)a.addProperty(n,e[n]);a.properties.push("END:".concat(t))}else a.properties.push("".concat(t,":").concat(e.toString()));return a},a.addAlarm=function(t){var e={ACTION:t.action};(t.description&&(e.DESCRIPTION=Y(t.description)),t.summary&&(e.SUMMARY=Y(t.summary)),t.duration&&(e.DURATION=a.getAlarmDuration(t.duration)),t.repeat&&(e.REPEAT=t.repeat),t.attach)&&(e[t.attach.params?"ATTACH;".concat(t.attach.params):"ATTACH"]=t.attach.url);return t.trigger instanceof Date?e["TRIGGER;VALUE=DATE-TIME"]=f(t.trigger,r):e.TRIGGER=a.getAlarmDuration(t.trigger),a.addProperty("VALARM",e)},a.render=function(){var t=v([a],a.additionalEvents,!0).reduce((function(t,e){return v(v(v(v(v([],t,!0),["BEGIN:VEVENT"],!1),e.properties,!0),e.getMeta(),!0),["END:VEVENT"],!1)}),[]);return v(v(["BEGIN:VCALENDAR","VERSION:2.0"],t,!0),["PRODID:".concat(S()),"END:VCALENDAR"],!1).join("\n")},a.setInitialParams(),a}return y(n,t),n}(T);t.CalendarBase=T,t.GoogleCalendar=N,t.ICalendar=L,t.OutlookCalendar=C,t.YahooCalendar=U}));
//# sourceMappingURL=datebook.min.js.map