Skip to content

Commit

Permalink
refactor: enhance initSearchBar
Browse files Browse the repository at this point in the history
  • Loading branch information
fraxken committed Dec 7, 2024
1 parent 5160493 commit 17f14b6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 50 deletions.
105 changes: 58 additions & 47 deletions public/core/search-nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,42 @@ import { createDOMElement, parseNpmSpec } from "../common/utils";
import { SearchBar } from "../components/searchbar/searchbar";

// CONSTANTS
const kSearchbarId = "searchbar-tpl";
const kSearchbarId = "searchbar-content";

export function initSearchNav(data, nsn, secureDataSet) {
const searchNavElement = document.getElementById("search-nav");
// reset
searchNavElement.innerHTML = "";
const pkgs = data.lru;
const hasAtLeast2Packages = pkgs.length > 1;
const hasExactly2Packages = pkgs.length === 2;
const packagesContainer = document.createElement("div");
packagesContainer.classList.add("packages");

for (const pkg of pkgs) {
// Initialize Search nav

searchNavElement.appendChild(
initPackagesNavigation(data)
);

const searchElement = document.getElementById(kSearchbarId);
const searchElementClone = searchElement.content.cloneNode(true);
searchNavElement.appendChild(searchElementClone);

// Initialize searchbar
{
const dataListElement = document.getElementById("package-list");
for (const info of secureDataSet.packages) {
const content = `<p>${info.flags} ${info.name}</p><b>${info.version}</b>`;
dataListElement.insertAdjacentHTML("beforeend", `<div class="package hide" data-value="${info.id}">${content}</div>`);
}
}
window.searchbar = new SearchBar(nsn, secureDataSet.linker);
}

function initPackagesNavigation(data) {
const fragment = document.createDocumentFragment();
const packages = data.lru;

const hasAtLeast2Packages = packages.length > 1;
const hasExactly2Packages = packages.length === 2;
const container = createDOMElement("div", {
classList: ["packages"]
});

for (const pkg of packages) {
const { name, version } = parseNpmSpec(pkg);

const pkgElement = createDOMElement("div", {
Expand All @@ -38,62 +60,51 @@ export function initSearchNav(data, nsn, secureDataSet) {
});

if (hasAtLeast2Packages && pkg !== data.root) {
addRemoveButton(pkgElement, { hasExactly2Packages });
pkgElement.appendChild(
renderPackageRemoveButton(pkgElement.dataset.name, { hasExactly2Packages })
);
}

packagesContainer.appendChild(pkgElement);
container.appendChild(pkgElement);
}

searchNavElement.appendChild(packagesContainer);

const plusButtonElement = document.createElement("button");
plusButtonElement.classList.add("add");
plusButtonElement.appendChild(createDOMElement("p", {
text: "+"
}));
const plusButtonElement = createDOMElement("button", {
classList: ["add"],
childs: [
createDOMElement("p", { text: "+" })
]
});
plusButtonElement.addEventListener("click", () => {
window.navigation.setNavByName("search--view");
});

searchNavElement.appendChild(plusButtonElement);

const searchElement = document.getElementById(kSearchbarId);
const searchElementClone = searchElement.content.cloneNode(true);
searchNavElement.appendChild(searchElementClone);
fragment.append(container, plusButtonElement);

// Initialize searchbar
{
const dataListElement = document.getElementById("package-list");
for (const info of secureDataSet.packages) {
const content = `<p>${info.flags} ${info.name}</p><b>${info.version}</b>`;
dataListElement.insertAdjacentHTML("beforeend", `<div class="package hide" data-value="${info.id}">${content}</div>`);
}
}
window.searchbar = new SearchBar(nsn, secureDataSet.linker);
return fragment;
}

function addRemoveButton(pkgElement, options) {
function renderPackageRemoveButton(packageName, options) {
const {
hasExactly2Packages
} = options;

// we allow to remove a package when at least 2 packages are present
const removeButton = document.createElement("button");
removeButton.classList.add("remove");
removeButton.textContent = "x";
const removeButton = createDOMElement("button", {
classList: ["remove"],
text: "x"
});

removeButton.addEventListener("click", (event) => {
event.stopPropagation();
const pkgToRemove = pkgElement.dataset.name;
window.socket.send(JSON.stringify({ action: "REMOVE", pkg: pkgToRemove }));
window.socket.send(JSON.stringify({ action: "REMOVE", pkg: packageName }));

if (hasExactly2Packages) {
const allPackages = [...document.getElementById("search-nav").querySelectorAll(".package")];
for (const pkg of allPackages) {
const removeBtn = pkg.querySelector(".remove");
if (removeBtn) {
removeBtn.remove();
}
}
document
.getElementById("search-nav")
.querySelectorAll(".package")
.forEach((element) => element.querySelector(".remove")?.remove());
}
}, { once: true });
pkgElement.appendChild(removeButton);

return removeButton;
}
3 changes: 1 addition & 2 deletions public/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ async function init(options = { navigateToNetworkView: false }) {
}

// update search nav
const searchNavElement = document.getElementById("search-nav");
const pkgs = searchNavElement.querySelectorAll(".package");
const pkgs = document.querySelectorAll("#search-nav > .package");
for (const pkg of pkgs) {
if (pkg.dataset.name.startsWith(window.activePackage)) {
pkg.classList.add("active");
Expand Down
2 changes: 1 addition & 1 deletion views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ <h2><i class="icon-keyboard"></i>[[=z.token('settings.shortcuts.title')]]</h2>
</div>
</template>

<template id="searchbar-tpl">
<template id="searchbar-content">
<div id="searchbar">
<i class="icon-globe-alt-outline"></i>
<div class="search-items"></div>
Expand Down

0 comments on commit 17f14b6

Please sign in to comment.