1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/components/mediaLibraryEditor/mediaLibraryEditor.js

250 lines
9.4 KiB
JavaScript
Raw Normal View History

/* eslint-disable indent */
/**
* Module for media library editor.
* @module components/mediaLibraryEditor/mediaLibraryEditor
*/
2020-08-14 08:46:34 +02:00
import 'jquery';
import loading from '../loading/loading';
import dialogHelper from '../dialogHelper/dialogHelper';
import dom from '../../scripts/dom';
import libraryoptionseditor from '../libraryoptionseditor/libraryoptionseditor';
import globalize from '../../scripts/globalize';
import '../../elements/emby-button/emby-button';
import '../listview/listview.css';
import '../../elements/emby-button/paper-icon-button-light';
import '../formdialog.css';
import '../../elements/emby-toggle/emby-toggle';
import '../../assets/css/flexstyles.css';
2018-10-23 01:05:09 +03:00
function onEditLibrary() {
2020-01-26 19:02:37 +03:00
if (isCreating) {
return false;
}
isCreating = true;
loading.show();
const dlg = dom.parentWithClass(this, 'dlg-libraryeditor');
let libraryOptions = libraryoptionseditor.getLibraryOptions(dlg.querySelector('.libraryOptions'));
libraryOptions = Object.assign(currentOptions.library.LibraryOptions || {}, libraryOptions);
ApiClient.updateVirtualFolderOptions(currentOptions.library.ItemId, libraryOptions).then(() => {
hasChanges = true;
isCreating = false;
loading.hide();
dialogHelper.close(dlg);
}, () => {
isCreating = false;
loading.hide();
});
return false;
}
2018-10-23 01:05:09 +03:00
function addMediaLocation(page, path, networkSharePath) {
const virtualFolder = currentOptions.library;
const refreshAfterChange = currentOptions.refresh;
ApiClient.addMediaPath(virtualFolder.Name, path, networkSharePath, refreshAfterChange).then(() => {
2019-05-06 17:26:18 -07:00
hasChanges = true;
refreshLibraryFromServer(page);
}, () => {
2020-08-14 08:46:34 +02:00
import('../toast/toast').then((toast) => {
2020-05-04 12:44:12 +02:00
toast(globalize.translate('ErrorAddingMediaPathToVirtualFolder'));
2019-05-06 17:26:18 -07:00
});
});
2018-10-23 01:05:09 +03:00
}
function updateMediaLocation(page, path, networkSharePath) {
const virtualFolder = currentOptions.library;
2018-10-23 01:05:09 +03:00
ApiClient.updateMediaPath(virtualFolder.Name, {
Path: path,
NetworkPath: networkSharePath
}).then(() => {
2019-05-06 17:26:18 -07:00
hasChanges = true;
refreshLibraryFromServer(page);
}, () => {
2020-08-14 08:46:34 +02:00
import('../toast/toast').then((toast) => {
2020-05-04 12:44:12 +02:00
toast(globalize.translate('ErrorAddingMediaPathToVirtualFolder'));
2019-05-06 17:26:18 -07:00
});
});
2018-10-23 01:05:09 +03:00
}
function onRemoveClick(btnRemovePath, location) {
const button = btnRemovePath;
const virtualFolder = currentOptions.library;
2020-01-26 19:02:37 +03:00
2020-08-14 08:46:34 +02:00
import('../confirm/confirm').then(({default: confirm}) => {
2018-10-23 01:05:09 +03:00
confirm({
2020-05-04 12:44:12 +02:00
title: globalize.translate('HeaderRemoveMediaLocation'),
text: globalize.translate('MessageConfirmRemoveMediaLocation'),
2020-08-13 21:23:51 +09:00
confirmText: globalize.translate('Delete'),
2020-05-04 12:44:12 +02:00
primary: 'delete'
}).then(() => {
const refreshAfterChange = currentOptions.refresh;
ApiClient.removeMediaPath(virtualFolder.Name, location, refreshAfterChange).then(() => {
2019-05-06 17:26:18 -07:00
hasChanges = true;
2020-05-04 12:44:12 +02:00
refreshLibraryFromServer(dom.parentWithClass(button, 'dlg-libraryeditor'));
}, () => {
2020-08-14 08:46:34 +02:00
import('../toast/toast').then((toast) => {
2020-08-02 01:23:57 +09:00
toast(globalize.translate('ErrorDefault'));
2019-05-06 17:26:18 -07:00
});
});
});
});
2018-10-23 01:05:09 +03:00
}
function onListItemClick(e) {
const listItem = dom.parentWithClass(e.target, 'listItem');
2020-01-26 19:02:37 +03:00
2018-10-23 01:05:09 +03:00
if (listItem) {
const index = parseInt(listItem.getAttribute('data-index'));
const pathInfos = (currentOptions.library.LibraryOptions || {}).PathInfos || [];
2020-07-30 16:07:13 +02:00
const pathInfo = index == null ? {} : pathInfos[index] || {};
const originalPath = pathInfo.Path || (index == null ? null : currentOptions.library.Locations[index]);
const btnRemovePath = dom.parentWithClass(e.target, 'btnRemovePath');
2020-01-26 19:02:37 +03:00
if (btnRemovePath) {
onRemoveClick(btnRemovePath, originalPath);
return;
}
2020-05-04 12:44:12 +02:00
showDirectoryBrowser(dom.parentWithClass(listItem, 'dlg-libraryeditor'), originalPath, pathInfo.NetworkPath);
2018-10-23 01:05:09 +03:00
}
}
function getFolderHtml(pathInfo, index) {
let html = '';
html += `<div class="listItem listItem-border lnkPath" data-index="${index}" style="padding-left:.5em;">`;
html += `<div class="${pathInfo.NetworkPath ? 'listItemBody two-line' : 'listItemBody'}">`;
2019-05-06 17:26:18 -07:00
html += '<h3 class="listItemBodyText">';
html += pathInfo.Path;
2020-05-04 12:44:12 +02:00
html += '</h3>';
2020-01-26 19:02:37 +03:00
2019-05-06 17:26:18 -07:00
if (pathInfo.NetworkPath) {
html += `<div class="listItemBodyText secondary">${pathInfo.NetworkPath}</div>`;
2019-05-06 17:26:18 -07:00
}
2020-01-26 19:02:37 +03:00
2020-05-04 12:44:12 +02:00
html += '</div>';
html += `<button type="button" is="paper-icon-button-light" class="listItemButton btnRemovePath" data-index="${index}"><span class="material-icons remove_circle"></span></button>`;
2020-05-04 12:44:12 +02:00
html += '</div>';
2019-05-06 17:26:18 -07:00
return html;
2018-10-23 01:05:09 +03:00
}
function refreshLibraryFromServer(page) {
ApiClient.getVirtualFolders().then(result => {
const library = result.filter(f => {
2020-01-26 19:02:37 +03:00
return f.Name === currentOptions.library.Name;
2018-10-23 01:05:09 +03:00
})[0];
2020-01-26 19:02:37 +03:00
2019-05-06 17:26:18 -07:00
if (library) {
currentOptions.library = library;
renderLibrary(page, currentOptions);
}
});
2018-10-23 01:05:09 +03:00
}
function renderLibrary(page, options) {
let pathInfos = (options.library.LibraryOptions || {}).PathInfos || [];
2020-01-26 19:02:37 +03:00
if (!pathInfos.length) {
pathInfos = options.library.Locations.map(p => {
2020-01-26 19:02:37 +03:00
return {
Path: p
};
});
}
2019-05-06 17:26:18 -07:00
if (options.library.CollectionType === 'boxsets') {
2020-05-04 12:44:12 +02:00
page.querySelector('.folders').classList.add('hide');
2019-05-06 17:26:18 -07:00
} else {
2020-05-04 12:44:12 +02:00
page.querySelector('.folders').classList.remove('hide');
2019-05-06 17:26:18 -07:00
}
2020-01-26 19:02:37 +03:00
2020-05-04 12:44:12 +02:00
page.querySelector('.folderList').innerHTML = pathInfos.map(getFolderHtml).join('');
2018-10-23 01:05:09 +03:00
}
function onAddButtonClick() {
2020-05-04 12:44:12 +02:00
showDirectoryBrowser(dom.parentWithClass(this, 'dlg-libraryeditor'));
2018-10-23 01:05:09 +03:00
}
function showDirectoryBrowser(context, originalPath, networkPath) {
2020-08-14 08:46:34 +02:00
import('../directorybrowser/directorybrowser').then((directoryBrowser) => {
const picker = new directoryBrowser();
2018-10-23 01:05:09 +03:00
picker.show({
2020-01-26 19:02:37 +03:00
enableNetworkSharePath: true,
2020-07-30 16:07:13 +02:00
pathReadOnly: originalPath != null,
2018-10-23 01:05:09 +03:00
path: originalPath,
networkSharePath: networkPath,
2020-01-26 19:02:37 +03:00
callback: function (path, networkSharePath) {
if (path) {
if (originalPath) {
updateMediaLocation(context, originalPath, networkSharePath);
} else {
addMediaLocation(context, path, networkSharePath);
}
}
2019-05-06 17:26:18 -07:00
picker.close();
2018-10-23 01:05:09 +03:00
}
2020-01-26 19:02:37 +03:00
});
});
2018-10-23 01:05:09 +03:00
}
function onToggleAdvancedChange() {
const dlg = dom.parentWithClass(this, 'dlg-libraryeditor');
2020-05-04 12:44:12 +02:00
libraryoptionseditor.setAdvancedVisible(dlg.querySelector('.libraryOptions'), this.checked);
2018-10-23 01:05:09 +03:00
}
function initEditor(dlg, options) {
2019-05-06 17:26:18 -07:00
renderLibrary(dlg, options);
2020-05-04 12:44:12 +02:00
dlg.querySelector('.btnAddFolder').addEventListener('click', onAddButtonClick);
dlg.querySelector('.folderList').addEventListener('click', onListItemClick);
dlg.querySelector('.chkAdvanced').addEventListener('change', onToggleAdvancedChange);
dlg.querySelector('.btnSubmit').addEventListener('click', onEditLibrary);
libraryoptionseditor.embed(dlg.querySelector('.libraryOptions'), options.library.CollectionType, options.library.LibraryOptions).then(() => {
2020-05-04 12:44:12 +02:00
onToggleAdvancedChange.call(dlg.querySelector('.chkAdvanced'));
2019-05-06 17:26:18 -07:00
});
2018-10-23 01:05:09 +03:00
}
function onDialogClosed() {
2019-05-06 17:26:18 -07:00
currentDeferred.resolveWith(null, [hasChanges]);
2018-10-23 01:05:09 +03:00
}
2020-06-26 19:30:44 +03:00
export class showEditor {
constructor(options) {
const deferred = jQuery.Deferred();
currentOptions = options;
currentDeferred = deferred;
hasChanges = false;
2020-08-14 08:46:34 +02:00
import('./mediaLibraryEditor.template.html').then((template) => {
2020-06-26 19:30:44 +03:00
const dlg = dialogHelper.createDialog({
size: 'small',
modal: false,
removeOnClose: true,
scrollY: false
});
2020-06-26 19:30:44 +03:00
dlg.classList.add('dlg-libraryeditor');
dlg.classList.add('ui-body-a');
dlg.classList.add('background-theme-a');
dlg.classList.add('formDialog');
2020-07-18 09:21:15 +01:00
dlg.innerHTML = globalize.translateHtml(template);
2020-06-26 19:30:44 +03:00
dlg.querySelector('.formDialogHeaderTitle').innerHTML = options.library.Name;
initEditor(dlg, options);
dlg.addEventListener('close', onDialogClosed);
dialogHelper.open(dlg);
dlg.querySelector('.btnCancel').addEventListener('click', () => {
dialogHelper.close(dlg);
});
refreshLibraryFromServer(dlg);
});
return deferred.promise();
2018-10-23 01:05:09 +03:00
}
}
let currentDeferred;
let currentOptions;
let hasChanges = false;
let isCreating = false;
2019-05-06 17:26:18 -07:00
2020-06-26 17:31:48 +03:00
/* eslint-enable indent */
2020-06-26 19:30:44 +03:00
export default showEditor;