(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 += '
'; var selectId = 'selectPlaylistToAddTo' + new Date().getTime(); html += '
'; html += ''; html += ''; html += '
'; html += '
'; html += ''; html += '

'; html += ''; html += ''; 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'); }); } //$(document).on('pageinit', ".playlistEditorPage", function () { // var page = this; // $('.itemsContainer', page).on('itemsrendered', function () { // $('.btnNewPlaylist', page).off('click.newplaylistpanel').on('click.newplaylistpanel', function () { // showNewPlaylistPanel(page, []); // }); // }); // $('#selectPlaylistToAddTo', page).on('change', function () { // if (this.value) { // $('.newPlaylistInfo', page).hide(); // $('#txtNewPlaylistName', page).removeAttr('required'); // } else { // $('.newPlaylistInfo', page).show(); // $('#txtNewPlaylistName', page).attr('required', 'required'); // } // }); //}); 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) { return item.SupportsPlaylists; } }; })(jQuery, document);