(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() {
Dashboard.showLoadingMsg();
$('.newPlaylistPanel').panel('destroy').remove();
var html = '
';
html += '
' + Globalize.translate('HeaderAddToPlaylist') + '
';
html += '
';
html += '
';
html += '
';
$(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 = '';
selectHtml += result.Items.map(function (o) {
return '';
}).join('');
select.html(selectHtml).selectmenu('refresh');
select.val(lastPlaylistId || '').selectmenu('refresh').trigger('change');
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,
IncludeItemTypes: "Playlist",
SortBy: 'SortName'
};
ApiClient.getItems(Dashboard.getCurrentUserId(), options).done(function (result) {
var html = '';
html += '';
html += result.Items.map(function (i) {
return '';
});
select.html(html).val('').selectmenu('refresh').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", {
Ids: $('.fldSelectedItemIds', panel).val() || '',
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) {
showNewPlaylistPanel(items);
},
supportsPlaylists: function (item) {
if (item.Type == 'Program') {
return false;
}
return item.RunTimeTicks || item.IsFolder || item.Type == "Genre" || item.Type == "MusicGenre" || item.Type == "MusicArtist";
}
};
})(jQuery, document);