j-ulrich / jquery-simulate-ext

jQuery simulate extended
https://j-ulrich.github.io/jquery-simulate-ext/
Other
146 stars 48 forks source link

Ignore native mouse movements? #12

Closed murtaugh closed 10 years ago

murtaugh commented 10 years ago

Is there a way to ignore the actual movement of the mouse while a simulation is running?

j-ulrich commented 10 years ago

What exactly do you mean with "ignore the actual movement"? Do you mean to disable that the user is able to move the mouse cursor while a simulation is running? Then, definitely no. Or do you mean to trigger some action only on simulated mouse move events? That should be possible by adding some flag on the events (simulated or something) to distinguish the simulated events from real events.

murtaugh commented 10 years ago

I meant the latter, except I want to prevent mouse events from occurring on elements that are actively being managed by the simulator.

For example, I have a simulation that drags elements from one side of the screen to another, but any mouse movements made in the browser window by the user while the simulation is running disrupts the simulation.

bschelling commented 10 years ago

same problem here. think this would be a great improvement - otherwise cool :+1:

bschelling commented 10 years ago

btw this seems only to happen in chrome. will try to add the flag..

j-ulrich commented 10 years ago

I already started investigating a method to add flags to events, but the problem is that the dispatchEvent() method strips away all custom flags from the event objects. However, it could be done by using jQuery.trigger() instead of dispatchEvent() but this requires changes in jquery.simulate.js. Long story short: I will implement it in 1.3.0 but it'll take some time.

j-ulrich commented 10 years ago

Using the new eventProps option (already available in the latest commits but officially supported with the upcoming version 1.3.0), "ignoring" simulated events can be achieved like this:

$('#myElement').on('mousemove', function(event) {
    if (event.simulated !== true) {
        // event is not simulated
    };
});
$('#myElement').simulate('drag-n-drop', {dx: 100, eventProps: {jQueryTrigger: true, simulated: true}});

It is important to set jQueryTrigger: true and use the jquery.simulate.js version from the jquery-simulate-ext repository.

NOTE: This solution uses jQuery.trigger() instead of dispatchEvent(). This means that the event will only trigger event handlers attached with the same jQuery instance and not handlers attached using addEventListener() or using another versions of jQuery (see http://bugs.jquery.com/ticket/11047 for more information). In most cases, this should not be a problem.