-
-
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 23, 2024
1 parent
a9263b8
commit 47a245f
Showing
2 changed files
with
29 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,40 @@ | ||
document.querySelectorAll('.download-ics').forEach(link => { | ||
link.addEventListener('click', function (event) { | ||
event.preventDefault(); | ||
function downloadICS(event, start, end, title, description, location, url) { | ||
// Prevent default action | ||
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 = ` | ||
// 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} | ||
DTSTART:${start} | ||
DTEND:${end} | ||
SUMMARY:${title} | ||
DESCRIPTION:${description} | ||
LOCATION:${location} | ||
URL:${url} | ||
END:VEVENT | ||
END:VCALENDAR | ||
`.trim(); | ||
`.trim(); | ||
|
||
// Encode iCal string in Base64 | ||
const base64String = btoa(icalString); | ||
// 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'; | ||
// Create a download link | ||
const downloadLink = document.createElement('a'); | ||
downloadLink.href = 'data:text/calendar;base64,' + base64String; | ||
downloadLink.download = `${title.replace(/[^a-z0-9]/gi, '_').toLowerCase()}.ics`; | ||
downloadLink.style.display = 'none'; | ||
|
||
// Append the link to the body | ||
document.body.appendChild(downloadLink); | ||
// Append the link to the body | ||
document.body.appendChild(downloadLink); | ||
|
||
// Programmatically click the download link | ||
downloadLink.click(); | ||
// Programmatically click the download link | ||
downloadLink.click(); | ||
|
||
// Remove the link from the document | ||
document.body.removeChild(downloadLink); | ||
}); | ||
}); | ||
// Remove the link from the document | ||
document.body.removeChild(downloadLink); | ||
} |