PDA

View Full Version : Programmaticly expand a menu



gtsafas
12 Aug 2016, 11:49 AM
Hi,

I have been trying to write some javascript to expand a menu and select based on an menu item. I have tried several things, most seem to almost work.

Sorry for the code quality, it started very clean but many iterations has added complexity. The goal is to have a reference to any menu item and fully expand the menu from the toolbar -> sub menus -> to the items parent menu and then select the menu item.



function getMenuParent(root){
var parentItem = null;

function findParent(cmp) {
if (cmp.isMenuItem) {
var parentMenu = cmp.parentMenu;
var parentMenuItem = parentMenu.parentItem || parentMenu.ownerItem;

if (!parentMenuItem) {
parentItem = parentMenu.up();
} else {
findParent(parentMenuItem);
}
}
}

findParent(root);
return parentItem;
}

function expandMenu(rootMenuButton, stopAt) {
function _doExpand(menu){
Ext.each(menu.items.items, function(item){
if (item == stopAt) {
menu.setActiveItem(item);
return false;
}

if (item.menu) {
menu.setActiveItem(item);
item.expandMenu();
item.menu.on('show', function() {
_doExpand(item.menu);
}, this, {'single': true});

}
});
}
rootMenuButton.showMenu();
_doExpand(rootMenuButton.menu);
}

function openMenu(root){
var actions = [];
var parentItem = null;

var rootAction = null;

function getActions(cmp) {
if (cmp.isMenuItem) {
var parentMenu = cmp.parentMenu;
var parentMenuItem = parentMenu.parentItem || parentMenu.ownerItem;

if (!parentMenuItem) {
parentItem = parentMenu.up();
rootAction = function () {
parentMenu.up().menu.on('show', function() {
if (actions.length > 0) {
console.log(['FIRING SHOW FOR PARENT MENU', actions]);
var nextAction = actions.pop()
nextAction();
}
}, this, {'single': true});
parentMenu.up().el.dom.click();
}
} else {
actions.push(function() {
parentMenuItem.menu.on('show', function() {
if (actions.length > 0) {
console.log(['FIRING SHOW FOR SUB MENU', actions]);
var nextAction = actions.pop()
nextAction();
}
}, this, {'single': true});
parentMenu.setActiveItem(parentMenuItem);
parentMenuItem.expandMenu();
});
getActions(parentMenuItem);
}
}
}

getActions(root);
return rootAction
}

jdkhamba
17 Aug 2016, 8:53 AM
A little hacky but doable:

https://fiddle.sencha.com/#fiddle/1fcq