This example demonstrates how to use the MenuBar widget to create a menu bar entirely from JavaScript that functions like one found in a traditional desktop application.
Begin by defining an array of MenuItem configuration properties that describe each item in the MenuBar.
1 | /* |
2 | Define an array of object literals, each containing |
3 | the data necessary to create the items for a MenuBar. |
4 | */ |
5 | |
6 | var aItemData = [ |
7 | |
8 | { |
9 | text: "<em id=\"yahoolabel\">Yahoo!</em>", |
10 | submenu: { |
11 | id: "yahoo", |
12 | itemdata: [ |
13 | "About Yahoo!", |
14 | "YUI Team Info", |
15 | "Preferences" |
16 | ] |
17 | } |
18 | |
19 | }, |
20 | |
21 | { |
22 | text: "File", |
23 | submenu: { |
24 | id: "filemenu", |
25 | itemdata: [ |
26 | |
27 | { text: "New File", helptext: "Ctrl + N" }, |
28 | "New Folder", |
29 | { text: "Open", helptext: "Ctrl + O" }, |
30 | { |
31 | text: "Open With...", |
32 | submenu: { |
33 | id: "applications", |
34 | itemdata: [ |
35 | "Application 1", |
36 | "Application 2", |
37 | "Application 3", |
38 | "Application 4" |
39 | ] |
40 | } |
41 | }, |
42 | { text: "Print", helptext: "Ctrl + P" } |
43 | |
44 | ] |
45 | } |
46 | |
47 | }, |
48 | |
49 | { |
50 | text: "Edit", |
51 | submenu: { |
52 | id: "editmenu", |
53 | itemdata: [ |
54 | |
55 | [ |
56 | { text: "Undo", helptext: "Ctrl + Z" }, |
57 | { text: "Redo", helptext: "Ctrl + Y", disabled: true } |
58 | ], |
59 | |
60 | [ |
61 | { text: "Cut", helptext: "Ctrl + X", disabled: true }, |
62 | { text: "Copy", helptext: "Ctrl + C", disabled: true }, |
63 | { text: "Paste", helptext: "Ctrl + V" }, |
64 | { text: "Delete", helptext: "Del", disabled: true } |
65 | ], |
66 | |
67 | [ { text: "Select All", helptext: "Ctrl + A" } ], |
68 | |
69 | [ |
70 | { text: "Find", helptext: "Ctrl + F" }, |
71 | { text: "Find Again", helptext: "Ctrl + G" } |
72 | ] |
73 | |
74 | ] } |
75 | |
76 | }, |
77 | |
78 | "View", |
79 | |
80 | "Favorites", |
81 | |
82 | "Tools", |
83 | |
84 | "Help" |
85 | ]; |
view plain | print | ? |
Next use the onDOMReady
method of the Event utility to instantiate the MenuBar as soon as the
page's DOM is available for scripting.
1 | /* |
2 | Initialize and render the MenuBar when the page's DOM is ready |
3 | to be scripted. |
4 | */ |
5 | |
6 | YAHOO.util.Event.onDOMReady(function () { |
7 | |
8 | /* |
9 | Instantiate a MenuBar: The first argument passed to the |
10 | constructor is the id of the element to be created; the |
11 | second is an object literal of configuration properties. |
12 | */ |
13 | |
14 | var oMenuBar = new YAHOO.widget.MenuBar("mymenubar", { |
15 | lazyload: true, |
16 | itemdata: aItemData |
17 | }); |
18 | |
19 | |
20 | /* |
21 | Since this MenuBar instance is built completely from |
22 | script, call the "render" method passing in a node |
23 | reference for the DOM element that its should be |
24 | appended to. |
25 | */ |
26 | |
27 | oMenuBar.render(document.body); |
28 | |
29 | |
30 | /* |
31 | Add a "show" event listener that keeps the left-most |
32 | submenu against the left edge of the browser viewport. |
33 | */ |
34 | |
35 | function onSubmenuShow() { |
36 | |
37 | if (this.id == "yahoo") { |
38 | |
39 | this.cfg.setProperty("x", 0); |
40 | |
41 | } |
42 | |
43 | } |
44 | |
45 | |
46 | // Subscribe to the "show" event for each submenu |
47 | |
48 | oMenuBar.subscribe("show", onSubmenuShow); |
49 | |
50 | }); |
view plain | print | ? |
The "lazyload" property is set to "true" to help speed up the initial load time of the MenuBar instance by deferring the initialization and rendering of each submenu until just before it is initially made visible. The "itemdata" property is set to the array of MenuItem configuration properties; each item in the array will be used to add a new item to the MenuBar when it is rendered. Lastly, it is necessary to add a "show" event handler to keep the position of the first, left-most submenu positioned flush to the edge of the browser's viewport. This is because by default Menu instances try to keep themselves positioned 10 pixels away from the edge of the browser viewport.
Often the first item in a menu bar has an icon as its label, but no text. It is easy to achieve this using CSS, while still ensuring the text of the MenuItem is available to users of a screen reader.
Start by wrapping the MenuItem's text label in an <em>
element. Next, give the <em>
a fixed width, and set the
"text-indent" property to a value that pushes the text beyond the boundaries of
the width. Use the "overflow" property to hide the text. Lastly, apply an
image to the MenuItem instance via the "background" property.
1 | em#yahoolabel { |
2 | |
3 | text-indent: -6em; |
4 | display: block; |
5 | background: url(http://us.i1.yimg.com/us.yimg.com/i/us/nt/b/purpley.1.0.gif) center center no-repeat; |
6 | width: 2em; |
7 | overflow: hidden; |
8 | |
9 | } |
view plain | print | ? |
Copyright © 2008 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings