Chapter 7. Dynamic Menus and Interactive Elements

Some of the techniques that we’re about to discuss in this chapter and the next can be used inappropriately and needlessly, and can create issues with usability and accessibility standards, but we’re not going to ignore them, because if you haven’t already been asked for one or more of these website enhancements, you will be. In this chapter, we’ll go over adding drop-down menus to your Joomla! template and discuss various ways of displaying Flash content. It is likely that two out of every five clients have already asked you for drop-down menus, slick Flash headers, and other interactive content tidbits that they insist will give their site some Pizazzz

I find anyone uttering the “P” word extremely annoying. Anyone using this word (or other words like it) is definitely not part of the development or design team. Unfortunately, the people who do use such words, as Steve Krug notes in his excellent book Don’t Make Me Think, are usually the CEO, a VP, or someone else with money for the project, and where possible, you give them exactly what they want. So Pizazzz it is.

Note

Don’t Make Me Think. A Common Sense Approach to Website Usability is an excellent book on website design for usability and testing, and anyone who has anything to do with website development or design can greatly benefit from it. You’ll learn why people really leave websites, how to make your site more usable and accessible, and even how to survive those executive design whims (without the use of a hammer). You can find out more from Steve’s site, which is at http://www.sensible.com/.

Dynamic Menus

This is the nice thing about Joomla!: it’s all dynamic. Once you’ve installed Joomla! and designed a great template for it, anyone with the right level of administrative capability can log into the administration panel and add, edit, and delete content and menu items. But generally, when people ask for dynamic menus, what they really want are those appearing and disappearing drop-down menus, they like because it quickly gives a site a very “busy” feel: “Wow, these guys have so much going on, they need drop-down menus to conserve real estate!”

I must add my own disclaimer, I don’t like drop downs. It’s not that they’re wrong or bad; they just don’t meet my own aesthetics, and I personally don’t think that they are user friendly. I’d prefer to see a menu system that, if it requires subsections, displays them somewhere consistently on the page, either by having a vertical navigation bar expanded to display the subsections underneath, or if a horizontal menu is used, show additional subsections in a set location on the page.

Dynamic Menus

Figure 7.1 Vertical and horizontal menus with consistent sub menus displayed.

I like to be able to look around and say: “OK, I’m in the New Items | Cool Dink section and I can also check out Red Dinks and Retro Dinks within this section”. I personally find having to constantly go back up to the menu and drop-down options to remind myself of what’s available annoying. If I still haven’t convinced you not to use drop downs, read on.

Drop Downs

So you’re going to use drop downs. Again, it’s not wrong, but I would caution you to help your client take a look at their site’s target users before implementing them. If there’s a good chance that most users are going to be using the latest browsers, which support current JavaScript, CSS, and Flash standards and everyone has great mobility and is mouse-ready, then there’s really no issue, go for it.

However, if it becomes apparent that some of the site’s target users will be using older browsers or physical handicaps that will limit them to tabbing through content, you must consider not using drop downs or provide an alternative means of getting through the content such as alternate templates.

Note

Alternate Templates. You know how to make great Joomla! templates, so why not make more than one? Using Joomla!’s Template Switcher module would enable users to chose a template that displays navigation in a way that lets them tab through the content.

I was especially negative about drop-down menus, because until recently they required bulky JavaScripting or Flash, which makes having a clean, semantic, SEO-friendly XHTML difficult.

The Suckerfish method developed by Patrick Griffiths and Dan Webb of AListApart.com is wonderful because it takes valid, semantically accurate unordered lists and using (almost) pure CSS, creates drop downs (IE per usual, poses a problem or two for us, so some minimal DOM JavaScripting is needed to compensate and achieve the correct effect even in that browser.). The drop downs are not tab accessible, but they will simply be displayed as a single, clear unordered list in older browsers that don’t support the required CSS, and they will allow for very easy template switching if you allow users alternative options. If you haven’t heard of or worked with the Suckerfish method, I would recommend that you read Dan and Patrick’s article, which is at http://alistapart.com/articles/dropdowns.

I suggest that you play around with the sample code provided in this article so that you understand exactly how it works. Next, we’ll look at how to apply this method to your Joomla! template.

SuckeroomlaFish

The essential part of this effect is getting your menu items to show up as unordered lists with unordered sublists. Once you do that, the rest of the magic can be easily handled by finessing the CSS that Patrick and Dan suggest into your template’s CSS and placing the DOM script in your template’s index.php header tag.

As you may recall, in the second half of Chapter 3, we set our topmenu and mainmenu options to be output as Flat Lists, a.k.a. unordered lists. We then styled the topmenu to display as a Horizontal list similar to what Patrick and Dan described in the first part of the Style It section of their Suckerfish article. For this example, I’m going to use my mainmenu, which is not a horizontal menu, but you’ll quickly see that’s OK too.

All we need now are those second level sublists. This is easily done by going to the Menu | mainmenu manager in Joomla! and creating additional menu items by selecting the New button from the top-right. The key is to just make sure that your new menu items have the parent item listed as the existing menu item you want them to be under, and not Top. You should now see your subitems back in the Menu Manager.

SuckeroomlaFish

Figure 7.2 Submenus

What If Nothing Drops?

In theory, all one would have to do is, go over to Modules | Site Modules, select your mainmenu or topmenu module (or any menu you’d like to apply this drop-down effect to), and make sure that Menu Style is set to Flat List and that Expand Menu is set to Yes. This would tell the menu to display in unordered lists and to show all the submenus constantly rather than just when the main menu item has been clicked.

There’s just one small problem. This doesn’t work. It’s not just the Expand Menu option either: submenus in general simply do not work if you’re displaying your menus as Flat Lists at this time in Joomla! 1.0.x. (I tried it from versions 1.0.8 to 1.0.12, and got zip, nada, squat.)

Never fear; as is the case with most open-source things, some very clever geek has figured out a solution to this problem and has it readily available for download as a module for Joomla!. Daniel Ecer has saved the day, so head over to his site, and download the Extended Menu module from the download section on this page: http://de.siteof.de/extended-menu.html.

Installing the Extended Menu Module

If you’ve never installed a Joomla! Module, here’s your chance. The Menu module is all zipped up and ready to go. Once you’ve downloaded it, simply log on to your Administration Panel and head over to Installers | Modules. You’ should then browse, select the ZIP file, and hit Upload File and Install. (It’s exactly like installing a template which we discussed in Chapter 5.)

Once you have received the File Upload - Success message, you just need to move your menu items over into this new module. No worries, it’s easy. If you go to Modules | Site Modules and you should see the new Extended Menu. Be sure to publish the new Extended Menu and unpublish your previous menu. Now, click on the Extended Menu and set your preferences.

Installing the Extended Menu Module

Figure 7.3 Expanding menu preferences

You’ll notice that there are over three times as many preferences to choose from as before, but you’ll want to make sure that the Extend Menu module is located in the same position as your old menu (in my case, the “main” module location) and that it’s assigned to the same menu name that your old menu (in my case, mainmenu). Now, you’ll want to set your Menu Style to Flat List. Last, we’ll set Expand Menu to Yes and we should be good to go. Let’s check it out:

Installing the Extended Menu Module

Figure 7.4 Unordered lists with sublists

Selecting the menu and checking the DOM inspector shows us that the menu is in fact being displayed using an unordered list with unordered sub-lists.

Applying the CSS to Joomla!

We’re now ready to proceed with the rest of Patrick and Dan’s suggestions. To start, let’s just take their suggested code and see what happens. The unordered-list CSS that Patrick and Dan provide in their web article is intended to format the sublists as drop-down menus and looks like this:

    ul { /* all lists */
            padding: 0;
            margin: 0;
            list-style: none;
    }
    li { /* all list items */
            float: left;
            position: relative;
            width: 10em;
    }
    li ul { /* second-level lists */
            display: none;
            position: absolute;
            top: 1em;
            left: 0;
    }
    li>ul { /* to override top and left in browsers other than IE, which
    will position to the top right of the containing li, rather than
    bottom left */
            top: auto;
            left: auto;
    }
    li:hover ul, li.over ul { /* lists nested under hovered list items */
            display: block;
}

Now, in Joomla!, our menu item’s ul has an id called mainlevel, so Dan and Patrick’s code will need to be tweaked in order to work with Joomla!. And there may or may not be lots of other unordered lists used in our site, so we want to be sure that we only affect ul’s and li’s within that mainlevel id. Also, we want our menu list to remain vertical and have our drop downs coming out to the side, so we’ll simply tweak the CSS a bit to move items out to the left and add #mainlevel to each element in the Suckerfish CSS. The following code takes Dan and Patrick’s CSS, and tweaks it to work with our Joomla! template as follows:

#mainlevel { /* the mainlevel ul (no need to add ul here) */
        padding: 0;
        margin: 0;
            list-style: none;
}
#mainlevel li { /* all list items inside ul */
        width: 160px;
            border-bottom: 1px solid #333;
}
#mainlevel li ul { /* second-level lists */
        display: none;
        position: absolute;
            padding-left: 5px;
            padding-right: 10px;
            text-align: right;
            /*these are for IE placement only*/
        width: 160px;
            margin-top: 0px;
        margin-left: -200px;
            /**/
            list-style: none;
            background-color: #ddd;
}
#mainlevel li ul li { /* second level list items in ul */
        border-bottom: 1px solid #333;
}
#mainlevel li>ul { /* to override top and left in browsers other than
IE, which will position to the top right of the containing li, rather
than bottom left */
        width: 157px;
            margin-top: -15px;
        margin-left: -170px;
}
#mainlevel li:hover ul, #mainlevel li.over ul { /* lists nested under
hovered list items */
        display: block;
}

Applying the DOM Script to Joomla!

The last bit is the JavaScript that makes the hover work in IE. I call it is the DOM script (as many people do), but it’s basically just a JavaScript that rewrites your markup (how your DOM is being perceived by IE) on the fly. Basically, this drop-down effect relies on the CSS hover attribute. However, at this time, CSS in IE only recognizes the hover attribute if it is applied to (link) entity rules. This script appends our additional .over class to the li items in IE only.

You’ll need to add this script to your index.php page’s header tag. Dan and Patrick named their ul’s id nav, and that’s what this script is looking for. Our ul’s id is named mainlevel, so if you simply switching out navRoot = document.getElementById("nav"); to navRoot = document.getElementById("mainlevel"); it will work in IE as well.

The full script in your index.php page’s header tag should look like the following:

<script type="text/javascript"><!--//-->
<!
[CDATA[//><!--
startList = function() {
    if (document.all&&document.getElementById) {
        navRoot = document.getElementById("mainlevel");
        for (i=0; i<navRoot.childNodes.length; i++) {
            node = navRoot.childNodes[i];
            if (node.nodeName=="LI") {
                node.onmouseover=function() {
                this.className+=" over";
                }
                node.onmouseout=function() {
                this.className=this.className.replace(" over", "");
                }
            }
        }
    }
}
window.onload=startList;

//--><!
]]></script>

For demonstration purposes, I’ve kept the CSS pretty barebones and ugly, but when we check this out in our browser we now see the following:

Applying the DOM Script to Joomla!

Figure 7.5 Submenu items

It’s working! At this point, all that’s left is to fix the CSS to make it look exactly the way you want—semantic, SEO, and accessible as possible dynamic menus in Joomla!.

Note

More Suckerfish: Daniel Ecer, the author or the Extended Menu module has made several different Joomla! menu templates that use this SuckerFish method. You can download them from http://de.siteof.de/extended-menu-templates.html. Daniel’s templates offer unique approaches to using the Suckerfish method, and he also takes advantage of Patrick and Dan’s revisited “Son-of-Suckerfish” method, which offers multiple levels and an even further pared down DOM JavaScript. Check it out here: http://www.htmldog.com/articles/suckerfish/dropdowns/.

Using Flash

Adobe Flash has come quite a long way since my first experience with it as a Macromedia product (version 2 in 1997). Yet it still does not adhere to W3C standards, requires a plugin to view, and worst of all is a pretty pricey proprietary product. So why is everyone so hot on using it? Love it or hate it, Flash is here to stay. It does have a few advantages which we’ll take a quick look at.

The Flash player plugin does boast the highest saturation rate around (way above other media player plugins), and it now readily accommodates audio and video. It’s pretty easy to add and upgrade for all major browsers. The price may seem prohibitive at first, but once you’re in for the initial purchase, upgrades for the standard and pro software versions are reasonable, and many third-party software companies offer very cheap authoring tools that allow you to create animations and author content using the Flash-player format. (In most cases, no one needs to know you’re using the $50 version of Swish and not the $800 Flash 8 Pro to create your content.)

Above all, it can do so much more than just play video and audio (like most plugins). You can create seriously rich and interactive content, even entire applications with it, and no matter what you create with it, it is going to look and work exactly the same on all browsers and platforms. These are just a few of the reasons why so many developers chose to build content for the Flash player.

Oh, and did we mention you can easily make visually slick, super cool stuff that has audio and music in it? Yeah, that’s why your client wants it in their site.

The Template

The topmost requested use of Flash is usually in the form of a snazzy header within the template of the site. The idea is that various relevant or random photographs or designs load into the header with some super cool slick animation (and possibly audio) every time a page loads or a section changes.

We’re going to assume that, if you’re using anything that requires the Flash player, you’re pretty comfortable with generating content for it. So we’re not going to show you any Flash timeline tricks or ActionScripting. We’re simply here to help you get it into your Joomla! template.

For the most part, you can simply take the HTML object embed code that Flash (or other third-party tools) will generate for you and paste it into the header area of your Joomla! index.php template file. As long it’s positioned correctly, has a correct height and width, and you’re not accidentally overwriting any parts of the template that contain moduleLoader or other valuable PHP code, you’re good to go.

Figure 7.6 shows an object embed tag placed inside the index.php Joomla! template.

The Template

Figure 7.6 Flash embed tags inside index.php code

Pass Flash a Joomla! Variable

You’ve now popped a nice Flash header into your template. Here’s a quick trick to make it all the more impressive. If you’d like to keep track of what page your Joomla! user has clicked on and display a relevant image or animation in the header, you can pass your Flash SWF file a variable from Joomla! using PHP. The variable $Itemid is used by Joomla! to denote specific page content. If you’ve set Joomla! to use the standard URL strings you will probably notice something like this:

http://mysite.com/index.php?option=com_content&task=view&id=5&Itemid=6

If you’ve set Joomla! to use SEO-friendly URLs you will notice something like the following:

http://mysite.com/index.php/com_content/view/5/6

In the full URL string you can see the $Itemid variable being passed a value of 6. In the SEO-friendly URL, the $Itemid is always the last variable (item after a forward slash) shown. So here we can see that the page in question is $Itemid=6. Let’s say that we have a Flash file that will load a different header randomly but, every time we’re on page $Itemid 6, we want a special animation to play.

In your Flash authoring program, set up a series of animations or images that will load or play based on a variable set in the root time line called itemid. You’ll pass this variable to your ActionScript. If the variable does not equal 6, then any animation may play, but if the variable is 6, then our specific one will play.

Now, let’s get our PHP variable into our SWF file. Add the following to your object embed code where your SWFs are called:

<param name="movie"    value="http://fullpathtofile//myswfname.swf
                                       ?itemid=<?echo'$Itemid';?>" />
<embed src="http://fullpathtofile/myswfname.swf
                                       ?itemid=<?echo"$Itemid";?>"...

Note

Place the full path to your SWF file in the src and value parameters for the embed tags! You can use <?php echo $mosConfig_live_site;?>/flash dir/swffilename.swf too. This just makes sure that your SWF file will load properly.

Now, every time that someone loads a page or clicks a link on your site, this PHP is going to be render out as:

myswfname.swf?itemid=6

or whatever the $Itemid for that page is. So your Flash file’s ActionScript is going to look for a variable called itemid in the root or level0, and do whatever you told it to do based on that value.

For extra credit, you can play around with the other variables passed to your Joomla! template via Joomla! and load special animations or images based on the section $task or $id variables passed to your template. Also, you can send more than one variable to your SWF by appending them together using & (ampersand) characters. For instance:

<embed src="http://fullpathname/myswfname.swf
                      ?itemid=<?echo"$Itemid";?>&id=?echo"$id";?>"...

There are a lot of possibilities for Flash control there.

Getting Around IE’s ActiveX Restrictions

Recently, the IE browser increased its security so that users have to validate content that shows up in the Flash player (or any other player). The animation will kick off, but there will be this grey outline around your Flash content area which may or may not mess up your design.

If your header content doesn’t require anything clicked in it, then the grey box is the only problem. If you have content in your header, which can be clicked on or moused over, then the user will have to double-click in the Flash content area first before the content itself becomes clickable. This can confuse some users and make them think your content is broken. You can get around this validation issue by including your Flash content via a JavaScript include. The following two JavaScripts will call the JavaScript code and set the parameters for the code:

<script src="<?php echo $mosConfig_live_site;?>/js/flash/loadFlash.js"
    type="text/javascript"></script>
<!--
// Globals
// Major version of Flash required
var requiredMajorVersion = 8;//or whatever version you'd like
// Minor version of Flash required
var requiredMinorVersion = 0;
// Revision of Flash required
var requiredRevision = 0;
// the version of javascript supported
var jsVersion = 1.0;
// -->
</script>

You’ll also need to add in this VB script which will do some “IE ActiveX magic” (and even I’m not entirely sure exactly what it does, but the loadFlash.js file won’t work without it):

<script language="VBScript" type="text/vbscript">
<!-- // Visual basic helper required to detect Flash Player ActiveX
    control version information
Function VBGetSwfVer(i)
  on error resume next
  Dim swControl, swVersion
  swVersion = 0
  set swControl = CreateObject("ShockwaveFlash.ShockwaveFlash." +
                                                           CStr(i))
  if (IsObject(swControl)) then
    swVersion = swControl.GetVariable("$version")
  end if
  VBGetSwfVer = swVersion
End Function
// -->
</script>

You’ll then call the embedded code above within a script like the following, which sets the file parameters and defines your alternative content (content that the user will see if they do not have the Flash Player or have the wrong Flash player version):

<script type="text/javascript">
<!--Set your alt content here for people who don't have flash-->
var altContent = '<img src="<?php echo $mosConfig_live_site;?>
                             flash/StaticReplaceImage.jpg" border="0"
                                        width="240" height="311" />';
<!--Calls the actual flash file and passes parameters-->
loadFlash('<?php echo $mosConfig_live_site;?>/flash/mySwfname.swf',
                        240, 310, 'Hope Tree', 'FFFFFF', altContent);
</script>

You included JavaScript (loadFlash.js), which will reference the Flash player version that you’re testing for and the VB ActiveX script, write the Flash embed tag on the fly to DOM of your pages (very similar to how the SuckerFish menu works in IE), and bypass IE’s ActiveX security restriction. The loadFlash.js will look like the following:

<!--
function loadFlash(file, width, height, name, bgcolor, altContent){
var hasRightVersion = DetectFlashVer(requiredMajorVersion,
                             requiredMinorVersion, requiredRevision);
if(hasRightVersion) {  // if we've detected an acceptable version
    var oeTags = '<object classid=
                        "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'
    + 'width="'+width+'" height="'+height+'"'
    + 'codebase="http://download.macromedia.com/pub/shockwave/
                                            cabs/flash/swflash.cab">'
    + '<param name="menu" value="false" />
                        <param name="movie" value="'+file+'" />
                        <param name="quality" value="high" />
                        <param name="bgcolor" value="#'+bgcolor+'" />
                        <param name="wmode" value="transparent" />'
    + '<embed src="'+file+'" quality="high" bgcolor="#'+bgcolor+'" '
    + 'width="'+width+'" height="'+height+'" name="'+name+'"
                                                      align="middle"'
    + 'play="true"'
    + 'loop="false"'
    + 'quality="high"'
        + 'wmode="transparent"'
        + 'menu="false"'
    + 'allowScriptAccess="sameDomain"'
    + 'type="application/x-shockwave-flash"'
    + 'pluginspage="http://www.macromedia.com/go/getflashplayer">'
    + '</embed>'
    + '</object>';
    document.write(oeTags);   // embed the flash movie
  } else {  // flash is too old or we can't detect the plugin
    var alternateContent = altContent;
    document.write(alternateContent);  // insert non-flash content
  }
}//end loadFlash
// -->

What’s nice about this method is that we account for the occasional user who does not have Flash, in which case they will see a nicely designed static image, which is defined in the “alternate content”. This can let the user know they’re missing out on the cool Flash content and ask them to get the Flash plugin. However, the actual content experience of the Joomla! site is not halted.

In a Joomla! Page

For content that’s going to go into a specific Joomla! page, you’re in luck. There’s a Flash content wizard button in the TinyMCE WYSIWYG editor.

In a Joomla! Page

Figure 7.7 Adding the Flash wizard

There’s one small problem: again, the button will directly place an object embed tag into the page with no version check and of course, if the user is browsing with IE, they will have to click in the grey box area. The good news is that the Javascript that we created above for the header can be leveraged anywhere on your site! You’re already including it in the main template, so the script is available to any content on your site.

While there is an HTML button in your WYSIWYG editor, you may find that once you save the page, it still overwrites the custom HTML you entered. This is no good. It will be better to go to the Joomla! Administrator Panel, and change the WYSIWYG editor option on the far right, to None. (You may have to get the Joomla! Super Administrator do this for you, if you do not have Super Administrator access.)

Once you have turned off the WYSIWYG editor, you’ll be able to open any Joomla! content page, insert the player version, and loadFlash.js scripts above as follows. (The VB script is already in the index.php template and you’re just referencing the loadFlash.js file so the only code you’ll need to add is shown in Figure 7.8.)

In a Joomla! Page

Figure 7.8 The HTML view

Note

Speaking of Interactive Elements! Add fun and relevant instant updating content to your site! Using the HTML view of the content editor on a page is a great way to add additional fun elements to your Joomla! content. For instance, got a Flicker account? Most community, news, and social application websites offer neat “widgets” that you can place within your own site that are usually served as small JavaScripts. You can copy and paste these scripts into any page or module in the Joomla! site using the HTML view for that content item. Got an AdWords or AdBright account? Paste your Google code directly into your template or into select content pages (for those optimal “hot spots” that they talk about on their site) and start making money on your site!

Summary

In this chapter, we’ve looked at getting drop-down Suckerfish menus and Flash content quickly and painlessly into your Joomla! site. Next up: getting AJAX with dynamic interactive forms into your Joomla! project.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.41.214