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

252 lines
9.4 KiB
JavaScript
Raw Normal View History

/* eslint-disable indent */
/**
* Module for media library editor.
* @module components/mediaLibraryEditor/mediaLibraryEditor
*/
import jQuery from 'jQuery';
import loading from 'loading';
import dialogHelper from 'dialogHelper';
import dom from 'dom';
import libraryoptionseditor from 'components/libraryoptionseditor/libraryoptionseditor';
import globalize from 'globalize';
import 'emby-button';
import 'listViewStyle';
import 'paper-icon-button-light';
import 'formDialogStyle';
import 'emby-toggle';
import 'flexStyles';
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);
}, () => {
import('toast').then(({default: 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);
}, () => {
import('toast').then(({default: 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
import('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'),
confirmText: globalize.translate('ButtonDelete'),
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'));
}, () => {
import('toast').then(({default: toast}) => {
2020-05-04 12:44:12 +02:00
toast(globalize.translate('DefaultErrorMessage'));
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 || [];
const pathInfo = null == index ? {} : pathInfos[index] || {};
const originalPath = pathInfo.Path || (null == index ? 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) {
import('directorybrowser').then(({default: 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,
2018-10-23 01:05:09 +03:00
pathReadOnly: null != originalPath,
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
}
export class editor {
constructor() {
this.show = options => {
const deferred = jQuery.Deferred();
2019-05-06 17:26:18 -07:00
currentOptions = options;
currentDeferred = deferred;
hasChanges = false;
2020-06-11 21:52:00 +03:00
import('text!./components/mediaLibraryEditor/mediaLibraryEditor.template.html').then(({default: template}) => {
const dlg = dialogHelper.createDialog({
2020-05-11 21:43:41 +02:00
size: 'small',
2019-05-06 17:26:18 -07:00
modal: false,
removeOnClose: true,
scrollY: false
});
2020-05-04 12:44:12 +02:00
dlg.classList.add('dlg-libraryeditor');
dlg.classList.add('ui-body-a');
dlg.classList.add('background-theme-a');
dlg.classList.add('formDialog');
dlg.innerHTML = globalize.translateDocument(template);
2020-05-04 12:44:12 +02:00
dlg.querySelector('.formDialogHeaderTitle').innerHTML = options.library.Name;
2019-05-06 17:26:18 -07:00
initEditor(dlg, options);
2020-05-04 12:44:12 +02:00
dlg.addEventListener('close', onDialogClosed);
2019-05-06 17:26:18 -07:00
dialogHelper.open(dlg);
dlg.querySelector('.btnCancel').addEventListener('click', () => {
2019-05-06 17:26:18 -07:00
dialogHelper.close(dlg);
});
refreshLibraryFromServer(dlg);
});
2019-05-06 17:26:18 -07:00
return deferred.promise();
2020-01-26 19:02:37 +03:00
};
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 */
export default editor;