cyp/app/js/fs.js
Ondrej Zara e0bbd1107b search
2019-04-01 15:16:39 +02:00

74 lines
1.8 KiB
JavaScript

import * as app from "./app.js";
import * as mpd from "./lib/mpd.js";
import * as html from "./lib/html.js";
import * as player from "./player.js";
import * as format from "./lib/format.js";
import * as ui from "./lib/ui.js";
import Search from "./lib/search.js";
let node, search;
function buildHeader(path) {
let header = node.querySelector("header");
html.clear(header);
let button = html.button({}, "/", header);
button.addEventListener("click", e => list(""));
path.split("/").filter(x => x).forEach((name, index, all) => {
let button = html.button({}, name, header);
let path = all.slice(0, index+1).join("/");
button.addEventListener("click", e => list(path));
});
search.reset();
header.appendChild(search.getNode());
}
function buildDirectory(data, parent) {
let path = data["directory"];
let name = path.split("/").pop();
let node = ui.group(ui.CTX_FS, name, path, parent);
node.addEventListener("click", e => list(path));
node.dataset.name = name;
return node;
}
function buildFile(data, parent) {
let node = ui.song(ui.CTX_FS, data, parent);
let name = data["file"].split("/").pop();
node.dataset.name = name;
return node;
}
function buildResults(results) {
let ul = node.querySelector("ul");
html.clear(ul);
results["directory"].forEach(directory => buildDirectory(directory, ul));
results["file"].forEach(file => buildFile(file, ul));
}
async function list(path) {
let results = await mpd.listPath(path);
buildResults(results);
buildHeader(path);
}
function onSearch(e) {
Array.from(node.querySelectorAll("[data-name]")).forEach(node => {
let name = node.dataset.name;
node.style.display = (search.match(name) ? "" : "none");
});
}
export async function activate() {
list("");
}
export function init(n) {
node = n;
search = new Search(node.querySelector(".search"));
search.addEventListener("input", onSearch);
}