jellyfin-web/dashboard-ui/bower_components/emby-webcomponents/collectioneditor/collectioneditor.js

280 lines
8.9 KiB
JavaScript
Raw Normal View History

2016-06-11 08:55:39 -07:00
define(['shell', 'dialogHelper', 'loading', 'layoutManager', 'connectionManager', 'scrollHelper', 'embyRouter', 'globalize', 'emby-checkbox', 'emby-input', 'paper-icon-button-light', 'emby-select', 'material-icons', 'css!./../formdialog', 'emby-button'], function (shell, dialogHelper, loading, layoutManager, connectionManager, scrollHelper, embyRouter, globalize) {
2016-05-21 19:28:47 -07:00
var currentServerId;
function parentWithClass(elem, className) {
while (!elem.classList || !elem.classList.contains(className)) {
elem = elem.parentNode;
if (!elem) {
return null;
}
}
return elem;
}
function onSubmit(e) {
loading.show();
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;
}
function createCollection(apiClient, dlg) {
var url = apiClient.getUrl("Collections", {
Name: dlg.querySelector('#txtNewCollectionName').value,
IsLocked: !dlg.querySelector('#chkEnableInternetMetadata').checked,
Ids: dlg.querySelector('.fldSelectedItemIds').value || ''
//ParentId: getParameterByName('parentId') || LibraryMenu.getTopParentId()
});
apiClient.ajax({
type: "POST",
url: url,
dataType: "json"
}).then(function (result) {
loading.hide();
var id = result.Id;
dialogHelper.close(dlg);
redirectToCollection(apiClient, id);
});
}
function redirectToCollection(apiClient, id) {
apiClient.getItem(apiClient.getCurrentUserId(), id).then(function (item) {
embyRouter.showItem(item);
});
}
function addToCollection(apiClient, dlg, id) {
var url = apiClient.getUrl("Collections/" + id + "/Items", {
Ids: dlg.querySelector('.fldSelectedItemIds').value || ''
});
apiClient.ajax({
type: "POST",
url: url
}).then(function () {
loading.hide();
dialogHelper.close(dlg);
require(['toast'], function (toast) {
2016-05-21 23:08:44 -07:00
toast(globalize.translate('sharedcomponents#MessageItemsAdded'));
2016-05-21 19:28:47 -07:00
});
});
}
function triggerChange(select) {
select.dispatchEvent(new CustomEvent('change', {}));
}
function populateCollections(panel) {
loading.show();
var select = panel.querySelector('#selectCollectionToAddTo');
panel.querySelector('.newCollectionInfo').classList.add('hide');
var options = {
Recursive: true,
IncludeItemTypes: "BoxSet",
SortBy: "SortName"
};
var apiClient = connectionManager.getApiClient(currentServerId);
apiClient.getItems(apiClient.getCurrentUserId(), options).then(function (result) {
var html = '';
2016-05-21 23:08:44 -07:00
html += '<option value="">' + globalize.translate('sharedcomponents#OptionNew') + '</option>';
2016-05-21 19:28:47 -07:00
html += result.Items.map(function (i) {
return '<option value="' + i.Id + '">' + i.Name + '</option>';
});
select.innerHTML = html;
select.value = '';
triggerChange(select);
loading.hide();
});
}
function getEditorHtml() {
var html = '';
2016-05-21 21:17:28 -07:00
html += '<div class="dialogContent smoothScrollY">';
html += '<div class="dialogContentInner centeredContent">';
2016-05-21 19:28:47 -07:00
html += '<form class="newCollectionForm" style="margin:auto;">';
html += '<div>';
2016-05-21 21:17:28 -07:00
html += globalize.translate('sharedcomponents#NewCollectionHelp');
2016-05-21 19:28:47 -07:00
html += '</div>';
html += '<div class="fldSelectCollection">';
html += '<br/>';
html += '<br/>';
2016-05-21 21:17:28 -07:00
html += '<select is="emby-select" label="' + globalize.translate('sharedcomponents#LabelCollection') + '" id="selectCollectionToAddTo" autofocus></select>';
2016-05-21 19:28:47 -07:00
html += '</div>';
html += '<div class="newCollectionInfo">';
2016-05-28 11:03:38 -07:00
html += '<div class="inputContainer">';
html += '<input is="emby-input" type="text" id="txtNewCollectionName" required="required" label="' + globalize.translate('sharedcomponents#LabelName') + '" />';
2016-05-21 21:17:28 -07:00
html += '<div class="fieldDescription">' + globalize.translate('sharedcomponents#NewCollectionNameExample') + '</div>';
2016-05-21 19:28:47 -07:00
html += '</div>';
2016-06-11 08:55:39 -07:00
html += '<label class="checkboxContainer">';
html += '<input is="emby-checkbox" type="checkbox" id="chkEnableInternetMetadata" />';
html += '<span>' + globalize.translate('sharedcomponents#SearchForCollectionInternetMetadata') + '</span>';
html += '</label>';
2016-05-21 19:28:47 -07:00
// newCollectionInfo
html += '</div>';
html += '<div>';
2016-06-04 17:17:35 -07:00
html += '<button is="emby-button" type="submit" class="raised btnSubmit block">' + globalize.translate('sharedcomponents#ButtonOk') + '</button>';
2016-05-21 19:28:47 -07:00
html += '</div>';
html += '<input type="hidden" class="fldSelectedItemIds" />';
html += '</form>';
2016-05-21 21:17:28 -07:00
html += '</div>';
html += '</div>';
2016-05-21 19:28:47 -07:00
return html;
}
2016-05-21 21:17:28 -07:00
function onHelpClick(e) {
shell.openUrl(this.href);
e.preventDefault();
return false;
}
2016-05-21 19:28:47 -07:00
function initEditor(content, items) {
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');
}
});
2016-05-21 23:08:44 -07:00
content.querySelector('form').addEventListener('submit', onSubmit);
2016-05-21 19:28:47 -07:00
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);
}
}
function collectioneditor() {
var self = this;
self.show = function (options) {
var items = options.items || {};
currentServerId = options.serverId;
2016-05-21 21:17:28 -07:00
var dialogOptions = {
removeOnClose: true,
scrollY: false
};
2016-05-21 19:28:47 -07:00
2016-05-21 21:17:28 -07:00
if (layoutManager.tv) {
dialogOptions.size = 'fullscreen';
} else {
dialogOptions.size = 'small';
}
var dlg = dialogHelper.createDialog(dialogOptions);
dlg.classList.add('formDialog');
2016-05-21 19:28:47 -07:00
var html = '';
2016-05-21 21:17:28 -07:00
var title = items.length ? globalize.translate('sharedcomponents#AddToCollection') : globalize.translate('sharedcomponents#NewCollection');
2016-05-21 19:28:47 -07:00
html += '<div class="dialogHeader" style="margin:0 0 2em;">';
2016-06-09 23:54:03 -07:00
html += '<button is="paper-icon-button-light" class="btnCancel autoSize" tabindex="-1"><i class="md-icon">arrow_back</i></button>';
2016-05-21 19:28:47 -07:00
html += '<div class="dialogHeaderTitle">';
html += title;
html += '</div>';
2016-06-09 23:54:03 -07:00
html += '<a class="btnHelp" href="https://github.com/MediaBrowser/Wiki/wiki/Collections" target="_blank" style="margin-left:auto;margin-right:.5em;display:inline-block;padding:.25em;display:flex;align-items:center;" title="' + globalize.translate('sharedcomponents#Help') + '"><i class="md-icon">info</i><span style="margin-left:.25em;">' + globalize.translate('sharedcomponents#Help') + '</span></a>';
2016-05-21 19:28:47 -07:00
html += '</div>';
html += getEditorHtml();
dlg.innerHTML = html;
document.body.appendChild(dlg);
initEditor(dlg, items);
dlg.querySelector('.btnCancel').addEventListener('click', function () {
dialogHelper.close(dlg);
});
2016-05-21 21:17:28 -07:00
if (layoutManager.tv) {
scrollHelper.centerFocus.on(dlg.querySelector('.dialogContent'), false);
}
return new Promise(function (resolve, reject) {
dlg.addEventListener('close', resolve);
dialogHelper.open(dlg);
});
2016-05-21 19:28:47 -07:00
};
}
return collectioneditor;
});