MenuItem.parent

By Opera Software

From Opera 15 onward, Opera 11 & 12’s extension format is no longer supported, and instead, we’ve switched to Chromium’s extension model. Check out our new documentation for developing extensions for Opera 15 and higher and start building your own extensions.

Description:

The read-only parent attribute is the parent object on which the current context menu item resides. By default, and on object creation, this attribute is set to null.

Syntax:

readonly MenuContext parent

Example:

The following example creates a main menu item and two sub-menu items. The sub-menu items refer to the parent object to set their icons.

<!--
  The configuration file ('config.xml').
-->
<?xml version='1.0' encoding='utf-8'?>
<widget xmlns="http://www.w3.org/ns/widgets">
    ...
    <feature name="opera:contextmenus"/>
    ...
</widget>
//
// The background process (e.g. index.html)
//

if (opera.contexts.menu) {
  var menu = opera.contexts.menu;

  // Create menu item properties objects
  var itemProps = {
    title: 'Translate this page',
    icon: 'images/translate.png',
    type: 'folder'
  }
  var itemPropsEnglish = {
    title: 'English',
    onclick: function(event) {
      doTranslate(event.pageURL, 'English');
    }
  };
  var itemPropsJapanese = {
    title: 'Japanese',
    onclick: function(event) {
      doTranslate(event.pageURL, 'Japanese');
    }
  };

  // Create menu items with the specified properties
  var item = menu.createItem(itemProps);
  var itemEnglish = menu.createItem(itemPropsEnglish);
  var itemJapanese = menu.createItem(itemPropsJapanese);

  // Add the menu item to the context menu
  menu.addItem(item);
  // Add the sub-menu items to the main menu item, with the same icon
  item.addItem(itemEnglish);
  item.addItem(itemJapanese);
  itemEnglish.icon = itemEnglish.parent.icon;
  itemJapanese.icon = itemJapanese.parent.icon;
}

This article is licensed under a Creative Commons Attribution 3.0 Unported license.

Comments

No new comments accepted.