-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Wiktor
committed
Jun 21, 2024
1 parent
952bb44
commit a9263b8
Showing
5 changed files
with
95 additions
and
4 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
document.querySelectorAll('.download-ics').forEach(link => { | ||
link.addEventListener('click', function (event) { | ||
event.preventDefault(); | ||
|
||
// Get event details from data attributes | ||
const eventDetails = { | ||
start: this.getAttribute('data-start'), | ||
end: this.getAttribute('data-end'), | ||
title: this.getAttribute('data-title'), | ||
description: this.getAttribute('data-description'), | ||
location: this.getAttribute('data-location'), | ||
url: this.getAttribute('data-url') | ||
}; | ||
|
||
// Create iCal string | ||
const icalString = ` | ||
BEGIN:VCALENDAR | ||
VERSION:2.0 | ||
PRODID:-//Sample Corp//NONSGML Event//EN | ||
BEGIN:VEVENT | ||
UID:${new Date().toISOString()}@sample.com | ||
DTSTAMP:${new Date().toISOString().replace(/[-:]/g, '').split('.')[0]}Z | ||
DTSTART:${eventDetails.start} | ||
DTEND:${eventDetails.end} | ||
SUMMARY:${eventDetails.title} | ||
DESCRIPTION:${eventDetails.description} | ||
LOCATION:${eventDetails.location} | ||
URL:${eventDetails.url} | ||
END:VEVENT | ||
END:VCALENDAR | ||
`.trim(); | ||
|
||
// Encode iCal string in Base64 | ||
const base64String = btoa(icalString); | ||
|
||
// Create a download link | ||
const downloadLink = document.createElement('a'); | ||
downloadLink.href = 'data:text/calendar;base64,' + base64String; | ||
downloadLink.download = `${eventDetails.title.replace(/[^a-z0-9]/gi, '_').toLowerCase()}.ics`; | ||
downloadLink.style.display = 'none'; | ||
|
||
// Append the link to the body | ||
document.body.appendChild(downloadLink); | ||
|
||
// Programmatically click the download link | ||
downloadLink.click(); | ||
|
||
// Remove the link from the document | ||
document.body.removeChild(downloadLink); | ||
}); | ||
}); |