views:

39

answers:

1

Mootools: How to Allow and Disallow var drag depending on checkbox checked or not?

window.addEvent('domready',function() {
  var z = 2;
  $$('#dragable').each(function(e) {
    var drag = new Drag.Move(e,{
      grid: false,
      preventDefault: true,
      onStart: function() {
        e.setStyle('z-index',z++);
      }
    });
  });
});
function check(tag){
  if(tag.checked){
    //checkbox checked
    //How to Disallow Drag.Move for #dragable ?
    //Unfortunately so it does not work - drag.destroy(); drag.removeEvents();
  }else{
    //How to Allow Drag.Move for #dragable ?
  }
}
<input type="checkbox" onClick="check(this);">
<div id="dragable">Drag-able DIV</div>

Thanks!

+1  A: 

Store the instance of Drag in MooTools Element Store so when the checkbox is clicked, we can retrieve this instance and manipulate it.

Drag.Move is an extension to the base Drag class, and if you see the docs, you will notice it has two methods for this situation:

You need to call these methods on the drag object that gets created when you call new Drag.Move(..) to enable or disable dragging.

So first create the drag object as you are already doing:

var drag = new Drag.Move(e, {
    ...
});

And then store a reference of this drag object inside the Element Store for later retrieval.

e.store('Drag', drag);

You can use any key you want here - I've used "Drag".

Then later in the check function, retrieve the drag object, and call attach or detach on it depending on the state of the checkbox.

function check(elem) {
    var drag = elem.retrieve('Drag'); // retrieve the instance we stored before

    if(elem.checked) {
        drag.detach(); // disable dragging
    }
    else {
        drag.attach(); // enable dragging
    }
}

See your example modified to work this the checkbox.

On a side note, if you are retrieving an element by id, you don't need to use $$ as ideally there should only be only element with that id. $$("#dragable") is just too redundant and less performant. Use document.id('dragable') or $("dragable") instead.

Anurag
@Anurag: Thanks! Unfortunately it is not working with `document.id('dragable')` http://jsfiddle.net/89RJp/2/
Binyamin
@Binyamin - There were several errors in the link you gave. I sneakily changed the name from "dragable" to "draggable" for one :)Then `document.id(..)` returns a single element, so you can't loop over it using `each`. I have updated your example here - http://jsfiddle.net/89RJp/3/ so you can compare your version with this. Also, keep on the lookout for console logs using Firebug in Firefox or Developer Tools in Chrome/Safari. They are very helpful in seeing what went wrong.
Anurag
@Anurag: I add there also `mouseenter`, `mouseleave` events http://jsfiddle.net/89RJp/4/, and tried to make `mouseleave` event Disallow if checkbox checked, but it is not working. Could you please help me to fix it!? Thank you very, very much!
Binyamin
see /5, and don't forget to check console logs again :)
Anurag
I was trying to Disallow `mouseleave` event if checkbox checked, not `drag`, and http://jsfiddle.net/89RJp/4/ is not working. !? In http://jsfiddle.net/89RJp/5/ it stops `drag` event.
Binyamin
@Anurag: Do you have some solution?
Binyamin
@Binyamin - I don't understand what you are trying to do on `mouseleave`. Could you describe in more details what is it that you're trying to do there?
Anurag
@Anurag: I get already right answer http://jsfiddle.net/89RJp/6/ in http://stackoverflow.com/questions/3222504/mootools-how-to-disallow-mouseenter-and-mouseleave-event-when-checkbox-check/3222678#3222678
Binyamin