Created
September 6, 2019 05:50
-
-
Save daltonrooney/4ae77a1139b9f23a78e2ba04bc25932c to your computer and use it in GitHub Desktop.
Output calendar links from event in Vue.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var MS_IN_MINUTES = 60 * 1000; | |
var formatTime = function (date) { | |
date = new Date(date * 1000); | |
return date.toISOString().replace(/-|:|\.\d\d\d/g, "") | |
}; | |
var calendarGenerators = { | |
google: function (event) { | |
var startTime = formatTime(event.start); | |
var endTime = formatTime(event.end); | |
var href = encodeURI([ | |
'https://www.google.com/calendar/render', | |
'?action=TEMPLATE', | |
'&text=' + (event.title || ''), | |
'&dates=' + (startTime || ''), | |
'/' + (endTime || ''), | |
'&details=' + (event.description || ''), | |
'&location=' + (event.address || ''), | |
'&sprop=&sprop=name:' | |
].join('')); | |
return href; | |
}, | |
ics: function (event) { | |
var startTime = formatTime(event.start); | |
var endTime = formatTime(event.end); | |
var href = encodeURI( | |
'data:text/calendar;charset=utf8,' + [ | |
'BEGIN:VCALENDAR', | |
'VERSION:2.0', | |
'BEGIN:VEVENT', | |
'URL:' + document.URL, | |
'DTSTART:' + (startTime || ''), | |
'DTEND:' + (endTime || ''), | |
'SUMMARY:' + (event.title || ''), | |
'DESCRIPTION:' + (event.description || ''), | |
'LOCATION:' + (event.address || ''), | |
'END:VEVENT', | |
'END:VCALENDAR'].join('\n')); | |
return href; | |
}, | |
ical: function (event) { | |
return this.ics(event); | |
}, | |
outlook: function (event) { | |
return this.ics(event); | |
} | |
}; | |
var generateCalendars = function (event) { | |
return { | |
google: calendarGenerators.google(event), | |
ical: calendarGenerators.ical(event), | |
outlook: calendarGenerators.outlook(event) | |
}; | |
}; | |
var validParams = function (params) { | |
return params !== undefined && params.data.start !== undefined && | |
(params.data.end !== undefined); | |
} | |
export function createCalendar(params) { | |
if (!validParams(params)) { | |
console.log('Event details missing.'); | |
return; | |
} | |
return generateCalendars(params.data); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div> | |
<a target="_blank" :href="calendarLinks.google">Google</a> | |
<a :href="calendarLinks.ical">iCal</a> | |
<a :href="calendarLinks.outlook">Outlook</a> | |
</div> | |
</template> | |
<script> | |
import { createCalendar } from "./calendar"; | |
export default { | |
props: { | |
title: String, | |
start: String, | |
end: String, | |
description: String, | |
address: String | |
}, | |
computed: { | |
calendarLinks() { | |
return createCalendar({ | |
data: { | |
title: this.title, | |
start: this.start, | |
end: this.end, | |
description: this.description, | |
address: this.address | |
} | |
}); | |
} | |
} | |
}; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment