From eddaf494a4d4049a631dbb87d1bc9aea3a611cd6 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Thu, 31 Aug 2023 20:11:42 +0200 Subject: [PATCH] appstore: implement sort-by-newest on catalog --- store/app.py | 2 +- store/templates/catalog.html | 62 ++++++++++++++++++++++++++++++++---- 2 files changed, 57 insertions(+), 7 deletions(-) diff --git a/store/app.py b/store/app.py index 2e01d40a..364a9d1b 100644 --- a/store/app.py +++ b/store/app.py @@ -120,7 +120,7 @@ def index(): @app.route('/catalog') def browse_catalog(): - return render_template("catalog.html", init_search=request.args.get("search"), init_category=request.args.get("category"), user=session.get('user', {}), catalog=catalog, timestamp_now=int(time.time())) + return render_template("catalog.html", init_sort=request.args.get("sort"), init_search=request.args.get("search"), init_category=request.args.get("category"), user=session.get('user', {}), catalog=catalog, timestamp_now=int(time.time())) @app.route('/app/') diff --git a/store/templates/catalog.html b/store/templates/catalog.html index c3730cca..c4814dd8 100644 --- a/store/templates/catalog.html +++ b/store/templates/catalog.html @@ -2,6 +2,7 @@ {% macro appCard(app, infos, timestamp_now, catalog) -%}
@@ -106,9 +107,9 @@ id="selectsort" class="inline-block rounded-md border-gray-200 text-sm ml-1 pl-1 pr-7 h-8 py-0" > - - - + + +
@@ -126,7 +127,7 @@
-
+
{% for app, infos in catalog['apps'].items() %} {% if infos['level'] and infos['level'] > 4 %} {{ appCard(app, infos, timestamp_now, catalog) }} @@ -155,7 +156,7 @@

-
+
{% for app, infos in catalog['apps'].items() %} {% if not infos['level'] or infos['level'] <= 4 %} {{ appCard(app, infos, timestamp_now, catalog) }} @@ -170,6 +171,7 @@ let typeInterval = 500; // Half a second let searchInput = document.getElementById('search'); let selectCategory = document.getElementById('selectcategory'); + let selectSort = document.getElementById('selectsort'); function liveSearch() { // Locate the card elements @@ -209,15 +211,55 @@ updateQueryArgsInURL() } + function liveSort(container_to_sort) { + let sortBy = selectSort.value.trim(); + var toSort = document.getElementById(container_to_sort).children; + toSort = Array.prototype.slice.call(toSort, 0); + if (sortBy === "newest") { + toSort.sort(function(a, b) { + return a.dataset.addedincatalog - b.dataset.addedincatalog; + }); + } + else if (sortBy === "") { + toSort.sort(function(a, b) { + return a.dataset.appid > b.dataset.appid; + }); + } + var parent = document.getElementById(container_to_sort); + parent.innerHTML = ""; + + for(var i = 0, l = toSort.length; i < l; i++) { + parent.appendChild(toSort[i]); + } + + updateQueryArgsInURL() + } + function updateQueryArgsInURL() { let search_query = searchInput.value.trim(); let category = selectCategory.value.trim(); + let sortBy = selectSort.value.trim(); if ('URLSearchParams' in window) { var queryArgs = new URLSearchParams(window.location.search) if (search_query) { queryArgs.set("search", search_query) } else { queryArgs.delete("search"); }; if (category) { queryArgs.set("category", category) } else { queryArgs.delete("category"); }; - history.pushState(null, '', window.location.pathname + '?' + queryArgs.toString()); + if (sortBy) { queryArgs.set("sort", sortBy) } else { queryArgs.delete("sortBy"); }; + + let newUrl; + if (queryArgs.toString()) + { + newUrl = window.location.pathname + '?' + queryArgs.toString(); + } + else + { + newUrl = window.location.pathname; + } + + if (newUrl != window.location.pathname + window.location.search) + { + history.pushState(null, '', newUrl); + } } } @@ -231,7 +273,15 @@ typingTimer = setTimeout(liveSearch, typeInterval); }); + selectSort.addEventListener('change', () => { + liveSort("catalogGoodQuality"); + liveSort("catalogLowQuality"); + }); + liveSearch(); + liveSort("catalogGoodQuality"); + liveSort("catalogLowQuality"); + {% endblock %}