Bug in dhtmlscheduler_units.js -> scheduler._mouse_coords

Hi.

I think I found a bug in the scheduler._mouse_coords function in dhtmlscheduler_units.js (2.3)
The Method crashes on me, when I drop an event into the unit_tab and it tries to determine the mouse position.
It seems the _drag_event is undefined.

I fixed it for me by changing

    scheduler._mouse_coords = function () {
        var K = scheduler._props[this._mode];
        var J = E.apply(this, arguments);
        if (K) {
            var H = this._drag_event;
            if (this._drag_id) {
                H = this.getEvent(this._drag_id);
                this._drag_event._dhx_changed = true
            }
            var I = Math.min(J.x + K.position, K.options.length - 1);
            H[K.map_to] = K.options[I].key;
            J.x = I / 10000000
        }
        return J
    };

to

    scheduler._mouse_coords = function () {
        var K = scheduler._props[this._mode];
        var J = E.apply(this, arguments);
        if (K) {
            var H = this._drag_event;
            if (this._drag_id) {
                H = this.getEvent(this._drag_id);
                this._drag_event._dhx_changed = true
            }
            var I = Math.min(J.x + K.position, K.options.length - 1);
            if (H) {H[K.map_to] = K.options[I].key;} // <------------------------- CHANGE IS HERE
            J.x = I / 10000000
        }
        return J
    };

I currently have no time to investigate further, but if you need additional information I would be glad to help.

Regards,
Gerrit

Hello,

Thank you for your feedback.

Can you please specify exact steps to reproduce this issue?
I have tried creating and moving existing events out of the scheduler’s div, on the sections yet everything seems to be working correctly.

Best regards,
Ilya

Sorry, I mean when I use some own drag&drop procedure to handle external links dragged into the area.

when the ondrop-event is fired, the following is executed:

function doDrop(e) {
  e = e || window.event;
  var pos=scheduler._mouse_coords(e); // <----------- problem occurs here
  scheduler.mousepos = pos;
  ... and so on...

scheduler.mousepos is used later in the process

Hi Gerrit,
Do you have knowledge of try catch statements?
Please refer to the following document:
w3schools.com/JS/js_try_catch.asp

The error is expected, you just need to handle it, so you can leave the catch statement empty.

Kind regards
Greg

Alright.
If it is expected I will use try-catch.
Thanks.

I’m afraid the try-catch is not working for me.
I need the coordinates to assign some data (that I drag into the units view from the outside) to the correct section. Without the coordinates its all assigned to section 0.
Do I need to use some parts of “External Drag & Drop”-Module to archive this?

Regards,
Gerrit

If your original solution worked for you — use it, it won’t do any harm.

Best regards,
Ilya

I’m afraid my original solution is not fully functional either.
Apart from the need to modify the source code (makes updates more difficult), there are some problems because the parameter is not getting set.

Regards,
Gerrit

Hello again.

Firstly the good news: I started from scratch (discarding all the mind numbing stuff my predecessor was putting into the application) and most problems are resolved. :slight_smile:

However, I am still unable to find a solution for this external dragging.
For some obscure reason the y-value is set, but the x-value (the appropriate unit) is either false or 1e-7.

Following this post http://forum.dhtmlx.com/viewtopic.php?f=6&t=14356 the relevant code now lokks as this:

function doDrop(e) {
    e = e || window.event;
    scheduler._drag_event = {};
    var pos = scheduler._mouse_coords(e);
    scheduler._drag_event=null; 
    alert(pos.x+' # '+pos.y);

The alert states something like “0 # 112” for a drop into the first of two units/sections in unit view and “1e-7 # 123” if dropping into the second unit/section column.
The y seems valid, but why can’t I get a valid x?
I am expecting a “0” for the first column and “1” for the second. Is that right?

How else could I get the section_id of the column I’m dropping into?

Regards,
Gerrit

Hello,

I am expecting a "0" for the first column and "1" for the second. Is that right?

Absolutely. This is a problem and it should be fixed in the upcoming version.

As a temporary solution you can do the following:

var true_x = pos.x*10000000; var section = scheduler._props.unit.options[true_x]; /* unit -- name of the your units tab, section.key, section.label are available as well as other properties (if were set) */

Best regards,
Ilya

Fantastic!
I didn’t even consider it was just off by a fixed factor ,-)
Thank you so much.

Hello,

I guess I hastily replied before:

In reality pos.x means different things depending on the current view. In units view it could be sections, in week view — date. That’s why for custom views we most likely will leave pos.x = 0.

Now for the whole Drag and drop thing — we have an extension for this. Check out samples\06_timeline\05_drag.html sample, maybe this will help you better.

Best regards,
Ilya

Thank you for the reply.
I was under the impression that external DragDrop works with some external dhtmlx-apps like dhtmlxTree.

What I am doing is the following:

  1. Open scheduler in one window of internet explorer.
  2. Open another instance of IE, load a page with some links in it.
  3. Drag the link (e.g. “http://domain/some.html?data=123,987”) into the scheduler
  4. In scheduler I parse the link and receive the desired information (here I will get “123,987”).
  5. Send this data to the database with returns the detailed data for event with id 123 and event with id 987.
  6. Use this data to create the events in scheduler.

Would externalDragDrop help in this case?

Regards,
Gerrit

You can call from onDrop something like

var temp = scheduler.attachEvent("onEventCreated", function(id,e){ if (!scheduler.callEvent("onExternalDragIn", [id, e])){ this._drag_mode = this._drag_id = null; this.deleteEvent(id); } }); if (scheduler.matrix[scheduler._mode]) scheduler.dblclick_dhx_matrix_cell(last_event); else scheduler._on_dbl_click(last_event); scheduler.detachEvent(temp);

where last_event - HTML event object, related to mouse actions

As result scheduler will simulate double-click at target position , which will create new event and trigger onExternalDragIn event - which can be used for further customization.

Above code doesn’t relay on any properties of scheduler so will work for all modes of existing scheduler and must not have problems in future.

			},

I can’t use the on_dbl_click-event, for I disabled it in config.

Now somehow
var pos = scheduler._mouse_coords(e);
doesn’t return anything else for pos.x than 0 anymore in unit view.
Not sure why…
So the situation is as following now:

  • In calendar-view scheduler._mouse_coords(e) works fine.
    For y I get number of 5min-intervals since midnight, so y=120 means 10 o’clock
    For x I get column-index, so in week view x==0 means monday, x==1 means tuesday and so on.

  • In Timeline view I can handle the result
    For y I get the number of 5min-intervals since the first displayed time. So, lowest hour displayed is 7 o’clock, so when I drop something at 10 o’clock I get 36 (365 min + 760 = 600 = 10 o’clock)
    For x I get 1/10000000 of the row-index, so multiplying with 10000000 gives me the desired result

  • In Unit view I have most trouble
    y is handled as in calendar, so thats ok
    x is always 0 now, so I have no way to determine the section i drop in. It used to be as in timeline view, but not anymore, propably due to new dhtmlxscheduler_units.js send by support.

Apart from the inconsistency, I have no idea how to determine the section now.
I can get the event coordinates with my own functions but they are relative to screen and/or document.
How can I determine the current section from these absolute position?

regards,
Gerrit

OK, this seems to work:

      e = e || window.event;
      var pos = scheduler._mouse_coords(e);
      var sec_id = scheduler._drag_event.section_id;
      var start  = new Date(scheduler._min_date.valueOf() + (pos.y * scheduler.config.time_step) * 60000);

So: hooray!

The x-coordinates is actually set to 0 in my (custom) dhtmlxscheduler_units.js:

scheduler._mouse_coords=function(){
		var pr = scheduler._props[this._mode];
		var pos=p.apply(this,arguments);
		if (pr){
			if(!this._drag_event) this._drag_event = {};
			var ev = this._drag_event;
			if (this._drag_id){
				ev = this.getEvent(this._drag_id);
				this._drag_event._dhx_changed = true;
			}
			var unit_ind = Math.min(pos.x+pr.position,pr.options.length-1);
			ev[pr.map_to]=pr.options[unit_ind].key;
			pos.x = 0;
			pos.custom = true;
		}
		return pos;
}

Why, I don’t know :slight_smile:

Hello,

Oh, I believe you know why :slight_smile: , as I said earlier:

And have I sent you an updated file for units view which already included this change.

There won’t be inconsistency as timeline view will also return pos.x = 0 in the future.

As another option you can check Stanislav solution.

Best regards,
Ilya

Yes, I know you said it should return 0, I just don’t see why, cause the calendar doesn’t.
That is the inconsistency I mean :slight_smile:

However, after discovering the solution without the need of pos.x I’m happy to see it go. :slight_smile:

And sadly Stanislavs solution won’t work for me as I needed to disable the create_on_doubleclick-Option.

But anyway it is not needed anymore

Thank you both for your fast help!

Regards,
Gerrit