Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[0.7.x] Releases list sorting #14

Merged
merged 7 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added basic sorting by name feature
  • Loading branch information
Serhii-DV committed Nov 16, 2023
commit 15cb7b84bbd52a65ff5a7b3329d11787fa50e701
2 changes: 1 addition & 1 deletion dist/vendors.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/vendors.js.map

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions src/popup/components/releases-list.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getDataAttribute } from "../../modules/html";

class ReleasesList extends HTMLElement {
constructor() {
super();
Expand All @@ -8,12 +10,18 @@ class ReleasesList extends HTMLElement {

const selectAllCheckboxId = self.getPrefixed('selectAllCheckbox');
const searchInputId = self.getPrefixed('searchInput');
const sortingId = self.getPrefixed('sorting');
const template = document.createElement('template');
template.innerHTML = `
<div class="content-header input-group input-group-sm sticky-top">
<input type="text" id="${searchInputId}" class="form-control form-control-sm" placeholder="Search...">
<div class="control-buttons btn-group btn-group-sm" role="group" aria-label="Control buttons">
</div>
<button id="${sortingId}-button" class="btn btn-outline-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false" title="Sorting">Sorting</button>
<ul id="${sortingId}" class="dropdown-menu dropdown-menu-end">
<li><a class="dropdown-item" href="#" data-dir="asc" data-sorting-title="A..z">By name A..z</a></li>
<li><a class="dropdown-item" href="#" data-dir="desc" data-sorting-title="z..A">By name z..A</a></li>
</ul>
</div>
<table class="table table-hover table-sm table-transparent table-borderless">
<thead>
Expand Down Expand Up @@ -72,6 +80,36 @@ class ReleasesList extends HTMLElement {

self.refreshStatus();
}

function setupSorting() {
const sortingUl = self.querySelector('#' + sortingId);
const sortingBtn = self.querySelector('#' + sortingId + '-button');
const sortingOptions = sortingUl.querySelectorAll('.dropdown-item');

sortingOptions.forEach((option) => {
option.addEventListener('click', (e) => {
const el = e.target;
sortingBtn.innerText = getDataAttribute(el, 'sorting-title');
sortTable(1, getDataAttribute(el, 'dir'));
});
});
}

const sortTable = (columnIndex, dir) => {
const rows = Array.from(table.rows).slice(1); // Exclude the header row

rows.sort((a, b) => {
const x = a.cells[columnIndex].textContent.toLowerCase();
const y = b.cells[columnIndex].textContent.toLowerCase();

return dir === "asc" ? x.localeCompare(y) : y.localeCompare(x);
});

// Reorder the rows in the table
rows.forEach((row) => table.tBodies[0].appendChild(row));
};

setupSorting();
}

refreshStatus() {
Expand Down