## This macro file is a temporary holder for all the 'new' menu macros.

#macro (menuMacros_renderBrowseMenu)
    <content tag="browse-menu">
        #menuMacros_renderMenu("browse-menu-link", $action.getText("system.space.menu"), "browse", ["system.space", "system.browse"])
    </content>
#end

#macro (menuMacros_renderGlobalBrowseMenu)
    #menuMacros_renderMenu("browse-menu-link", $action.getText("system.space.menu"), "browse", ["system.browse"])
#end

#macro (menuMacros_renderUserMenu)
    #if ($bootstrap.isSetupComplete())
        #if ($user)
            #menuMacros_renderMenu("user-menu-link", $generalUtil.htmlEncode($user.fullName), "user", "system.user")
        #else
             #foreach ($item in $action.webInterfaceManager.getDisplayableItems("system.user/anonymous", $action.webInterfaceContext))
                <li class="ajs-menu-item normal">#renderItemLink($item false)</li>
            #end
        #end
    #end
#end

#macro (menuMacros_renderAddMenu)
    #menuMacros_renderMenu("add-menu-link", $action.getText("system.content.add.menu"), "add", "system.content.add")
#end

#macro (menuMacros_renderActionMenu)
    #menuMacros_renderMenu("action-menu-link", $action.getText("system.content.action.menu"), "action", "system.content.action")
#end

#* Renders button(s) based on the provided section key. e.g. page view and edit buttons.
   Optionally, the $mode variable can be set to an item key for a class of 'current' to be applied
   to the button.

   Method Parameters:
   * buttonSectionKey - determines the web items to display as buttons
*#
#macro (menuMacros_renderButton $buttonSectionKey)
    #foreach ($item in $webInterfaceManager.getDisplayableItems($buttonSectionKey, $action.webInterfaceContext))
        #set ($current = false)
        #if ($mode == $item.key)
            #set ($current = true)
        #end
        <li class="ajs-button normal">#renderItemLink($item $current)</li>
    #end
#end


#* Renders a drop down menu e.g. content add, action menus.

   Method Parameters:
   * menuId        - the id of the menu
   * menuTitle     - the menu title, which is displayed as the drop down menu link text
   * menuClass     - the class of the menu drop down link
   * webSectionKeys - determines the web items to display in the menu
*#
#macro(menuMacros_renderMenu $menuId $menuTitle $menuClass $webSectionKeys)
    #set($webMenu = $webMenuManager.getMenu($menuId, $webSectionKeys, $action.webInterfaceContext))

    ## don't display the menu if there are no menu sections
    #if(!$webMenu.isEmpty())
    <li class="normal ajs-menu-item">
        <a id="$webMenu.id" class="$!menuClass trigger ajs-menu-title" href="#"><span><span>$!menuTitle</span></span></a> ## two spans necessary for IE backgrounds
        <div class="hidden ajs-drop-down">
            #foreach ($section in $webMenu.sections)
            <ul #if($section.id) id="$section.id"#end
                #if($section.className) class="$section.className"#end>
                #foreach ($item in $section.items)
                    #renderItem($item)
                #end
            </ul>
            #end
        </div>
    </li>
    #end
#end

#macro (renderItem $item)
    #set ($isCurrent = ($context == $item.key))
    <li>#renderItemLink($item $isCurrent)</li>
#end

#macro (renderItemLink $item $isCurrent)
    #set ($itemRenderedUrl = '')
    #set ($itemRenderedUrl = $item.link.getDisplayableUrl($req, $action.webInterfaceContext))
    #set ($itemLabel = $item.label.getDisplayableLabel($req, $action.webInterfaceContext))
    #set ($itemClass = '')
    #set ($itemTooltip = $item.tooltip.getDisplayableLabel($req, $action.webInterfaceContext))
    #set ($itemClass = $item.styleClass)

    #if ($isCurrent)
        #addClassName($itemClass "current")
    #end

    <a #if ($item.link.id) id="$item.link.id"#end #if ($itemRenderedUrl != '') href="$itemRenderedUrl"#end #if ($itemClass) class="$itemClass"#end #if ($item.link.hasAccessKey()) accessKey="$item.link.getAccessKey($helper)"#end #if ($itemTooltip) title="$itemTooltip"#end>
        #if ($item.icon)
            #set ($icon = $item.icon)
            <span>
                <img alt="$itemLabel" src="$icon.url.getDisplayableUrl($req, $action.webInterfaceContext)" height="$icon.height" width="$icon.width" title="$itemLabel">
                $itemLabel
            </span>## span for IE
        #else
           <span>$itemLabel</span>## span for IE
        #end
    </a>
#end
