Skip to content

Commit

Permalink
feat(nossas): change behavior menu i18n with modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
igr-santos committed Mar 6, 2024
1 parent 0e7e189 commit e46e082
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 28 deletions.
40 changes: 40 additions & 0 deletions app/nossas/design/cms_menus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from menus.base import Modifier
from menus.menu_pool import menu_pool
from cms.models import Page


class I18nModifier(Modifier):
"""
This modifier makes the i18n_title attribute of a page
accessible for the menu system
"""

def modify(self, request, nodes, namespace, root_id, post_cut, breadcrumb):
if post_cut:
page_nodes = {n.id: n for n in nodes if n.attr["is_page"]}

pages = Page.objects.filter(id__in=page_nodes.keys())
exclude_nodes = []
for page in pages:
if request.LANGUAGE_CODE in page.get_languages():
node = page_nodes[page.id]
node.attr["i18n_menu_title"] = page.get_menu_title()

if len(node.children) > 0:
node.children = self.modify(
request,
node.children,
namespace,
root_id,
post_cut,
breadcrumb,
)
else:
exclude_nodes.append(page.id)

nodes = list(filter(lambda x: x.id not in exclude_nodes, nodes))

return nodes


menu_pool.register_modifier(I18nModifier)
4 changes: 2 additions & 2 deletions app/nossas/design/templates/nossas/footer_menu.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{% load menu_tags i18n_tags %}
{% load menu_tags %}

{% for child in children %}
<div class="mb-3 me-5 d-flex flex-col" style="max-width:216px;">
<a class="nav-link text-white" href="{{ child.attr.redirect_url|default:child.get_absolute_url }}">{% get_i18n_menu_title child %}</a>
<a class="nav-link text-white" href="{{ child.attr.redirect_url|default:child.get_absolute_url }}">{{ child.attr.i18n_menu_title }}</a>
{% if child.children %}
{% show_menu from_level to_level extra_inactive extra_active "nossas/footer_submenu.html" "" "" child %}
{% endif %}
Expand Down
4 changes: 1 addition & 3 deletions app/nossas/design/templates/nossas/footer_submenu.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{% load i18n_tags %}

{% for child in children %}
<a class="nav-link text-white" href="{{ child.attr.redirect_url|default:child.get_absolute_url }}">{% get_i18n_menu_title child %}</a>
<a class="nav-link text-white" href="{{ child.attr.redirect_url|default:child.get_absolute_url }}">{{ child.attr.i18n_menu_title }}</a>
{% endfor %}
8 changes: 3 additions & 5 deletions app/nossas/design/templates/nossas/mega_menu.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{% load menu_tags i18n_tags %}
{% load menu_tags %}

{% for child in children %}
{% if forloop.counter != 1 %}
<li class="child px-2{% if child.selected %} selected{% endif %}{% if child.ancestor %} ancestor{% endif %}{% if child.sibling %} sibling{% endif %}{% if child.descendant %} descendant{% endif %}">
{% if child.children %}
<a href="{{ child.attr.redirect_url|default:child.get_absolute_url }}" class="dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
{% get_i18n_menu_title child %}
{{ child.attr.i18n_menu_title }}
{% include "nossas/svg/chevron-down.svg" %}
</a>
<div class="mega-menu">
Expand All @@ -14,8 +13,7 @@
</ul>
</div>
{% else %}
<a href="{{ child.attr.redirect_url|default:child.get_absolute_url }}">{% get_i18n_menu_title child %}</a>
<a href="{{ child.attr.redirect_url|default:child.get_absolute_url }}">{{ child.attr.i18n_menu_title }}</a>
{% endif %}
</li>
{% endif %}
{% endfor %}
4 changes: 2 additions & 2 deletions app/nossas/design/templates/nossas/menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
<li class="child{% if child.selected %} selected{% endif %}{% if child.ancestor %} ancestor{% endif %}{% if child.sibling %} sibling{% endif %}{% if child.descendant %} descendant{% endif %}">
{% if child.children %}
<button class="dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
{{ child.get_menu_title }}
{{ child.attr.i18n_menu_title }}
{% include "nossas/svg/chevron-down.svg" %}
</button>
<ul class="dropdown-menu w-100 mt-0" aria-labelledby="navbarDropdown">
{% show_menu from_level to_level extra_inactive extra_active "nossas/menu.html" "" "" child %}
</ul>
{% else %}
<a href="{{ child.attr.redirect_url|default:child.get_absolute_url }}">{{ child.get_menu_title }}</a>
<a href="{{ child.attr.redirect_url|default:child.get_absolute_url }}">{{ child.attr.i18n_menu_title }}</a>
{% endif %}
</li>
{% endfor %}
2 changes: 1 addition & 1 deletion app/nossas/design/templates/nossas/submenu.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{% for child in split_child %}
<li class="child{% if child.selected %} selected{% endif %}{% if child.ancestor %} ancestor{% endif %}{% if child.sibling %} sibling{% endif %}{% if child.descendant %} descendant{% endif %}">

<a href="{{ child.attr.redirect_url|default:child.get_absolute_url }}">{{ child.get_menu_title }}</a>
<a href="{{ child.attr.redirect_url|default:child.get_absolute_url }}">{{ child.attr.i18n_menu_title }}</a>
</li>
{% endfor %}
</div>
Expand Down
15 changes: 0 additions & 15 deletions app/nossas/design/templatetags/i18n_tags.py

This file was deleted.

0 comments on commit e46e082

Please sign in to comment.