From edd344cd9af5d4c0ca1ccd491bca004956264708 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Wed, 29 Jun 2022 13:07:33 +0200 Subject: add tooltips to track overview --- fietsboek/models/track.py | 25 +++++++++++++++++++++++++ fietsboek/static/fietsboek.js | 22 +++++++++++++++++----- fietsboek/static/theme.css | 4 ++++ fietsboek/templates/home.jinja2 | 2 +- fietsboek/templates/layout.jinja2 | 30 +++++++++++++++++++++--------- 5 files changed, 68 insertions(+), 15 deletions(-) diff --git a/fietsboek/models/track.py b/fietsboek/models/track.py index 001d4b9..4c0583b 100644 --- a/fietsboek/models/track.py +++ b/fietsboek/models/track.py @@ -13,9 +13,13 @@ from sqlalchemy import ( from sqlalchemy.orm import relationship from sqlalchemy.types import TypeDecorator +from pyramid.i18n import TranslationString as _ + import enum import gzip +from markupsafe import Markup + from .meta import Base from .. import util @@ -104,6 +108,27 @@ class Track(Base): return util.tour_metadata(self.gpx_data)["uphill"] return self.cache.uphill + def html_tooltip(self, localizer): + """Generate a quick summary of the track as a HTML element. + + This can be used in Bootstrap tooltips. + + :param localizer: The localizer used for localization. + :type localizer: pyramid.i18n.Localizer + :return: The generated HTML. + :rtype: Markup + """ + rows = [ + (_("tooltip.table.length"), f'{round(self.length, 2)} km'), + (_("tooltip.table.uphill"), f'{round(self.uphill, 2)} m'), + (_("tooltip.table.downhill"), f'{round(self.downhill, 2)} m'), + ] + rows = [ + "{}{}".format(localizer.translate(name), value) + for (name, value) in rows + ] + return Markup(f'{"".join(rows)}
') + class TrackCache(Base): __tablename__ = 'track_cache' diff --git a/fietsboek/static/fietsboek.js b/fietsboek/static/fietsboek.js index 85711e9..0f0685d 100644 --- a/fietsboek/static/fietsboek.js +++ b/fietsboek/static/fietsboek.js @@ -5,14 +5,17 @@ function tagClicked(event) { function updateTagList() { let input = document.querySelector('#formTagsInput'); - input.value = ""; - document.querySelectorAll(".tag-badge").forEach((t) => { - input.value += t.textContent + " "; - }); - input.value = input.value.trim(); + if (input) { + input.value = ""; + document.querySelectorAll(".tag-badge").forEach((t) => { + input.value += t.textContent + " "; + }); + input.value = input.value.trim(); + } } document.addEventListener('DOMContentLoaded', function(event) { + /* Enable the "Add tag" button */ let $ = (selector) => document.querySelector(selector); let button = $("#add-tag-btn"); if (button) { @@ -32,6 +35,15 @@ document.addEventListener('DOMContentLoaded', function(event) { }) } + /* Enable clicking on a tag to remove it */ document.querySelectorAll(".tag-badge").forEach((t) => t.addEventListener("click", tagClicked)); + + /* Initialize the pre-loaded tags */ updateTagList(); + + /* Enable tooltips */ + var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]')) + var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) { + return new bootstrap.Tooltip(tooltipTriggerEl, {sanitize: false}) + }); }); diff --git a/fietsboek/static/theme.css b/fietsboek/static/theme.css index 4f10970..274b030 100644 --- a/fietsboek/static/theme.css +++ b/fietsboek/static/theme.css @@ -5,6 +5,10 @@ body { background: #efefef; } +.tooltip-inner { + max-width: 400px; +} + .list-group.list-group-root { padding: 0; overflow: hidden; diff --git a/fietsboek/templates/home.jinja2 b/fietsboek/templates/home.jinja2 index 4e78e7d..ccd9af0 100644 --- a/fietsboek/templates/home.jinja2 +++ b/fietsboek/templates/home.jinja2 @@ -12,7 +12,7 @@ {{ month_name(request, month.month) }} — {{ (month.total_length / 1000) | round(2) }} km
{% for track in month %} - {{ track.date.day }}: {{ track.title | default(track.date, true) }} + {{ track.date.day }}: {{ track.title | default(track.date, true) }} {% endfor %}
{% endfor %} diff --git a/fietsboek/templates/layout.jinja2 b/fietsboek/templates/layout.jinja2 index b445b2f..2515f01 100644 --- a/fietsboek/templates/layout.jinja2 +++ b/fietsboek/templates/layout.jinja2 @@ -7,7 +7,7 @@ - {% block title %}Fietslog{% endblock %} + {% block title %}Fietsboek{% endblock %}