0

This topic follows this one about using fullcalendar :

how to set duration for external events in fullcalendar

Now I'm trying to use the resources. For now it's just testing so I used the following code:

document.addEventListener('DOMContentLoaded', function() {
var calendarEl = document.getElementById('calendar');

var calendar = new FullCalendar.Calendar(calendarEl, {
    schedulerLicenseKey: 'GPL-My-Project-Is-Open-Source',
    plugins: [ 'resourceTimeGrid' ],
    timeZone: 'UTC',
    defaultView: 'resourceTimeGridFourDay',
    datesAboveResources: true,
    header: {
        left: 'prev,next',
        center: 'title',
        right: 'resourceTimeGridDay,resourceTimeGridFourDay'
    },
    views: {
        resourceTimeGridFourDay: {
            type: 'resourceTimeGrid',
            duration: { days: 4 },
            buttonText: '4 days'
        }
    },
    resources: [
        { id: 'a', title: 'Room A' },
        { id: 'b', title: 'Room B' }
    ],
    events: 'https://fullcalendar.io/demo-events.json?with-resources=2'
});

calendar.render();
});

I'm inside a Symfony project. In terms of Javascript calls I had this :

                <script src="https://cdn.jsdelivr.net/npm/@fullcalendar/core@4.1.0/main.min.js"></script>
            <script src="https://cdn.jsdelivr.net/npm/@fullcalendar/interaction@4.1.0/main.min.js"></script>
            <script src="https://cdn.jsdelivr.net/npm/@fullcalendar/daygrid@4.1.0/main.min.js"></script>
            <script src="https://cdn.jsdelivr.net/npm/@fullcalendar/timegrid@4.1.0/main.min.js"></script>

So I added these 2 calls:

            <script src="https://cdn.jsdelivr.net/npm/@fullcalendar/resource-common@4.1.0/main.min.js"></script>
            <script src="https://cdn.jsdelivr.net/npm/@fullcalendar/resource-timegrid@4.1.0/main.min.js"></script>

All of this was copy / pasted from the demo pages. Now when I load my page I have this error message and i don't understand why :

Uncaught TypeError: Cannot read property 'addEventListener' of null

It seems the error is triggered here :

    // Event Delegation
// ----------------------------------------------------------------------------------------------------------------
function listenBySelector(container, eventType, selector, handler) {
    function realHandler(ev) {
        var matchedChild = elementClosest(ev.target, selector);
        if (matchedChild) {
            handler.call(matchedChild, ev, matchedChild);
        }
    }
    container.addEventListener(eventType, realHandler);
    return function () {
        container.removeEventListener(eventType, realHandler);
    };
}

on this part :

container.addEventListener(eventType, realHandler);

What do you think may cause this issue ? A javascript missinh maybe ?

ADyson
  • 57,178
  • 14
  • 51
  • 63
user2449253
  • 103
  • 2
  • 13
  • I get a totally different error if I use your code: https://codepen.io/ADyson82/pen/RwPOxjg?editable=true&editors=001 (check the console). It's easily resolved by adding the "resourceDayGrid" JS file as well. It's not really documented anywhere that this is a dependency, as far as I can see, which is a bit of an omission by the fullCalendar developers. – ADyson Apr 02 '20 at 12:58
  • I don't see the error you've stated anywhere. Note that my demo uses fullCalendar 4.4. rather than 4.1, so it's possibly due to a bug which has since been resolved. Try upgrading to 4.4, it won't break anything else. If you still have the error after upgrading and then adding the resourceDayGrid file, then I would guess your error comes from somewhere else in the code. You didn't say where this "listenBySelector" function is located, or how it was being called (the stack trace should tell you). The error maens that the `container` variable is null, but to know why we'd have to see the stack. – ADyson Apr 02 '20 at 13:00
  • I'm not sure how to upgrade inside my Symfony project... I installed the bundle with tattali/calendar-bundle so I just type composer require tattali/calendar-bundle. Do you know how i should upgrade now ? – user2449253 Apr 02 '20 at 13:38
  • Well the instructions at https://github.com/tattali/CalendarBundle (item 3) specifically tells you to write the script tags yourself, so the package isn't providing the version info, and it won't be damaged if you make your own changes. So it's easy - just a manual override to the ` – ADyson Apr 02 '20 at 13:45
  • Yes that's what I did but i still get the same error... it's reallly strange – user2449253 Apr 02 '20 at 13:56
  • The error is on https://cdn.jsdelivr.net/npm/@fullcalendar/core@4.4.0/main.min.js – user2449253 Apr 02 '20 at 14:03
  • I really can't reproduce it. I can only suggest - make sure you've got all the script files pointing to the same version of fullCalendar. Make sure they're in the same order as the ones in my CodePen, make sure you added all the needed CSS files for the right version as well. And if you have any other javascript in your page, try removing it temporarily and see if that fixes it. – ADyson Apr 02 '20 at 14:27
  • 1
    I found out the source of the error, starting from scratch with just a simple display of the calendar with resources columns. Now I'm going to add the drag and drop events and I'll get back here to post the results :) – user2449253 Apr 02 '20 at 14:29

1 Answers1

1

The error was due to a bad id in the div supposed to contain the calendar...

Everything is actually working fine and I was able to combine dragging events + using resources. The full code is :

<script src="https://cdn.jsdelivr.net/npm/@fullcalendar/core@4.4.0/main.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@fullcalendar/interaction@4.4.0/main.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@fullcalendar/daygrid@4.4.0/main.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@fullcalendar/timegrid@4.4.0/main.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@fullcalendar/resource-common@4.4.0/main.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@fullcalendar/resource-daygrid@4.4.0/main.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@fullcalendar/resource-timegrid@4.4.0/main.min.js"></script>

<script type="text/javascript">
    document.addEventListener('DOMContentLoaded', function() {
        var Calendar = FullCalendar.Calendar;
        var Draggable = FullCalendarInteraction.Draggable;

        var calendarEl = document.getElementById('calendar-holder');
        var containerEl = document.getElementById('external-events');


        // initialize the external events
        new Draggable(containerEl, {
            itemSelector: '.fc-event',
            eventData: function(eventEl) {
                var json_event = eventEl.getAttribute("data-event");
                var event_array = JSON.parse(json_event);
                var event_duration = event_array['duration'];
                var event_title = event_array['title'];
                var event_color = event_array['color'];

                return {
                    title: event_title, //eventEl.innerText,
                    duration: event_duration,
                    backgroundColor: event_color,
                };
            }
        });

        var calendar = new FullCalendar.Calendar(calendarEl, {
            schedulerLicenseKey: 'GPL-My-Project-Is-Open-Source',
            plugins: [ 'interaction', 'dayGrid', 'timeGrid', 'resourceTimeGrid' ],
            timeZone: 'UTC',
            defaultView: 'resourceTimeGridFourDay',
            datesAboveResources: true,
            header: {
                left: 'prev,next',
                center: 'title',
                right: 'resourceTimeGridDay,resourceTimeGridFourDay'
            },
            views: {
                resourceTimeGridFourDay: {
                    type: 'resourceTimeGrid',
                    duration: { days: 4 },
                    buttonText: '4 days'
                }
            },
            resources: [
                { id: 'a', title: 'Room A' },
                { id: 'b', title: 'Room B' }
            ],
            events: 'https://fullcalendar.io/demo-events.json?with-resources=2'
        });

        calendar.render();
    });
</script>

Thank you ADyson once again, for taking the time to help me !

user2449253
  • 103
  • 2
  • 13