jellyfin-web/dashboard-ui/scripts/playlistmanager.js

225 lines
6.0 KiB
JavaScript
Raw Normal View History

(function ($, document) {
var lastPlaylistId = '';
function redirectToPlaylist(id) {
var context = getParameterByName('context');
ApiClient.getItem(Dashboard.getCurrentUserId(), id).done(function (item) {
Dashboard.navigate(LibraryBrowser.getHref(item, context));
});
}
function onAddToPlaylistFormSubmit() {
Dashboard.showLoadingMsg();
var panel = $(this).parents('.newPlaylistPanel');
var playlistId = $('select.selectPlaylistToAddTo', panel).val();
if (playlistId) {
lastPlaylistId = playlistId;
addToPlaylist(panel, playlistId);
} else {
createPlaylist(panel);
}
return false;
}
function getNewPlaylistPanel() {
2014-08-14 06:24:30 -07:00
Dashboard.showLoadingMsg();
$('.newPlaylistPanel').panel('destroy').remove();
var html = '<div data-role="panel" data-position="right" data-display="overlay" class="newPlaylistPanel" data-position-fixed="true" data-theme="a">';
html += '<h3>' + Globalize.translate('HeaderAddToPlaylist') + '</h3>';
html += '<br />';
html += '<form class="addToPlaylistForm">';
var selectId = 'selectPlaylistToAddTo' + new Date().getTime();
html += '<div>';
html += '<label for="' + selectId + '">' + Globalize.translate('LabelSelectPlaylist') + '</label>';
html += '<select id="' + selectId + '" class="selectPlaylistToAddTo" data-mini="true">';
html += '</select>';
html += '</div>';
html += '<br />';
html += '<div class="fldNewPlaylist" style="display:none;">';
html += '<label for="txtNewPlaylistName">' + Globalize.translate('LabelName') + '</label>';
html += '<input type="text" id="txtNewPlaylistName" />';
html += '</div>';
html += '<p>';
html += '<input class="fldSelectedItemIds" type="hidden" />';
html += '<button type="submit" data-icon="plus" data-mini="true" data-theme="b">' + Globalize.translate('ButtonSubmit') + '</button>';
html += '</p>';
html += '</form>';
html += '</div>';
$(document.body).append(html);
var elem = $('.newPlaylistPanel').panel({}).trigger('create').on("panelclose", function () {
$(this).off("panelclose").remove();
});
var select = $('#' + selectId, elem).on('change', function () {
if (this.value) {
$('.fldNewPlaylist', elem).hide();
$('input', elem).removeAttr('required');
} else {
$('.fldNewPlaylist', elem).show();
$('input', elem).attr('required', 'required');
}
}).trigger('change');
ApiClient.getItems(Dashboard.getCurrentUserId(), {
IncludeItemTypes: 'Playlist',
recursive: true,
SortBy: 'SortName'
}).done(function (result) {
var selectHtml = '<option value="">' + Globalize.translate('OptionNewPlaylist') + '</option>';
selectHtml += result.Items.map(function (o) {
return '<option value="' + o.Id + '">' + o.Name + '</option>';
}).join('');
2015-09-03 10:01:51 -07:00
select.html(selectHtml);
2014-08-21 19:24:38 -07:00
2015-09-03 10:01:51 -07:00
select.val(lastPlaylistId || '').trigger('change');
2014-08-14 06:24:30 -07:00
Dashboard.hideLoadingMsg();
});
$('form', elem).on('submit', onAddToPlaylistFormSubmit);
return elem;
}
function showNewPlaylistPanel(items) {
var panel = getNewPlaylistPanel().panel('toggle');
$('.fldSelectedItemIds', panel).val(items.join(','));
populatePlaylists(panel);
}
function populatePlaylists(panel) {
var select = $('select.selectPlaylistToAddTo', panel);
if (!select.length) {
$('#txtNewPlaylistName', panel).val('').focus();
return;
}
$('.newPlaylistInfo', panel).hide();
var options = {
Recursive: true,
2014-08-24 08:48:06 -07:00
IncludeItemTypes: "Playlist",
SortBy: 'SortName'
};
ApiClient.getItems(Dashboard.getCurrentUserId(), options).done(function (result) {
var html = '';
html += '<option value="">' + Globalize.translate('OptionNewPlaylist') + '</option>';
html += result.Items.map(function (i) {
return '<option value="' + i.Id + '">' + i.Name + '</option>';
});
2015-09-03 10:01:51 -07:00
select.html(html).val('').trigger('change');
});
}
function createPlaylist(panel) {
var url = ApiClient.getUrl("Playlists", {
Name: $('#txtNewPlaylistName', panel).val(),
Ids: $('.fldSelectedItemIds', panel).val() || '',
userId: Dashboard.getCurrentUserId()
});
ApiClient.ajax({
type: "POST",
url: url,
dataType: "json"
}).done(function (result) {
Dashboard.hideLoadingMsg();
var id = result.Id;
panel.panel('toggle');
redirectToPlaylist(id);
});
}
function addToPlaylist(panel, id) {
var url = ApiClient.getUrl("Playlists/" + id + "/Items", {
2014-08-21 19:24:38 -07:00
Ids: $('.fldSelectedItemIds', panel).val() || '',
2014-08-24 08:48:06 -07:00
userId: Dashboard.getCurrentUserId()
});
ApiClient.ajax({
type: "POST",
url: url
}).done(function () {
Dashboard.hideLoadingMsg();
panel.panel('toggle');
Dashboard.alert(Globalize.translate('MessageAddedToPlaylistSuccess'));
});
}
window.PlaylistManager = {
showPanel: function (items) {
2015-09-05 11:05:29 -07:00
require(['jqmpanel'], function() {
showNewPlaylistPanel(items);
});
},
supportsPlaylists: function (item) {
2015-04-11 18:38:38 -07:00
if (item.Type == 'Program') {
return false;
}
2014-12-18 21:20:07 -07:00
return item.RunTimeTicks || item.IsFolder || item.Type == "Genre" || item.Type == "MusicGenre" || item.Type == "MusicArtist";
}
};
})(jQuery, document);