UNPKG

@barinbritva/add-to-calendar

Version:

[![Dependencies counter](https://img.shields.io/badge/dependencies-none-green?style=flat-square)](https://github.com/barinbritva/add-to-calendar/blob/master/package.json) [![License](https://img.shields.io/npm/l/micromatch?style=flat-square)](https://gith

146 lines (145 loc) 4.65 kB
import { DateHelper } from '../Utils/DateHelper'; import { StringHelper } from '../Utils/StringHelper'; export const Methods = { Publish: 'PUBLISH', Request: 'REQUEST', Reply: 'REPLY', Add: 'ADD', Cancel: 'CANCEL' }; export class ICalendar { constructor() { this.invitationMeta = { contentLanguage: 'EN', method: Methods.Publish }; } configure(invitationMeta) { this.invitationMeta = Object.assign(Object.assign({}, this.invitationMeta), invitationMeta); return this; } createLink(event) { const eventData = this.convertEventToData(event); return `data:text/calendar;charset=utf8,${this.dataPiecesToContent(eventData, true)}`; } createFile(event) { const eventData = this.convertEventToData(event); return this.dataPiecesToContent(eventData); } convertEventToData(event) { const data = [ { key: 'BEGIN', value: 'VCALENDAR' }, { key: 'VERSION', value: '2.0' }, { key: 'PRODID', value: '-//barinbritva//add-to-calendar//' + this.invitationMeta.contentLanguage }, { key: 'CALSCALE', value: 'GREGORIAN' }, { key: 'METHOD', value: this.invitationMeta.method }, { key: 'BEGIN', value: 'VEVENT' }, { key: 'UID', value: event.uid }, { key: 'DTSTAMP', value: StringHelper.clearPunctuation(DateHelper.dateToDateTimeString(new Date())) }, { key: 'DTSTART', value: StringHelper.clearPunctuation(event.getStartDateAsString()) }, { key: 'DTEND', value: StringHelper.clearPunctuation(event.getEndDateAsString()) }, { key: 'SUMMARY', value: this.escapeSpecialChars(event.title) }, { key: 'DESCRIPTION', value: this.escapeSpecialChars(event.description) } ]; if (event.location != null) { data.push({ key: 'LOCATION', value: this.escapeSpecialChars(event.locationName) }); if (event.locationCoordinates != null) { event.locationCoordinates; data.push({ key: 'GEO', value: this.escapeSpecialChars(`${event.locationCoordinates.latitude};${event.locationCoordinates.longitude}`) }); } } if (event.hasAttendees()) { event.attendees.forEach((attendee, index) => { var _a; const attendeeType = index === 0 ? 'ORGANIZER' : 'ATTENDEE'; let attendeeEmail; let attendeeName; if (typeof attendee === 'string') { attendeeEmail = attendee; attendeeName = attendee; } else { attendeeEmail = attendee[0]; attendeeName = (_a = attendee[1]) !== null && _a !== void 0 ? _a : attendee[0]; } data.push({ key: `${attendeeType};CN="${attendeeName}"`, value: this.escapeSpecialChars('mailto:' + attendeeEmail) }); }); } data.push({ key: 'END', value: 'VEVENT' }, { key: 'END', value: 'VCALENDAR' }); return data; } escapeSpecialChars(text) { if (text == null) { return text; } return text .replace(/,/gm, ',') .replace(/;/gm, ';') .replace(/\n/gm, '\\n') .replace(/(\\n)[\s\t]+/gm, '\\n'); } dataPiecesToContent(data, encode = false) { let fileParts = []; for (const key in data) { const dataItem = data[key]; if (dataItem.value == null) { continue; } fileParts.push(`${encode ? encodeURIComponent(dataItem.key) : dataItem.key}` + ':' + `${encode ? encodeURIComponent(dataItem.value) : dataItem.value}`); } return fileParts.join('\n'); } }