ar\html\menu
This module provides a simple way to generate accessible menu's. You can create a menu object with the ar('html')->menu()
or ar('html/menu')->el()
method and then use the methods described below.
Example 1 - a simple menu bar:
<pinp>
echo ar('html')->menu()->bar();
</pinp>
Will ouput:
<ul>
<li>
<a href="http://.../">foo</a>
</li>
<li>
<a href="http://.../">bar</a>
</li>
<li>
<a href="http://.../">xyzzy</a>
</li>
</ul>
The menu class extends the ar_htmlElement
class, so you can use any of the dom methods as well.
methods
(object) |
bar |
Returns a query object which finds the direct children of the root object. |
(object) |
crumbs |
Returns a query object which finds all parents of the current object, up to the root. |
(object) |
el |
Returns a new ar_html_menuElement. |
(object) |
sitemap |
Returns a query object which finds all children of the root object. |
(object) |
tree |
Returns a query object which finds parents, siblings and children of the current object. |
bar
Ariadne > 2.7.5
(object) ar('html/menu')->bar( $options = array() )
(array) |
$options |
An optional list of options. |
This method returns a query object (ar_storeFind) which will find all the direct children of the top object. The top can be specified by setting $options['top']
to the correct path.
crumbs
Ariadne > 2.7.5
(object) ar('html/menu')->crumbs( $options = array() )
(array) |
$options |
An optional list of options. |
This method returns a query object (ar_storeParents) which will find all theparents of the current object up to and including the top object. The top can be specified by setting $options['top']
to the correct path. The current object can be specified by setting $options['current']
to the correct path.
el
Ariadne > 2.7.5
(object) ar('html/menu')->el( $tagName = 'ul', ... )
(object) ar('html/menu')->element( $tagName = 'ul', ... )
(string) |
$tagName |
The tag name to use for the menu list element. Defaults to <ul> |
(array) |
$attributes |
A list of attributes for the menu element. Optional. |
(mixed) |
$childNodes |
A list of childnodes as a string or an ar_htmlNodes object. Optional. |
This method returns a new ar_html_menuElement
object. This class extends the ar_htmlElement
class, so you can access its content using the html/xml dom methods. This method works exactly like the ar\html::el()
method. To fill the menu with menu items, use the ar\html\menuElement::fill()
method.
sitemap
Ariadne > 2.7.5
(object) ar('html/menu')->sitemap( $options = array() )
(array) |
$options |
An optional list of options. |
This method returns a query object (ar_storeFind) whichwill find all the children of the top object. You can set the following options:
(int) |
maxDepth |
How many levels of items to show. The root doesn't count. |
(bool) |
skipOrphans |
Whether or not to skip menu items without an immediate parent menu item. |
(bool) |
skipTop |
Whether or not to skip the top most menu item. |
(string) |
top |
The path to the topmost object or root of the menu. |
tree
Ariadne > 2.7.5
(object) ar('html/menu')->tree( $options = array() )
(array) |
$options |
An optional list of options. |
This method returns a query object (ar_storeFind) which will find all parents, siblings and children of the current menu item. The top can be specified by setting $options['top']
to the correct path. The current object can be specified by setting $options['current']
to the correct path.
In addition you can set the following options:
(bool) |
children |
Whether or not to add the children of the current menu item. |
(int) |
maxDepth |
How many levels of items to show. The root doesn't count. |
(bool) |
siblings |
Whether or not to add the siblings of parent items. |
(bool) |
skipOrphans |
Whether or not to skip menu items without an immediate parent menu item. |
(bool) |
skipTop |
Whether or not to skip the top most menu item. |
ar\html\menuElement methods
(In Ariadne upto 2.7.5 this is the ar/html/menu class, but works exactly the same.)
(object) |
autoID |
Adds automatically generated id's to each list item. |
(object) |
bar |
Fills the menu with the direct children of the root object. |
(object) |
childIndicators |
Adds a class 'menuHasChildren' to list items which have a submenu. |
(object) |
crumbs |
Fills the menu with the parents of the current object. |
(object) |
current |
Sets the current object. |
(object) |
fill |
Fills the menu with the given data. |
(object) |
levels |
Adds a menuLevels-n class to each menu and menu item. |
(object) |
root |
Sets the root url and optionally path for the menu. |
(string) |
script |
Returns javascript code for some menu styles. |
(object) |
sitemap |
Fills the menu with all children of the root object. |
(object) |
stripe |
Adds striping classes to each list item (menuOdd/Even/First/Last). |
(object) |
style |
Returns a stylesheet object with a particular menu style. |
(object) |
template |
Sets the template to use for the auto fill methods (bar, crumbs, sitemap and tree). |
(object) |
tree |
Fills the menu with parents, siblings and children of the current object. |
autoID
(object) $htmlMenu->autoID( $root = 'menu', $element = null )
(string) |
$root |
The root id to use, defaults to 'menu'. |
(object) |
$element |
The parent element to start adding the generated id's. Defaults to the menu object. |
This method adds automatically generated id's to each list item in the menu, starting with the given parent element. It returns the menu object.
Example 2 - a tree menu with automatic id's
<pinp>
echo ar('html')->menu()->tree()->autoID();
</pinp>
Will ouput:
<ul id="menu-ul">
<li id="menu-0">
<a href="http://.../">Foo</a>
<ul id="menu-0-ul">
<li id="menu-0-0">
<a href="http://.../">Bar</a>
</li>
<li id="menu-0-1">
<a href="http://.../">xyzzy</a>
</li>
...
</ul>
</li>
...
</ul>
bar
(object) $htmlMenu->bar( $options = array() )
(array) |
$options |
An optional list of options. |
This method fills the menu object with all the direct children of the top object. The top can be specified through either the root()
method or by setting $options['top']
to the correct path. It returns the menu object.
childIndicators
(object) $htmlMenu->childIndicators()
This method adds the class 'menuHasChildren
' to all list items that contain a submenu. It returns the menu object.
crumbs
(object) $htmlMenu->crumbs( $options = array() )
(array) |
$options |
An optional list of options. |
This method fills the menu object with all theparents of the current object up to and including the top object. The top can be specified through either the root()
method or by setting $options['top']
to the correct path. The current object can be specified through either the current()
method or by setting $options['current']
to the correct path. It returns the menu object.
current
(object) $htmlMenu->current( $path )
(string) |
$path |
The path to the current active menu item. |
This method sets the current menu item. It returns the menu object.
fill
(object) $htmlMenu->fill( $list, $options = array() )
(mixed) |
$list |
An array or ariadne query object to use to fill the menu. |
(array) |
$options |
An optional list of options. |
Ariadne > 2.7.5
(object) $htmlMenu->fill( $list, ... )
(mixed) |
$list |
An array or ariadne query object to use to fill the menu. |
This method fills the menu object with the items in $list
if it is an array, or the result from the query if it is an ariadne query. If $list
is an array, it must have one of the following formats:
Simple format with ariadne objects:
$list = array(
'/' => 'Root',
'/a/' => 'Item A',
'/a/b/' => 'Item B',
'/c/' => 'Item C'
);
The parent - child relations are automatically deduced from the keys (path) of the array.
Extended format with ariadne objects:
$list = array(
'/' => array(
'name' => 'Root',
'url' => 'http://www.ariadne-cms.org/',
'title' => 'Ariadne CMS',
'id' => 'arRoot',
'class' => 'rootClass',
'attributes' => array(
'style' => 'width: 200px;'
)
),
...
);
title
, id
and class
are set on the menu items hyperlink (<a>
), the contents of the attributes
array are set on the menu items list item (<li>
).
Predefined parent child relations:
$list = array(
array(
'name' => 'Root',
'url' => 'http://www.ariadne-cms.org/',
'children' => array(
array(
'name' => 'Item A',
'path' => 'a/',
'children' => array(
array(
'name' => 'Item B',
'path' => 'b/'
)
)
),
...
)
)
);
In the last example the urls for each item will be build based on the parents url. So Item B will have the url 'http://www.ariadne-cms.org/a/b/
'. You can do more interesting stuff with the path option:
?x=y |
Will automatically add the query parameters to the parents url, even if it already has a query part. |
#anchor |
Will add the anchor to the parents url, overwriting previous anchors. |
[a-z]+: |
e.g. http: , ftp: or mailto: followed by any other text. In this case the path will be copied verbatim to the url. |
/path/ |
Will generate a url by prepending the root url to the path. |
Finally, you can also pass an ariadne query object to fill the menu:
<pinp>
echo ar('html')->menu()->fill( ar::find( "object.implements='pdir'" ) );
</pinp>
In Ariadne versions > 2.7.5 you can add as many arguments to fill as you want, combining the different possibilities. If you need to set an option, you can no longer pass these as an array to fill()
. Instead add a call to configure()
with the same options array, e.g.:
<pinp> echo ar('html')->menu() ->configure( array( 'skipOrphans' => true ) ) ->fill( ar::find( "object.implements='pdir'" ) ); </pinp>
levels
(object) $htmlMenu->levels( $depth, $start = 0, $root = null )
(int) |
$depth |
The maximum depth to count. |
(object) |
$root |
The root element to start the levels counter. |
(int) |
$start |
The starting number for the first level. |
This method adds classes to each list item and menu with the current level. It returns the menu object. e.g:
<pinp>
ar('html')->menu()->tree()->levels(2);
</pinp>
Will generate:
<ul class="menuLevel-0">
<li class="menuLevel-0">
<a href="http://../">Foo</a>
<ul class="menuLevel-1">
<li class="menuLevel-1">
<a href="http://.../">Bar</a>
</li>
<li class="menuLevel-1">
<a href="http://.../">xyzzy</a>
</li>
</ul>
</li>
</ul>
root
(object) $htmlMenu->root( $url, $path = '/' )
(string) |
$url |
The root url for the menu. |
(string) |
$path |
The root path for the menu. |
Sets the root url and optionally the root path for the menu. All items with relative paths will get urls based on these.
script
(string) ar_html_menu::script( $type, $matches = array() )
(string) |
$type |
The type of menu to get the javascript for, currently only 'dropdown' will return a script. |
(mixed) |
$matches |
The class name or id of the menu to attach the script to. Or an array of class names or id's. If not specified it will not attach the script to any menu, you must do that yourself. |
Returns a string with some javascript methods and optionally initialization code. Currently there is only a script for the dropdown menu type. This is only used for backwards compatibility with Internet Explorer < 8 or IE8 in quirksmode.
If $matches is not specified, the script will only contain the function that initializes a menu, but without an immediate call to it. You must initialize the menu yourself, e.g. in a window.onload
method. The dropdown menu can be initialized by calling the arMenuHover()
method with either '#id
' or '.class
' as argument.
sitemap
(object) $htmlMenu->sitemap( $options = array() )
(array) |
$options |
An optional list of options. |
This method fills the menu object with all the children of the top object. You can set the following options:
(int) |
maxDepth |
How many levels of items to show. The root doesn't count. |
(bool) |
skipOrphans |
Whether or not to skip menu items without an immediate parent menu item. |
(bool) |
skipTop |
Whether or not to skip the top most menu item. |
(string) |
top |
The path to the topmost object or root of the menu. |
stripe
(object) $htmlMenu->stripe( $options = array() )
(array) |
$options |
An optional list of options. |
This method adds classes to each list item corresponding with even and odd items as well as first and last items. e.g:
<pinp>
ar('html')->menu()->tree()->stripe();
</pinp>
Will generate:
<ul>
<li class="menuFirst menuOdd menuLast">
<a href="http://../">Foo</a>
<ul>
<li class="menuFirst menuOdd">
<a href="http://.../">Bar</a>
</li>
<li class="menuLast menuEven">
<a href="http://.../">xyzzy</a>
</li>
</ul>
</li>
</ul>
Optionally you can also let the striping continue through child menu's by setting the stripingContinue
option to true:
<pinp>
ar('html')->menu()->tree()
->stripe( array( 'stripingContinue' => true ) );
</pinp>
Will generate:
<ul>
<li class="menuFirst menuOdd">
<a href="http://../">Foo</a>
<ul>
<li class="menuEven">
<a href="http://.../">Bar</a>
</li>
<li class="menuLast menuOdd">
<a href="http://.../">xyzzy</a>
</li>
</ul>
</li>
</ul>
You can change the stripe pattern by setting the striping
option to a suitable list pattern expression:
<pinp>
ar('html')->menu()->tree()
->stripe( array(
'stripingContinue' => true,
'striping' => ar::listPattern( '( .{2} menuThird )*' )
) );
</pinp>
Will generate:
<ul>
<li>
<a href="http://../">Foo</a>
<ul>
<li>
<a href="http://.../">Bar</a>
</li>
<li class="menuThird">
<a href="http://.../">xyzzy</a>
</li>
</ul>
</li>
</ul>
style
(object) $htmlMenu->style( $type = '' )
(string) |
$type |
The type of menu you want to set the stylesheet to. Supported are 'bar ', 'tree ', 'crumbs ', 'sitemap ', 'dropdown ' and 'tabs '. |
Changes the $css
property of the menu and returns it. The $css
property is a stylesheet object which you can alter through the ar_css
api.
The 'tabs' style provides the following variables which can be changed with the bind()
method:
menuActiveItemBgColor |
transparent | |
menuBgColor |
#E4E4E4 | |
menuBorderColor |
black | |
menuItemBgColor |
white |
The 'dropdown' style provides the following variables:
menuBorderColor |
black | |
menuHoverItemBgColor |
#E4E4E4 | |
menuItemBgColor |
transparent | |
menuItemWidth |
auto | |
menuSubItemBgColor |
white | |
menuSubItemWidth |
10em |
template
(object) $htmlMenu->template( $template )
(string) |
$template |
The name of the fill template. |
Sets the template to call when filling the menu through an ariadne query or predefined fill method ( bar()
, crumbs()
, tree()
and sitemap()
). Defaults to system.get.name.phtml
.
tree
(object) $htmlMenu->tree( $options = array() )
(array) |
$options |
An optional list of options. |
This method fills the menu object with the parents, siblings and children of the current menu item. The top can be specified through either the root()
method or by setting $options['top']
to the correct path. The current object can be specified through either the current()
method or by setting $options['current']
to the correct path. It returns the menu object.
In addition you can set the following options:
(bool) |
children |
Whether or not to add the children of the current menu item. |
(int) |
maxDepth |
How many levels of items to show. The root doesn't count. |
(bool) |
siblings |
Whether or not to add the siblings of parent items. |
(bool) |
skipOrphans |
Whether or not to skip menu items without an immediate parent menu item. |
(bool) |
skipTop |
Whether or not to skip the top most menu item. |