mirror of
https://github.com/jellyfin/jellyfin-web
synced 2025-03-30 19:56:21 +00:00
Use ts sdk for playlist editor
This commit is contained in:
parent
fa16daabb0
commit
0163da2df8
1 changed files with 76 additions and 74 deletions
|
@ -1,10 +1,14 @@
|
||||||
|
import { BaseItemKind } from '@jellyfin/sdk/lib/generated-client/models/base-item-kind';
|
||||||
|
import { ItemSortBy } from '@jellyfin/sdk/lib/generated-client/models/item-sort-by';
|
||||||
|
import { getItemsApi } from '@jellyfin/sdk/lib/utils/api/items-api';
|
||||||
|
import { getPlaylistsApi } from '@jellyfin/sdk/lib/utils/api/playlists-api';
|
||||||
import escapeHtml from 'escape-html';
|
import escapeHtml from 'escape-html';
|
||||||
import type { ApiClient } from 'jellyfin-apiclient';
|
|
||||||
|
|
||||||
import dom from 'scripts/dom';
|
import dom from 'scripts/dom';
|
||||||
import globalize from 'scripts/globalize';
|
import globalize from 'scripts/globalize';
|
||||||
import * as userSettings from 'scripts/settings/userSettings';
|
import * as userSettings from 'scripts/settings/userSettings';
|
||||||
import { PluginType } from 'types/plugin';
|
import { PluginType } from 'types/plugin';
|
||||||
|
import { toApi } from 'utils/jellyfin-apiclient/compat';
|
||||||
|
|
||||||
import dialogHelper from '../dialogHelper/dialogHelper';
|
import dialogHelper from '../dialogHelper/dialogHelper';
|
||||||
import loading from '../loading/loading';
|
import loading from '../loading/loading';
|
||||||
|
@ -40,16 +44,15 @@ function onSubmit(this: HTMLElement, e: Event) {
|
||||||
|
|
||||||
if (panel) {
|
if (panel) {
|
||||||
const playlistId = panel.querySelector<HTMLSelectElement>('#selectPlaylistToAddTo')?.value;
|
const playlistId = panel.querySelector<HTMLSelectElement>('#selectPlaylistToAddTo')?.value;
|
||||||
const apiClient = ServerConnections.getApiClient(currentServerId);
|
|
||||||
|
|
||||||
if (playlistId) {
|
if (playlistId) {
|
||||||
userSettings.set('playlisteditor-lastplaylistid', playlistId);
|
userSettings.set('playlisteditor-lastplaylistid', playlistId);
|
||||||
addToPlaylist(apiClient, panel, playlistId)
|
addToPlaylist(panel, playlistId)
|
||||||
?.catch(err => {
|
?.catch(err => {
|
||||||
console.error('[PlaylistEditor] Failed to add to playlist %s', playlistId, err);
|
console.error('[PlaylistEditor] Failed to add to playlist %s', playlistId, err);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
createPlaylist(apiClient, panel)
|
createPlaylist(panel)
|
||||||
?.catch(err => {
|
?.catch(err => {
|
||||||
console.error('[PlaylistEditor] Failed to create playlist', err);
|
console.error('[PlaylistEditor] Failed to create playlist', err);
|
||||||
});
|
});
|
||||||
|
@ -62,40 +65,41 @@ function onSubmit(this: HTMLElement, e: Event) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function createPlaylist(apiClient: ApiClient, dlg: DialogElement) {
|
function createPlaylist(dlg: DialogElement) {
|
||||||
|
const apiClient = ServerConnections.getApiClient(currentServerId);
|
||||||
|
const api = toApi(apiClient);
|
||||||
|
|
||||||
|
const itemIds = dlg.querySelector<HTMLInputElement>('.fldSelectedItemIds')?.value || '';
|
||||||
|
|
||||||
loading.show();
|
loading.show();
|
||||||
|
|
||||||
const url = apiClient.getUrl('Playlists', {
|
return getPlaylistsApi(api)
|
||||||
Name: dlg.querySelector<HTMLInputElement>('#txtNewPlaylistName')?.value,
|
.createPlaylist({
|
||||||
Ids: dlg.querySelector<HTMLInputElement>('.fldSelectedItemIds')?.value || '',
|
name: dlg.querySelector<HTMLInputElement>('#txtNewPlaylistName')?.value,
|
||||||
|
ids: itemIds.split(','),
|
||||||
userId: apiClient.getCurrentUserId()
|
userId: apiClient.getCurrentUserId()
|
||||||
});
|
})
|
||||||
|
.then(result => {
|
||||||
return apiClient.ajax({
|
|
||||||
type: 'POST',
|
|
||||||
url: url,
|
|
||||||
dataType: 'json',
|
|
||||||
contentType: 'application/json'
|
|
||||||
}).then(result => {
|
|
||||||
loading.hide();
|
loading.hide();
|
||||||
|
|
||||||
const id = result.Id;
|
|
||||||
dlg.submitted = true;
|
dlg.submitted = true;
|
||||||
dialogHelper.close(dlg);
|
dialogHelper.close(dlg);
|
||||||
redirectToPlaylist(apiClient, id);
|
|
||||||
|
redirectToPlaylist(result.data.Id);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function redirectToPlaylist(apiClient: ApiClient, id: string) {
|
function redirectToPlaylist(id: string | undefined) {
|
||||||
appRouter.showItem(id, apiClient.serverId());
|
appRouter.showItem(id, currentServerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
function addToPlaylist(apiClient: ApiClient, dlg: DialogElement, id: string) {
|
function addToPlaylist(dlg: DialogElement, id: string) {
|
||||||
|
const apiClient = ServerConnections.getApiClient(currentServerId);
|
||||||
|
const api = toApi(apiClient);
|
||||||
const itemIds = dlg.querySelector<HTMLInputElement>('.fldSelectedItemIds')?.value || '';
|
const itemIds = dlg.querySelector<HTMLInputElement>('.fldSelectedItemIds')?.value || '';
|
||||||
|
|
||||||
if (id === 'queue') {
|
if (id === 'queue') {
|
||||||
playbackManager.queue({
|
playbackManager.queue({
|
||||||
serverId: apiClient.serverId(),
|
serverId: currentServerId,
|
||||||
ids: itemIds.split(',')
|
ids: itemIds.split(',')
|
||||||
});
|
});
|
||||||
dlg.submitted = true;
|
dlg.submitted = true;
|
||||||
|
@ -105,16 +109,13 @@ function addToPlaylist(apiClient: ApiClient, dlg: DialogElement, id: string) {
|
||||||
|
|
||||||
loading.show();
|
loading.show();
|
||||||
|
|
||||||
const url = apiClient.getUrl(`Playlists/${id}/Items`, {
|
return getPlaylistsApi(api)
|
||||||
Ids: itemIds,
|
.addItemToPlaylist({
|
||||||
|
playlistId: id,
|
||||||
|
ids: itemIds.split(','),
|
||||||
userId: apiClient.getCurrentUserId()
|
userId: apiClient.getCurrentUserId()
|
||||||
});
|
})
|
||||||
|
.then(() => {
|
||||||
return apiClient.ajax({
|
|
||||||
type: 'POST',
|
|
||||||
url: url
|
|
||||||
|
|
||||||
}).then(() => {
|
|
||||||
loading.hide();
|
loading.hide();
|
||||||
|
|
||||||
dlg.submitted = true;
|
dlg.submitted = true;
|
||||||
|
@ -137,17 +138,18 @@ function populatePlaylists(editorOptions: PlaylistEditorOptions, panel: DialogEl
|
||||||
|
|
||||||
panel.querySelector('.newPlaylistInfo')?.classList.add('hide');
|
panel.querySelector('.newPlaylistInfo')?.classList.add('hide');
|
||||||
|
|
||||||
const options = {
|
|
||||||
Recursive: true,
|
|
||||||
IncludeItemTypes: 'Playlist',
|
|
||||||
SortBy: 'SortName',
|
|
||||||
EnableTotalRecordCount: false
|
|
||||||
};
|
|
||||||
|
|
||||||
const apiClient = ServerConnections.getApiClient(currentServerId);
|
const apiClient = ServerConnections.getApiClient(currentServerId);
|
||||||
|
const api = toApi(apiClient);
|
||||||
const SyncPlay = pluginManager.firstOfType(PluginType.SyncPlay)?.instance;
|
const SyncPlay = pluginManager.firstOfType(PluginType.SyncPlay)?.instance;
|
||||||
|
|
||||||
return apiClient.getItems(apiClient.getCurrentUserId(), options).then(result => {
|
return getItemsApi(api)
|
||||||
|
.getItems({
|
||||||
|
userId: apiClient.getCurrentUserId(),
|
||||||
|
includeItemTypes: [ BaseItemKind.Playlist ],
|
||||||
|
sortBy: [ ItemSortBy.SortName ],
|
||||||
|
recursive: true
|
||||||
|
})
|
||||||
|
.then(({ data }) => {
|
||||||
let html = '';
|
let html = '';
|
||||||
|
|
||||||
if ((editorOptions.enableAddToPlayQueue !== false && playbackManager.isPlaying()) || SyncPlay?.Manager.isSyncPlayEnabled()) {
|
if ((editorOptions.enableAddToPlayQueue !== false && playbackManager.isPlaying()) || SyncPlay?.Manager.isSyncPlayEnabled()) {
|
||||||
|
@ -156,7 +158,7 @@ function populatePlaylists(editorOptions: PlaylistEditorOptions, panel: DialogEl
|
||||||
|
|
||||||
html += `<option value="">${globalize.translate('OptionNew')}</option>`;
|
html += `<option value="">${globalize.translate('OptionNew')}</option>`;
|
||||||
|
|
||||||
html += result.Items?.map(i => {
|
html += data.Items?.map(i => {
|
||||||
return `<option value="${i.Id}">${escapeHtml(i.Name)}</option>`;
|
return `<option value="${i.Id}">${escapeHtml(i.Name)}</option>`;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue