Javascript Jump Menu in TS

2009 December 1 | Filed in: typolink, TMENU, HMENU, Typoscript, Blog Christopher Torgalson

www.typo3apprentice.com wakes up after a long slumber and explains how to create a better jump menu with the Typoscript HMENU object and a bit of CSS and Javascript

Javascript Jump Menu in TS

This first post at www.typo3apprentice.com in far too long was motivated by Jochen Fröhlich's blog post List subpages in a dropdown selection box. In the post, Jochen shows the power of TYPO3's HMENU and TMENU objects by creating a select element listing pages in a menu. The menu uses a bit of javascript to jump to the selected page when a page is chosen.

I found the article interesting, having just completed a similar jump menu for a still-in-development site, and because he'd (quite correctly) used TMENU's CUR state to set the 'selected' attribute on the option element where I had used javascript. In this way, his solution was clearly superior.

However, there were a couple of points about Jochen's jump menu that I thought were handled better in my example:

  • this one usestypolink (see .typolink) for all occurrences of any part of a url
  • this one doesn't use any inline javascript at all

The following is my variant on the Typoscript jump menu (with the sensible addition of the use of CUR as per Jochen's example). First, we start with the planned HTML output:

The Desired Output

  1. <formid="jump-menu"action="index.php?id=32"method="post">
  2.   <div>
  3.     <select>
  4.       <optionvalue="">Choose a page...</option>
  5.       <optgrouplabel="Pages A-M">
  6.         <optionvalue="http://example.com/index.php?id=120">Apples</option>
  7.       </optgroup>
  8.       <optgrouplabel="Pages N-Z"></option>
  9.         <optionvalue="http://example.com/index.php?id=123">Oranges</option>
  10.       </optgroup>
  11.     </select>
  12.   </div>
  13. </form>

Jump Menu Planned Markup Download plain text version

This HTML is rendered by the browser like this:

HTML output

This is generated by a two-level TMENU; the top level provides the two <optgroup> elements, and the second level provides the alphabetically-sorted <option> elements:

Screenshot of menu structure
The menu structure

The Typoscript

That menu and the following typoscript will produce the needed output. Note the extensive use of .typolink and getText Typoscript datatypes to retrieve the full url to the pages in the menu.

  1. lib.jumpMenu = COA
  2. lib.jumpMenu {
  3.   ### Create the opening form element; set action attribute to current page:
  4.   5 = HTML
  5.   5{
  6.     value {
  7.       typolink {
  8.         parameter.data = TSFE:id
  9.         returnLast = url
  10.       }
  11.       wrap = <form id="jump-menu" action="|" method="post">
  12.     }
  13.   }
  14.  
  15.   ### Create the select, optgroup and option elements:
  16.   10 = HMENU
  17.   10{
  18.     ### Form element contents must be contained in a block-level element
  19.     wrap = <div>|</div>
  20.     special = directory
  21.     special.value = {$lib.jumpMenu.pid}
  22.    
  23.     1 = TMENU
  24.     1{
  25.       ### Begin the select--we want a blank option at the top:
  26.       wrap = <select><option value=""class="empty">Choose a page...</option>|</select>
  27.       expAll = 1
  28.       noBlur = 1
  29.    
  30.       ### We have two top level pages named 'Pages A-M' and 'Pages N-Z'. These will become optgroup elements:
  31.       NO {
  32.         doNotShowLink = 1
  33.         allWrap.cObject = HTML
  34.         allWrap.cObject {
  35.           value.dataWrap = <optgroup label="{field:title}">
  36.         }
  37.         wrapItemAndSub = |</optgroup>
  38.       }
  39.     }
  40.    
  41.     ### Create the dynamic option elements:
  42.     2 = TMENU
  43.     2{
  44.       NO {
  45.         ### Don't show the link--it's just as easy to build it in this case:
  46.         doNotShowLink = 1
  47.         ### Create an option tag that looks something like this '<option value="http://example.com/index.php?id=235">Page Title</option>':
  48.         allWrap.cObject = HTML
  49.         allWrap.cObject {
  50.           value {
  51.             typolink {
  52.               parameter.data = field:uid
  53.               returnLast = url
  54.             }
  55.             dataWrap = <option value="http://{getenv:HTTP_HOST}/|">{field:title}</option>
  56.           }
  57.         }
  58.       }
  59.      
  60.       CUR < .NO
  61.       CUR = 1
  62.       CUR {
  63.         ### When it's the current page, we add the selected attribute to the option element:
  64.         allWrap.cObject.value.dataWrap = <option selected="selected" value="http://{getenv:HTTP_HOST}|">{field:title}</option>
  65.       }
  66.     }
  67.   }
  68.  
  69.   ### Create the closing form tag:
  70.   ###
  71.   15 = HTML
  72.   15{
  73.     value = </form>
  74.   }
  75. }

Typoscript Jump Menu Download plain text version

The Javascript

Along with the markup, we need to provide a layer for the jump menu's behaviour–in other words we need some javascript or PHP code for the jump menu do actually do anything. In this case, we use javascript. Using a framework like jQuery or Mootools, this is easily accomplished:

jQuery Version

  1. // jQuery jump menu
  2. $(document).ready(function(){
  3.   $('#jump-menu')
  4.   .show()
  5.   .change(function(){
  6.     var jumpUrl = $(this).find(':selected').val();
  7.     if(jumpUrl != ''){
  8.       location.href = jumpUrl;
  9.     }
  10.   })
  11. });

jQuery for Jump Menu Download plain text version

Mootools Version

  1. // Mootools jump menu
  2. window.addEvent('domready', function(){
  3.   $('jump-menu')
  4.   .setStyle('display', 'block')
  5.   .addEvent('change', function(){
  6.     var jumpUrl = this.getElement(':selected').value;
  7.     if(jumpUrl != ''){
  8.       location.href = jumpUrl;
  9.     }
  10.   })
  11. });

Mootools for Jump Menu Download plain text version

Comments

No comments

Add comment

*
*
* required field

www.typo3apprentice.com
A Bedlam Hotel project © 2007—2010

Report a problem with this page

Bookmark and Share