tags:

views:

318

answers:

2

I'm using Yui to build a "popup" menu that works a bit differently with the mouse than usual. This is not a ContextMenu, because I want it to respond to left clicks, and the ContextMenu seems bent on responding to right clicks.

Following the examples, if I do this, the menu comes up and everything is close to how I want it:

YAHOO.util.Event.addListener(myClickTarget, 'click', myThingGotClicked);

In my myThingGotClicked function, I manually set the menu's position and show() it.

My problem is that I want to "bind" the menu visibility to the state of the mouse button. That is, on a mouseDown, I want the menu to come up, and on a mouseUp, I want the menu to disappear (selecting the active item, if any). So, listening to the 'click' event doesn't do the right thing, because a "click" is only sent after mouseUp.

The "obvious" solution is to do this:

YAHOO.util.Event.addListener(myClickTarget, 'mousedown', myThingGotClicked);

But this doesn't work. Stepping through in a debugger, you can see that it does actually bring up the menu on a mousedown, but then something immediately hides the menu. At full speed, it looks like nothing happens at all.

Any thoughts?

+1  A: 

Hi Wordman -

The problem is that the MenuManager class listens for the mousedown event at the document level and hides all visible Menu instances. So, since you are building a unique sort of Menu implementation, you'll need to stop the propagation of the mousedown event inside your handler so that the MenuManager doesn't handle the event. Here is some pseudo code for you:

var myThingGotClicked = function (event) {

YAHOO.util.Event.stopPropagation(event);

// Do other stuff

};

YAHOO.util.Event.on(myClickTarget, 'mousedown', myThingGotClicked);

  • Todd
A: 

That's a bit closer, as the menu does pop up, but if you try to make a selection in the menu, the text selection of the page underneath goes sort of nuts. I also need to add a mouseup handler, I think, as the menu doesn't go down on mouse release.

What I really want here are menus that work like menus on every version of the Mac OS (until more recently when OS X added the "click to make the menu 'sticky' to the default behavior).