Creating a context menu for a DOM element using the MenuBuilder
framework involves two steps. First you create the NativeMenu instance
that defines the menu structure using the
Menu.createFromXML()
or
Menu.createFromJSON()
method.
You then assign that menu as the context menu for a DOM element
by calling the
Menu.setAsContextMenu()
method.
Because a context menu consists of a single menu, the top-level
menu items in the menu data structure serve as the items in the
single menu. Any menu item that contains child menu items defines a
submenu. To assign a NativeMenu as the context menu for a DOM element,
call the
Menu.setAsContextMenu()
method. This method
requires two parameters: the NativeMenu to set as the context menu,
and the id (a string) of the DOM element to which it is assigned:
var treeContextMenu = air.ui.Menu.createFromXML("treeContextMenu.xml");
air.ui.Menu.setAsContextMenu(treeContextMenu, "navTree");
If you omit the DOM element parameter, the method uses the HTML
document from which the method is called as the default value. In
other words, the menu is set as the context menu for the HTML document’s
entire window. This technique is convenient for removing the default
context menu from an entire HTML window by passing
null
for
the first parameter, as in this example:
air.ui.Menu.setAsContextMenu(null);
You can also remove an assigned context menu from any DOM element.
Call the
setAsContextMenu()
method and pass
null
and
the element id as the two arguments.