Custom Collision Code

I am trying to implement a custom collision function. If I insert an event onto an existing event, the old events wraps around the new event. For example:

If I have event a:
|AAAAAAAA|

Then insert event B, I get:

|AA|BB|CC|

To start with, I wanted to just update the old events end date to the new events start date, but while I can do this, the update is not propagated to the server:

[code] scheduler.attachEvent(“onEventCollision”, function (ev, evs) {

			scheduler.getEvent(evs[0].id).end_date = ev.start_date
			scheduler.updateEvent(scheduler.getEvent(evs[0].id))

			return false;
		});[/code]

Could someone point me in the correct direction?

You need to trigger dataprocessor

scheduler.getEvent(evs[0].id).end_date = ev.start_date scheduler.updateEvent(scheduler.getEvent(evs[0].id)); //this just repain event dp.setUpdated(evs[0].id, true); //this one will trigger data saving

Hi Stanislav,

Thanks for the help. With you assistance I am almost there, but now run into a different issue.

When adding the new event (event C above), I am unable to send the correct message to the server and the update is lost.

My function now looks like below:

                scheduler.attachEvent("onEventCollision", function(ev, evs) {

                    var intersectStart = (
                        (ev.end_date > evs[0].start_date) &&
                        (ev.start_date < evs[0].start_date)
                    );

                    var intersectEnd = (
                        (ev.start_date < evs[0].end_date) &&
                        (ev.end_date > evs[0].end_date)
                    );

                    var intersectMiddle = (
                        (ev.start_date > evs[0].start_date) &&
                        (ev.start_date < evs[0].end_date) &&
                        (ev.end_date < evs[0].end_date)
                    );

                    if(intersectStart)  {
                        evs[0].start_date = ev.end_date;
                        // Repaint and update
                        scheduler.updateEvent(scheduler.getEvent(evs[0].id))
                        dp.setUpdated(evs[0].id, true);
                        return false;
                     } else if (intersectEnd) {
                        evs[0].end_date = ev.start_date;
                        // Repaint and update
                        scheduler.updateEvent(scheduler.getEvent(evs[0].id))
                        dp.setUpdated(evs[0].id, true);
                        return false;
                    } else if (intersectMiddle) {
                        // Need this to grab the event
                        var newEventId = scheduler.uid();

                        var newEvent = {
                            start_date: ev.end_date,
                            end_date: evs[0].end_date,
                            text: evs[0].text,
                            id: newEventId,
                            rec_type: evs[0].rec_type,
                            shift_type: evs[0].shift_type,
                            event_length: evs[0].event_length,
                            event_pid: evs[0].event_pid,
                            section_id: evs[0].section_id,
                            color: evs[0].color,           
                        };

                        // Create the new event
                        scheduler.addEvent(newEvent);
                        dp.setUpdated(newEventId, "inserted", false);

                        // Change the first events start date, update display, send to server
                        evs[0].end_date = ev.start_date;
                        scheduler.updateEvent(scheduler.getEvent(evs[0].id))
                        dp.setUpdated(evs[0].id, true);
                        return false;
                    }
                    return true;
                });

When I move the event over, I the following is sent:

1361555201667_start_date:2013-02-20 18:00
1361555201667_end_date:2013-02-20 20:30
1361555201667_text:Sales Inbox
1361555201667_id:1361555201667
1361555201667_rec_type:
1361555201667_shift_type:SI
1361555201667_event_length:0
1361555201667_event_pid:
1361555201667_section_id:rac
1361555201667_color:#FF4242
1361555201667_undefined:undefined // Why undefined?
1361555201667_!nativeeditor_status:deleted    // Why deleted
ids:1361555201667

I can’t figure out why these messages are being sent.

Could you help out again?

Have a great weekend.

Cheers,
Ray

Can you provide some kind of sample where problem can be checked?

You have in code

dp.setUpdated(evs[0].id, true);
return false;

which do the next

  • save triggered
  • block operation

If operation is event creation the order of actions is the next

  • creating started
  • save triggered
  • operation blocked
  • as result scheduler need to delete the event now, and because you already have triggered data saving it will try to delete it from server side as well.

Hi Stanislav,

I’m afraid the application is running internally behind our firewall. In my previous example, the if clause’s that deal with intersectStart and intersectEnd both work fine, it is only the intersectMiddle case that does not.

I am assuming the problem is within the following snippet:

                       // Create the new event
                        scheduler.addEvent(newEvent);
                        dp.setUpdated(newEventId, "inserted", false);

                        // Change the first events start date, update display, send to server
                        evs[0].end_date = ev.start_date;
                        scheduler.updateEvent(scheduler.getEvent(evs[0].id))
                        dp.setUpdated(evs[0].id, true);
                        return false;

My understanding was that returning ‘false’ within the function allows prevents the default collision behavior?

Thanks for your help.

Sorry for confusion, my previous response is wrong :frowning:
When you are returning false from onEventCollision it permits collision, so there will not be event data reverting.

I’m not quite sure why the problem occurs. The one more possible reason is the addEvent instruction, which is correct, but it takes event_pid from source event. event_pid is a field which used for creating exceptions for the recurring event, and just coping it to the new event may result in some hardly predictable results. Can you try to remove rec_type, event_length, event_pid from newEvent object and check does it fix the issue or not ?

After spending a few hours debugging, I’ve narrowed this down to the dates I am passing to the addEvent method. The rec_type, event_length or PID have no impact.

For example, using hard-coded strings, the event is created successfully using this code:

[code] var newEvent = {
start_date: “23-08-2010”,
end_date: “24-08-2010”,
text: evs[0].text,
id: newEventId,
rec_type: evs[0].rec_type,
shift_type: evs[0].shift_type,
event_length: evs[0].event_length,
event_pid: evs[0].event_pid,
section_id: evs[0].section_id,
color: evs[0].color,
};

                    // Create the new event
                    scheduler.addEvent(newEvent);

[/code]

However, when using the values taken from the entered event, I receive the error:

[code] var newEvent = {
start_date: ev.end_date,
end_date: evs[0].end_date,
text: evs[0].text,
id: newEventId,
rec_type: evs[0].rec_type,
shift_type: evs[0].shift_type,
event_length: evs[0].event_length,
event_pid: evs[0].event_pid,
section_id: evs[0].section_id,
color: evs[0].color,
};

                    // Create the new event
                    scheduler.addEvent(newEvent);

[/code]

I therefore decided to check what format the events were being transmitted in, and I have the following in my init:

function init() { scheduler.config.api_date = "%Y-%m-%d %H:%i"; scheduler.config.xml_date="%Y-%m-%d %H:%i"; scheduler.config.repeat_date = "%Y-%m-%d"; scheduler.config.edit_on_create = true; scheduler.config.first_hour = 7; scheduler.config.last_hour = 24; scheduler.config.details_on_create = true; scheduler.config.details_on_dblclick = true; scheduler.config.time_step = 30; // Values are stored in UTC on the server scheduler.config.server_utc = true;

I hope this can help!

Try to change the code as

var newEvent = { start_date: new Date(ev.end_date.valueOf()), end_date: new Date(evs[0].end_date.valueOf()),

In existing code the old and new events are sharing the same date object, so changes to one object may be reflected in other one, which can cause to strange side effects.

Above code will create new date objects ( similar to string usage, but without unnecessary date-string-date conversation )

Perfect. That did the trick. Thanks a lot for your help, I really appreciate the support you offer.