Installation icon for dijit.MenuItem

We have a case where we only know the icon for a menu item at runtime. I know that there is an iconClass parameter for diji.MenuItem , but that doesn’t help much if we don’t dynamically add CSS rules at runtime using dojox.html.insertCssRule - there should be a better way!

Here is an example of what we are trying to do:

 pMenu = new dijit.Menu({ targetNodeIds: ["NEW_APP"], leftClickToOpen: true }); pMenu.popupDelay = 100; pMenu.addChild(new dijit.PopupMenuItem({ label: "clocks", iconSrc: "image/clocks.png", onClick: dojo.hitch(core.editor, core.editor.createNewApp) })); 
+4
source share
2 answers

Of course, there is a better way, although not ideal, something like:

myMenuItem.iconNode.style.cssText = "background-image: url(...); width: 16px, height: 16px";

+3
source

Link to http://robrobbins.info/?p=372 for an older version of dojo. In newer syntax, a class can be defined as follows to do the same:

 define("Foo/FooMenuItem", ['dojo', 'dijit/dijit', "dojo/_base/declare", "dijit/MenuItem"], function(dojo, dijit, declare, MenuItem) { return declare("Foo.FooMenuItem", [MenuItem], { iconSrc: "unknown", _setIconSrcAttr: {node: "iconNode", type: "attribute", attribute: "src" } }); }); 

In the simple Foo.FooMenuItem class, the "icon" property can simply be set when the class is initialized and the set of values ​​is inserted into the img src file registered for the icon. You can refer to it like this:

 pMenu.addChild(new Foo.FooMenuItem ({ label: "clocks", iconSrc: "image/clocks.png", onClick: dojo.hitch(core.editor, core.editor.createNewApp) })); 
+3
source

Source: https://habr.com/ru/post/1299228/


All Articles