Skip to content

Commit

Permalink
ICS fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Wiktor committed Jun 23, 2024
1 parent a9263b8 commit 47a245f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 52 deletions.
18 changes: 3 additions & 15 deletions _pages/talks.html
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,7 @@ <h3>Stołówka</h3>
<br>
{% endfor %}
<b>Kiedy</b>: {{ talk.starttime | date: "%H:%M" }} - {{ talk.endtime | date: "%H:%M" }}<br>
<a href="#" class="download-ics"
data-start="{{ talk.starttime }}"
data-end="{{ talk.endtime }}"
data-title="{{ talk.title }}"
data-description="---"
data-url="{{ talk.url | absolute_url }}"
data-location="Ośrodek Wypoczynkowy „SPORTOWA OSADA”, Zarzecze 12, 98-260 Strumiany, Poland">Dodaj do kalendarza</a><br>
<a href="#" onclick="downloadICS(event, '{{ talk.starttime }}', '{{ talk.endtime }}', '{{ talk.title }}', '---', 'Osrodek Sportowa Osada Zarzecze 12 Strumiany', '{{ talk.url | absolute_url }}')">Dodaj do kalendarza</a><br>
</li>
{% endfor %}
</ul>
Expand All @@ -246,7 +240,7 @@ <h3>Inne</h3>
<br>
{%if talk.where_exactly %}
{%if talk.where_exactly_geourl %}
Gdzie: <a href="{{ talk.where_exactly_geourl }}">{{ talk.where_exactly }}</a>
Gdsszie: <a href="{{ talk.where_exactly_geourl }}">{{ talk.where_exactly }}</a>
<br>
{% else %}
Gdzie: {{ talk.where_exactly }}
Expand All @@ -262,13 +256,7 @@ <h3>Inne</h3>
<br>
{% endfor %}
Kiedy: {{ talk.starttime | date: "%H:%M" }} - {{ talk.endtime | date: "%H:%M" }}<br>
<a href="#" class="download-ics"
data-start="{{ talk.starttime }}"
data-end="{{ talk.endtime }}"
data-title="{{ talk.title }}"
data-description="---"
data-url="#"
data-location="Ośrodek Wypoczynkowy „SPORTOWA OSADA”, Zarzecze 12, 98-260 Strumiany, Poland">Dodaj do kalendarza</a><br>
<a href="#" onclick="downloadICS(event, '{{ talk.starttime }}', '{{ talk.endtime }}', '{{ talk.title }}', '---', 'Osrodek Sportowa Osada Zarzecze 12 Strumiany', '{{ talk.url | absolute_url }}')">Dodaj do kalendarza</a><br>
</li>
{% endfor %}
</ul>
Expand Down
63 changes: 26 additions & 37 deletions assets/js/ics.js
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);
}

0 comments on commit 47a245f

Please sign in to comment.