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

237 lines
8.7 KiB
JavaScript
Raw Normal View History

/* eslint-disable indent */
/**
* Module for media library editor.
* @module components/mediaLibraryEditor/mediaLibraryEditor
*/
2022-01-30 00:27:26 +03:00
import escapeHtml from 'escape-html';
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';
2021-01-26 16:25:38 -05:00
import '../listview/listview.scss';
2020-08-14 08:46:34 +02:00
import '../../elements/emby-button/paper-icon-button-light';
2021-01-26 16:25:38 -05:00
import '../formdialog.scss';
2020-08-14 08:46:34 +02:00
import '../../elements/emby-toggle/emby-toggle';
import '../../styles/flexstyles.scss';
import './style.scss';
2020-10-18 14:19:41 +01:00
import toast from '../toast/toast';
import confirm from '../confirm/confirm';
2020-11-25 00:17:24 -05:00
import template from './mediaLibraryEditor.template.html';
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-10-18 14:19:41 +01: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-10-18 14:19:41 +01: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
confirm({
title: globalize.translate('HeaderRemoveMediaLocation'),
text: globalize.translate('MessageConfirmRemoveMediaLocation'),
confirmText: globalize.translate('Delete'),
primary: 'delete'
}).then(() => {
const refreshAfterChange = currentOptions.refresh;
ApiClient.removeMediaPath(virtualFolder.Name, location, refreshAfterChange).then(() => {
hasChanges = true;
refreshLibraryFromServer(dom.parentWithClass(button, 'dlg-libraryeditor'));
}, () => {
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) {
2023-03-09 00:01:05 -05:00
const index = parseInt(listItem.getAttribute('data-index'), 10);
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 = '';
2022-07-14 17:59:37 -04:00
html += `<div class="listItem listItem-border lnkPath" data-index="${index}">`;
html += `<div class="${pathInfo.NetworkPath ? 'listItemBody two-line' : 'listItemBody'}">`;
2019-05-06 17:26:18 -07:00
html += '<h3 class="listItemBodyText">';
2022-01-30 00:27:26 +03:00
html += escapeHtml(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) {
2022-01-30 00:27:26 +03:00
html += `<div class="listItemBodyText secondary">${escapeHtml(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>';
2022-02-24 20:15:24 +03:00
html += `<button type="button" is="paper-icon-button-light" class="listItemButton btnRemovePath" data-index="${index}"><span class="material-icons remove_circle" aria-hidden="true"></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) {
2023-03-29 00:38:22 -04:00
import('../directorybrowser/directorybrowser').then(({ default: DirectoryBrowser }) => {
2021-09-08 02:46:43 +03:00
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 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('.btnSubmit').addEventListener('click', onEditLibrary);
libraryoptionseditor.embed(dlg.querySelector('.libraryOptions'), options.library.CollectionType, options.library.LibraryOptions);
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-11-25 00:17:24 -05:00
const dlg = dialogHelper.createDialog({
size: 'small',
modal: false,
removeOnClose: true,
scrollY: false
});
dlg.classList.add('dlg-libraryeditor');
dlg.classList.add('ui-body-a');
dlg.classList.add('background-theme-a');
dlg.classList.add('formDialog');
dlg.innerHTML = globalize.translateHtml(template);
2022-01-30 00:27:26 +03:00
dlg.querySelector('.formDialogHeaderTitle').innerText = options.library.Name;
2020-11-25 00:17:24 -05:00
initEditor(dlg, options);
dlg.addEventListener('close', onDialogClosed);
dialogHelper.open(dlg);
dlg.querySelector('.btnCancel').addEventListener('click', () => {
dialogHelper.close(dlg);
2020-06-26 19:30:44 +03:00
});
2020-11-25 00:17:24 -05:00
refreshLibraryFromServer(dlg);
2020-06-26 19:30:44 +03:00
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;