Refactored main menu navigation
This commit is contained in:
@@ -139,8 +139,8 @@
|
||||
{% endblock %}
|
||||
|
||||
{% block web_theme_main_navigation %}
|
||||
{% main_navigation %}
|
||||
{% for link in navigation_main_links %}
|
||||
{% get_top_menu_links %}
|
||||
{% for link in menu_links %}
|
||||
{% with "true" as as_li %}
|
||||
{% with "true" as hide_active_anchor %}
|
||||
{% with "active" as li_class_active %}
|
||||
@@ -154,25 +154,6 @@
|
||||
{% endblock %}
|
||||
|
||||
{% block web_theme_secondary_navigation %}
|
||||
{% main_navigation %}
|
||||
{% if navigation_secondary_links %}
|
||||
<div class="secondary-navigation">
|
||||
<ul class="wat-cf">
|
||||
{% for link in navigation_secondary_links %}
|
||||
{% with "true" as as_li %}
|
||||
{% with "true" as hide_active_anchor %}
|
||||
{% with "active" as li_class_active %}
|
||||
{% with "first" as li_class_first %}
|
||||
{% include "generic_subnavigation.html" %}
|
||||
{% endwith %}
|
||||
{% endwith %}
|
||||
{% endwith %}
|
||||
{% endwith %}
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% get_object_navigation_links "form_header" as form_navigation_links %}
|
||||
{% if form_navigation_links %}
|
||||
<div class="secondary-navigation">
|
||||
@@ -191,7 +172,6 @@
|
||||
</ul>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% endblock %}
|
||||
|
||||
{% block web_theme_sidebar %}
|
||||
@@ -206,6 +186,18 @@
|
||||
{% endwith %}
|
||||
{% endif %}
|
||||
|
||||
{% get_object_navigation_links "secondary_menu" as object_navigation_links %}
|
||||
{% if object_navigation_links %}
|
||||
<div class="block">
|
||||
<h3>{% trans "Secondary actions" %}</h3>
|
||||
<ul class="navigation">
|
||||
{% with "true" as as_li %}
|
||||
{% include "generic_navigation.html" %}
|
||||
{% endwith %}
|
||||
</ul>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% get_object_navigation_links as object_navigation_links %}
|
||||
{% if object_navigation_links %}
|
||||
<div class="block">
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
object_navigation = {}
|
||||
multi_object_navigation = {}
|
||||
menu_links = []
|
||||
model_list_columns = {}
|
||||
sidebar_templates = {}
|
||||
top_menu_entries = []
|
||||
|
||||
|
||||
def register_multi_item_links(src, links, menu_name=None):
|
||||
"""
|
||||
Register a multiple item action action to be displayed in the
|
||||
generic list template
|
||||
"""
|
||||
|
||||
multi_object_navigation.setdefault(menu_name, {})
|
||||
if hasattr(src, '__iter__'):
|
||||
for one_src in src:
|
||||
@@ -17,6 +22,10 @@ def register_multi_item_links(src, links, menu_name=None):
|
||||
|
||||
|
||||
def register_links(src, links, menu_name=None):
|
||||
"""
|
||||
Associate a link to a model a view, or an url
|
||||
"""
|
||||
|
||||
object_navigation.setdefault(menu_name, {})
|
||||
if hasattr(src, '__iter__'):
|
||||
for one_src in src:
|
||||
@@ -27,14 +36,29 @@ def register_links(src, links, menu_name=None):
|
||||
object_navigation[menu_name][src]['links'].extend(links)
|
||||
|
||||
|
||||
def register_menu(links):
|
||||
for link in links:
|
||||
menu_links.append(link)
|
||||
def register_top_menu(name, link, children_views=None, children_path_regex=None, position=None):
|
||||
"""
|
||||
Register a new menu entry for the main menu displayed at the top
|
||||
of the page
|
||||
"""
|
||||
|
||||
menu_links.sort(lambda x, y: 1 if x > y else -1, lambda x: x['position'] if 'position' in x else 1)
|
||||
entry = {'link': link, 'name': name}
|
||||
if children_views:
|
||||
entry['children_views'] = children_views
|
||||
if children_path_regex:
|
||||
entry['children_path_regex'] = children_path_regex
|
||||
if position is not None:
|
||||
top_menu_entries.insert(position, entry)
|
||||
else:
|
||||
top_menu_entries.append(entry)
|
||||
|
||||
|
||||
def register_model_list_columns(model, columns):
|
||||
"""
|
||||
Define which columns will be displayed in the generic list template
|
||||
for a given model
|
||||
"""
|
||||
|
||||
model_list_columns.setdefault(model, [])
|
||||
model_list_columns[model].extend(columns)
|
||||
|
||||
|
||||
@@ -13,68 +13,39 @@ from django.template import Context
|
||||
from common.utils import urlquote
|
||||
|
||||
from navigation.api import object_navigation, multi_object_navigation, \
|
||||
menu_links as menu_navigation, sidebar_templates
|
||||
top_menu_entries, sidebar_templates
|
||||
from navigation.forms import MultiItemForm
|
||||
from navigation.utils import resolve_to_name
|
||||
|
||||
register = Library()
|
||||
|
||||
|
||||
def process_links(links, view_name, url):
|
||||
"""
|
||||
Process menu navigation links
|
||||
"""
|
||||
items = []
|
||||
active_item = None
|
||||
#for item, count in zip(links, range(len(links))):
|
||||
for item in links:
|
||||
item_view = 'view' in item and item['view']
|
||||
item_url = 'url' in item and item['url']
|
||||
new_link = item.copy()
|
||||
if view_name == item_view or url == item_url:
|
||||
new_link['active'] = True
|
||||
active_item = item
|
||||
else:
|
||||
new_link['active'] = False
|
||||
if 'links' in item:
|
||||
for child_link in item['links']:
|
||||
child_view = 'view' in child_link and child_link['view']
|
||||
child_url = 'url' in child_link and child_link['url']
|
||||
if view_name == child_view or url == child_url:
|
||||
active_item = item
|
||||
new_link.update({
|
||||
'url': item_view and reverse(item_view) or item_url or u'#',
|
||||
})
|
||||
items.append(new_link)
|
||||
|
||||
return items, active_item
|
||||
|
||||
|
||||
class NavigationNode(Node):
|
||||
def __init__(self, navigation, *args, **kwargs):
|
||||
self.navigation = navigation
|
||||
|
||||
class TopMenuNavigationNode(Node):
|
||||
def render(self, context):
|
||||
request = Variable('request').resolve(context)
|
||||
view_name = resolve_to_name(request.META['PATH_INFO'])
|
||||
current_path = request.META['PATH_INFO']
|
||||
current_view = resolve_to_name(current_path)
|
||||
|
||||
main_items, active_item = process_links(links=self.navigation, view_name=view_name, url=request.META['PATH_INFO'])
|
||||
context['navigation_main_links'] = main_items
|
||||
if active_item and 'links' in active_item:
|
||||
secondary_links, active_item = process_links(links=active_item['links'], view_name=view_name, url=request.META['PATH_INFO'])
|
||||
context['navigation_secondary_links'] = secondary_links
|
||||
all_menu_links = [entry.get('link', {}) for entry in top_menu_entries]
|
||||
menu_links = resolve_links(context, all_menu_links, current_view, current_path)
|
||||
|
||||
for index, link in enumerate(top_menu_entries):
|
||||
children_views = link.get('children_views', [])
|
||||
if current_view in children_views:
|
||||
menu_links[index]['active'] = True
|
||||
|
||||
children_path_regex = link.get('children_path_regex', [])
|
||||
for child_path_regex in children_path_regex:
|
||||
if re.compile(child_path_regex).match(current_path.lstrip('/')):
|
||||
menu_links[index]['active'] = True
|
||||
|
||||
context['menu_links'] = menu_links
|
||||
return ''
|
||||
|
||||
|
||||
@register.tag
|
||||
def main_navigation(parser, token):
|
||||
#args = token.split_contents()
|
||||
|
||||
# if len(args) != 3 or args[1] != 'as':
|
||||
# raise TemplateSyntaxError("'get_all_states' requires 'as variable' (got %r)" % args)
|
||||
|
||||
#return NavigationNode(variable=args[2], navigation=navigation)
|
||||
return NavigationNode(navigation=menu_navigation)
|
||||
def get_top_menu_links(parser, token):
|
||||
return TopMenuNavigationNode()
|
||||
|
||||
|
||||
def resolve_arguments(context, src_args):
|
||||
@@ -98,7 +69,7 @@ def resolve_arguments(context, src_args):
|
||||
return args, kwargs
|
||||
|
||||
|
||||
def resolve_links(context, links, current_view, current_path, parsed_query_string):
|
||||
def resolve_links(context, links, current_view, current_path, parsed_query_string=None):
|
||||
context_links = []
|
||||
for link in links:
|
||||
new_link = copy.copy(link)
|
||||
|
||||
Reference in New Issue
Block a user