The following example uses the MenuBuilder framework to define
a context menu for a region of text. It shows how to define the
menu structure using XML as the data source. For an application
that specifies an identical menu structure using a JSON array, see
Example: A JSON MenuBuilder data source
.
The application consists of two files.
The first file is the menu data source, in a file named “textContextMenu.xml.” While
this example uses menu item nodes named “menuitem,” the actual name of
the XML nodes doesn’t matter. As described previously, only the
structure of the XML and the attribute values affect the structure
of the generated menu.
<?xml version="1.0" encoding="utf-8" ?>
<root>
<menuitem label="MenuItem A"/>
<menuitem label="MenuItem B" type="check" toggled="true"/>
<menuitem label="MenuItem C" enabled="false"/>
<menuitem type="separator"/>
<menuitem label="MenuItem D">
<menuitem label="SubMenuItem D-1"/>
<menuitem label="SubMenuItem D-2"/>
<menuitem label="SubMenuItem D-3"/>
</menuitem>
</root>
The second file is the source code for the application user interface
(the HTML file specified as the initial window in the application.xml
file:
<html>
<head>
<title>XML-based menu data source example</title>
<script type="text/javascript" src="AIRAliases.js"></script>
<script type="text/javascript" src="AIRMenuBuilder.js"></script>
<style type="text/css">
#contextEnabledText
{
margin-left: auto;
margin-right: auto;
margin-top: 100px;
width: 50%
}
</style>
</head>
<body>
<div id="contextEnabledText">This block of text is context menu enabled. Right click or Command-click on the text to view the context menu.</div>
<script type="text/javascript">
// Create a NativeMenu from "textContextMenu.xml" and set it
// as context menu for the "contextEnabledText" DOM element:
var textMenu = air.ui.Menu.createFromXML("textContextMenu.xml");
air.ui.Menu.setAsContextMenu(textMenu, "contextEnabledText");
// Remove the default context menu from the page:
air.ui.Menu.setAsContextMenu(null);
</script>
</body>
</html>