Skip to content

Commit 90f2759

Browse files
committed
WD-17150 - Add aliases section to snap details
1 parent bedb5c7 commit 90f2759

2 files changed

Lines changed: 71 additions & 0 deletions

File tree

templates/store/snap-details/_details.html

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,65 @@ <h5 class="p-muted-heading">Last updated</h5>
1919
<hr>
2020
{% endif %}
2121

22+
{%- if aliases -%}
23+
{%- set show_more = "Show more" -%}
24+
{%- set show_less = "Show less" -%}
25+
26+
{#-
27+
# The list starts displaying maximum 3 elements with the rest hidden.
28+
# With the script provided and the link element rendered the user can toggle
29+
# displaying and hiding all the aliases.
30+
-#}
31+
<h5 class="p-muted-heading">Aliases</h5>
32+
<ul class="p-list js-expandable-list">
33+
{%- set aliases_len = aliases|length -%}
34+
{%- set default_visible_aliases = 3 -%}
35+
{%- for alias in aliases -%}
36+
{%- set invisible_class = "" -%}
37+
{%- if loop.index > default_visible_aliases -%}
38+
{%- set invisible_class = "js-hidable u-hide" -%}
39+
{%- endif -%}
40+
<li class="p-list__item {{ invisible_class }}">
41+
{{ alias[0]|safe }} &rsaquo; {{ alias[1]|safe }}
42+
</li>
43+
{%- endfor -%}
44+
{%- if aliases_len > default_visible_aliases -%}
45+
<li class="p-list__item">
46+
<a class="js-toggle-full-list" href="#" title="Display more/less aliases">
47+
{{ show_more }}
48+
</a>
49+
</li>
50+
{%- endif -%}
51+
</ul>
52+
<hr>
53+
54+
<script>
55+
// Handle aliases "show more" button
56+
const list = document.querySelector(".js-expandable-list");
57+
const showMoreButton = document.querySelector(".js-toggle-full-list");
58+
let hasAllElementsDisplayed = false;
59+
60+
const updateAliasesListUI = (displayAllElements, elements) => {
61+
// change button
62+
showMoreButton.innerHTML = displayAllElements ? "{{ show_less }}" : "{{ show_more }}";
63+
// toggle hide classes
64+
for (const element of elements) {
65+
element.classList.toggle('u-hide', !displayAllElements);
66+
}
67+
}
68+
69+
if (list && showMoreButton) {
70+
const listElements = list.querySelectorAll(".js-hidable");
71+
showMoreButton.addEventListener("click", (e) => {
72+
e.preventDefault();
73+
// switch toggle
74+
hasAllElementsDisplayed = !hasAllElementsDisplayed;
75+
updateAliasesListUI(hasAllElementsDisplayed, listElements);
76+
});
77+
}
78+
</script>
79+
{%- endif -%}
80+
2281
{% if links %}
2382
{% if links["website"] %}
2483
<h5 class="p-muted-heading">Websites</h5>

webapp/store/snap_details_views.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
"links",
4040
]
4141

42+
FIELDS_EXTRA_DETAILS = [
43+
"aliases",
44+
]
45+
4246

4347
def snap_details_views(store):
4448
snap_regex = "[a-z0-9-]*[a-z][a-z0-9-]*"
@@ -208,6 +212,14 @@ def snap_details(snap_name):
208212
status_code = 200
209213

210214
context = _get_context_snap_details(snap_name)
215+
extra_details = device_gateway.get_snap_details(
216+
snap_name, fields=FIELDS_EXTRA_DETAILS)
217+
218+
if extra_details["aliases"]:
219+
context["aliases"] = [
220+
[f"{extra_details['package_name']}.{alias_obj['target']}", alias_obj['name']]
221+
for alias_obj in extra_details["aliases"]
222+
]
211223

212224
country_metric_name = "weekly_installed_base_by_country_percent"
213225
os_metric_name = "weekly_installed_base_by_operating_system_normalized"

0 commit comments

Comments
 (0)