jellyfin-web/dashboard-ui/components/collectioneditor/collectioneditor.js

228 lines
6.3 KiB
JavaScript
Raw Normal View History

2016-01-30 12:31:22 -07:00
define(['paperdialoghelper', 'paper-checkbox', 'paper-dialog', 'paper-input'], function (paperDialogHelper) {
2015-10-13 22:46:11 -07:00
function onSubmit() {
Dashboard.showLoadingMsg();
var panel = $(this).parents('paper-dialog')[0];
var collectionId = $('#selectCollectionToAddTo', panel).val();
if (collectionId) {
addToCollection(panel, collectionId);
} else {
createCollection(panel);
}
return false;
}
function createCollection(dlg) {
var url = ApiClient.getUrl("Collections", {
Name: $('#txtNewCollectionName', dlg).val(),
IsLocked: !$('#chkEnableInternetMetadata', dlg).checked(),
Ids: $('.fldSelectedItemIds', dlg).val() || ''
//ParentId: getParameterByName('parentId') || LibraryMenu.getTopParentId()
});
ApiClient.ajax({
type: "POST",
url: url,
dataType: "json"
2015-12-14 08:43:03 -07:00
}).then(function (result) {
2015-10-13 22:46:11 -07:00
Dashboard.hideLoadingMsg();
var id = result.Id;
2015-12-14 08:43:03 -07:00
paperDialogHelper.close(dlg);
2015-10-13 22:46:11 -07:00
redirectToCollection(id);
});
}
function redirectToCollection(id) {
var context = getParameterByName('context');
2015-12-14 08:43:03 -07:00
ApiClient.getItem(Dashboard.getCurrentUserId(), id).then(function (item) {
2015-10-13 22:46:11 -07:00
Dashboard.navigate(LibraryBrowser.getHref(item, context));
});
}
function addToCollection(dlg, id) {
var url = ApiClient.getUrl("Collections/" + id + "/Items", {
Ids: $('.fldSelectedItemIds', dlg).val() || ''
});
ApiClient.ajax({
type: "POST",
url: url
2015-12-14 08:43:03 -07:00
}).then(function () {
2015-10-13 22:46:11 -07:00
Dashboard.hideLoadingMsg();
2015-12-14 08:43:03 -07:00
paperDialogHelper.close(dlg);
2015-10-13 22:46:11 -07:00
Dashboard.alert(Globalize.translate('MessageItemsAdded'));
});
}
function onDialogClosed() {
$(this).remove();
Dashboard.hideLoadingMsg();
}
function populateCollections(panel) {
Dashboard.showLoadingMsg();
var select = $('#selectCollectionToAddTo', panel);
$('.newCollectionInfo', panel).hide();
var options = {
Recursive: true,
IncludeItemTypes: "BoxSet",
SortBy: "SortName"
};
2015-12-14 08:43:03 -07:00
ApiClient.getItems(Dashboard.getCurrentUserId(), options).then(function (result) {
2015-10-13 22:46:11 -07:00
var html = '';
html += '<option value="">' + Globalize.translate('OptionNewCollection') + '</option>';
html += result.Items.map(function (i) {
return '<option value="' + i.Id + '">' + i.Name + '</option>';
});
select.html(html).val('').trigger('change');
Dashboard.hideLoadingMsg();
});
}
function getEditorHtml() {
var html = '';
2015-12-15 13:10:41 -07:00
html += '<form class="newCollectionForm" style="margin:auto;">';
2015-10-13 22:46:11 -07:00
html += '<div class="fldSelectCollection">';
html += '<label for="selectCollectionToAddTo">' + Globalize.translate('LabelSelectCollection') + '</label>';
html += '<select id="selectCollectionToAddTo" data-mini="true"></select>';
html += '</div>';
html += '<div class="newCollectionInfo">';
html += '<div>';
html += '<paper-input type="text" id="txtNewCollectionName" required="required" label="' + Globalize.translate('LabelName') + '"></paper-input>';
html += '<div class="fieldDescription">' + Globalize.translate('NewCollectionNameExample') + '</div>';
html += '</div>';
html += '<br />';
html += '<br />';
html += '<div>';
html += '<paper-checkbox id="chkEnableInternetMetadata">' + Globalize.translate('OptionSearchForInternetMetadata') + '</paper-checkbox>';
html += '</div>';
// newCollectionInfo
html += '</div>';
html += '<br />';
html += '<div>';
html += '<button type="submit" class="clearButton" data-role="none"><paper-button raised class="submit block">' + Globalize.translate('ButtonOk') + '</paper-button></button>';
html += '</div>';
html += '<input type="hidden" class="fldSelectedItemIds" />';
html += '</form>';
return html;
}
function initEditor(content, items) {
$('#selectCollectionToAddTo', content).on('change', function () {
if (this.value) {
$('.newCollectionInfo', content).hide();
$('#txtNewCollectionName', content).removeAttr('required');
} else {
$('.newCollectionInfo', content).show();
$('#txtNewCollectionName', content).attr('required', 'required');
}
});
$('.newCollectionForm', content).off('submit', onSubmit).on('submit', onSubmit);
$('.fldSelectedItemIds', content).val(items.join(','));
if (items.length) {
$('.fldSelectCollection', content).show();
populateCollections(content);
} else {
$('.fldSelectCollection', content).hide();
$('#selectCollectionToAddTo', content).html('').val('').trigger('change');
}
}
2015-10-14 07:47:38 -07:00
function collectioneditor() {
2015-10-13 22:46:11 -07:00
var self = this;
self.show = function (items) {
items = items || [];
2015-12-14 08:43:03 -07:00
var dlg = paperDialogHelper.createDialog({
size: 'small'
});
2015-10-13 22:46:11 -07:00
2016-01-30 13:59:09 -07:00
dlg.classList.add('ui-body-b');
dlg.classList.add('background-theme-b');
2015-12-14 08:43:03 -07:00
var html = '';
var title = items.length ? Globalize.translate('HeaderAddToCollection') : Globalize.translate('HeaderNewCollection');
2015-10-13 22:46:11 -07:00
2015-12-15 13:10:41 -07:00
html += '<div class="dialogHeader">';
2016-01-30 13:59:09 -07:00
html += '<paper-icon-button icon="close" class="btnCancel" tabindex="-1"></paper-icon-button>';
2015-12-15 13:10:41 -07:00
html += '<div class="dialogHeaderTitle">';
html += title;
html += '</div>';
html += '</div>';
2015-10-13 22:46:11 -07:00
2015-12-14 08:43:03 -07:00
html += getEditorHtml();
2015-10-13 22:46:11 -07:00
2015-12-14 08:43:03 -07:00
dlg.innerHTML = html;
document.body.appendChild(dlg);
2015-10-13 22:46:11 -07:00
2015-12-15 13:10:41 -07:00
initEditor(dlg, items);
2015-10-13 22:46:11 -07:00
2015-12-14 08:43:03 -07:00
$(dlg).on('iron-overlay-closed', onDialogClosed);
2015-10-13 22:46:11 -07:00
2015-12-14 08:43:03 -07:00
paperDialogHelper.open(dlg);
2015-10-13 22:46:11 -07:00
2015-12-15 13:10:41 -07:00
$('.btnCancel', dlg).on('click', function () {
2015-10-13 22:46:11 -07:00
2015-12-14 08:43:03 -07:00
paperDialogHelper.close(dlg);
2015-10-13 22:46:11 -07:00
});
};
}
2015-10-14 07:47:38 -07:00
return collectioneditor;
2015-10-13 22:46:11 -07:00
});