===================
Z3C Menu directives
===================

Show how we can use the menu directive. Register the meta configuration for 
the directive.

First let's setup our defualt menu item template first:

  >>> import os
  >>> import zope.component
  >>> from zope.publisher.interfaces.browser import IDefaultBrowserLayer
  >>> from zope.publisher.interfaces.browser import IBrowserView
  >>> from z3c.template.interfaces import IContentTemplate
  >>> from z3c.template.template import TemplateFactory
  >>> import z3c.menu.ready2go
  >>> baseDir = os.path.split(z3c.menu.ready2go.__file__)[0]
  >>> itemTemplate = os.path.join(baseDir, 'item.pt')
  >>> itemTemplateFactory = TemplateFactory(itemTemplate, 'text/html')
  >>> zope.component.provideAdapter(itemTemplateFactory,
  ...     (IBrowserView, IDefaultBrowserLayer), IContentTemplate)

  >>> import sys
  >>> from zope.configuration import xmlconfig
  >>> import z3c.menu.ready2go
  >>> context = xmlconfig.file('meta.zcml', z3c.menu.ready2go)

We need to register our checker adapter which can check if a menu item is 
selected or not:

  >>> import zope.component
  >>> from z3c.menu.ready2go import checker
  >>> zope.component.provideAdapter(checker.ContextSelectedChecker)

Let's define a content object:

  >>> from z3c.menu.ready2go import testing
  >>> sampleContent = testing.Sample('Sample Content')

Now add the content object to our site root:

  >>> root['sample'] = sampleContent

Now we can define our test menu manager:

  >>> from zope.viewlet.manager import ViewletManager
  >>> from z3c.menu.ready2go import manager
  >>> FirstMenu = ViewletManager('left', testing.IFirstMenu,
  ...     bases=(manager.MenuManager,))

  >>> SecondMenu = ViewletManager('left', testing.ISecondMenu,
  ...     bases=(manager.MenuManager,))

And we need a view which knows about it's parent:

  >>> from zope.publisher.browser import TestRequest
  >>> request = TestRequest()
  >>> firstView = testing.FirstView(sampleContent, request)
  >>> testing.IFirstView.providedBy(firstView)
  True

  >>> secondView = testing.SecondView(sampleContent, request)
  >>> testing.ISecondView.providedBy(secondView)
  True

As you can see the menu is not selected if we access the page:

  >>> firstMenu = FirstMenu(sampleContent, request, firstView)
  >>> testing.IFirstMenu.providedBy(firstMenu)
  True

  >>> firstMenu.update()
  >>> firstMenu.render()
  u''

  >>> secondMenu = SecondMenu(sampleContent, request, secondView)
  >>> testing.ISecondMenu.providedBy(secondMenu)
  True

  >>> secondMenu.update()
  >>> secondMenu.render()
  u''

Now we need some menu items for the first menu:

  >>> from zope.publisher.interfaces.browser import IBrowserView
  >>> from zope.publisher.interfaces.browser import IBrowserRequest
  >>> from zope.viewlet.interfaces import IViewlet
  >>> zope.component.provideAdapter(
  ...     testing.FirstMenuItem,
  ...     (zope.interface.Interface, IBrowserRequest,
  ...     IBrowserView, testing.IFirstMenu),
  ...     IViewlet, name='First Menu')

  >>> zope.component.provideAdapter(
  ...     testing.SecondMenuItem,
  ...     (zope.interface.Interface, IBrowserRequest,
  ...     IBrowserView, testing.IFirstMenu),
  ...     IViewlet, name='Second Menu')

And we need some menu items for the second menu:

  >>> zope.component.provideAdapter(
  ...     testing.FirstMenuItem,
  ...     (zope.interface.Interface, IBrowserRequest,
  ...     IBrowserView, testing.ISecondMenu),
  ...     IViewlet, name='First Menu')

  >>> zope.component.provideAdapter(
  ...     testing.SecondMenuItem,
  ...     (zope.interface.Interface, IBrowserRequest,
  ...     IBrowserView, testing.ISecondMenu),
  ...     IViewlet, name='Second Menu')

Now render the menu manager again and you can see that we've got some menu
items. but you can see that this menu items are not selected:

  >>> firstMenu = FirstMenu(sampleContent, request, firstView)
  >>> firstMenu.update()
  >>> print firstMenu.render()
  <li>
    <a><span>Second Menu</span></a>
  </li>
  <li>
    <a><span>First Menu</span></a>
  </li>

  >>> secondMenu = SecondMenu(sampleContent, request, firstView)
  >>> secondMenu.update()
  >>> print secondMenu.render()
  <li>
    <a><span>Second Menu</span></a>
  </li>
  <li>
    <a><span>First Menu</span></a>
  </li>

Now we can register a menu selector for our page whihc renders the menu
as selected if we access the page:

  >>> context = xmlconfig.string("""
  ... <configure
  ...     xmlns:z3c="http://namespaces.zope.org/z3c">
  ...   <z3c:menuSelector
  ...       view=".testing.IFirstView"
  ...       manager=".testing.IFirstMenu"
  ...       menu=".testing.FirstMenuItem"
  ...       />
  ... </configure>
  ... """, context)

After we registered a menu selector for the first view and first menu, we will
see that the first menu get rendered as selected on the first menu:

  >>> firstMenu = FirstMenu(sampleContent, request, firstView)
  >>> firstMenu.update()
  >>> print firstMenu.render()
  <li>
    <a><span>Second Menu</span></a>
  </li>
  <li class="selected">
    <a><span>First Menu</span></a>
  </li>

But not on the second menu:

  >>> secondMenu = SecondMenu(sampleContent, request, firstView)
  >>> secondMenu.update()
  >>> print secondMenu.render()
  <li>
    <a><span>Second Menu</span></a>
  </li>
  <li>
    <a><span>First Menu</span></a>
  </li>
