Skip to content

Commit

Permalink
Merge pull request #87 from mind84/bootstrap_5_tooltip
Browse files Browse the repository at this point in the history
update html tooltip to handle bootstrap 5 tabs
  • Loading branch information
Gustry authored Sep 24, 2024
2 parents c001bac + 472488d commit 8416128
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
15 changes: 12 additions & 3 deletions lizmap_server/get_feature_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,13 @@ def append_maptip(cls, string: str, layer_name: str, feature_id: Union[str, int]

@classmethod
def feature_list_to_replace(
cls, cfg: dict, project: QgsProject, relation_manager: QgsRelationManager, xml: str) -> List[Result]:
cls,
cfg: dict,
project: QgsProject,
relation_manager: QgsRelationManager,
xml: str,
bootstrap_5: bool,
) -> List[Result]:
""" Parse the XML and check for each layer according to the Lizmap CFG file. """
features = []
for layer_name, feature_id in GetFeatureInfoFilter.parse_xml(xml):
Expand Down Expand Up @@ -120,7 +126,8 @@ def feature_list_to_replace(
root = config.invisibleRootContainer()

# Need to eval the html_content
html_content = Tooltip.create_popup_node_item_from_form(layer, root, 0, [], '', relation_manager)
html_content = Tooltip.create_popup_node_item_from_form(
layer, root, 0, [], '', relation_manager, bootstrap_5)
html_content = Tooltip.create_popup(html_content)

# Maybe we can avoid the CSS on all features ?
Expand Down Expand Up @@ -175,9 +182,11 @@ def responseComplete(self):

xml = request.body().data().decode("utf-8")

bootstrap_5 = params.get('CSS_FRAMEWORK', '').upper() == 'BOOTSTRAP5'

# noinspection PyBroadException
try:
features = self.feature_list_to_replace(cfg, project, relation_manager, xml)
features = self.feature_list_to_replace(cfg, project, relation_manager, xml, bootstrap_5)
except Exception as e:
if to_bool(os.getenv("CI")):
logger.log_exception(e)
Expand Down
23 changes: 19 additions & 4 deletions lizmap_server/tooltip.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def create_popup_node_item_from_form(
headers: list,
html: str,
relation_manager: QgsRelationManager,
bootstrap_5: bool = False,
) -> str:
regex = re.compile(r"[^a-zA-Z0-9_]", re.IGNORECASE)
a = ''
Expand Down Expand Up @@ -173,9 +174,22 @@ def create_popup_node_item_from_form(
if visibility and not active:
active = visibility
h += '\n' + SPACES
h += (
'<li class="{}"><a href="#popup_dd_[% $id %]_{}" data-toggle="tab">{}</a></li>'
).format(active, regex.sub('_', node.name()), node.name())
id_tab = regex.sub('_', node.name())
if bootstrap_5:
h += (
f'<li class="nav-item">'
f'<button class="nav-link {active}" data-bs-toggle="tab" '
f'data-bs-target="#popup_dd_[% $id %]_{id_tab}">'
f'{node.name()}'
f'</button>'
f'</li>'
)
else:
h += (
f'<li class="{active}">'
f'<a href="#popup_dd_[% $id %]_{id_tab}" data-toggle="tab">{node.name()}</a>'
f'</li>'
)
headers.append(h)

if lvl > 1:
Expand All @@ -190,7 +204,8 @@ def create_popup_node_item_from_form(

level += 1
for n in node.children():
h = Tooltip.create_popup_node_item_from_form(layer, n, level, headers, html, relation_manager)
h = Tooltip.create_popup_node_item_from_form(
layer, n, level, headers, html, relation_manager, bootstrap_5)
# If it is not root children, add html
if lvl > 0:
a += h
Expand Down
2 changes: 1 addition & 1 deletion test/test_get_feature_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def test_single_get_feature_info_ascii(client):
"FILTER": "accents:\"NAME_1\" = 'Bret\\'agne'",
}

rv = client.get(_build_query_string(qs, use_urllib3=True), PROJECT)
rv = client.get(_build_query_string(qs, use_urllib=True), PROJECT)
data = _check_request(rv)

if Qgis.QGIS_VERSION_INT <= 31700:
Expand Down
8 changes: 4 additions & 4 deletions test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from PIL import Image
from qgis.server import QgsBufferServerResponse
from urllib3 import request
from urllib.parse import urlencode

__copyright__ = 'Copyright 2024, 3Liz'
__license__ = 'GPL version 3'
Expand Down Expand Up @@ -58,10 +58,10 @@ def xpath_text(self, path: str) -> str:
return ' '.join(e.text for e in self.xpath(path))


def _build_query_string(params: dict, use_urllib3: bool=False) -> str:
def _build_query_string(params: dict, use_urllib: bool = False) -> str:
""" Build a query parameter from a dictionary. """
if use_urllib3:
return "?" + request.urlencode(params)
if use_urllib:
return "?" + urlencode(params)

query_string = '?'
for k, v in params.items():
Expand Down

0 comments on commit 8416128

Please sign in to comment.