verbb / calendar-links

Generate add to calendar links for Google, iCal and other calendar systems
MIT License
16 stars 6 forks source link

Not compatible with IE / Edge #4

Closed ul8 closed 5 years ago

ul8 commented 6 years ago

Thanks for the plugin!

data:text/calendar calendar links are not supported in IE/Edge, I think this should go in the README.

Here's a JS snippet that makes it work. Insert the JS code below into your twig template, {% set addToCalendarLink = ... %} somewhere above it, and add a link somewhere with id="download-event".

{% js at endBody %}
(function () {

        this.EventHandler = function (linkData, fileData) {
            this.fileData = fileData;
            this.linkData = linkData;
            document.querySelector("a#download-event").addEventListener("click", getIcs.bind(this, event));
        }

    function isIEdge () {
            return ( window.Blob && window.navigator.msSaveOrOpenBlob );
        }

        function getIcs () {
            if (isIEdge()) {
                var fileData = [this.fileData];
                blobObject = new Blob(fileData);
                window.navigator.msSaveOrOpenBlob(blobObject, 'event.ics');
            } else {
                window.location.href=this.linkData;
            }
            event.preventDefault();
        }
}());
eventHandler = new EventHandler("{{ addToCalendarLink.ics() }}","{{ addToCalendarLink.ics() | replace({'%0A':'\\n'}) | trim('data:text/calendar;charset=utf8,\\n', 'left') }}");
{% endjs %}
sjelfull commented 6 years ago

Wow, that's some beautiful workaround right there. Would you mind sending a pull request that adds this to the readme?

sjelfull commented 5 years ago

Thanks, added to readme. Will tag a release soon.