mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-02 06:01:10 +00:00
Adds playlist and favorite affordances to the player and lists
This commit is contained in:
1 parent
525ac1348d
commit
3e32068573
23 files changed
+411
-39
No files matched your search
Binary file not shown.
Binary file not shown.
@@ -138,4 +138,7 @@
|
||||
<data name="LoadMore" xml:space="preserve">
|
||||
<value>تحميل المزيد</value>
|
||||
</data>
|
||||
<data name="AddToPlaylist" xml:space="preserve">
|
||||
<value>إضافة إلى قائمة التشغيل</value>
|
||||
</data>
|
||||
</root>
|
||||
@@ -138,4 +138,7 @@
|
||||
<data name="LoadMore" xml:space="preserve">
|
||||
<value>Mehr laden</value>
|
||||
</data>
|
||||
<data name="AddToPlaylist" xml:space="preserve">
|
||||
<value>Zur Wiedergabeliste hinzufügen</value>
|
||||
</data>
|
||||
</root>
|
||||
@@ -138,4 +138,7 @@
|
||||
<data name="LoadMore" xml:space="preserve">
|
||||
<value>Load more</value>
|
||||
</data>
|
||||
<data name="AddToPlaylist" xml:space="preserve">
|
||||
<value>Add to playlist</value>
|
||||
</data>
|
||||
</root>
|
||||
@@ -138,4 +138,7 @@
|
||||
<data name="LoadMore" xml:space="preserve">
|
||||
<value>Cargar más</value>
|
||||
</data>
|
||||
<data name="AddToPlaylist" xml:space="preserve">
|
||||
<value>Añadir a la lista de reproducción</value>
|
||||
</data>
|
||||
</root>
|
||||
@@ -138,4 +138,7 @@
|
||||
<data name="LoadMore" xml:space="preserve">
|
||||
<value>Charger plus</value>
|
||||
</data>
|
||||
<data name="AddToPlaylist" xml:space="preserve">
|
||||
<value>Ajouter à la liste de lecture</value>
|
||||
</data>
|
||||
</root>
|
||||
@@ -138,4 +138,7 @@
|
||||
<data name="LoadMore" xml:space="preserve">
|
||||
<value>Carica altro</value>
|
||||
</data>
|
||||
<data name="AddToPlaylist" xml:space="preserve">
|
||||
<value>Aggiungi alla playlist</value>
|
||||
</data>
|
||||
</root>
|
||||
@@ -138,4 +138,7 @@
|
||||
<data name="LoadMore" xml:space="preserve">
|
||||
<value>さらに読み込む</value>
|
||||
</data>
|
||||
<data name="AddToPlaylist" xml:space="preserve">
|
||||
<value>プレイリストに追加</value>
|
||||
</data>
|
||||
</root>
|
||||
@@ -138,4 +138,7 @@
|
||||
<data name="LoadMore" xml:space="preserve">
|
||||
<value>Load more</value>
|
||||
</data>
|
||||
<data name="AddToPlaylist" xml:space="preserve">
|
||||
<value>Add to playlist</value>
|
||||
</data>
|
||||
</root>
|
||||
@@ -138,4 +138,7 @@
|
||||
<data name="LoadMore" xml:space="preserve">
|
||||
<value>Încarcă mai multe</value>
|
||||
</data>
|
||||
<data name="AddToPlaylist" xml:space="preserve">
|
||||
<value>Adaugă în listă de redare</value>
|
||||
</data>
|
||||
</root>
|
||||
@@ -138,4 +138,7 @@
|
||||
<data name="LoadMore" xml:space="preserve">
|
||||
<value>Завантажити більше</value>
|
||||
</data>
|
||||
<data name="AddToPlaylist" xml:space="preserve">
|
||||
<value>Додати до списку відтворення</value>
|
||||
</data>
|
||||
</root>
|
||||
@@ -138,4 +138,7 @@
|
||||
<data name="LoadMore" xml:space="preserve">
|
||||
<value>加载更多</value>
|
||||
</data>
|
||||
<data name="AddToPlaylist" xml:space="preserve">
|
||||
<value>添加到播放列表</value>
|
||||
</data>
|
||||
</root>
|
||||
@@ -53,6 +53,7 @@
|
||||
["album"] = _localizer["Album"].Value,
|
||||
["track"] = _localizer["Track"].Value,
|
||||
["add"] = _localizer["Add"].Value,
|
||||
["addToPlaylist"] = _localizer["AddToPlaylist"].Value,
|
||||
["addFailed"] = _localizer["AddFailed"].Value,
|
||||
["refreshFailed"] = _localizer["RefreshFailed"].Value,
|
||||
["selectLibrary"] = _localizer["SelectLibrary"].Value,
|
||||
@@ -751,6 +752,33 @@
|
||||
if (entry.coverPath)
|
||||
playButton.dataset.coverArtPath = entry.coverPath;
|
||||
row.appendChild(playButton);
|
||||
|
||||
// The favorite heart and the add-to-playlist action of the row work through the global favorites and playlist
|
||||
// scripts, so the row binds no listener of its own and a themed row only has to render the same attributes.
|
||||
const favoriteToggle = document.createElement('button');
|
||||
favoriteToggle.type = 'button';
|
||||
favoriteToggle.className = 'card-favorite-toggle playlist-entry-favorite';
|
||||
favoriteToggle.setAttribute('data-favorite-toggle', '');
|
||||
favoriteToggle.setAttribute('data-favorite-media-kind', 'MusicTrack');
|
||||
favoriteToggle.setAttribute('data-favorite-media-item-id', entry.mediaItemId);
|
||||
row.appendChild(favoriteToggle);
|
||||
|
||||
const addToPlaylistButton = document.createElement('button');
|
||||
addToPlaylistButton.type = 'button';
|
||||
addToPlaylistButton.className = 'confirm-button playlist-entry-add-to-playlist';
|
||||
addToPlaylistButton.textContent = '+';
|
||||
// An empty localized string must not replace the accessible name and the hover text with empty values.
|
||||
if (strings.addToPlaylist) {
|
||||
addToPlaylistButton.setAttribute('aria-label', strings.addToPlaylist);
|
||||
addToPlaylistButton.title = strings.addToPlaylist;
|
||||
}
|
||||
addToPlaylistButton.dataset.playlistAdd = '';
|
||||
addToPlaylistButton.dataset.playlistLibraryId = entry.libraryId;
|
||||
addToPlaylistButton.dataset.playlistTrackId = entry.mediaItemId;
|
||||
addToPlaylistButton.dataset.playlistAlbumId = entry.collectionId;
|
||||
addToPlaylistButton.dataset.playlistArtistId = entry.creatorId;
|
||||
addToPlaylistButton.dataset.playlistTitle = entry.mediaItemTitle || '';
|
||||
row.appendChild(addToPlaylistButton);
|
||||
}
|
||||
|
||||
// The index passed to this function is local to the rendered rows. Disabling the move controls requires the global position,
|
||||
|
||||
@@ -31,7 +31,10 @@
|
||||
- The hook attributes the audio player binds: 'data-audio-player-play-pause', '-stop', '-previous', '-next', '-repeat',
|
||||
'-progress', '-progress-played', '-progress-downloaded', '-progress-undownloaded', '-current-time', '-total-time',
|
||||
'-track-name', '-album-name', '-now-playing-title', '-now-playing-release-type', '-now-playing-release',
|
||||
'-now-playing-year', '-now-playing-artist', '-artwork-carousel', '-cover-thumbnail', '-cover-image', '-volume', '-volume-level', '-mute', '-queue', '-playlist-combobox' and '-add-queue-to-playlist'.
|
||||
'-now-playing-year', '-now-playing-artist', '-favorite', '-artwork-carousel', '-cover-thumbnail', '-cover-image',
|
||||
'-volume', '-volume-level', '-mute', '-queue', '-playlist-combobox' and '-add-queue-to-playlist'.
|
||||
The favorite toggle ('-favorite') sits in the condensed bar, between repeat and mute. The player sets its media kind
|
||||
and current track Id and hides it while no track is loaded. A missing hook gets the built-in toggle in that place.
|
||||
The queue-to-playlist action is a standard application combo box, so a theme renders the usual 'enlightenment-combobox'
|
||||
markup and puts the '-playlist-combobox' hook on its root. The player only reads the 'data-value' of the selected text
|
||||
when the add button is pressed. A theme that renders one half of the action gets a complete built-in pair, placed in a
|
||||
@@ -72,8 +75,11 @@
|
||||
'library'), a whole library play affordance uses 'data-audio-player-play-library' with 'data-library-id', a whole library
|
||||
shuffle affordance uses 'data-audio-player-play-library-shuffled' with 'data-library-id', and a play next affordance uses
|
||||
'data-audio-player-play-next'. A track trigger must carry libraryId, trackId, albumId and artistId, because the playback
|
||||
manifest route needs all four Ids. The 'Play Affordances on the Music Pages' section of
|
||||
docs/technical/theming/Theming.md documents this contract for theme authors.
|
||||
manifest route needs all four Ids. The add-to-playlist actions of the music pages and playlist rows use the
|
||||
'data-playlist-add' marker with the 'data-playlist-library-id', '-track-id', '-album-id', '-artist-id' and '-title'
|
||||
scope attributes, handled by the global playlist script the layout loads. The 'Play Affordances on the Music Pages'
|
||||
and 'The Shared Add to Playlist Action' sections of docs/technical/theming/Theming.md document both contracts for
|
||||
theme authors.
|
||||
*@
|
||||
|
||||
<div class="audio-player-container not-shown" hidden>
|
||||
@@ -146,6 +152,11 @@
|
||||
<span class="audio-player-repeat-one-badge" aria-hidden="true">1</span>
|
||||
</button>
|
||||
</div>
|
||||
<!-- Favorite toggle of the current track: icon only, no rectangle, between repeat and mute. The player writes its favorite attributes when a track plays. -->
|
||||
<button type="button" class="audio-player-button audio-player-favorite" data-audio-player-favorite
|
||||
aria-label="@localizer["FavoritesAdd"]" hidden>
|
||||
<span class="audio-player-favorite-icon" aria-hidden="true"></span>
|
||||
</button>
|
||||
<div class="audio-player-volume-control">
|
||||
<button type="button" class="audio-player-button audio-player-mute" data-audio-player-mute aria-label="@localizer["AudioPlayerMute"]" aria-pressed="false">
|
||||
<span class="audio-player-mute-icon"></span>
|
||||
|
||||
@@ -101,7 +101,8 @@
|
||||
tracksAdded: '@localizer["PlaylistTracksAdded"]',
|
||||
tracksAddFailed: '@localizer["PlaylistTracksAddFailed"]',
|
||||
noTracksToAdd: '@localizer["PlaylistNoTracksToAdd"]',
|
||||
add: '@localizer["AudioPlayerAdd"]'
|
||||
add: '@localizer["AudioPlayerAdd"]',
|
||||
addToPlaylist: '@localizer["PlaylistAddToPlaylist"]'
|
||||
}
|
||||
};
|
||||
// The endpoints and labels of the favorite toggles that every media page and theme template can render. The script reads which
|
||||
|
||||
@@ -1328,6 +1328,24 @@ body {
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
/* The hearts of a playlist row and of a player queue item are inline actions, so they are always visible and never take the position of a card heart. */
|
||||
.playlist-entry-favorite.card-favorite-toggle,
|
||||
.audio-player-queue-item-actions .card-favorite-toggle {
|
||||
position: static;
|
||||
display: flex;
|
||||
flex: 0 0 auto;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
/* The add-to-playlist action of a playlist row is compact, because it sits in a row that already carries the playback, reordering and removal controls. */
|
||||
.playlist-entry-add-to-playlist {
|
||||
width: 22px;
|
||||
min-width: 22px;
|
||||
height: 22px;
|
||||
padding: 0;
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
.card-actions-menu {
|
||||
display: none;
|
||||
position: fixed;
|
||||
@@ -1762,6 +1780,35 @@ html.audio-player-initializing .audio-player-container {
|
||||
cursor: default;
|
||||
}
|
||||
|
||||
/* The favorite toggle of the condensed bar is icon only, with no rectangle of its own: it reuses the transparent box of a transport button, and the stylesheet draws the heart. */
|
||||
.audio-player-favorite[hidden] {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.audio-player-favorite-icon {
|
||||
display: block;
|
||||
font-size: 16px;
|
||||
line-height: 1;
|
||||
color: #E6E6E6;
|
||||
}
|
||||
|
||||
.audio-player-favorite-icon::after {
|
||||
content: '\2665';
|
||||
}
|
||||
|
||||
.audio-player-favorite:hover .audio-player-favorite-icon {
|
||||
color: #FFFFFF;
|
||||
}
|
||||
|
||||
.audio-player-favorite.is-favorite .audio-player-favorite-icon {
|
||||
color: #FF5D5D;
|
||||
}
|
||||
|
||||
.audio-player-favorite[data-favorite-state="loading"] {
|
||||
opacity: 0.5;
|
||||
cursor: progress;
|
||||
}
|
||||
|
||||
/* The skip icons are drawn with CSS, so a theme restyles them without an image. */
|
||||
.audio-player-previous-icon,
|
||||
.audio-player-next-icon {
|
||||
@@ -2159,6 +2206,18 @@ html.audio-player-initializing .audio-player-container {
|
||||
font-variant-numeric: tabular-nums;
|
||||
}
|
||||
|
||||
/* The favorite and add-to-playlist actions of a queue item sit after its duration, so a queued track can be saved without starting it. A queue item without a track renders the container empty. */
|
||||
.audio-player-queue-item-actions {
|
||||
display: flex;
|
||||
flex: 0 0 auto;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
.audio-player-queue-item-actions:empty {
|
||||
display: none;
|
||||
}
|
||||
|
||||
/* The shared playlist picker of the add-to-playlist card actions. It reuses the application dialog surface on its own
|
||||
overlay, so it never takes part in the queue of the global dialog service. */
|
||||
.playlist-picker-overlay {
|
||||
|
||||
@@ -11,7 +11,8 @@
|
||||
* controls, the keyboard shortcuts, and the loading state a seek into a not yet downloaded position shows; the volume
|
||||
* and mute the controls change are persisted to the stored playback state of the user on their own. The expanded bar also
|
||||
* offers appending the current queue, in order, to one of the playlists of the user, through a theme hook with a built-in
|
||||
* fallback when the theme does not render it.
|
||||
* fallback when the theme does not render it. The condensed bar also renders the favorite toggle of the current track,
|
||||
* and every queue item carries its own favorite toggle, so a user can favorite what is playing and what is queued without leaving the bar.
|
||||
*
|
||||
* Public control API (window.LuminaAudioPlayer.instance)
|
||||
* ----------------------------------------
|
||||
@@ -723,6 +724,9 @@
|
||||
this.vuMeterAnalyser = null;
|
||||
// The raw theme sub templates of the dynamic content, read once with the bar elements and null when the theme does not provide them.
|
||||
this.subTemplates = { queueItem: null, playlistItem: null };
|
||||
// The track whose favorite toggle is currently shown in the bar, so a repeated render of the same track does
|
||||
// not rewrite the attributes the favorites script already hydrated.
|
||||
this.renderedFavoriteTrackId = null;
|
||||
// The playlists of the user, used by the control that adds the current queue to a playlist, and the in flight load of
|
||||
// them, so an expansion and a focus on the select share one request instead of racing each other.
|
||||
this.playlists = [];
|
||||
@@ -771,6 +775,7 @@
|
||||
this.isAvailable = true;
|
||||
|
||||
this.collectDomReferences();
|
||||
this.ensureFavoriteControl();
|
||||
this.ensureVuMeter();
|
||||
this.bindPlaybackControls();
|
||||
this.bindKeyboardShortcuts();
|
||||
@@ -781,8 +786,7 @@
|
||||
this.suppressInitialTransitions();
|
||||
this.applyHiddenState();
|
||||
this.releaseInitialHiddenState();
|
||||
// The volume and mute controls are rendered from the state the player already holds, so the bar shows the
|
||||
// default volume even before the stored state is read.
|
||||
// The volume and mute controls are rendered from the state the player already holds, so the bar shows the default volume even before the stored state is read.
|
||||
this.renderVolume();
|
||||
this.renderTransportState();
|
||||
|
||||
@@ -823,6 +827,7 @@
|
||||
this.dom.nowPlayingYear = query('[data-audio-player-now-playing-year]');
|
||||
this.dom.nowPlayingArtist = query('[data-audio-player-now-playing-artist]');
|
||||
this.dom.artworkCarousel = query('[data-audio-player-artwork-carousel]');
|
||||
this.dom.favorite = query('[data-audio-player-favorite]');
|
||||
// The sub template sources are read once, with the bar elements, because the theme markup is part of the bar
|
||||
// the player was bound to, and the player is never recreated by a navigation.
|
||||
this.subTemplates = {
|
||||
@@ -869,6 +874,75 @@
|
||||
this.vuMeterUnsubscribe = this.events.on(LuminaAudioPlayer.PlayerEvent.PREFERENCES_CHANGED, () => this.applyVuMeterSettings());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the favorite toggle of the condensed bar when the active theme does not render the hook.
|
||||
*
|
||||
* The favorite belongs to the bar rather than to a page, so it is created once with the bar elements and survives
|
||||
* every in-place navigation. A theme that renders the 'data-audio-player-favorite' hook owns where the control
|
||||
* sits; a theme that does not gets a built-in icon-only button between the repeat and mute controls.
|
||||
*/
|
||||
ensureFavoriteControl() {
|
||||
if (this.dom.favorite)
|
||||
return;
|
||||
const bar = this.container.querySelector('.audio-player-bar');
|
||||
if (!bar)
|
||||
return;
|
||||
const favorite = document.createElement('button');
|
||||
favorite.type = 'button';
|
||||
favorite.className = 'audio-player-button audio-player-favorite';
|
||||
favorite.dataset.audioPlayerFavorite = '';
|
||||
favorite.hidden = true;
|
||||
const icon = document.createElement('span');
|
||||
icon.className = 'audio-player-favorite-icon';
|
||||
icon.setAttribute('aria-hidden', 'true');
|
||||
favorite.appendChild(icon);
|
||||
// The volume control follows the repeat control in the bar. Inserting before it keeps the built-in favorite
|
||||
// between the two, exactly where the fallback markup places it.
|
||||
const volumeControl = this.container.querySelector('.audio-player-volume-control');
|
||||
if (volumeControl && volumeControl.parentNode === bar)
|
||||
bar.insertBefore(favorite, volumeControl);
|
||||
else
|
||||
bar.appendChild(favorite);
|
||||
this.dom.favorite = favorite;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows the favorite toggle of the current track in the condensed bar.
|
||||
*
|
||||
* The favorites script hydrates the toggle when the player writes the Id of a new track. Writing the same Id again
|
||||
* would make the script observe an attribute change and request a state it already shows, so an unchanged track
|
||||
* skips the write. The toggle is hidden while there is no track to favorite.
|
||||
*/
|
||||
renderFavorite() {
|
||||
const favorite = this.dom.favorite;
|
||||
if (!favorite)
|
||||
return;
|
||||
const descriptor = this.descriptor;
|
||||
const trackId = descriptor && descriptor.trackId ? descriptor.trackId : '';
|
||||
if (!trackId) {
|
||||
favorite.hidden = true;
|
||||
favorite.removeAttribute('data-favorite-toggle');
|
||||
favorite.removeAttribute('data-favorite-media-kind');
|
||||
favorite.removeAttribute('data-favorite-media-item-id');
|
||||
this.renderedFavoriteTrackId = null;
|
||||
return;
|
||||
}
|
||||
favorite.hidden = false;
|
||||
if (this.renderedFavoriteTrackId === trackId)
|
||||
return;
|
||||
// The player shows the toggle before the favorites script hydrates it, so it borrows the label of the action
|
||||
// once. The script replaces it with the add or remove label as soon as the state arrives.
|
||||
const favoritesStrings = window.favoritesConfiguration && window.favoritesConfiguration.strings;
|
||||
if (favoritesStrings && favoritesStrings.add && !favorite.hasAttribute('aria-label'))
|
||||
favorite.setAttribute('aria-label', favoritesStrings.add);
|
||||
// The favorites script owns the state, the labels and the pressed state from this point on. The attributes are
|
||||
// written together, so the observer sees one item change and hydrates it once.
|
||||
favorite.setAttribute('data-favorite-toggle', '');
|
||||
favorite.setAttribute('data-favorite-media-kind', 'MusicTrack');
|
||||
favorite.setAttribute('data-favorite-media-item-id', trackId);
|
||||
this.renderedFavoriteTrackId = trackId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the current preferences to the VU meter, so a settings change that arrives after the bar was built, or
|
||||
* a change saved on the settings page, takes effect without recreating the player.
|
||||
@@ -4156,6 +4230,7 @@
|
||||
// The artwork chosen from the carousel belongs to the album that was showing when it was chosen. A new track resets the selection to the cover of its own album.
|
||||
this.nowPlayingArtworkPath = descriptor ? (descriptor.coverArtPath || '') : '';
|
||||
this.renderNowPlayingDetails();
|
||||
this.renderFavorite();
|
||||
if (this.dom.totalTime)
|
||||
this.dom.totalTime.textContent = formatDuration(this.duration);
|
||||
this.renderTransportState();
|
||||
@@ -5100,6 +5175,7 @@
|
||||
* Renders one queue item, from the theme sub template when the theme provides one and from the built-in markup
|
||||
* otherwise. The rendered root always carries the 'data-audio-player-queue-entry-id' attribute, whatever the theme
|
||||
* markup is, so the queue list can start the entry the user clicked without the theme repeating the identity.
|
||||
* Every entry that references a track also carries its favorite toggle and add-to-playlist action, either from the theme sub template or from the built-in markup.
|
||||
* @param {Object} entry - The queue entry, with an entryId, a descriptor and a source.
|
||||
* @param {number} index - The zero based position of the item in the queue.
|
||||
* @param {number} totalItems - The number of items in the queue.
|
||||
@@ -5138,6 +5214,16 @@
|
||||
}
|
||||
if (!rendered)
|
||||
rendered = this.buildDefaultQueueItem(viewModel);
|
||||
// An older theme queue item may predate the favorite toggle. Appending the built-in one keeps every queue entry
|
||||
// favoritable, exactly as the condensed bar always falls back. querySelector does not match the root itself, so
|
||||
// a theme that puts the marker on the item root is checked with matches as well.
|
||||
const hasFavoriteToggle = rendered.matches('[data-favorite-toggle]')
|
||||
|| rendered.querySelector('[data-favorite-toggle]') !== null;
|
||||
if (!hasFavoriteToggle) {
|
||||
const favorite = this.buildQueueItemFavorite(viewModel);
|
||||
if (favorite)
|
||||
rendered.appendChild(favorite);
|
||||
}
|
||||
rendered.setAttribute('data-audio-player-queue-entry-id', entry.entryId);
|
||||
// The player writes the state classes on the item root itself, so a theme queue item template keeps the current and played distinction even when its markup omits the state classes.
|
||||
rendered.classList.toggle('is-current', viewModel.isCurrent);
|
||||
@@ -5181,8 +5267,33 @@
|
||||
entry.appendChild(cover);
|
||||
entry.appendChild(title);
|
||||
entry.appendChild(duration);
|
||||
entry.appendChild(this.buildQueueItemFavorite(viewModel));
|
||||
return entry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the favorite toggle of a queue item.
|
||||
*
|
||||
* Every queue entry that references a track can be favorited without starting it. The control is built through the
|
||||
* DOM, so the track title cannot break out of an attribute, and it carries the same attributes a theme sub template
|
||||
* renders, so both paths use the global favorites script.
|
||||
* @param {Object} viewModel - The queue item model, per {@link LuminaAudioPlayer.AudioPlayer#renderQueueItem}.
|
||||
* @returns {HTMLElement} The favorite container of the item.
|
||||
*/
|
||||
buildQueueItemFavorite(viewModel) {
|
||||
const actions = document.createElement('span');
|
||||
actions.className = 'audio-player-queue-item-actions';
|
||||
if (!viewModel.trackId)
|
||||
return actions;
|
||||
const favoriteToggle = document.createElement('button');
|
||||
favoriteToggle.type = 'button';
|
||||
favoriteToggle.className = 'card-favorite-toggle audio-player-queue-item-favorite';
|
||||
favoriteToggle.setAttribute('data-favorite-toggle', '');
|
||||
favoriteToggle.setAttribute('data-favorite-media-kind', 'MusicTrack');
|
||||
favoriteToggle.setAttribute('data-favorite-media-item-id', viewModel.trackId);
|
||||
actions.appendChild(favoriteToggle);
|
||||
return actions;
|
||||
}
|
||||
}
|
||||
|
||||
LuminaAudioPlayer.AudioPlayer = AudioPlayer;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/**
|
||||
* Favorite toggles: the hearts of the media cards (books, artists and albums, and the cards of the favorites page) and of the
|
||||
* track rows of the album page. The detail pages of an item have no toggle.
|
||||
* Favorite toggles: the hearts of the media cards (books, artists and albums, and the cards of the favorites page), of the
|
||||
* track rows of the album page, of the rows of a saved playlist, and of the current track and the queue items of the audio
|
||||
* player bar. The detail pages of an item have no toggle.
|
||||
*
|
||||
* A toggle is any focusable element, normally a button, that carries the `data-favorite-toggle` marker together with the
|
||||
* `data-favorite-media-kind` (`MusicArtist`, `MusicAlbum`, `MusicTrack` or `Book`) and `data-favorite-media-item-id`
|
||||
@@ -142,19 +143,46 @@
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds every toggle of the document that refers to an item.
|
||||
* Builds the index of every toggle of the document, grouped by item key.
|
||||
*
|
||||
* A page can render the same item more than once, for example the artist in a header and in a list, and every one of
|
||||
* them must show the same state. The toggles are looked up when a state is written, not remembered, so a toggle that
|
||||
* was added while a request was in flight is also updated and a removed one is not touched.
|
||||
* @param {string} key - The key of the item.
|
||||
* @returns {Array<Element>} The toggles of the item.
|
||||
* A status flush resolves many items at once, and every resolved item has to write its state on all of its toggles.
|
||||
* Reading the document once and grouping the toggles by key keeps that write linear in the number of toggles,
|
||||
* instead of rescanning the whole document for every item. The toggles a caller passes in are added as well, so an
|
||||
* element that is not attached to the document yet is still updated.
|
||||
* @param {Array<Element>} [additionalToggles] - The toggles to include besides the ones of the document.
|
||||
* @returns {Map<string, Array<Element>>} The toggles of each item key.
|
||||
*/
|
||||
function findToggles(key) {
|
||||
return collectToggles(document).filter(toggle => {
|
||||
function buildToggleIndex(additionalToggles) {
|
||||
const index = new Map();
|
||||
const seenToggles = new Set();
|
||||
const addToggle = toggle => {
|
||||
// The document pass and the list a caller passed in can hold the same element, so it is added only once.
|
||||
if (seenToggles.has(toggle))
|
||||
return;
|
||||
seenToggles.add(toggle);
|
||||
const item = readToggleItem(toggle);
|
||||
return item !== null && item.key === key;
|
||||
});
|
||||
if (!item)
|
||||
return;
|
||||
const toggles = index.get(item.key);
|
||||
if (toggles)
|
||||
toggles.push(toggle);
|
||||
else
|
||||
index.set(item.key, [toggle]);
|
||||
};
|
||||
collectToggles(document).forEach(addToggle);
|
||||
if (Array.isArray(additionalToggles))
|
||||
additionalToggles.forEach(addToggle);
|
||||
return index;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the toggles of an item from a built index.
|
||||
* @param {Map<string, Array<Element>>} index - The index built by {@link buildToggleIndex}.
|
||||
* @param {string} key - The key of the item.
|
||||
* @returns {Array<Element>} The toggles of the item, or an empty array when the item has none.
|
||||
*/
|
||||
function getIndexedToggles(index, key) {
|
||||
return index.get(key) || [];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -215,9 +243,12 @@
|
||||
* Writes the settled state of an item on every toggle that shows it.
|
||||
* @param {string} key - The key of the item.
|
||||
* @param {boolean} isFavorite - Whether the item is a favorite of the current user.
|
||||
* @param {Map<string, Array<Element>>} [index] - An already built toggle index, so a batch of items does not scan the document once per item.
|
||||
*/
|
||||
function applyFavoriteState(key, isFavorite) {
|
||||
findToggles(key).forEach(toggle => renderToggle(toggle, isFavorite ? 'favorited' : 'not-favorited', isFavorite));
|
||||
function applyFavoriteState(key, isFavorite, index) {
|
||||
const toggleIndex = index || buildToggleIndex();
|
||||
getIndexedToggles(toggleIndex, key)
|
||||
.forEach(toggle => renderToggle(toggle, isFavorite ? 'favorited' : 'not-favorited', isFavorite));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -353,13 +384,20 @@
|
||||
if (!checks.has(item.key))
|
||||
checks.set(item.key, { item, revision: getRevision(item.key) });
|
||||
}
|
||||
// Nothing to ask and nothing to write, so the document is not scanned for an empty batch.
|
||||
if (checks.size === 0)
|
||||
return;
|
||||
// One document pass serves every item of the batch, so hydrating a queue of N tracks with one toggle per item
|
||||
// costs one scan of the document instead of N scans, which is what made the previous lookup quadratic. A toggle
|
||||
// added while the requests are in flight is picked up by the observer of the document and hydrated on its own.
|
||||
const index = buildToggleIndex(toggles);
|
||||
await Promise.all(Array.from(checks.values(), check => queueStatusCheck(check.item).then(
|
||||
isFavorite => {
|
||||
if (getRevision(check.item.key) === check.revision && !mutationsInFlight.has(check.item.key))
|
||||
applyFavoriteState(check.item.key, isFavorite);
|
||||
applyFavoriteState(check.item.key, isFavorite, index);
|
||||
},
|
||||
() => {
|
||||
findToggles(check.item.key)
|
||||
getIndexedToggles(index, check.item.key)
|
||||
.filter(toggle => toggle.getAttribute('data-favorite-state') === 'loading')
|
||||
.forEach(toggle => renderToggle(toggle, 'failed', isPressed(toggle)));
|
||||
})));
|
||||
@@ -376,7 +414,8 @@
|
||||
*/
|
||||
async function changeFavoriteAsync(item, shouldFavorite) {
|
||||
const previousStates = new Map();
|
||||
findToggles(item.key).forEach(toggle => {
|
||||
const initialIndex = buildToggleIndex();
|
||||
getIndexedToggles(initialIndex, item.key).forEach(toggle => {
|
||||
previousStates.set(toggle, { state: toggle.getAttribute('data-favorite-state'), isFavorite: isPressed(toggle) });
|
||||
renderToggle(toggle, 'loading', isPressed(toggle));
|
||||
});
|
||||
@@ -412,7 +451,8 @@
|
||||
}
|
||||
|
||||
// A toggle added while the request was in flight has no remembered state, so it gets the state that was true before the change.
|
||||
findToggles(item.key).forEach(toggle => {
|
||||
const restoreIndex = buildToggleIndex();
|
||||
getIndexedToggles(restoreIndex, item.key).forEach(toggle => {
|
||||
const previous = previousStates.get(toggle) || { state: shouldFavorite ? 'not-favorited' : 'favorited', isFavorite: !shouldFavorite };
|
||||
const state = previous.state === 'favorited' || previous.state === 'not-favorited' ? previous.state : 'failed';
|
||||
renderToggle(toggle, state, previous.isFavorite);
|
||||
@@ -526,8 +566,15 @@
|
||||
continue;
|
||||
}
|
||||
mutation.addedNodes.forEach(node => {
|
||||
if (node.nodeType === Node.ELEMENT_NODE)
|
||||
collectToggles(node).forEach(toggle => toggles.add(toggle));
|
||||
if (node.nodeType !== Node.ELEMENT_NODE)
|
||||
return;
|
||||
collectToggles(node).forEach(toggle => {
|
||||
// A re-appended row, such as the rows of a playlist after a drag reorder, keeps the state it already
|
||||
// shows. Re-checking it would send a status request for a heart that is already settled on screen.
|
||||
const state = toggle.getAttribute('data-favorite-state');
|
||||
if (state !== 'favorited' && state !== 'not-favorited')
|
||||
toggles.add(toggle);
|
||||
});
|
||||
});
|
||||
}
|
||||
if (toggles.size > 0)
|
||||
|
||||
@@ -4,7 +4,9 @@
|
||||
* The module owns one modal playlist picker. A page opens it with the scope or the single track it wants to add. A scope
|
||||
* is resolved by the server into its ordered track Ids and appended in one request; a single track is appended through
|
||||
* the single entry route. The actual card and track menu items are small wrappers that give the menu item its scope and
|
||||
* call <c>LuminaPlaylists.showAddScopeDialogAsync</c>.
|
||||
* call <c>LuminaPlaylists.showAddScopeDialogAsync</c>. A theme template or a page script can also render a declarative
|
||||
* action, which needs no script of its own: an element with the `data-playlist-add` marker and the scope attributes
|
||||
* documented in the theming guide opens the same picker through the delegated click listener of this module.
|
||||
*
|
||||
* The scope is resolved on the server so the browser never enumerates a whole library, and the server can add all of its
|
||||
* tracks to the playlist atomically. A single track is already identified, so it uses the cheaper single-entry route
|
||||
@@ -15,6 +17,15 @@
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
// The layout may render this script on a page that already ran it, and the in-place navigation can evaluate scripts
|
||||
// again. A second set of listeners would send every request twice, so the first load owns the document.
|
||||
if (window.LuminaPlaylists)
|
||||
return;
|
||||
|
||||
// The declarative add action: an element that carries the marker and the scope attributes documented in the theming
|
||||
// guide. The listener reads them when the user clicks, so a page can render the action before its data is known.
|
||||
const ADD_TRIGGER_SELECTOR = '[data-playlist-add]';
|
||||
|
||||
// The layout renders the playlist id of the route as an empty Guid because the id is not known until the user picks
|
||||
// a playlist here. Replacing this sentinel keeps the route construction in one place and avoids hand-built paths.
|
||||
const EMPTY_GUID = '00000000-0000-0000-0000-000000000000';
|
||||
@@ -443,6 +454,63 @@
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the scope of a declarative add action from its data attributes.
|
||||
*
|
||||
* The attributes are read when the click happens, so a page or a theme may render the action before the scope is
|
||||
* known and write the values later, which is what the audio player does for the current track. A missing library Id
|
||||
* makes the action a no-op, because every scope is resolved inside one library.
|
||||
* @param {Element} trigger - The element that carries the `data-playlist-add` marker.
|
||||
* @returns {Object|null} The scope, or null when the action has no library to resolve the scope in.
|
||||
*/
|
||||
function readScopeFromTrigger(trigger) {
|
||||
const dataset = trigger && trigger.dataset ? trigger.dataset : null;
|
||||
if (!dataset || !dataset.playlistLibraryId)
|
||||
return null;
|
||||
return {
|
||||
libraryId: dataset.playlistLibraryId,
|
||||
trackId: dataset.playlistTrackId || '',
|
||||
albumId: dataset.playlistAlbumId || '',
|
||||
artistId: dataset.playlistArtistId || '',
|
||||
title: dataset.playlistTitle || '',
|
||||
addToPlaylistLabel: getStrings().addToPlaylist || '',
|
||||
returnFocus: trigger
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens the picker for a declarative add action, through event delegation.
|
||||
*
|
||||
* The module binds one listener on the document, so a control works anywhere in the swapped page content and inside
|
||||
* the player bar, including controls that a page or a theme renders later. The default action is prevented, so a link
|
||||
* does not navigate, and further bubbling is stopped once the event reaches the document. A handler on an ancestor
|
||||
* that already ran before the document listener is not undone; the player queue ignores controls inside its entries,
|
||||
* and a menu that hosts such an action closes itself in its own handler.
|
||||
* @param {MouseEvent} event - The click event.
|
||||
*/
|
||||
function handleAddTriggerClick(event) {
|
||||
const target = event.target;
|
||||
if (!target || typeof target.closest !== 'function')
|
||||
return;
|
||||
const trigger = target.closest(ADD_TRIGGER_SELECTOR);
|
||||
if (!trigger)
|
||||
return;
|
||||
// A disabled control explains itself through its text; opening a picker from it would be a surprising action.
|
||||
if (trigger.disabled === true || trigger.getAttribute('aria-disabled') === 'true')
|
||||
return;
|
||||
const scope = readScopeFromTrigger(trigger);
|
||||
if (!scope)
|
||||
return;
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
showAddScopeDialogAsync(scope).catch(error => {
|
||||
console.error('Playlists: opening the add-to-playlist picker failed.', error);
|
||||
showNotification(getStrings().tracksAddFailed, true);
|
||||
});
|
||||
}
|
||||
|
||||
document.addEventListener('click', handleAddTriggerClick);
|
||||
|
||||
window.LuminaPlaylists = {
|
||||
showAddScopeDialogAsync
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user