mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
*: Implements tags for apps
- Add tags to Info component of apps. Use only English tags for all operations. Localized tags are used for presentation to the user only. Add tags to all the apps. Conventions (English): 1. Tags describing use cases should be in kebab case. 2. Protocols in tag names should be in their canonical format. 3. Tags needn't be 100% technically correct. This can get in the way of comparing apps using a tag. Words that describe use cases that users can easily understand should be preferred over being pedantic. 4. Tags should be short, ideally not more than 2 words. Avoid conjunctions like "and", "or" in tags. 5. Avoid redundant words like "server", or "web-clients". Most apps on FreedomBox are either servers or web clients. 6. Keep your nouns singular in tags. - Use query params to filter the Apps page by tags. When all tags are removed, redirect to /apps. - Add UI elements to add and remove tag filters in the Apps page. Make the UI similar to GitLab issue tags. Since there are 40 apps, there will be at least 40 tags. Selecting a tag from a dropdown will be difficult on mobile devices. A fuzzy search is useful to find tags to add to the filter. Allow user to find the best match for the search term and highlight it visually. The user can then press Enter to select the highlighted tag. Make tag search case-insensitive. Make the dropdown menu scrollable with a fixed size. User input is debounced by 300 ms during search. - tests: Add missing mock in test_module_loader.py - Add functional tests [sunil] - 'list' can be used instead of 'List' for typing in recent Python versions. - Reserve tripe-quoted strings for docstrings. - Undo some changes in module initialization, use module_name for logging errors. - isort and yapf changes. - Encode parameters before adding them to the URL. Tests: - Tested the functionality of filtering by tag with one tag and two tags. Signed-off-by: Joseph Nuthalapati <njoseph@riseup.net> Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
1 parent
e2ae29acb2
commit
e5b7ed4faf
93 files changed
+622
-72
No files matched your search
+170
@@ -0,0 +1,170 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
/**
|
||||
* @licstart The following is the entire license notice for the JavaScript
|
||||
* code in this page.
|
||||
*
|
||||
* This file is part of FreedomBox.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* @licend The above is the entire license notice for the JavaScript code
|
||||
* in this page.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Update the URL path and history based on the selected tags.
|
||||
*
|
||||
* If no tags are selected, redirects to the base apps path. Otherwise,
|
||||
* constructs a new URL with query parameters for each tag and updates
|
||||
* the browser history.
|
||||
*
|
||||
* @param {string[]} tags - An array of selected tag names.
|
||||
*/
|
||||
function updatePath(tags) {
|
||||
const appsPath = window.location.pathname;
|
||||
if (tags.length === 0) {
|
||||
this.location.assign(appsPath);
|
||||
} else {
|
||||
let queryParams = tags.map(tag => `tag=${tag}`).join('&');
|
||||
let newPath = `${appsPath}?${queryParams}`;
|
||||
this.history.pushState({ tags: tags }, '', newPath);
|
||||
this.location.assign(newPath);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of tags currently displayed, excluding a specific tag if provided.
|
||||
*
|
||||
* Iterates through the tag badges in the UI, extracts their text content,
|
||||
* and returns an array of tag names.
|
||||
*
|
||||
* @param {string} [tagToRemove] - The name of the tag to exclude.
|
||||
* @returns {string[]} An array of tag names currently displayed.
|
||||
*/
|
||||
function getTags(tagToRemove) {
|
||||
const tagBadges = document.querySelectorAll('#selected-tags .tag-badge');
|
||||
return Array.from(tagBadges)
|
||||
.map(tag => tag.dataset.tag)
|
||||
.filter(tagName => tagName !== tagToRemove);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter and highlight the best matching tag based on the search term.
|
||||
*
|
||||
* This function updates the visibility and highlighting of dropdown items
|
||||
* to match the user's input in the search box. It determines the best
|
||||
* matching item and marks it as active.
|
||||
*
|
||||
* @param {KeyboardEvent} event - The keyboard event that triggered the search.
|
||||
*/
|
||||
function findMatchingTag(addTagInput, dropdownItems) {
|
||||
const searchTerm = addTagInput.value.toLowerCase().trim();
|
||||
|
||||
// Remove highlighting from all items
|
||||
dropdownItems.forEach(item => item.classList.remove('active'));
|
||||
|
||||
let bestMatch = null;
|
||||
dropdownItems.forEach(item => {
|
||||
const text = item.textContent.toLowerCase();
|
||||
if (text.includes(searchTerm)) {
|
||||
item.style.display = 'block';
|
||||
function matchesEarly () {
|
||||
return text.indexOf(searchTerm) < bestMatch.textContent.toLowerCase().indexOf(searchTerm);
|
||||
};
|
||||
if (bestMatch === null || matchesEarly()) {
|
||||
bestMatch = item;
|
||||
}
|
||||
} else {
|
||||
item.style.display = 'none';
|
||||
}
|
||||
});
|
||||
|
||||
// Highlight the best match
|
||||
if (bestMatch) {
|
||||
bestMatch.classList.add('active');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Manage tag-related UI interactions for filtering and displaying apps.
|
||||
*
|
||||
* This script manages the user interface for filtering apps based on
|
||||
* selected tags. It provides functionality for adding and removing tags,
|
||||
* updating the URL based on selected tags, and displaying a set of
|
||||
* available tags in a searchable dropdown.
|
||||
*/
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
|
||||
// Remove Tag handler.
|
||||
document.querySelectorAll('.remove-tag').forEach(button => {
|
||||
button.addEventListener('click', () => {
|
||||
let tag = button.dataset.tag;
|
||||
let tags = getTags(tag);
|
||||
updatePath(tags);
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Searchable dropdown for selecting tags.
|
||||
*
|
||||
* As the user types in the input field, the dropdown list is filtered
|
||||
* to show only matching items. The best matching item (first match if
|
||||
* multiple match) is highlighted. Pressing Enter selects the
|
||||
* highlighted item and adds it as a tag.
|
||||
*/
|
||||
const addTagInput = document.getElementById('add-tag-input');
|
||||
const dropdownItems = document.querySelectorAll('li.dropdown-item');
|
||||
|
||||
var timeoutId;
|
||||
addTagInput.addEventListener('keyup', (event) => {
|
||||
clearTimeout(timeoutId);
|
||||
// Select the active tag if the user pressed Enter
|
||||
if (event.key === 'Enter') {
|
||||
dropdownItems.forEach(item => {
|
||||
if (item.classList.contains('active')) {
|
||||
item.click();
|
||||
}
|
||||
});
|
||||
}
|
||||
// Debounce the user input for search with 300ms delay.
|
||||
timeoutId = setTimeout(findMatchingTag(addTagInput, dropdownItems), 300);
|
||||
});
|
||||
|
||||
dropdownItems.forEach(item => {
|
||||
item.addEventListener('click', () => {
|
||||
const selectedTag = item.dataset.value;
|
||||
|
||||
// Add the selected tag and update the path.
|
||||
let tags = getTags('');
|
||||
tags.push(selectedTag);
|
||||
updatePath(tags);
|
||||
|
||||
// Reset the input field and dropdown.
|
||||
addTagInput.value = '';
|
||||
dropdownItems.forEach(item => {
|
||||
item.style.display = '';
|
||||
item.classList.remove('active');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// Handle browser back/forward navigation.
|
||||
window.addEventListener('popstate', function (event) {
|
||||
if (event.state && event.state.tags) {
|
||||
updatePath(event.state.tags);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -309,7 +309,6 @@ html {
|
||||
}
|
||||
|
||||
#wrapper {
|
||||
min-height: 100%;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
@@ -321,6 +320,59 @@ html {
|
||||
margin-bottom: 1.25rem;
|
||||
}
|
||||
|
||||
/* Tag Input Container */
|
||||
.tag-input {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
border: 1px solid #ced4da;
|
||||
border-radius: .25rem;
|
||||
padding: .375rem .75rem;
|
||||
width: 100%;
|
||||
background-color: #fff;
|
||||
margin-bottom: 3rem;
|
||||
}
|
||||
|
||||
/* Selected Tags */
|
||||
.tag-input #selected-tags {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
margin-right: .5rem;
|
||||
}
|
||||
|
||||
.tag-input .tag {
|
||||
background-color: #e9ecef; /* Light gray background */
|
||||
border-radius: .25rem;
|
||||
padding: .25rem .5rem;
|
||||
margin-right: .25rem;
|
||||
margin-bottom: .25rem;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
/* Remove tag button */
|
||||
.tag-badge .remove-tag {
|
||||
background-color: #f8f9fa; /* Match the tag's background color */
|
||||
border: none;
|
||||
padding: 0.25rem 0.5rem;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
/* Adjust input field width */
|
||||
.tag-input input[type="search"] {
|
||||
flex-grow: 1;
|
||||
border: none;
|
||||
outline: none;
|
||||
box-shadow: none;
|
||||
width: auto;
|
||||
min-width: 3rem;
|
||||
}
|
||||
|
||||
/* dropdown-menu for tags is a scrollable list */
|
||||
.tag-input .dropdown-menu {
|
||||
bottom: calc(-100% - 10px);
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
@media (min-width: 768px) {
|
||||
.content-container {
|
||||
padding: 1.5rem 3rem 3rem;
|
||||
|
||||
Reference in new issue
Block a user