Add a burger manu to the navbar when used on mobiles

This commit is contained in:
Rodolphe Breard 2018-06-11 12:13:07 +02:00
parent 957c884f78
commit 2f243139bf
3 changed files with 92 additions and 58 deletions

View file

@ -7,6 +7,7 @@
<link rel="stylesheet" href="{% static 'bulma/css/style.min.css' %}">
<link rel="shortcut icon" href="{% static "khaganat/images/favicon.ico" %}">
<link rel="stylesheet" href="{% static "khaganat/css/khaganat.css" %}">
<script defer src="{% static "navbar/navbar.js" %}"></script>
{% block headers %}{% endblock %}
<title>Khaganat - {% block title %}{% endblock %}</title>
</head>

View file

@ -0,0 +1,28 @@
// See Bulma documentation
// https://bulma.io/documentation/components/navbar/
document.addEventListener('DOMContentLoaded', function () {
// Get all "navbar-burger" elements
var $navbarBurgers = Array.prototype.slice.call(document.querySelectorAll('.navbar-burger'), 0);
// Check if there are any navbar burgers
if ($navbarBurgers.length > 0) {
// Add a click event on each of them
$navbarBurgers.forEach(function ($el) {
$el.addEventListener('click', function () {
// Get the target from the "data-target" attribute
var target = $el.dataset.target;
var $target = document.getElementById(target);
// Toggle the class on both the "navbar-burger" and the "navbar-menu"
$el.classList.toggle('is-active');
$target.classList.toggle('is-active');
});
});
}
});

View file

@ -2,63 +2,68 @@
{% load i18n %}
<nav class="navbar is-light">
<div class="navbar-brand">
<a class="navbar-item" href="{% url 'index' %}"><img src="{% static "khaganat/images/icon_khaganat.png" %}" alt="Khaganat"></a>
</div>
<div class="navbar-menu">
<div class="navbar-start">
{% for e in elems %}
{% if e.localized_link %}
<a class="navbar-item" href="{{ e.localized_link }}"{% if e.new_window %} target="_blank"{% endif %}>{{ e }}</a>
{% elif e.children %}
<div class="navbar-item has-dropdown is-hoverable">
<a class="navbar-link">{{ e.description.short_name }}</a>
<div class="navbar-dropdown">
{% for c in e.children %}
{% if c.is_separator %}<hr class="navbar-divider">{% else %}
<a class="navbar-item" href="{% if c.add_locale %}/{{ current_lang_code }}{% endif %}{{ c.localized_link }}"{% if c.new_window %} target="_blank"{% endif %}{% if c.description.description %} title="{{ c.description.description }}"{% endif %}>
<span>
<span><img src="{% static c.icon_path %}" alt="" /></span>
<span>{{ c }}</span>
</span>
</a>
{% endif %}
{% endfor %}
</div>
</div>
{% else %}
<span class="navbar-item">{{ e }}</span>
{% endif %}
{% endfor %}
</div>
<div class="navbar-end">
{% if user.is_authenticated %}
<div class="navbar-item has-dropdown is-hoverable">
<a class="navbar-link">{% trans "my_account"|capfirst %}</a>
<div class="navbar-dropdown">
{% if user.is_superuser %}
<a class="navbar-item" href="{% url "admin:index" %}">{% trans "administration"|capfirst %}</a>
{% endif %}
<a class="navbar-item" href="{% url "logout" %}">{% trans "logout"|capfirst %}</a>
</div>
</div>
{% else %}
<div class="navbar-item">
<div>
<a class="button is-link" href="{% url "login" %}?next={{ current_url }}">{% trans "login"|capfirst %}</a>
<a class="button is-white" href="{% url "register" %}">{% trans "register"|capfirst %}</a>
</div>
</div>
{% endif %}
<div class="navbar-brand">
<a class="navbar-item" href="{% url 'index' %}"><img src="{% static "khaganat/images/icon_khaganat.png" %}" alt="Khaganat"></a>
<a role="button" class="navbar-burger" data-target="navMenu" aria-label="menu" aria-expanded="false">
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
</a>
</div>
<div class="navbar-menu" id="navMenu">
<div class="navbar-start">
{% for e in elems %}
{% if e.localized_link %}
<a class="navbar-item" href="{{ e.localized_link }}"{% if e.new_window %} target="_blank"{% endif %}>{{ e }}</a>
{% elif e.children %}
<div class="navbar-item has-dropdown is-hoverable">
<a class="navbar-link">{{ e.description.short_name }}</a>
<div class="navbar-dropdown">
{% for c in e.children %}
{% if c.is_separator %}<hr class="navbar-divider">{% else %}
<a class="navbar-item" href="{% if c.add_locale %}/{{ current_lang_code }}{% endif %}{{ c.localized_link }}"{% if c.new_window %} target="_blank"{% endif %}{% if c.description.description %} title="{{ c.description.description }}"{% endif %}>
<span>
<span><img src="{% static c.icon_path %}" alt="" /></span>
<span>{{ c }}</span>
</span>
</a>
{% endif %}
{% endfor %}
</div>
</div>
{% else %}
<span class="navbar-item">{{ e }}</span>
{% endif %}
{% endfor %}
</div>
<div class="navbar-end">
{% if user.is_authenticated %}
<div class="navbar-item has-dropdown is-hoverable">
<a class="navbar-link">{% trans "my_account"|capfirst %}</a>
<div class="navbar-dropdown">
{% if user.is_superuser %}
<a class="navbar-item" href="{% url "admin:index" %}">{% trans "administration"|capfirst %}</a>
{% endif %}
<a class="navbar-item" href="{% url "logout" %}">{% trans "logout"|capfirst %}</a>
</div>
</div>
{% else %}
<div class="navbar-item">
<div>
<a class="button is-link" href="{% url "login" %}?next={{ current_url }}">{% trans "login"|capfirst %}</a>
<a class="button is-white" href="{% url "register" %}">{% trans "register"|capfirst %}</a>
</div>
</div>
{% endif %}
<div class="navbar-item has-dropdown is-hoverable">
<a class="navbar-link">{{ current_lang_name|capfirst }}</a>
<div class="navbar-dropdown">
{% for lang_code, lang_name, lang_url in all_langs %}
<a class="navbar-item{% if lang_code == current_lang_code %} is-active{% endif %}" href="{{ lang_url }}">{{ lang_name|capfirst }}</a>
{% endfor %}
</div>
</div>
</div>
</div>
<div class="navbar-item has-dropdown is-hoverable">
<a class="navbar-link">{{ current_lang_name|capfirst }}</a>
<div class="navbar-dropdown">
{% for lang_code, lang_name, lang_url in all_langs %}
<a class="navbar-item{% if lang_code == current_lang_code %} is-active{% endif %}" href="{{ lang_url }}">{{ lang_name|capfirst }}</a>
{% endfor %}
</div>
</div>
</div>
</div>
</nav>