Source code for bag.web.pyramid.nav

"""A simple navigation menu system for web apps.

The menu structure can be defined in advance, before any http requests come.
Example::

    from bag.web.pyramid.nav import NavEntry, Route, Static

    menu = [
        NavEntry('Home', url=Route('home')),
        NavEntry('Support', url=Route('support')),
        NavEntry('Terms and conditions',
                 url=Static('my_app:static/terms.html')),
        NavEntry('Account', children=[
            NavEntry('Settings', url=Route('settings')),
            NavEntry('Log out', url=Route('logout')),
        ]),
        NavEntry('What the world is saying about us',
                 url='https://www.google.com/search?q=about+us')
    ]

A Kajiki template using the Pure CSS framework, without submenus:

.. code-block:: html

    <div class="pure-menu pure-menu-open pure-menu-horizontal">
      <a href="#" class="pure-menu-heading">My website</a>
      <ul>
        <li py:for="item in menu" class="${item.css_class(request)}">
          <a href="${item.href(request)}">${item.label}</a>
        </li>
      </ul>
    </div>

Another example template using Mako and Bootstrap 3:

.. code-block:: html

    <ul class="nav nav-pills pull-left">
    % for item in menu:
      % if item.children:
        <li class="dropdown"><a class="dropdown-toggle"
            data-toggle="dropdown">${item.label}
          <b class="caret"></b></a>
          <ul class="dropdown-menu">
            % for subitem in item.children:
              <li class="${subitem.css_class(request)}"><a
                  href="${subitem.href(request)}">${subitem.label}</a></li>
            % endfor
          </ul>
        </li>
      % else:
        <li class="${item.css_class(request)}"><a
            href="${item.href(request)}">${item.label}</a></li>
      % endif
    % endfor
    </ul>
"""

from abc import abstractmethod, ABCMeta
from typing import Any, Dict, List, Optional, Union





[docs]class Route(BaseLink): """A link that is defined by a Pyramid route name.""" def __init__(self, route_name: str) -> None: """Instantiate.""" self.url = route_name
[docs] def href(self, request) -> str: """Return the route_path() of this instance.""" return request.route_path(self.url)
[docs]class Static(BaseLink): """A link that is defined by a Pyramid static URL spec.""" def __init__(self, url_spec: str) -> None: """Instantiate.""" self.url_spec = url_spec
[docs] def href(self, request) -> str: """Return the static_path() of this instance.""" return request.static_path(self.url_spec)
hrefable = Union[str, BaseLink, None]