1

I use this example under jsfiddle.net, to build a drag&drop system and if I create a click event on shirts images (draggables), this event doesn't work.

How to combine drag and click events to the draggables elements with Mootools?

window.addEvent('domready', function(){

    $$('.item').addEvent('mousedown', function(event){
        //event.stop();

        // `this` refers to the element with the .item class
        var shirt = this;

        var clone = shirt.clone().setStyles(shirt.getCoordinates()).setStyles({
            opacity: 0.7,
            position: 'absolute'
        }).inject(document.body);

        var drag = new Drag.Move(clone, {

            droppables: $('cart'),

            onDrop: function(dragging, cart){

                dragging.destroy();

                if (cart != null){
                    shirt.clone().inject(cart);
                    cart.highlight('#7389AE', '#FFF');
                }
            },
            onEnter: function(dragging, cart){
                cart.tween('background-color', '#98B5C1');
            },
            onLeave: function(dragging, cart){
                cart.tween('background-color', '#FFF');
            },
            onCancel: function(dragging){
                dragging.destroy();
            }
        });
        drag.start(event);
    });

    // This doesn't work
    $$('.item').addEvent('click', function(event){
        console.log('click');
    });

});
6
  • 2
    the event.stop(); will preventDefault and stopPropagation so the mousedown won't bubble into click. furthermore, it clones and applies tuff to a new element. so replace the event stop with this.fireEvent('click', event); to bubble it up manually - though strictly speaking, a click is on mouseup. Commented Sep 9, 2014 at 12:24
  • Look, I commented the line with event.stop, but the result is the same Commented Sep 9, 2014 at 12:27
  • 2
    meh. jsfiddle.net/dimitar/qsjj1jpe/4 Commented Sep 9, 2014 at 12:29
  • Thanks. And for the dblclick? this.fireEvent('dblclick', event); is considered a simple click event. Commented Sep 9, 2014 at 12:37
  • i don't know. recommend you drop mootools drag and drop and use something light that can be scaled and can work with touch events etc - eg interactjs.io - it won't affect anything as it's very very thin and won't attach/detach/affect any events. Commented Sep 9, 2014 at 13:38

1 Answer 1

3

the event.stop(); will preventDefault and stopPropagation so the mousedown won't bubble into click.

furthermore, it clones and applies stuff to a new element and somewheere along the lines, mootools-more will stop the event once again.

so replace the event.stop with this.fireEvent('click', event); to bubble it up manually - though strictly speaking, a click is on mouseup and you kind of need to wait for that instead.

http://jsfiddle.net/dimitar/qsjj1jpe/4/

Not the answer you're looking for? Browse other questions tagged or ask your own question.