Merge pull request #5108 from joepavitt/install-search-sort

Default Palette Search: Sort by Downloads
This commit is contained in:
Nick O'Leary
2025-04-28 11:42:59 +01:00
committed by GitHub
2 changed files with 30 additions and 11 deletions

View File

@@ -583,7 +583,9 @@ RED.palette.editor = (function() {
packageList.editableList('addItem',{count:loadedList.length})
return;
}
// sort the filtered modules
filteredList.sort(activeSort);
// render the items in the package list
for (var i=0;i<Math.min(10,filteredList.length);i++) {
packageList.editableList('addItem',filteredList[i]);
}
@@ -595,15 +597,26 @@ RED.palette.editor = (function() {
}
}
function sortModulesRelevance(A,B) {
var currentFilter = searchInput.searchBox('value').trim();
if (currentFilter === "") {
return sortModulesAZ(A,B);
const defaultSort = sortModulesDownloads(A,B);
const currentFilter = searchInput.searchBox('value').trim();
if (defaultSort === 0) {
// same number of downloads
if (currentFilter === "") {
return sortModulesAZ(A,B);
}
var i = A.info.index.indexOf(currentFilter) - B.info.index.indexOf(currentFilter);
if (i === 0) {
return sortModulesAZ(A,B);
}
return i;
}
var i = A.info.index.indexOf(currentFilter) - B.info.index.indexOf(currentFilter);
if (i === 0) {
return sortModulesAZ(A,B);
}
return i;
return defaultSort;
}
function sortModulesDownloads(A,B) {
// check A has the info.downloads property - if not exising, prioritise it (likely a custom user catalogue)
const a = A.info && typeof A.info.downloads !== 'undefined' ? A.info.downloads.week : Number.MAX_SAFE_INTEGER;
const b = B.info && typeof B.info.downloads !== 'undefined' ? B.info.downloads.week : Number.MAX_SAFE_INTEGER;
return b - a;
}
function sortModulesAZ(A,B) {
return A.info.id.localeCompare(B.info.id);
@@ -1120,8 +1133,11 @@ RED.palette.editor = (function() {
var descRow = $('<div class="red-ui-palette-module-meta"></div>').appendTo(headerRow);
$('<div>',{class:"red-ui-palette-module-description"}).text(entry.description).appendTo(descRow);
var metaRow = $('<div class="red-ui-palette-module-meta"></div>').appendTo(headerRow);
$('<span class="red-ui-palette-module-version"><i class="fa fa-tag"></i> '+entry.version+'</span>').appendTo(metaRow);
$('<span class="red-ui-palette-module-updated"><i class="fa fa-calendar"></i> '+formatUpdatedAt(entry.updated_at)+'</span>').appendTo(metaRow);
$('<span class="red-ui-palette-module-version" title="Latest Version"><i class="fa fa-tag"></i> '+entry.version+'</span>').appendTo(metaRow);
$('<span class="red-ui-palette-module-updated" title="Last Updated"><i class="fa fa-calendar"></i> '+formatUpdatedAt(entry.updated_at)+'</span>').appendTo(metaRow);
if (entry.downloads?.week !== undefined) {
$('<span class="red-ui-palette-module-downloads" title="Downloads - Last 7 Days"><i class="fa fa-download"></i> '+(new Intl.NumberFormat().format(entry.downloads.week))+'</span>').appendTo(metaRow);
}
if (loadedCatalogs.length > 1) {
$('<span class="red-ui-palette-module-updated"><i class="fa fa-cubes"></i>' + (entry.catalog.name || entry.catalog.url) + '</span>').appendTo(metaRow);
}

View File

@@ -119,6 +119,9 @@
.red-ui-palette-module-updated {
margin-left: 10px;
}
.red-ui-palette-module-downloads {
margin-left: 10px;
}
.red-ui-palette-module-link {
margin-left: 5px;
}
@@ -230,7 +233,7 @@
white-space: nowrap;
@include mixins.enable-selection;
}
.red-ui-palette-module-version, .red-ui-palette-module-updated, .red-ui-palette-module-link {
.red-ui-palette-module-version, .red-ui-palette-module-updated, .red-ui-palette-module-link, .red-ui-palette-module-downloads {
font-style:italic;
font-size: 0.8em;
@include mixins.enable-selection;