almende / vis

⚠️ This project is not maintained anymore! Please go to https://github.com/visjs
7.85k stars 1.48k forks source link

Drag item from one timeline to another #1612

Open itfourp opened 8 years ago

itfourp commented 8 years ago

I use the vis.js timeline in a project where I want to drag an item from one timeline over to another. I used jQuery drag&drop and it works quite well on Chrome, but it does not work on IE11; probably there is a problem with the propagation of the pointerdown event (does hammer maybe stop the propagation?). I wrote an event-handler to stop propagation of the pointerdown event before the timeline gets it and then drag&drop worked, but now I was unable to select any items. I need both features though. Is there a way to drag items from one timeline to another without relying on another library? Or is there maybe a better way than stopping the propagation of the event?

Best regards! itfourp

josdejong commented 8 years ago

Drag&drop is not supported right now, I will put this as a feature request.

mojoaxel commented 8 years ago

@itfourp Maybe you could find the time to share your current state as an simple example e.g. on jsbin!? It would be much easier to get to the root of the problem.

itfourp commented 8 years ago

@mojoaxel Hey, thank you for answering. I found a workaround for the problem a while ago.

if (Browser.IsMicrosoftInternetExplorer) {
    item.on("pointerdown", function (event) {
        event.stopPropagation();
    });
    item.on("click", function (event) {
        var clicked = $(event.currentTarget);
        var id = clicked.data("id");
        timeline.setSelection([id], { selectEvent: event });
    });
}

Since that prevented selecting items I had to modify the setSelection event.

Timeline.prototype.setSelection = function (ids, options) {
    this.itemSet && this.itemSet.setSelection(ids);
    if (options && options.focus) {
      this.focus(ids, options);
    } else if (options && options.selectEvent) {
      var newSelection = this.itemSet.getSelection();
      if (newSelection.length > 0 || oldSelection.length > 0) {
        this.body.emitter.emit('select', {
          items: newSelection,
          event: event
        });
      }
    }
};

Not quite the best solution, but for now it works.

mojoaxel commented 8 years ago

@ttjoseph Thanks for providing the code-snippets. Maybe we can Integrate it in the library? Feel free to create a Pull Request :wink:

itfourp commented 8 years ago

The first code snippet is done in the view that I used the vis-timeline in, so I cannot integrate that code. The second snippet was more like a dirty quick fix to make it work for me (and could probably be done much better). I'll create a Pull Request for the second part of the code once I've got the time to put it into the latest version (i'm still with 4.14.0). Would be great to have it in here, so I can upgrade without any hassle then!

yotamberk commented 7 years ago

@itfourp Any update for the PR you promised? :wink:

itfourp commented 7 years ago

@yotamberk Sorry, I forgot about this and haven't updated my local version yet. I'll look into it next week and then I'm probably able to create the promised PR.

yotamberk commented 7 years ago

@itfourp Any update on this? Notice there is now a drag and drop option in the timeline. You should look at using that.

Clarence344 commented 7 years ago

@itfourp Have you an example of your solution? webpage oder codepen? I would like to drag an item from Timeline 1 to Timeline2, but its not possible to drag the item out of the container (border).

thanks

itfourp commented 7 years ago

@yotamberk Sorry, I've not been looking into this for a while, but I could not find much about those options in the docs. Is it already in there?

@Clarence344 This is the last thing I tried: http://jsbin.com/vapubaj/edit?html,output Please note this it not a clean solution, since in some browsers you cannot select draggable items.

About what we've been talking about in gitter, I wasn't able to find out what differences in IE/Edge/Chome exactly caused the selection problem and then I gave up.

Kandarp143 commented 6 years ago

@itfourp I am unable to open your example in jsbin it throws 502 error, can you provide me a working link.

itfourp commented 6 years ago

@Kandarp143 The example in jsbin still works for me. You could try this link as well: http://jsbin.com/gezimuwula

mjelecevic commented 6 years ago

How did you set drag event of jquery on the items of timeline?

itfourp commented 6 years ago

I've enabled jquery drag like this:

var draggableTimeline = new vis.Timeline( container );
for ( var prop in draggableTimeline.itemSet.items ) {
    if ( draggableTimeline.itemSet.items[prop].dom &&
        draggableTimeline.itemSet.items[prop].dom.box ) {
        var item = $( draggableTimeline.itemSet.items[prop].dom.box );
        //  enable drag for items
        item.draggable( {
            axis: "y",
            revert: "invalid",
            revertDuration: 150,
            scroll: false,
            helper: "clone",
            appendTo: "body",
            drag: function( event, ui ) {
                event.stopImmediatePropagation();
            },
            stop: function( event, ui ) {
                event.stopImmediatePropagation();
            }
        } );
    }
}
mjelecevic commented 6 years ago

Do I need to set in options editable: false for the timeline? I can't drag the items when I set draggable with jquery-ui. Because when I try to drag them my timeline is moving left or right. Do I need something else to set in the options for my timeline?

And do you know maybe is there any solution for the grid in dragging? I don't want to have the grid on drag.