If you happen to be using the Milonic menu system, you may have noticed the availability of a menu API "bolt-on" module. It's woefully underdocumented, and reveals just how ugly the architecture for this menu system really is. For example, global variables abound and there's trickiness around when to pass in a menu's ref (i.e. index into the _m global array of menus) versus a menu's internal name. This makes me very appreciative of the cleaner menu APIs I've seen in the successor JavaScript frameworks, such as Ext JS. I'm using the Milonic system with some older code; I wouldn't recommend it for new projects. I'm not sure I'd recommend Ext JS either, though; the Ext folks just did a bait-and-switch, moving from an LGPL licensing model to a dual GPL/commerical one. That's damn nasty, but I digress...
First off, understand that if you happen to create any menus without menu items, the menu API will blow up in random ways, e.g. "_m[..] is undefined"-type error messages. I had modified the API code to work around the problem until I figured out what was going on. So be aware of that gotcha.
So, for those functions where you need a menu "ref," there's no API function to get it given a menu's internal name. Nice. Here you go...
function mm_getMenuRef(sMenuName)
{
var sMenuName = sMenuName.toLowerCase();
for (var i=0; i < _m.length; i++)
{
if (_m[i] && _m[i][1] == sMenuName)
{
return i;
}
}
return -1;
}
Notice that -1 is returned if no menu with the name exists.
Now here's a function to delete a menu and all its descendants (not just children):
function mm_deleteMenuRecursively(sMenuName)
{
var iRef = mm_getMenuRef(sMenuName);
if (iRef >= 0)
{
var asNames = mm_getChildMenus(iRef).menus;
for (var i = asNames.length - 1; i > 0; i--)
{
mm_deleteMenuRecursively(asNames[i]);
}
mm_deleteMenu(mm_getMenuRef(sMenuName));
}
}
Happy menuing!