cyp/app/js/library.js

112 lines
2.8 KiB
JavaScript
Raw Permalink Normal View History

2019-03-25 22:49:23 +08:00
import * as mpd from "./lib/mpd.js";
import * as html from "./lib/html.js";
import * as ui from "./lib/ui.js";
2019-04-03 01:19:46 +08:00
import * as format from "./lib/format.js";
2019-03-25 22:49:23 +08:00
2019-03-31 06:15:32 +08:00
import Search from "./lib/search.js";
let node, search;
function nonempty(x) { return x.length > 0; }
2019-03-25 22:49:23 +08:00
function buildHeader(filter) {
filter = filter || {};
let header = node.querySelector("header");
html.clear(header);
2019-04-03 01:19:46 +08:00
search.reset();
header.appendChild(search.getNode());
2019-03-25 22:49:23 +08:00
let artist = filter["AlbumArtist"];
2019-03-25 22:49:23 +08:00
if (artist) {
let artistFilter = {"AlbumArtist":artist};
2019-04-03 01:19:46 +08:00
let button = html.button({icon:"artist"}, artist, header);
2019-03-25 22:49:23 +08:00
button.addEventListener("click", e => listAlbums(artistFilter));
let album = filter["Album"];
if (album) {
2019-04-03 01:19:46 +08:00
html.node("span", {}, format.SEPARATOR, header);
2019-03-25 22:49:23 +08:00
let albumFilter = Object.assign({}, artistFilter, {"Album":album});
2019-04-03 01:19:46 +08:00
let button = html.button({icon:"album"}, album, header);
2019-03-25 22:49:23 +08:00
button.addEventListener("click", e => listSongs(albumFilter));
}
}
2019-04-01 17:18:28 +08:00
2019-03-25 22:49:23 +08:00
}
function buildAlbum(album, filter, parent) {
let childFilter = Object.assign({}, filter, {"Album": album});
2019-03-29 03:28:55 +08:00
let node = ui.group(ui.CTX_LIBRARY, album, childFilter, parent);
2019-03-25 22:49:23 +08:00
node.addEventListener("click", e => listSongs(childFilter));
2019-04-01 21:16:39 +08:00
node.dataset.name = album;
2019-03-25 22:49:23 +08:00
return node;
}
function buildArtist(artist, filter, parent) {
let childFilter = Object.assign({}, filter, {"AlbumArtist": artist});
2019-03-29 03:28:55 +08:00
let node = ui.group(ui.CTX_LIBRARY, artist, childFilter, parent);
2019-03-25 22:49:23 +08:00
node.addEventListener("click", e => listAlbums(childFilter));
2019-04-01 21:16:39 +08:00
node.dataset.name = artist;
2019-03-25 22:49:23 +08:00
return node;
}
function buildSongs(songs, filter) {
let ul = node.querySelector("ul");
html.clear(ul);
2019-04-01 21:16:39 +08:00
songs.map(song => {
let node = ui.song(ui.CTX_LIBRARY, song, ul);
node.dataset.name = song["Title"];
});
2019-03-25 22:49:23 +08:00
}
function buildAlbums(albums, filter) {
let ul = node.querySelector("ul");
html.clear(ul);
2019-03-31 06:15:32 +08:00
albums.filter(nonempty).map(album => buildAlbum(album, filter, ul));
2019-03-25 22:49:23 +08:00
}
function buildArtists(artists, filter) {
let ul = node.querySelector("ul");
html.clear(ul);
2019-03-31 06:15:32 +08:00
artists.filter(nonempty).map(artist => buildArtist(artist, filter, ul));
2019-03-25 22:49:23 +08:00
}
async function listSongs(filter) {
let songs = await mpd.listSongs(filter);
buildSongs(songs, filter);
buildHeader(filter);
}
async function listAlbums(filter) {
let albums = await mpd.listTags("Album", filter);
buildAlbums(albums, filter);
buildHeader(filter);
}
async function listArtists(filter) {
let artists = await mpd.listTags("AlbumArtist", filter);
2019-03-25 22:49:23 +08:00
buildArtists(artists, filter);
buildHeader(filter);
}
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() {
listArtists();
}
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
}