-
Notifications
You must be signed in to change notification settings - Fork 0
/
UiBuilder.js
179 lines (146 loc) · 6.04 KB
/
UiBuilder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const NO_KNOWN_IMAGE = "https://upload.wikimedia.org/wikipedia/en/e/ed/Nyan_cat_250px_frame.PNG";
// Return a genre name that can be used in an anchor link
function buildGenreHref(gen) {
return gen.replaceAll(' ', '-');
}
function imgClosestTo200(imgs) {
if (!imgs || !Array.isArray(imgs) || imgs.length == 0) {
return NO_KNOWN_IMAGE;
}
const tgt_height = 250;
var closest_idx = 0;
for (var i=1; i<imgs.length; ++i) {
const delta = Math.abs(imgs[i].height - tgt_height);
const delta_min = Math.abs(imgs[closest_idx].height - tgt_height);
if (delta < delta_min) closest_idx = i;
}
return imgs[closest_idx].url;
};
function buildArtistGenresView(genres, art) {
const known_genres = art.genres
.filter(gen => genres.includes(gen))
.map(gen => `<li><a href=#${buildGenreHref(gen)}>${gen}</a><li>`)
.join('');
const unknown_genres = art.genres
.filter(gen => { return !genres.includes(gen); })
.map(gen => `<li class='unknown_genre'>${gen}<li>`)
.join('');
const genres_view = (genres.length == 0)?
'' :
`<p class="genresList">More:</p><ul class="genresList">
${known_genres}${unknown_genres}
</ul>`;
return genres_view;
};
function buildAlbumListView(art_name, albums) {
const lst = albums.map(album => {
var albumtype = '';
if (album.album_type == 'single') albumtype = '-Single:';
return `<li class='album_info'>
<a href='javascript:' onclick='UiBuilder.trampolineOnAlbumClicked("${art_name}", "${album.uri}")'>
<img src='${imgClosestTo200(album.images)}'/>
${(new Date(album.release_date)).getFullYear()}${albumtype} ${album.name}
</a>
</li>`
}).join('');
return `<ul class='albums'>${lst}</ul>`;
};
export class UiBuilder {
static self = null;
constructor() {
if (UiBuilder.self != null) {
console.error("This is a singleton, weird things may happen if you instanciate twice");
}
this.genres = [];
this.genreIndex = new Map();
this.artistIndex = new Map();
this.onArtistClickedCb = console.log;
this.onArtistExpandClickedCb = console.log;
this.onAlbumClickedCb = console.log;
this.known_albums = {};
// A unique id to represent each tile. Same artist may have multiple
// tiles in different sections
this.art_tile_unique_id = 42;
// Make the UiBuilder globally accessible, so that links can use it to trampoline
window.UiBuilder = UiBuilder;
UiBuilder.self = this;
}
setCollection(genreIndex, artistIndex) {
this.genreIndex = genreIndex;
this.artistIndex = artistIndex;
this.genres = Array.from(genreIndex.keys()).sort();
}
buildRecentlyPlayed(recently_played) {
if (recently_played.length == 0) return '';
const lastPlayedUi = recently_played.map(this.buildArtistTile).join('');
return `<h2>Recently played</h2><ul class='arts'>${lastPlayedUi}</ul>`;
}
buildGenresIndex() {
return this.genres.map(gen => `<li><a href="#${buildGenreHref(gen)}">${gen}</a></li>`).join('');
}
buildAllGenres() {
const buildArtistGroup = (arts) => {
const arts_group_body = arts.map(this.buildArtistTile).join('');
return `<ul class='arts'>${arts_group_body}</ul>`;
}
const buildSingleGenre = (gen) => {
const genre_body = buildArtistGroup(this.genreIndex.get(gen))
return `<h2 id='${buildGenreHref(gen)}'>${gen}</h2>` + genre_body;
}
return this.genres.map(buildSingleGenre).join('');
}
buildArtistTile = (art_name) => {
const art_info = this.artistIndex.get(art_name);
const unique_id = this.art_tile_unique_id++;
if (!art_info) {
return `<li><img src="${NO_KNOWN_IMAGE}"/>${art_name}</li>`;
} else {
const imgurl = imgClosestTo200(art_info.images);
return `<li id='art${unique_id}_node_${art_info.id}'>
<div class="expandView" onclick='UiBuilder.trampolineToggleArtistExtendedView("${unique_id}", "${art_name}")'>▼</div>
<a href='javascript:' onclick='UiBuilder.trampolineOnArtistClicked("${art_name}")'>
<img src='${imgurl}'/>
${art_name}
</a>
<div id='art${unique_id}_extended_info_${art_info.id}'>...</div>
</li>`;
}
}
// Event handlers
onAlbumClicked(cb) {this.onAlbumClickedCb = cb; }
onExtendedViewClicked(cb) { this.onArtistExpandClickedCb = cb; }
onArtistClicked(cb) { this.onArtistClickedCb = cb; }
// Event trampolines
static trampolineOnAlbumClicked(art_name, album_uri) {
const art_obj = UiBuilder.self.artistIndex.get(art_name);
UiBuilder.self.onAlbumClickedCb(art_obj, album_uri);
}
static trampolineToggleArtistExtendedView(tile_id, art_name) {
const art_obj = UiBuilder.self.artistIndex.get(art_name);
UiBuilder.self.onArtistExpandClickedCb(tile_id, art_obj);
}
static trampolineOnArtistClicked(art_name) {
const art_obj = UiBuilder.self.artistIndex.get(art_name);
UiBuilder.self.onArtistClickedCb(art_obj);
}
toggleExtendedView(tile_id, art, content_generator) {
const buildExtendedView = (art, albums) => {
return buildArtistGenresView(this.genres, art) +
buildAlbumListView(art.name, albums);
};
const art_view = document.getElementById(`art${tile_id}_node_${art.id}`);
const art_extended_view = document.getElementById(`art${tile_id}_extended_info_${art.id}`);
if (art_view.classList.contains('selected')) {
art_view.classList.remove('selected');
art_extended_view.classList.remove('selected');
} else {
// Remove class from all elements, so only one will have it
$(".selected").removeClass("selected");
art_view.classList.add('selected');
art_extended_view.classList.add('selected');
content_generator().then(albums => {
art_extended_view.innerHTML = buildExtendedView(art, albums);
});
}
}
};