I’m trying to understand the internal workings of the Calendar object in Knack. I want to add a input field of type calendar to allow direct jumping to a date, instead of clicking endlessly on the arrows to skip one month at a time. This will be added to the KTL.
I can see it in Knack’s code, and I can even invoke it.
This is what it looks like:
u.gotoDate = function (e, t, n) {
e instanceof Date ? W = I(e) : C(W, e, t, n);
L()
}
If I create a breakpoint on u.today = function() for trigger when I click on the Today button, I can set the object as a global variable temp1 and can call temp1.gotoDate(new Date(‘2018-12-31’));
Tada!!! The calendar shifts to that date!
But again, it’s always the same problem. Without a breakpoint, how do I get the calendar object “u”?
I’ve investigated Knack.views and Knack.router and more, but no success.
You’re looking at the docs for FullCalendar v6. Knack still uses v1.
You’ll have to intercept the existing eventDrop handler than Knack has set, and then re-call Knack’s handler, a bit like this:
const fc = Knack.views[viewKey].$('.knack-calendar').data('fullCalendar');
const originalEventDropHandler = fc.options.eventDrop;
fc.options.eventDrop = function (event, dayDelta, minuteDelta, allDay, revertFunc) {
// do your stuff here
console.log('eventDrop', { event, dayDelta, minuteDelta, allDay, revertFunc });
return originalEventDropHandler.call(this, ...arguments);
};
By the way, you can use that fc variable to call fullCalendar’s functions directly (ie. fc.gotoDate(new Date(...))), instead of calling with the function name as I did in the other reply. A little cleaner that way.
I have another question: Do you know how can I get a callback when a view has rendered? It’s supposed to be done with the viewRender event, but it’s not in Knack.
The closest I can breakpoint on is t.changeView(c) but I can’t see how I could get my callback. For example when I change the view type or when scroll through months or weeks.
I also tried to see if viewDisplay would help, based on your code, like this:
Just by reverse-engineering Knack’s rendering code. .data() is a jQuery function that allows you to store data associated with a given element, and later access it through a reference to the element. In this case, Knack uses it to keep a reference to the underlying FullCalendar instance; it’s used in some other places to keep track of filters, the inline table cell editor, and more.