This is a brief overview of how I initialise context menus in my OpenLaszlo application. There is nothing magic here, it is all in the documentation, but I hope this will save someone the time it took me to pull it all together.
CREATING THE CONTEXT MENU
This handler creates an empty context menu for the current object. A delegate is declared. The delegate will be called when the context menu is invoked, and is responsible for initialising the context menu. I use a delegate because the context menu depends on the application state.
<handler name="oninit">
var cm = new LzContextMenu();
cm.setDelegate(new LzDelegate(this, 'init_cm'));
this.setContextMenu(cm);
</handler>
INITIALISING THE CONTEXT MENU
This is the method that will be invoked to initialise the context menu.
The wrinkle in my application is that there is a ‘global’ context menu that is accessed by right-clicking anywhere, as well as a more object-specific context menu when a visual object is clicked. The specific context menu includes the global menu items as well. So this method first calls the method that initialises the global context menu (to make sure the global context menu is in the right state), then at the end of the method the menu items from the global context menu are copied into this context menu.
Otherwise this method is fairly straightforward. hideBuiltInItems() removes Flash-installed default menu items (but not all of them BTW). clearItems() removes all custom items from the context menu.
<method name="init_cm">
<![CDATA[
cm_util.init_cm(); // init global menu
var cm = this.getContextMenu();
cm.hideBuiltInItems();
cm.clearItems();
var item;
item = cm.makeMenuItem('label1',
new LzDelegate(xxx, 'yyy'));
item.value = parent;
cm.addItem(item); if (...)
{
if (...)
{
item = cm.makeMenuItem('label2',
new LzDelegate(zzz, 'aaa'));
}
else
{
item = cm.makeMenuItem('label3', null);
item.setEnabled(false);
}
cm.addItem(item);
}
if (...)
{
if (...)
{
item = cm.makeMenuItem('label4',
new LzDelegate(bbb, 'ccc'));
item.value = this;
item.setSeparatorBefore(true);
cm.addItem(item);
}
else
{
if (...)
{
item = cm.makeMenuItem('label5',
new LzDelegate(ddd, 'eee'));
item.value = this;
}
else
{
item = cm.makeMenuItem('label6', null);
item.setEnabled(false);
}
item.setSeparatorBefore(true);
cm.addItem(item);
}
}
// load global menu
var gcm = canvas.getContextMenu();
var gitems = gcm.getItems();
var glen = gitems.length;
for (var i = 0; i < glen; i++)
{
if (i == 0)
gitems[i].setSeparatorBefore(true);
cm.addItem(gitems[i]);
}
]]>
</method>
THE METHOD
This is an example of the methods invoked by clicking an item in the context menu. This example uses the ‘value’ parameter which can be set when creating the menu item, as is done is some of the menu items above.
<method name="eee" args="item">
return fff(item.value);
</method>
BUGS
From the documentation:
There is an unresolved bug with nesting of context menus in views that were not direct children of the canvas which seems to happen in Flash 7, but is not present when you compile for Flash 8 target (lzr=swf8). That is, in the Flash 7 runtime, you cannot put a menu on a view unless its immediate parent is the canvas.
RESOURCES
“View Source” right click menu item
OpenLaszlo: a way to propagate context menus
its really good link for openlaszlo….