Index of /~iam/submenu_bug

[ICO]NameLast modifiedSizeDescription

[DIR]Parent Directory   -  
[   ]chrome.manifest 06-Jun-2010 14:54 265  
[DIR]chrome/ 06-Jun-2010 14:54 -  
[DIR]components/ 06-Jun-2010 14:54 -  
[   ]install.rdf 06-Jun-2010 14:54 927  
[   ]makefile 06-Jun-2010 14:54 437  
[   ]submenu_bug.xpi 06-Jun-2010 14:54 3.0K 

A Warning: Submenus in Firefox extensions!

A Warning: Submenus in Firefox extensions!

This is the solution to the conundrum described here. It turns out that doing, dynamically created, submenus in extensions is easy as one would expect, as long as one does not make a very simple mistake. The mistake is not paying close attention to who is supposed to be handling the popupshowing event.

The salient point being "When using nested submenus, make sure to check in the popupshowing event that the event corresponds to the right popup. This is because the popup events bubble so the parent menu will recieve a popupshowing event whenever it opens, or any submenus open." which I snarfed from here.

Viz a viz:

function Submenu_Bug_populate(event){
    var menu = document.getElementById('submenu_bug_btn_popup');
    //without this *simple* guard here, life can be very very frustrating...
    if(event.target == menu){
      while(menu.hasChildNodes()){ menu.removeChild(menu.firstChild);  }
      for(var j = 0; j < 5; j++){
        if(j != 3){
          menu.appendChild(createMenuItem("Choice #" + j, j)); 
        } else {
          var submenu = document.createElement("menu");
          var popup = document.createElement("menupopup");
          popup.appendChild(createMenuItem("Subitem A" + j , j));
          popup.appendChild(createMenuItem("Subitem B" + j , j));
          submenu.setAttribute("label", "Choice #" + j);
          submenu.appendChild(popup);
          menu.appendChild(submenu);
        }
      }
    } 
}


function createMenuItem(label, value){
  var item = document.createElement("menuitem"); 
  item.setAttribute("label", label);
  item.setAttribute("class", "menuitem-iconic");
  item.setAttribute("value", value);
  return item;
}

Namaste!