Monday, March 13, 2006

OpenLaszlo right-click handler

I had a little trouble with the OpenLaszlo right-click handler, similar to what is described in this post. Specifically, the OpenLaszlo Guide says
To preserve the "about OpenLaszlo" information while disabling source, you can use a method like the following, on the canvas:

<method event="oninit">
this.removeViewSourceMenu();
</method>

<method name="removeViewSourceMenu">
var cm1 = new ContextMenu();
cm1.hideBuiltInItems();
cm1.addItem(this.__LZdefaultMenuItem);
this.setContextMenu(cm1);
</method>

This simply did not work for me. I even tried compiling the application and hitting the swf from my server - no luck.

I wound up doing what was described in the Changelog pages: building the context menus and handler functions inside <script> tags, and setting the context menu using the oninit method for specific views. However, I could never get this working on the canvas itself; it only worked for descendents of the canvas.

I did hunt through the source and find the way to open a popup window from the context menu. Here's a code snippet, similar to that in the changelog:

<script>
 function rightclick_handler(obj, menuobj) {
   Debug.write("hello");
 }

 function my_itemhandler(obj, item) {
  LzBrowser.loadURL("http://yourlink.com", "_blank");
  Debug.write("menu item selected", obj, item);
 }
  var cm1 = new ContextMenu(rightclick_handler);
  cm1.hideBuiltInItems();
  cm1.addItem(new ContextMenuItem("Goto my site", my_itemhandler));
</script>
...
<view>
...
 <method event="oninit">
  this.setContextMenu(cm1);
 </method event="oninit">
</view>

The hideBuiltInItems function is not useless; it prevents the Flash "Zoom In", "Zoom Out", "Show All", "Quality", etc menu items from appearing on right-click, as I learned when I removed it.

The LzBrowser.loadURL method is documented in the OpenLaszlo reference.

0 Comments:

Post a Comment

<< Home