User Tools

Site Tools


Action Menu Submenu

Summary

Below is an example demonstrating how you can create a hierarchical submenu (if it does not exist), or find a submenu (if it does exist), and position the submenu within the parent menu immediately below an action or menu (if present), via script.

API Areas of Interest

Example

Action_Menu_Add_Submenu.dsa
// Define an anonymous function;
// serves as our main loop,
// limits the scope of variables
(function(){
 
	// Get the action manager
	var oActionMgr = MainWindow.getActionMgr();
	// If we do not have the action manager
	if( !oActionMgr ){
		// We are done...
		return;
	}
 
	// Get the main menu (bar) for the application
	var oMainMenu = oActionMgr.getMenu();
	// If we do not have the main menu
	if( !oMainMenu ){
		// We are done...
		return;
	}
 
	// Get whether or not we have the moveItem function available
	var bCanMoveMenuItem = typeof( oMainMenu.moveItem ) == "function"; //4.22.0.2
 
	// Define the label path of the submenu
	var sMenuLabelPath = "&File/Open Recent";
 
	// Define the type of the submenu
	var nSubMenuType = DzActionMenu.OpenRecentMenu;
 
	// Define the classname of the action to position the submenu after
	var sAnchorAction = "DzOpenAction";
 
	// Define the label of the menu to position the submenu after
	var sAnchorMenu = "";
 
	// Initialize
	var bHadSubmenu = false;
 
	// Find the submenu
	var oSubMenu = oMainMenu.findSubMenu( sMenuLabelPath );
	// If we do not have the submenu
	if ( !oSubMenu ){
		// Create the submenu
		oSubMenu = oMainMenu.findOrCreateSubMenu( sMenuLabelPath, false );
	// If we have the submenu
	} else {
		// Update our flag
		bHadSubmenu = true;
	}
 
	// If we do not have the submenu
	if( !oSubMenu ){
		// We are done...
		return;
	}
 
	// If the menu is not the type we expect and we created it
	if( oSubMenu.menuType != nSubMenuType && !bHadSubmenu ){
		// Set the menu type to the type we want
		oSubMenu.menuType = nSubMenuType;
	}
 
	// If we cannot move menu items
	if( !bCanMoveMenuItem ){
		// We are done...
		return;
	}
 
	// Get the item for the submenu in the parent
	var oSubMenuItem = oSubMenu.getParentItem();
	// If we do not have a parent item
	if( !oSubMenuItem ){
		// We are done...
		return;
	}
 
	// Get the parent menu
	var oParentMenu = oSubMenuItem.getParentMenu();
	// If we do not have a parent menu
	if( !oParentMenu ){
		// We are done...
		return;
	}
 
	// Get the list of menu items in the parent menu
	var aMenuItems = oParentMenu.getItemList();
 
	// Get the index of the last menu item;
	// the position that the new submenu is created at
	var nLastIdx = aMenuItems.length - 1;
 
	// Declare working variable
	var oMenuItem;
 
	// Iterate over the list of menu items in reverse order;
	// we do this so that we can (potentially) make changes
	for( var i = nLastIdx; i >= 0; i -= 1 ){
		// Get the 'current' menu item
		oMenuItem = aMenuItems[ i ];
 
		// If the anchor action classname is not empty
		if( !sAnchorAction.isEmpty() ){
			// If the item type is not an action
			if( oMenuItem.type != DzActionMenuItem.Action
			&& oMenuItem.type != DzActionMenuItem.CustomAction
			&& oMenuItem.type != DzActionMenuItem.FileOpenAction
			&& oMenuItem.type != DzActionMenuItem.FileMergeAction ){
				// Next!!
				continue;
			}
 
			// If the item is not the action we want
			if( oMenuItem.action != sAnchorAction ){
				// Next!!
				continue;
			}
		// If the anchor menu label is not empty
		} else if( !sAnchorMenu.isEmpty() ){
			// If the item type is not a submenu
			if( oMenuItem.type != DzActionMenuItem.SubMenu ){
				// Next!!
				continue;
			}
 
			// If the menu label is not the one we want
			if( oMenuItem.label != sAnchorMenu ){
				// Next!!
				continue;
			}
		// Otherwise
		} else {
			// Next!!
			continue;
		}
 
		// Move the submenu item immediately after the anchor
		oParentMenu.moveItem( oSubMenuItem, i + 1 );
	}
 
	// Allow the application to update the menu
	processEvents();
 
// Finalize the function and invoke
})();