Opened 12 years ago

Closed 11 years ago

#4833 closed defect (wontfix)

No way to programmatically identify a menuItem

Reported by: guest Owned by:
Priority: high Milestone: 1.1
Component: Dijit Version: 0.9
Keywords: menu Cc: ghislain.seguin@…
Blocked By: Blocking:

Description

In one of my widget's template I create a context menu as follows:

<ul dojoType="dijit.Menu" dojoAttachPoint="contextMenu" style="display: none;">

<li dojoType="dijit.MenuItem?" iconClass="new"

onClick="onCreate">Create</li>

<li dojoType="dijit.MenuItem?" iconClass="mailFoldersActionRename"

onClick="onRename">Rename</li>

</ul>

I want to be able to enable/disable a menuItem at popup depending on which node the popup menu was triggered on. I could use the menuItem's label to identify the menu item but this is not a clean solution.

Here is the patch I propose:

Index: Menu.js =================================================================== --- Menu.js (revision 11056) +++ Menu.js (working copy) @@ -344,6 +344,9 @@

if false, the menu item is enabled disabled: false,

+ action identifier for this menu item + action: "", +

postCreate: function(){

dojo.setSelectable(this.domNode, false); this.setDisabled(this.disabled);

<<<<<<<<<<<<<<<<<<<

This way when defining the menu in my widget's template I can give an action identifier as follows:

<ul dojoType="dijit.Menu" dojoAttachPoint="contextMenu" style="display: none;">

<li dojoType="dijit.MenuItem?" iconClass="new"

action="CREATE" onClick="onCreate">Create</li>

<li dojoType="dijit.MenuItem?" iconClass="mailFoldersActionRename"

action="RENAME" onClick="onRename">Rename</li>

</ul>

Attachments (1)

4833.patch (385 bytes) - added by guest 12 years ago.

Download all attachments as: .zip

Change History (6)

Changed 12 years ago by guest

Attachment: 4833.patch added

comment:1 Changed 12 years ago by bill

I don't understand how simply adding an attribute to MenuItem? will let you enable/disable a menuItem at popup depending on which node the popup menu was triggered on. How do you know which node the popup menu was triggered on? And how do you control the enabling/disabling?

comment:2 Changed 12 years ago by guest

I have a dijit.Tree to which the Menu is bound. In the store that feeds the Tree, each item has an member array disabledActions containing the actions that don't apply to that specific item. When the context menu is shown, it it then very easy to disable the MenuItem? corresponding to the disabled action for the item on which the context menu was triggered.

Does it make a little more sense?

comment:3 Changed 12 years ago by guest

Here is the code to handle what I was just talking about:

enable menu items depending on the selected node menu.getChildren().forEach(

function(menuItem){

menuItem.setDisabled((dojo.indexOf(disabledActions, menuItem.action) != -1));

}

);

comment:4 Changed 12 years ago by bill

Milestone: 1.1

I see. This reminds me of how TreeNodes? etc. have an item (a dojo.data item) which can have any attributes you want.

Note that you can add an attribute to a widget on your own, without modifying dojo, just by doing

dijit.MenuItem.prototype.action="";

in your own js file.

Also note that to make this more general you might want to make the action an object, rather than a string, by adding this to MenuItem?.js:

action: null,

and then in your HTML file do

<div dojoType=dijit.MenuItem action="{foo: 1, bar: 'zaz'}">

Anyway, marking for consideration in 1.1

comment:5 Changed 11 years ago by bill

Resolution: wontfix
Status: newclosed

I thought about this. There are already three ways to programmatically identify a MenuItem?:

  • use the id (you can set an id on any widget)
  • use the iconClass
  • add a custom attribute to the MenuItem? (see above explanation)

So I don't see any reason to modify the dijit code to support more, sorry.

Note: See TracTickets for help on using tickets.