/**
* Shared add-to-playlist actions for the music library, artist and album cards, and for the player queue.
*
* 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 LuminaPlaylists.showAddScopeDialogAsync.
*
* 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
* instead. The picker is built here, not rendered by each page, so the cards, the album and artist pages and the player
* all share one dialog, one focus behavior and one error path. After a successful add the module dispatches the
* `lumina:playlist-updated` DOM event: an open playlist page can refresh itself, while the callers stay decoupled from it.
*/
(function () {
'use strict';
// 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';
// The picker ids must be unique per dialog, because the toggle and the title are linked by id. The sequence makes a
// second dialog safe even if a previous one was removed abruptly.
let pickerSequence = 0;
// Only one picker may be open: two overlays would compete for Escape and focus, and the user could add to two
// different playlists. This holds the close function and the overlay of the current one.
let activePicker = null;
/**
* Gets the playlist configuration the layout renders for every page.
*
* The configuration is the only contract between the layout and this module: it carries the routes and the localized
* labels. Reading it lazily, instead of capturing it when the script loads, lets the layout render the configuration
* after this deferred script has been parsed.
* @returns {Object|null} The configuration, or null when the layout did not render it.
*/
function getConfiguration() {
return window.playlistConfiguration || null;
}
/**
* Gets the localized strings of the playlist actions, with empty values when the layout did not render them.
* @returns {Object} The localized strings.
*/
function getStrings() {
const configuration = getConfiguration();
return configuration && configuration.strings ? configuration.strings : {};
}
/**
* Shows a success or error notification through the global notification service, with a console fallback.
*
* The notification service is provided by the layout, but the module must not assume it exists: it is also loaded
* on minimal pages and in test hosts. Falling back to the console keeps a failure visible without throwing from a
* UI action, which would leave the picker stuck in its busy state.
* @param {string} message - The localized message to show.
* @param {boolean} isError - True to show an error, false for a success.
*/
function showNotification(message, isError) {
if (!message)
return;
if (typeof notificationService !== 'undefined' && typeof NotificationType !== 'undefined') {
notificationService.show(message, isError ? NotificationType.ERROR : NotificationType.SUCCESS);
return;
}
if (isError)
console.error(`Playlists: ${message}`);
else
console.info(`Playlists: ${message}`);
}
/**
* Builds the standard application combo box used to pick a playlist.
*
* The application has one combobox contract: the markup below is styled by the global stylesheet, and the global
* site script handles opening, closing and copying the clicked option into the selected text. The module must not
* add its own selection logic on top of that, or the two would fight. It builds the same structure a Razor view
* would render, so the picker looks and behaves like every other select in the application.
* @param {string} id - The unique root Id of the combo box.
* @param {string} placeholder - The placeholder shown before a playlist is selected.
* @returns {Object} The combo box elements.
*/
function createCombobox(id, placeholder) {
const root = document.createElement('div');
root.className = 'enlightenment-combobox w-100';
root.id = id;
// The shine elements are purely decorative. They are kept when the options are rebuilt, so the dropdown does not
// flicker or lose its inset shadow while the playlist request refreshes the rows.
const shine = document.createElement('div');
shine.className = 'shine-effect';
shine.style.top = '1px';
root.appendChild(shine);
const toggle = document.createElement('input');
toggle.type = 'checkbox';
toggle.className = 'enlightenment-toggle-checkbox';
toggle.id = `${id}-toggle`;
root.appendChild(toggle);
const label = document.createElement('label');
label.className = 'enlightenment-toggle';
label.htmlFor = toggle.id;
const selected = document.createElement('span');
selected.className = 'enlightenment-selected-text';
selected.setAttribute('data-value', '');
selected.textContent = placeholder;
label.appendChild(selected);
root.appendChild(label);
const dropdown = document.createElement('div');
dropdown.className = 'enlightenment-dropdown';
const dropdownShine = document.createElement('div');
dropdownShine.className = 'shine-effect';
dropdownShine.style.top = '-4px';
dropdown.appendChild(dropdownShine);
// The disabled first option is the "nothing selected" row the global combobox script expects. It also gives
// the dropdown a stable first child when the user has no playlists yet.
const firstOption = document.createElement('div');
firstOption.className = 'enlightenment-option first-option disabled';
firstOption.setAttribute('data-value', '');
firstOption.textContent = placeholder;
dropdown.appendChild(firstOption);
root.appendChild(dropdown);
return { root, selected, dropdown, toggle };
}
/**
* Fills the playlist combo box with the playlists of the user.
*
* Only the option rows are replaced. The first option and the decorative elements are kept because the global site
* script and the stylesheet rely on them, and rebuilding the whole dropdown would reset its open state and focus.
* @param {Object} combobox - The combo box elements.
* @param {Array