Events Plugin 📆

How can I execute javascript after the calendar page has loaded?


edit: I explain my case.
I was asked to have multi-colored events, like:

I don’t want to create a fork of the plugin, so I achieved something with the following code.
The colors are related to event tags and replace the default category color.

I added the tag list in the event HTML this way: data-tags="{{event.topic.tags}}"

<script type="text/x-handlebars" data-template-name="javascripts/components/events-calendar-event">
<div class="{{event.classes}}" data-tags="{{event.topic.tags}}" {{action 'selectEvent' event.topic.url}} style="{{event.listStyle}}">
  {{#unless event.allDay}}
    <span style='{{event.dotStyle}}'>{{d-icon 'circle'}}</span>
  {{/unless}}
  {{#if event.time}}
    <span class="time">{{event.time}}</span>
  {{/if}}
  {{#if event.title}}
    <span class="title" style="{{event.titleStyle}}">{{replace-emoji event.title}}</span>
  {{/if}}
</div>

{{#if showEventCard}}
  {{events-calendar-card topic=event.topic selectEvent="selectEvent"}}
{{/if}}
</script>

I generate stripped patterns:

<script type="text/discourse-plugin" version="0.8.23">
api.onPageChange((url, title) => {
    // tags colors
    let colors = {muni: "#91D0B5", road: "#8ABBE2", freestyle: "#CFB3D3"};

    let events = document.getElementsByClassName("event");
    for (var i = 0; i < events.length; i++) {
        let event = events[i];
        let gradient = "";
        let tags = event.getAttribute("data-tags").split(",");
        for (j = 1; j <= tags.length; j++) {
            gradient +=
                ", " +
                colors[tags[j - 1]] +
                " " +
                j * 8 +
                "px, " +
                colors[tags[j - 1]] +
                " " +
                (j * 8 + 8) +
                "px";
        }
        event.style.background = "repeating-linear-gradient(-45deg" + gradient + ")";
    }
});
</script>

And I added a bit of CSS.

The result:
2 tags event:

3 tags event:
image

The issue I encounter is that my javascript is executed at the page change and won’t apply stripped colors on previous/next months’ events since the calendar dynamically loads its content.

If I’m in October and and I click “next” twice, going for December, this is how it appears:

This is why I’d like my js to be executed when the calendar content has loaded.

2 Likes