2019-01-23 11:33:34 +00:00
|
|
|
define(['dialogHelper', 'loading', 'apphost', 'layoutManager', 'connectionManager', 'appRouter', 'globalize', 'emby-checkbox', 'emby-input', 'paper-icon-button-light', 'emby-select', 'material-icons', 'css!./../formdialog', 'emby-button', 'emby-linkbutton', 'flexStyles'], function (dialogHelper, loading, appHost, layoutManager, connectionManager, appRouter, globalize) {
|
2019-01-10 15:39:37 +03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var currentServerId;
|
2018-10-23 01:05:09 +03:00
|
|
|
|
|
|
|
function parentWithClass(elem, className) {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
while (!elem.classList || !elem.classList.contains(className)) {
|
|
|
|
elem = elem.parentNode;
|
|
|
|
|
|
|
|
if (!elem) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return elem;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function onSubmit(e) {
|
|
|
|
loading.show();
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
var panel = parentWithClass(this, 'dialog');
|
|
|
|
|
|
|
|
var collectionId = panel.querySelector('#selectCollectionToAddTo').value;
|
|
|
|
|
|
|
|
var apiClient = connectionManager.getApiClient(currentServerId);
|
|
|
|
|
|
|
|
if (collectionId) {
|
|
|
|
addToCollection(apiClient, panel, collectionId);
|
|
|
|
} else {
|
|
|
|
createCollection(apiClient, panel);
|
|
|
|
}
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function createCollection(apiClient, dlg) {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
var url = apiClient.getUrl("Collections", {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
Name: dlg.querySelector('#txtNewCollectionName').value,
|
|
|
|
IsLocked: !dlg.querySelector('#chkEnableInternetMetadata').checked,
|
|
|
|
Ids: dlg.querySelector('.fldSelectedItemIds').value || ''
|
2018-10-23 01:05:09 +03:00
|
|
|
});
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
apiClient.ajax({
|
|
|
|
type: "POST",
|
|
|
|
url: url,
|
|
|
|
dataType: "json"
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
}).then(function (result) {
|
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
loading.hide();
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
var id = result.Id;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
dlg.submitted = true;
|
|
|
|
dialogHelper.close(dlg);
|
|
|
|
redirectToCollection(apiClient, id);
|
|
|
|
|
|
|
|
});
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function redirectToCollection(apiClient, id) {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
appRouter.showItem(id, apiClient.serverId());
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function addToCollection(apiClient, dlg, id) {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
var url = apiClient.getUrl("Collections/" + id + "/Items", {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
Ids: dlg.querySelector('.fldSelectedItemIds').value || ''
|
2018-10-23 01:05:09 +03:00
|
|
|
});
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
apiClient.ajax({
|
|
|
|
type: "POST",
|
|
|
|
url: url
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
}).then(function () {
|
|
|
|
|
|
|
|
loading.hide();
|
|
|
|
|
|
|
|
dlg.submitted = true;
|
|
|
|
dialogHelper.close(dlg);
|
|
|
|
|
|
|
|
require(['toast'], function (toast) {
|
|
|
|
toast(globalize.translate('sharedcomponents#MessageItemsAdded'));
|
|
|
|
});
|
|
|
|
});
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function triggerChange(select) {
|
2019-01-10 15:39:37 +03:00
|
|
|
select.dispatchEvent(new CustomEvent('change', {}));
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function populateCollections(panel) {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
loading.show();
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
var select = panel.querySelector('#selectCollectionToAddTo');
|
|
|
|
|
|
|
|
panel.querySelector('.newCollectionInfo').classList.add('hide');
|
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
var options = {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
Recursive: true,
|
|
|
|
IncludeItemTypes: "BoxSet",
|
|
|
|
SortBy: "SortName",
|
|
|
|
EnableTotalRecordCount: false
|
|
|
|
};
|
|
|
|
|
|
|
|
var apiClient = connectionManager.getApiClient(currentServerId);
|
|
|
|
apiClient.getItems(apiClient.getCurrentUserId(), options).then(function (result) {
|
|
|
|
|
|
|
|
var html = '';
|
|
|
|
|
|
|
|
html += '<option value="">' + globalize.translate('sharedcomponents#OptionNew') + '</option>';
|
|
|
|
|
|
|
|
html += result.Items.map(function (i) {
|
|
|
|
|
|
|
|
return '<option value="' + i.Id + '">' + i.Name + '</option>';
|
|
|
|
});
|
|
|
|
|
|
|
|
select.innerHTML = html;
|
|
|
|
select.value = '';
|
|
|
|
triggerChange(select);
|
|
|
|
|
|
|
|
loading.hide();
|
|
|
|
});
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function getEditorHtml() {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
var html = '';
|
|
|
|
|
|
|
|
html += '<div class="formDialogContent smoothScrollY" style="padding-top:2em;">';
|
|
|
|
html += '<div class="dialogContentInner dialog-content-centered">';
|
|
|
|
html += '<form class="newCollectionForm" style="margin:auto;">';
|
|
|
|
|
|
|
|
html += '<div>';
|
|
|
|
html += globalize.translate('sharedcomponents#NewCollectionHelp');
|
|
|
|
html += '</div>';
|
|
|
|
|
|
|
|
html += '<div class="fldSelectCollection">';
|
|
|
|
html += '<br/>';
|
|
|
|
html += '<br/>';
|
|
|
|
html += '<div class="selectContainer">';
|
|
|
|
html += '<select is="emby-select" label="' + globalize.translate('sharedcomponents#LabelCollection') + '" id="selectCollectionToAddTo" autofocus></select>';
|
|
|
|
html += '</div>';
|
|
|
|
html += '</div>';
|
|
|
|
|
|
|
|
html += '<div class="newCollectionInfo">';
|
|
|
|
|
|
|
|
html += '<div class="inputContainer">';
|
|
|
|
html += '<input is="emby-input" type="text" id="txtNewCollectionName" required="required" label="' + globalize.translate('sharedcomponents#LabelName') + '" />';
|
|
|
|
html += '<div class="fieldDescription">' + globalize.translate('sharedcomponents#NewCollectionNameExample') + '</div>';
|
|
|
|
html += '</div>';
|
|
|
|
|
|
|
|
html += '<label class="checkboxContainer">';
|
|
|
|
html += '<input is="emby-checkbox" type="checkbox" id="chkEnableInternetMetadata" />';
|
|
|
|
html += '<span>' + globalize.translate('sharedcomponents#SearchForCollectionInternetMetadata') + '</span>';
|
|
|
|
html += '</label>';
|
|
|
|
|
|
|
|
// newCollectionInfo
|
|
|
|
html += '</div>';
|
|
|
|
|
|
|
|
html += '<div class="formDialogFooter">';
|
|
|
|
html += '<button is="emby-button" type="submit" class="raised btnSubmit block formDialogFooterItem button-submit">' + globalize.translate('sharedcomponents#ButtonOk') + '</button>';
|
|
|
|
html += '</div>';
|
|
|
|
|
|
|
|
html += '<input type="hidden" class="fldSelectedItemIds" />';
|
|
|
|
|
|
|
|
html += '</form>';
|
|
|
|
html += '</div>';
|
|
|
|
html += '</div>';
|
|
|
|
|
|
|
|
return html;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function initEditor(content, items) {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
content.querySelector('#selectCollectionToAddTo').addEventListener('change', function () {
|
|
|
|
if (this.value) {
|
|
|
|
content.querySelector('.newCollectionInfo').classList.add('hide');
|
|
|
|
content.querySelector('#txtNewCollectionName').removeAttribute('required');
|
|
|
|
} else {
|
|
|
|
content.querySelector('.newCollectionInfo').classList.remove('hide');
|
|
|
|
content.querySelector('#txtNewCollectionName').setAttribute('required', 'required');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
content.querySelector('form').addEventListener('submit', onSubmit);
|
|
|
|
|
|
|
|
content.querySelector('.fldSelectedItemIds', content).value = items.join(',');
|
|
|
|
|
|
|
|
if (items.length) {
|
|
|
|
content.querySelector('.fldSelectCollection').classList.remove('hide');
|
|
|
|
populateCollections(content);
|
|
|
|
} else {
|
|
|
|
content.querySelector('.fldSelectCollection').classList.add('hide');
|
|
|
|
|
|
|
|
var selectCollectionToAddTo = content.querySelector('#selectCollectionToAddTo');
|
|
|
|
selectCollectionToAddTo.innerHTML = '';
|
|
|
|
selectCollectionToAddTo.value = '';
|
|
|
|
triggerChange(selectCollectionToAddTo);
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function centerFocus(elem, horiz, on) {
|
2019-01-10 15:39:37 +03:00
|
|
|
require(['scrollHelper'], function (scrollHelper) {
|
|
|
|
var fn = on ? 'on' : 'off';
|
|
|
|
scrollHelper.centerFocus[fn](elem, horiz);
|
|
|
|
});
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2019-01-10 15:39:37 +03:00
|
|
|
function CollectionEditor() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
CollectionEditor.prototype.show = function (options) {
|
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
var items = options.items || {};
|
|
|
|
currentServerId = options.serverId;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
var dialogOptions = {
|
2019-01-10 15:39:37 +03:00
|
|
|
removeOnClose: true,
|
|
|
|
scrollY: false
|
2018-10-23 01:05:09 +03:00
|
|
|
};
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (layoutManager.tv) {
|
|
|
|
dialogOptions.size = 'fullscreen';
|
|
|
|
} else {
|
|
|
|
dialogOptions.size = 'small';
|
|
|
|
}
|
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
var dlg = dialogHelper.createDialog(dialogOptions);
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
dlg.classList.add('formDialog');
|
|
|
|
|
|
|
|
var html = '';
|
|
|
|
var title = items.length ? globalize.translate('sharedcomponents#HeaderAddToCollection') : globalize.translate('sharedcomponents#NewCollection');
|
|
|
|
|
|
|
|
html += '<div class="formDialogHeader">';
|
|
|
|
html += '<button is="paper-icon-button-light" class="btnCancel autoSize" tabindex="-1"><i class="md-icon"></i></button>';
|
|
|
|
html += '<h3 class="formDialogHeaderTitle">';
|
|
|
|
html += title;
|
|
|
|
html += '</h3>';
|
|
|
|
|
|
|
|
if (appHost.supports('externallinks')) {
|
|
|
|
html += '<a is="emby-linkbutton" class="button-link btnHelp flex align-items-center" href="https://github.com/MediaBrowser/Wiki/wiki/Collections" target="_blank" style="margin-left:auto;margin-right:.5em;padding:.25em;" title="' + globalize.translate('sharedcomponents#Help') + '"><i class="md-icon"></i><span style="margin-left:.25em;">' + globalize.translate('sharedcomponents#Help') + '</span></a>';
|
|
|
|
}
|
|
|
|
|
|
|
|
html += '</div>';
|
|
|
|
|
|
|
|
html += getEditorHtml();
|
|
|
|
|
|
|
|
dlg.innerHTML = html;
|
|
|
|
|
|
|
|
initEditor(dlg, items);
|
|
|
|
|
|
|
|
dlg.querySelector('.btnCancel').addEventListener('click', function () {
|
|
|
|
|
|
|
|
dialogHelper.close(dlg);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (layoutManager.tv) {
|
|
|
|
centerFocus(dlg.querySelector('.formDialogContent'), false, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dialogHelper.open(dlg).then(function () {
|
|
|
|
|
|
|
|
if (layoutManager.tv) {
|
|
|
|
centerFocus(dlg.querySelector('.formDialogContent'), false, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dlg.submitted) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.reject();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return CollectionEditor;
|
2018-10-23 01:05:09 +03:00
|
|
|
});
|