Sunday, August 29, 2010

YUI "website top nav" Menu from JavaScript only

I recently had the opportunity to observe someone running into some difficulties trying to implement a YUI 2 Menu. The menu items were to be retrieved from a database, and needed to be dynamically updatable through an AJAX call - so using JSON rather than HTML to build the menu made sense in this case. According to YUI's menu page, "Menus can be created from simple, semantic markup on the page or purely through JavaScript".

In this case, getting the menu built "purely through JavaScript" was looking to be a little bit of a challenge. The "Website Top Nav With Submenus" was the type of menu desired. YUI's available menu examples included this type of menu built both "From Markup" and "From JavaScript". However, the "From JavaScript" version still built the top-level menu with HTML rather than JavaScript. While certainly possible, dynamically generating both HTML and JavaScript from the server-side certainly didn't seem ideal.

Recognizing that YUI is a very robust JavaScript library, I knew that there had to be a way to build the menu with only JavaScript. After starting with a new test script, I quickly found the 2 main parts to the solution:

  1. The MenuBar constructor accepts an "itemData" property as part of its 2nd "config" argument. The menu structure can be configured here for the top level, in a fashion identical to the "aSubmenuData" array of objects visible in the original YUI example.
  2. All menu items - including the top-level menus, submenus, and the menu bar itself - must have a unique ID for the HTML DOM, otherwise the item will not activate. For example, if a top-level menu is missing an ID, it will display, but not activate to display the submenus.

    Ideally, it'd be nice to have the "id" property optional. However, the "YAHOO.util.Dom.generateId()" function works well for this - and at least for the purposes of this example, I aliased it as simply "id".

The complete updated example is shown below. For lack of better data, the same example data from the original YUI example is used:

JavaScript source code:

    var id = YAHOO.util.Dom.generateId;
    var menuData = [
        text: "Communication",
        submenu: {id: id(), itemdata: [ 
          {text: "360", url: ""},
          {text: "Alerts", url: ""},
          {text: "Avatars", url: ""},
          {text: "Groups", url: ""},
          {text: "Internet Access", url: ""},
            text: "PIM", 
            submenu: { 
              id: id(), 
              itemdata: [
                {text: "Yahoo! Mail", url: ""},
                {text: "Yahoo! Address Book", url: ""},
                {text: "Yahoo! Calendar",  url: ""},
                {text: "Yahoo! Notepad", url: ""}
          {text: "Member Directory", url: ""},
          {text: "Messenger", url: ""},
          {text: "Mobile", url: ""},
          {text: "Flickr Photo Sharing", url: ""},
        text: "Shopping",
        submenu: {id: id(), itemdata: [
          {text: "Auctions", url: ""},
          {text: "Autos", url: ""},
          {text: "Classifieds", url: ""},
          {text: "Flowers & Gifts", url: ""},
          {text: "Real Estate", url: ""},
          {text: "Travel", url: ""},
          {text: "Wallet", url: ""},
          {text: "Yellow Pages", url: ""}
        text: "Entertainment",
        submenu: {id: id(), itemdata: [
          {text: "Fantasy Sports", url: ""},
          {text: "Games", url: ""},
          {text: "Kids", url: ""},
          {text: "Music", url: ""},
          {text: "Movies", url: ""},
          {text: "Radio", url: ""},
          {text: "Travel", url: ""},
          {text: "TV", url: ""}
        text: "Information",
        submenu: {id: id(), itemdata: [
          {text: "Downloads", url: ""},
          {text: "Finance", url: ""},
          {text: "Health", url: ""},
          {text: "Local", url: ""},
          {text: "Maps & Directions", url: ""},
          {text: "My Yahoo!", url: ""},
          {text: "News", url: ""},
          {text: "Search", url: ""},
          {text: "Small Business", url: ""},
          {text: "Weather", url: ""}
    new YAHOO.util.YUILoader({require: ["menu"], onSuccess: function(){
      var oMenuBar = new YAHOO.widget.MenuBar(id(), {
        autosubmenudisplay: true, 
        hidedelay: 750, 
        itemdata: menuData});

HTML source code:

  <div class="yui-skin-sam">
    <div id="com.ziesemer.demos.yuiMenuJsOnly.global_menu_parent" class="yuimenubarnav"></div>


No comments: