This example demonstrates how to set MenuItem configuration properties at runtime and listen for the changes through the "configChanged" event.
Note: By default clicking outside of a menu will hide it. Additionally, menu items without a submenu or a URL to navigate to will hide their parent menu when clicked. Click the "Show Menu" button below to make the menu visible if it is hidden.
A MenuItem has a number of configuration
properties that affect its behavior and visual rendering. Any of these
configuration properties can be set at runtime via the
setProperty
method of the item's configuration object
(accessible via its cfg
property.
This example ulitizes the following configuration properties: text, selected, disabled, helptext, url, emphasis, strongemphasis, and checked.
1 | /* |
2 | "configChange" event handler for each MenuItem instance - used |
3 | to log info about the configuration property that was changed. |
4 | */ |
5 | |
6 | function onMenuItemConfigChange(p_sType, p_aArgs) { |
7 | |
8 | var sPropertyName = p_aArgs[0][0], |
9 | sPropertyValue = p_aArgs[0][1]; |
10 | |
11 | YAHOO.log(("Index: " + this.index + ", " + |
12 | "Group Index: " + this.groupIndex + ", " + |
13 | "Custom Event Type: " + p_sType + ", " + |
14 | "\"" + sPropertyName + "\" Property Set To: \"" |
15 | + sPropertyValue + "\""), "info", "example12"); |
16 | |
17 | } |
18 | |
19 | |
20 | /* |
21 | Instantiate a Menu: The first argument passed to the constructor |
22 | is the id for the Menu element to be created, the second is an |
23 | object literal of configuration properties. |
24 | */ |
25 | |
26 | var oMenu = new YAHOO.widget.Menu("basicmenu", { fixedcenter: true }); |
27 | |
28 | |
29 | /* |
30 | Subscribe to the Menu instance's "itemAdded" event in order to |
31 | subscribe to the "configChange" event handler of each MenuItem |
32 | instance's configuration object. |
33 | */ |
34 | |
35 | oMenu.subscribe("itemAdded", function (p_sType, p_aArgs) { |
36 | |
37 | var oMenuItem = p_aArgs[0]; |
38 | |
39 | /* |
40 | Subscribe to the "configChange" event handler of each MenuItem |
41 | instance's configuration object. |
42 | */ |
43 | |
44 | oMenuItem.cfg.subscribe("configChanged", onMenuItemConfigChange); |
45 | |
46 | }); |
47 | |
48 | |
49 | /* |
50 | Add items to the Menu instance by passing an array of strings |
51 | (each of which represents the "text" configuration property of a |
52 | YAHOO.widget.MenuItem instance) to the "addItems" method. |
53 | */ |
54 | |
55 | oMenu.addItems([ |
56 | |
57 | "Selected MenuItem", |
58 | "Disabled MenuItem", |
59 | "MenuItem With A URL", |
60 | "Checked MenuItem" |
61 | |
62 | ]); |
63 | |
64 | |
65 | /* |
66 | Since this Menu instance is built completely from script, call the |
67 | "render" method passing in the DOM element that it should be |
68 | appended to. |
69 | */ |
70 | |
71 | oMenu.render("rendertarget"); |
72 | |
73 | |
74 | /* |
75 | Set a configuration property of each MenuItem to trigger the firing |
76 | of its configuration object's "configChanged" event. |
77 | */ |
78 | |
79 | oMenu.getItem(0).cfg.setProperty("selected", true); |
80 | oMenu.getItem(1).cfg.setProperty("disabled", true); |
81 | oMenu.getItem(2).cfg.setProperty("url", "http://www.yahoo.com"); |
82 | oMenu.getItem(3).cfg.setProperty("checked", true); |
view plain | print | ? |
You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.
INFO 143ms (+76) 11:26:27 AM:
LogReader instance0
LogReader initialized
INFO 67ms (+0) 11:26:27 AM:
example12
Index: undefined, Group Index: undefined, Custom Event Type: configChanged, "checked" Property Set To: "true"
INFO 67ms (+0) 11:26:27 AM:
example12
Index: undefined, Group Index: undefined, Custom Event Type: configChanged, "url" Property Set To: "http://www.yahoo.com"
INFO 67ms (+0) 11:26:27 AM:
example12
Index: undefined, Group Index: undefined, Custom Event Type: configChanged, "disabled" Property Set To: "true"
INFO 67ms (+10) 11:26:27 AM:
example12
Index: undefined, Group Index: undefined, Custom Event Type: configChanged, "selected" Property Set To: "true"
INFO 57ms (+0) 11:26:27 AM:
MenuManager
MenuItem yui-gen3 successfully registered.
INFO 57ms (+0) 11:26:27 AM:
Menu basicmenu
Item added. Text: Checked MenuItem, Index: 3, Group Index: 0
INFO 57ms (+0) 11:26:27 AM:
MenuManager
MenuItem yui-gen2 successfully registered.
INFO 57ms (+0) 11:26:27 AM:
Menu basicmenu
Item added. Text: MenuItem With A URL, Index: 2, Group Index: 0
INFO 57ms (+0) 11:26:27 AM:
MenuManager
MenuItem yui-gen1 successfully registered.
INFO 57ms (+1) 11:26:27 AM:
Menu basicmenu
Item added. Text: Disabled MenuItem, Index: 1, Group Index: 0
INFO 56ms (+0) 11:26:27 AM:
MenuManager
MenuItem yui-gen0 successfully registered.
INFO 56ms (+1) 11:26:27 AM:
Menu basicmenu
Item added. Text: Selected MenuItem, Index: 0, Group Index: 0
INFO 55ms (+0) 11:26:27 AM:
MenuManager
Menu basicmenu successfully registered.
INFO 55ms (+0) 11:26:27 AM:
MenuManager
DOM event handlers initialized.
INFO 55ms (+22) 11:26:27 AM:
Menu basicmenu
No source element found. Created element with id: basicmenu
INFO 33ms (+1) 11:26:27 AM:
Get
Appending node: ../../../2.x/build/event-mouseenter/event-mouseenter-min.js
INFO 32ms (+0) 11:26:27 AM:
Get
attempting to load ../../../2.x/build/event-mouseenter/event-mouseenter-min.js
INFO 32ms (+32) 11:26:27 AM:
Get
_next: q0, loaded: undefined
INFO 0ms (+0) 11:26:27 AM:
global
Logger initialized
Copyright © 2011 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings