cyp/app/js/elements/tag.js

57 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-03-13 06:03:26 +08:00
import * as html from "../html.js";
2020-03-13 17:52:03 +08:00
import * as art from "../art.js";
2020-03-13 06:03:26 +08:00
import Item from "../item.js";
2020-03-13 17:52:03 +08:00
2020-03-13 06:03:26 +08:00
const ICONS = {
"AlbumArtist": "artist",
"Album": "album"
}
export default class Tag extends Item {
constructor(type, value, filter) {
super();
this._type = type;
this._value = value;
this._filter = filter;
}
connectedCallback() {
2020-03-13 17:52:03 +08:00
const art = html.node("span", {className:"art"}, "", this);
this._fillArt(art);
2020-03-13 06:03:26 +08:00
this._buildTitle(this._value);
}
createChildFilter() {
return Object.assign({[this._type]:this._value}, this._filter);
}
2020-03-13 17:52:03 +08:00
async _fillArt(parent) {
const filter = this.createChildFilter();
let artist = filter["AlbumArtist"];
let album = filter["Album"];
let src = null;
if (artist && album) {
src = await art.get(this._mpd, artist, album);
if (!src) {
let songs = await this._mpd.listSongs(filter, [0,1]);
if (songs.length) {
src = await art.get(artist, album, songs[0]["file"]);
}
}
}
if (src) {
html.node("img", {src}, "", parent);
} else {
const icon = ICONS[this._type];
html.icon(icon, parent);
}
}
2020-03-13 06:03:26 +08:00
}
customElements.define("cyp-tag", Tag);