cyp/app/js/fs.js

75 lines
1.8 KiB
JavaScript
Raw Normal View History

2019-03-25 22:49:23 +08:00
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";
2019-03-31 06:15:32 +08:00
import Search from "./lib/search.js";
let node, search;
2019-03-25 22:49:23 +08:00
function buildHeader(path) {
let header = node.querySelector("header");
html.clear(header);
2019-03-26 19:35:47 +08:00
let button = html.button({}, "/", header);
button.addEventListener("click", e => list(""));
2019-03-25 22:49:23 +08:00
2019-03-26 19:35:47 +08:00
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));
});
2019-04-01 17:18:28 +08:00
2019-04-01 21:16:39 +08:00
search.reset();
2019-04-01 17:18:28 +08:00
header.appendChild(search.getNode());
2019-03-25 22:49:23 +08:00
}
function buildDirectory(data, parent) {
let path = data["directory"];
2019-03-26 17:09:26 +08:00
let name = path.split("/").pop();
2019-03-29 03:28:55 +08:00
let node = ui.group(ui.CTX_FS, name, path, parent);
2019-03-25 22:49:23 +08:00
node.addEventListener("click", e => list(path));
2019-04-01 21:16:39 +08:00
node.dataset.name = name;
2019-03-25 22:49:23 +08:00
return node;
}
function buildFile(data, parent) {
2019-04-01 21:16:39 +08:00
let node = ui.song(ui.CTX_FS, data, parent);
let name = data["file"].split("/").pop();
node.dataset.name = name;
return node;
2019-03-25 22:49:23 +08:00
}
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);
2019-03-26 19:35:47 +08:00
buildHeader(path);
2019-03-25 22:49:23 +08:00
}
2019-03-31 06:15:32 +08:00
function onSearch(e) {
2019-04-01 21:16:39 +08:00
Array.from(node.querySelectorAll("[data-name]")).forEach(node => {
let name = node.dataset.name;
node.style.display = (search.match(name) ? "" : "none");
});
2019-03-31 06:15:32 +08:00
}
2019-03-25 22:49:23 +08:00
export async function activate() {
list("");
}
export function init(n) {
node = n;
2019-03-31 06:15:32 +08:00
search = new Search(node.querySelector(".search"));
search.addEventListener("input", onSearch);
2019-03-25 22:49:23 +08:00
}