(function ($, document) {
function getNewCollectionPanel(createIfNeeded) {
var panel = $('.newCollectionPanel');
if (createIfNeeded && !panel.length) {
var html = '';
html += '
';
html += '
';
html += '';
html += '
';
html += '
';
panel = $(html).appendTo(document.body).trigger('create').find('.newCollectionPanel');
$('#selectCollectionToAddTo', panel).on('change', function () {
if (this.value) {
$('.newCollectionInfo', panel).hide();
$('#txtNewCollectionName', panel).removeAttr('required');
} else {
$('.newCollectionInfo', panel).show();
$('#txtNewCollectionName', panel).attr('required', 'required');
}
});
$('.newCollectionForm', panel).off('submit', onSubmit).on('submit', onSubmit);
}
return panel;
}
function showCollectionPanel(items) {
var panel = getNewCollectionPanel(true).panel('toggle');
$('.fldSelectedItemIds', panel).val(items.join(','));
require(['jqmicons']);
if (items.length) {
$('.fldSelectCollection', panel).show();
populateCollections(panel);
} else {
$('.fldSelectCollection', panel).hide();
$('#selectCollectionToAddTo', panel).html('').val('').selectmenu('refresh').trigger('change');
}
}
function populateCollections(panel) {
var select = $('#selectCollectionToAddTo', panel);
$('.newCollectionInfo', panel).hide();
var options = {
Recursive: true,
IncludeItemTypes: "BoxSet",
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 onSubmit() {
Dashboard.showLoadingMsg();
var panel = getNewCollectionPanel(false);
var collectionId = $('#selectCollectionToAddTo', panel).val();
if (collectionId) {
addToCollection(panel, collectionId);
} else {
createCollection(panel);
}
return false;
}
$(document).on('pageinit', ".collectionEditorPage", function () {
var page = this;
// The button is created dynamically
$(page).on('click', '.btnNewCollection', function () {
BoxSetEditor.showPanel([]);
});
});
function redirectToCollection(id) {
var context = getParameterByName('context');
ApiClient.getItem(Dashboard.getCurrentUserId(), id).done(function (item) {
Dashboard.navigate(LibraryBrowser.getHref(item, context));
});
}
function createCollection(panel) {
var url = ApiClient.getUrl("Collections", {
Name: $('#txtNewCollectionName', panel).val(),
IsLocked: !$('#chkEnableInternetMetadata', panel).checked(),
Ids: $('.fldSelectedItemIds', panel).val() || ''
//ParentId: getParameterByName('parentId') || LibraryMenu.getTopParentId()
});
ApiClient.ajax({
type: "POST",
url: url,
dataType: "json"
}).done(function (result) {
Dashboard.hideLoadingMsg();
var id = result.Id;
panel.panel('toggle');
redirectToCollection(id);
});
}
function addToCollection(panel, id) {
var url = ApiClient.getUrl("Collections/" + id + "/Items", {
Ids: $('.fldSelectedItemIds', panel).val() || ''
});
ApiClient.ajax({
type: "POST",
url: url
}).done(function () {
Dashboard.hideLoadingMsg();
panel.panel('toggle');
Dashboard.alert(Globalize.translate('MessageItemsAdded'));
});
}
window.BoxSetEditor = {
showPanel: function (items) {
showCollectionPanel(items);
},
supportsAddingToCollection: function (item) {
var invalidTypes = ['Person', 'Genre', 'MusicGenre', 'Studio', 'GameGenre', 'BoxSet', 'Playlist', 'UserView', 'CollectionFolder', 'Audio', 'Episode'];
return item.LocationType == 'FileSystem' && !item.CollectionType && invalidTypes.indexOf(item.Type) == -1 && item.MediaType != 'Photo';
}
};
})(jQuery, document);