When the action of some menu item involves calling a JavaScript function, that function can determine what the clicked item is. This helps having one handler for multiple actions, as you can learn in menu actions documentation. The MenuItem object provides methods and properties that allow one to investigate item properties or change its state, label, icon and so on.
Say we have the following menu:
<ul id="menu"> <li><a href="javascript:retval = my_handler(this);">Item</a></li> </ul>
That's a very simple menu with one item. That item calls a function handler and assigns its value to retval; we'll talk about retval in a moment. The function handler can be written like this:
function my_handler(action) { var menu_item = action.info; alert(menu_item.label); return false; }
So, in order to retrieve the menu item just use the “info” property of the first argument. Note that in order for this to work we need to include “this” when we designate my_handler (in the href attribute). The above handler just displays the label of the clicked menu item, whatever would that be, using the “label” property of the MenuItem object.
retval is a variable that exists in the context where the event handler is evaluated; assigning false to it (or not assigning it at all) will instruct DynarchMenu to close the popup shortly after the handler executed. There are cases where you might want to keep the popup on screen, such as when you have one popup full of checkboxes and you want to allow users to click them all before closing the menu, so in that case you could simply return true from the function handler; the value would be passed to retval which is hooked by DynarchMenu. Don't worry about globals--it's not global in that context.
On occasions you might need to modify items other than the clicked one in an action handler, or even if you're not in some action handler. How do you get a reference to the item in this case? DynarchMenu makes it pretty simple. First, you must declare unique ID-s (using the id attribute) to those LI-s that define items you think you might need to access. DynarchMenu internally keeps a hash table that maps these ID-s to object references. Example:
<script type="text/javascript"> var menu; function my_handler(action) { var clicked_item = action.info; var other_item = menu.items["item2"]; if (other_item.disabled) { // let's enable it other_item.disable(false); clicked_item.setLabel("Disable next item"); } else { other_item.disable(true); clicked_item.setLabel("Enable next item"); } }; </script> <ul id="menu"> <li id="item1"><a href="javascript:my_handler(this)">Enable next item</a></li> <li id="item2" class="disabled">Foo Bar</li> </ul>
The above example assumes that you initialize the menu with:
menu = DynarchMenu.setup("menu");
that is, you need to keep a reference to the DynarchMenu object in the “menu” variable.
So as you can see, if you have kept a reference to the DynarchMenu object (the “menu” variable in our case) then you can use it to retrieve items by ID, as in item = menu.items['id'].
The object has, besides “label”, a lot of other interesting properties:
As you can see, there are some interesting properties, such as “pressed”, “disabled” or “visible”--but setting them directly doesn't have any effect. The next section describes the methods that you can use to set or change these properties.
The above properties and methods are exemplified in our complex sample, Creating a toolbar.