Skip to content

Commit

Permalink
Merge branch 'simplify_tooltip_checks' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
jzohrab committed Sep 30, 2024
2 parents 3090836 + b3dd923 commit 145c3ba
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 53 deletions.
50 changes: 1 addition & 49 deletions lute/read/render/renderable_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import re
import functools
from lute.models.language import Language
from lute.models.term import Term, Status
from lute.models.term import Term

# from lute.utils.debug_helpers import DebugTimer

Expand Down Expand Up @@ -448,13 +448,6 @@ def __init__(self, term=None):
# Calls setter
self.term = term

# The tooltip should be shown for well-known/ignored TextItems
# that merit a tooltip. e.g., if there isn't any actual Term
# entity associated with this TextItem, nothing more is needed.
# Also, if there is a Term entity but it's mostly empty, a
# tooltip isn't useful.
self._show_tooltip: bool = None

# The flash message can be None, so we need an extra flag
# to determine if it has been loaded or not.
self._flash_message_loaded: bool = False
Expand All @@ -480,39 +473,7 @@ def term(self, t):
self._term = t
if t is None:
return

self.wo_status = t.status
if t.status >= 1 and t.status <= 5:
self._show_tooltip = True

@property
def show_tooltip(self):
"""
Show the tooltip if there is anything to show.
Lazy loaded as needed.
"""
if self._show_tooltip is not None:
return self._show_tooltip
if self.term is None:
return False

def blank_string(s):
return s is None or s.strip() == ""

def has_extra(cterm):
if cterm is None:
return False
no_extra = (
blank_string(cterm.translation)
and blank_string(cterm.romanization)
and cterm.get_current_image() is None
)
return not no_extra

self._show_tooltip = has_extra(self.term)
for p in self.term.parents:
self._show_tooltip = self._show_tooltip or has_extra(p)
return self._show_tooltip

@property
def flash_message(self):
Expand Down Expand Up @@ -567,22 +528,13 @@ def html_class_string(self):
classes = ["textitem", "click", "word"]
return " ".join(classes)

st = self.wo_status
classes = [
"textitem",
"click",
"word",
"word" + str(self.wo_id),
]

tooltip = (
st not in (Status.WELLKNOWN, Status.IGNORED, Status.UNKNOWN)
or self.show_tooltip
or self.flash_message is not None
)
if tooltip:
classes.append("showtooltip")

if self.flash_message is not None:
classes.append("hasflash")

Expand Down
4 changes: 3 additions & 1 deletion lute/read/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,11 @@ def edit_term_form(term_id):
@bp.route("/termpopup/<int:termid>", methods=["GET"])
def term_popup(termid):
"""
Show a term popup for the given DBTerm.
Get popup html for DBTerm, or None if nothing should be shown.
"""
d = get_popup_data(termid)
if d is None:
return ""
return render_template(
"read/termpopup.html",
term=d["term"],
Expand Down
15 changes: 14 additions & 1 deletion lute/read/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,22 @@ def start_reading(dbbook, pagenum, db_session):


def get_popup_data(termid):
"Get the data necessary to render a term popup."
"Get popup data, or None if popup shouldn't be shown."
term = Term.find(termid)

if term.status in [Status.UNKNOWN, Status.WELLKNOWN, Status.IGNORED]:
return None

def has_popup_data(cterm):
return (
(cterm.translation or "").strip() != ""
or (cterm.romanization or "").strip() != ""
or cterm.get_current_image() is not None
)

if not any(has_popup_data(t) for t in (term, *term.parents)):
return None

term_tags = [tt.text for tt in term.term_tags]

def make_array(t):
Expand Down
2 changes: 1 addition & 1 deletion lute/static/js/lute.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ function prepareTextInteractions() {

$('#thetext').tooltip({
position: _get_tooltip_pos(),
items: '.word.showtooltip',
items: '.word',
show: { easing: 'easeOutCirc' },
content: function (setContent) { tooltip_textitem_hover_content($(this), setContent); }
});
Expand Down
47 changes: 46 additions & 1 deletion tests/unit/read/test_service_popup_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,53 @@
Term popup data tests.
"""

from lute.models.term import Term
from lute.models.term import Term, Status
from lute.read.service import get_popup_data
from lute.db import db


def test_popup_data_is_none_if_no_data(spanish, app_context):
"Return None if no popup."
t = Term(spanish, "gato")
db.session.add(t)
db.session.commit()

d = get_popup_data(t.id)
assert d is None, "No data, no popup"

t.translation = "hello"
d = get_popup_data(t.id)
assert d is not None, "Have data, popup"

for s in [Status.UNKNOWN, Status.WELLKNOWN, Status.IGNORED]:
t.status = s
d = get_popup_data(t.id)
assert d is None, "No popup for these statuses"


def test_popup_data_is_none_for_some_statuses(spanish, app_context):
"Return None if no-popup statuses."
t = Term(spanish, "gato")
db.session.add(t)
db.session.commit()
t.translation = "hello"
d = get_popup_data(t.id)
assert d is not None, "Have data, popup"

for s in [Status.UNKNOWN, Status.WELLKNOWN, Status.IGNORED]:
t.status = s
d = get_popup_data(t.id)
assert d is None, "No popup for these statuses"

t.status = 1
d = get_popup_data(t.id)
assert d is not None, "Have data for these statuses"


def test_term_with_no_parents(spanish, app_context):
"Keep the lights on test, smoke only."
t = Term(spanish, "gato")
t.translation = "cat"
db.session.add(t)
db.session.commit()

Expand Down Expand Up @@ -101,6 +140,7 @@ def _c_to_string(c):
def test_single_term_not_included_in_own_components(spanish, app_context):
"Keep the lights on test, smoke only."
t = Term(spanish, "gato")
t.translation = "cat"
db.session.add(t)
db.session.commit()

Expand All @@ -111,6 +151,7 @@ def test_single_term_not_included_in_own_components(spanish, app_context):
def test_component_without_translation_not_returned(spanish, app_context):
"Component word is returned."
t = Term(spanish, "un gato")
t.translation = "a cat"
db.session.add(t)
make_terms([("gato", "")], spanish)
db.session.commit()
Expand All @@ -122,6 +163,7 @@ def test_component_without_translation_not_returned(spanish, app_context):
def test_component_word_with_translation_returned(spanish, app_context):
"Component word is returned."
t = Term(spanish, "un gato")
t.translation = "a cat"
db.session.add(t)
make_terms([("gato", "cat")], spanish)
db.session.commit()
Expand All @@ -133,6 +175,7 @@ def test_component_word_with_translation_returned(spanish, app_context):
def test_nested_multiword_components(spanish, app_context):
"Complete components are returned."
t = Term(spanish, "un gato gordo")
t.translation = "a fat cat"
db.session.add(t)
make_terms([("gato", "cat"), ("gat", "x"), ("un gato", "a cat")], spanish)
db.session.commit()
Expand All @@ -144,6 +187,7 @@ def test_nested_multiword_components(spanish, app_context):
def test_multiword_components_returned_in_order_of_appearance(spanish, app_context):
"Complete components are returned."
t = Term(spanish, "un gato gordo")
t.translation = "a fat cat"
db.session.add(t)
make_terms(
[
Expand All @@ -165,6 +209,7 @@ def test_multiword_components_returned_in_order_of_appearance(spanish, app_conte
def test_components_only_returned_once(spanish, app_context):
"Component not returned multiple times if present multiple times."
t = Term(spanish, "un gato gordo gato")
t.translation = "a cat fat cat"
db.session.add(t)
make_terms([("gato", "cat")], spanish)
db.session.commit()
Expand Down

0 comments on commit 145c3ba

Please sign in to comment.