import dom from 'dom'; import dialogHelper from 'dialogHelper'; import loading from 'loading'; import appHost from 'apphost'; import layoutManager from 'layoutManager'; import connectionManager from 'connectionManager'; import appRouter from 'appRouter'; import globalize from 'globalize'; import 'emby-checkbox'; import 'emby-input'; import 'paper-icon-button-light'; import 'emby-select'; import 'material-icons'; import 'css!./../formdialog'; import 'emby-button'; import 'flexStyles'; /* eslint-disable indent */ let currentServerId; function onSubmit(e) { loading.show(); const panel = dom.parentWithClass(this, 'dialog'); const collectionId = panel.querySelector('#selectCollectionToAddTo').value; const apiClient = connectionManager.getApiClient(currentServerId); if (collectionId) { addToCollection(apiClient, panel, collectionId); } else { createCollection(apiClient, panel); } e.preventDefault(); return false; } function createCollection(apiClient, dlg) { const url = apiClient.getUrl('Collections', { Name: dlg.querySelector('#txtNewCollectionName').value, IsLocked: !dlg.querySelector('#chkEnableInternetMetadata').checked, Ids: dlg.querySelector('.fldSelectedItemIds').value || '' }); apiClient.ajax({ type: 'POST', url: url, dataType: 'json' }).then(result => { loading.hide(); const id = result.Id; dlg.submitted = true; dialogHelper.close(dlg); redirectToCollection(apiClient, id); }); } function redirectToCollection(apiClient, id) { appRouter.showItem(id, apiClient.serverId()); } function addToCollection(apiClient, dlg, id) { const url = apiClient.getUrl(`Collections/${id}/Items`, { Ids: dlg.querySelector('.fldSelectedItemIds').value || '' }); apiClient.ajax({ type: 'POST', url: url }).then(() => { loading.hide(); dlg.submitted = true; dialogHelper.close(dlg); import('toast').then(({default: toast}) => { toast(globalize.translate('MessageItemsAdded')); }); }); } function triggerChange(select) { select.dispatchEvent(new CustomEvent('change', {})); } function populateCollections(panel) { loading.show(); const select = panel.querySelector('#selectCollectionToAddTo'); panel.querySelector('.newCollectionInfo').classList.add('hide'); const options = { Recursive: true, IncludeItemTypes: 'BoxSet', SortBy: 'SortName', EnableTotalRecordCount: false }; const apiClient = connectionManager.getApiClient(currentServerId); apiClient.getItems(apiClient.getCurrentUserId(), options).then(result => { let html = ''; html += ``; html += result.Items.map(i => { return ``; }); select.innerHTML = html; select.value = ''; triggerChange(select); loading.hide(); }); } function getEditorHtml() { let html = ''; html += '
'; html += '
'; html += '
'; html += '
'; html += globalize.translate('NewCollectionHelp'); html += '
'; html += '
'; html += '
'; html += '
'; html += '
'; html += ``; html += '
'; html += '
'; html += '
'; html += '
'; html += ``; html += `
${globalize.translate('NewCollectionNameExample')}
`; html += '
'; html += ''; // newCollectionInfo html += '
'; html += '
'; html += ``; html += '
'; html += ''; html += '
'; html += '
'; html += '
'; return html; } 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'); } }); 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'); const selectCollectionToAddTo = content.querySelector('#selectCollectionToAddTo'); selectCollectionToAddTo.innerHTML = ''; selectCollectionToAddTo.value = ''; triggerChange(selectCollectionToAddTo); } } function centerFocus(elem, horiz, on) { import('scrollHelper').then(scrollHelper => { const fn = on ? 'on' : 'off'; scrollHelper.centerFocus[fn](elem, horiz); }); } export class showEditor { constructor(options) { const items = options.items || {}; currentServerId = options.serverId; const dialogOptions = { removeOnClose: true, scrollY: false }; if (layoutManager.tv) { dialogOptions.size = 'fullscreen'; } else { dialogOptions.size = 'small'; } const dlg = dialogHelper.createDialog(dialogOptions); dlg.classList.add('formDialog'); let html = ''; const title = items.length ? globalize.translate('HeaderAddToCollection') : globalize.translate('NewCollection'); html += '
'; html += ''; html += '

'; html += title; html += '

'; if (appHost.supports('externallinks')) { // TODO: Change to Jellyfin docs html += `${globalize.translate('Help')}`; } html += '
'; html += getEditorHtml(); dlg.innerHTML = html; initEditor(dlg, items); dlg.querySelector('.btnCancel').addEventListener('click', () => { dialogHelper.close(dlg); }); if (layoutManager.tv) { centerFocus(dlg.querySelector('.formDialogContent'), false, true); } return dialogHelper.open(dlg).then(() => { if (layoutManager.tv) { centerFocus(dlg.querySelector('.formDialogContent'), false, false); } if (dlg.submitted) { return Promise.resolve(); } return Promise.reject(); }); } } /* eslint-enable indent */ export default showEditor;