Skip to content

Commit

Permalink
Merge branch 'midnight_blue-header' of https://github.com/aleeexgreee…
Browse files Browse the repository at this point in the history
…n/ckan into aleeexgreeen-midnight_blue-header
  • Loading branch information
amercader committed Nov 29, 2024
2 parents f72e375 + 73a53ac commit c51d967
Showing 4 changed files with 36 additions and 13 deletions.
4 changes: 3 additions & 1 deletion changes/8492.feature
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
Adds new logo and updates UI header in `midnight-blue` theme.
Adds new logo and updates UI header in `midnight-blue` theme.
Adds macro for creating navigation links.
Adds helpers `endpoint_from_url` and `page_is_active`.
26 changes: 24 additions & 2 deletions ckan/lib/helpers.py
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@
from markdown import markdown
from bleach import clean as bleach_clean, ALLOWED_TAGS, ALLOWED_ATTRIBUTES
from ckan.common import asbool, config, current_user
from flask import flash, has_request_context
from flask import flash, has_request_context, current_app
from flask import get_flashed_messages as _flask_get_flashed_messages
from flask import redirect as _flask_redirect
from flask import url_for as _flask_default_url_for
@@ -739,10 +739,32 @@ def get_flashed_messages(**kwargs: Any):
return _flask_get_flashed_messages(**kwargs)


@core_helper
def endpoint_from_url(url: str) -> str:
try:
urls = current_app.url_map.bind("")
match = urls.match(url)
endpoint = match[0]
except RuntimeError:
endpoint = ""
return endpoint


@core_helper
def page_is_active(
menu_item: str, active_blueprints: Optional[list[str]] = None) -> bool:
'''Returns whether the current link is the active page or not'''
'''
Returns whether the current link is the active page or not.
`menu_item`
Accepts a route (e.g. 'group.index') or a URL (e.g. '/group')
`active_blueprints`
contains a list of additional blueprints that should be considered
active besides the one in `menu_item`
'''
if menu_item.startswith("/"):
menu_item = endpoint_from_url(menu_item)

blueprint, endpoint = menu_item.split('.')

item = {
8 changes: 4 additions & 4 deletions ckan/templates-midnight-blue/header.html
Original file line number Diff line number Diff line change
@@ -94,10 +94,10 @@
{% set org_type = h.default_group_type('organization') %}
{% set group_type = h.default_group_type('group') %}

{{ nav.link(dataset_type ~ '.search', h.humanize_entity_type('package', dataset_type, 'main nav') or _('Datasets'), active_blueprints=[dataset_type, dataset_type + '_resource']) }}
{{ nav.link(org_type ~ '.index', h.humanize_entity_type('organization', org_type, 'main nav') or _('Organizations'), active_blueprints=[org_type]) }}
{{ nav.link(group_type ~ '.index', h.humanize_entity_type('group', group_type, 'main nav') or _('Groups'), active_blueprints=[group_type]) }}
{{ nav.link('home.about', _('About')) }}
{{ nav.link(h.url_for(dataset_type ~ '.search'), h.humanize_entity_type('package', dataset_type, 'main nav') or _('Datasets'), active_blueprints=[dataset_type, dataset_type + '_resource']) }}
{{ nav.link(h.url_for(org_type ~ '.index'), h.humanize_entity_type('organization', org_type, 'main nav') or _('Organizations'), active_blueprints=[org_type]) }}
{{ nav.link(h.url_for(group_type ~ '.index'), h.humanize_entity_type('group', group_type, 'main nav') or _('Groups'), active_blueprints=[group_type]) }}
{{ nav.link(h.url_for('home.about'), _('About')) }}
{% endblock %}
</ul>
{% endblock %}
11 changes: 5 additions & 6 deletions ckan/templates-midnight-blue/macros/nav_link.html
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
{#
Builds links for navigation

nav_item - the name of the defined nav item defined in config/routing as the named route of the same name
nav_item - the URL for the route or
the named route of the nav item as defined in config/routing
title - text used for the link
icon - icon name used for link

Example:

{% import 'macros/nav_link.html' as nav %}
{{ nav.link('home.about', _('About')") }}
{{ nav.link(h.url_for('home.about'), _('About')") }}

#}
{% macro link(nav_item, title, active_blueprints=[], icon="", url="") %}
{% macro link(nav_item, title, active_blueprints=[], icon="") %}

{% set active_page = h.page_is_active(nav_item, active_blueprints=active_blueprints) %}

{% set url = url if url else h.url_for(nav_item, id) %}

<li {% if active_page %}class="active"{% endif %}>
<a {% if active_page %}aria-current="page"{% endif %} href="{{ url }}">
<a {% if active_page %}aria-current="page"{% endif %} href="{{ nav_item }}">
{% if icon %}
<i class="fa fa-{{ icon }}"></i>
{% endif %}

0 comments on commit c51d967

Please sign in to comment.