Skip to content

Commit

Permalink
Highlight active section in navigation. (#13)
Browse files Browse the repository at this point in the history
Highlight, in the navigation bar, the section of the app that the user is
currently using.
  • Loading branch information
mcantelon committed Oct 19, 2023
1 parent 6696eea commit f948826
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
13 changes: 13 additions & 0 deletions AIPscan/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from flask_sqlalchemy import SQLAlchemy

from AIPscan.celery import configure_celery
from AIPscan.navbar import NavBar
from config import CONFIGS

db = SQLAlchemy()
Expand Down Expand Up @@ -34,6 +35,18 @@ def create_app(config_name="default"):

db.create_all()

# Define navigation bar
navbar = NavBar()
navbar.add("Archivematica Storage Services", "aggregator.storage_services")
navbar.add("AIPs", "reporter.view_aips")
navbar.add("Reports", "reporter.reports")

# Inject navigation bar into templates
@app.context_processor
def inject_navbar():
return dict(navbar=navbar)

# Set up 404 handling
@app.errorhandler(404)
def page_not_found(e):
return render_template("error/404.html"), 404
Expand Down
35 changes: 35 additions & 0 deletions AIPscan/navbar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from flask import url_for


class NavBarItem:
label = "" # Navigation link text
route = "" # Route ("transfer.index" for example)

def __init__(self, label, route):
self.label = label
self.route = route

def get_url(self):
return url_for(self.route)

Check warning on line 13 in AIPscan/navbar.py

View check run for this annotation

Codecov / codecov/patch

AIPscan/navbar.py#L13

Added line #L13 was not covered by tests

def is_active(self, request):
# Determine request Blueprint
rule = request.url_rule
request_blueprint = rule.endpoint.split(".")[0]

Check warning on line 18 in AIPscan/navbar.py

View check run for this annotation

Codecov / codecov/patch

AIPscan/navbar.py#L17-L18

Added lines #L17 - L18 were not covered by tests

if rule.endpoint == self.route:
# Found exact match between route and request
return True

Check warning on line 22 in AIPscan/navbar.py

View check run for this annotation

Codecov / codecov/patch

AIPscan/navbar.py#L22

Added line #L22 was not covered by tests
elif request_blueprint != "reporter":
# Determine route Blueprint to set active in entire Blueprint
route_blueprint = self.route.split(".")[0]

Check warning on line 25 in AIPscan/navbar.py

View check run for this annotation

Codecov / codecov/patch

AIPscan/navbar.py#L25

Added line #L25 was not covered by tests

# Found Blueprint-level match between route and request
return request_blueprint == route_blueprint

Check warning on line 28 in AIPscan/navbar.py

View check run for this annotation

Codecov / codecov/patch

AIPscan/navbar.py#L28

Added line #L28 was not covered by tests


class NavBar:
items = []

def add(self, label, route):
self.items.append(NavBarItem(label, route))
16 changes: 7 additions & 9 deletions AIPscan/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@
<a class="navbar-brand" href="/">AIPscan</a>
<div class="navbar-collapse" id="navbarNavDropdown">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="{{ url_for('aggregator.storage_services') }}">Archivematica Storage Services</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ url_for('reporter.view_aips') }}">AIPs</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ url_for('reporter.reports') }}">Reports</a>
</li>

{% for item in navbar.items %}
<li class="nav-item">
<a class="nav-link {{ 'active fw-bold' if item.is_active(request) else '' }}" href="{{ item.get_url() }}">{{ item.label }}</a>
</li>
{% endfor %}

<li class="nav-item">
<a class="nav-link" href="{{ url_for('api.doc') }}" target="apiframe">API</a>
</li>
Expand Down

0 comments on commit f948826

Please sign in to comment.