cyp/app/js/fs.js

57 lines
1.4 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";
let node;
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-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();
let node = ui.group(ui.GROUP_DIRECTORY, name, path, parent);
2019-03-25 22:49:23 +08:00
node.addEventListener("click", e => list(path));
return node;
}
function buildFile(data, parent) {
2019-03-26 17:09:26 +08:00
return ui.song(ui.SONG_FILE, data, parent);
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
}
export async function activate() {
list("");
}
export function init(n) {
node = n;
}