2020-06-09 22:37:34 +03:00
|
|
|
/* 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
|
|
|
|
2019-05-07 13:17:40 -07:00
|
|
|
function onEditLibrary() {
|
2020-01-26 19:02:37 +03:00
|
|
|
if (isCreating) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-07 13:17:40 -07:00
|
|
|
|
|
|
|
isCreating = true;
|
|
|
|
loading.show();
|
2020-06-09 22:37:34 +03:00
|
|
|
const dlg = dom.parentWithClass(this, 'dlg-libraryeditor');
|
|
|
|
let libraryOptions = libraryoptionseditor.getLibraryOptions(dlg.querySelector('.libraryOptions'));
|
2019-05-07 13:17:40 -07:00
|
|
|
libraryOptions = Object.assign(currentOptions.library.LibraryOptions || {}, libraryOptions);
|
2020-06-09 22:37:34 +03:00
|
|
|
ApiClient.updateVirtualFolderOptions(currentOptions.library.ItemId, libraryOptions).then(() => {
|
2019-05-07 13:17:40 -07:00
|
|
|
hasChanges = true;
|
|
|
|
isCreating = false;
|
|
|
|
loading.hide();
|
|
|
|
dialogHelper.close(dlg);
|
2020-06-09 22:37:34 +03:00
|
|
|
}, () => {
|
2019-05-07 13:17:40 -07:00
|
|
|
isCreating = false;
|
|
|
|
loading.hide();
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
function addMediaLocation(page, path, networkSharePath) {
|
2020-06-09 22:37:34 +03:00
|
|
|
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-06-09 22:37:34 +03:00
|
|
|
}, () => {
|
|
|
|
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) {
|
2020-06-09 22:37:34 +03:00
|
|
|
const virtualFolder = currentOptions.library;
|
2018-10-23 01:05:09 +03:00
|
|
|
ApiClient.updateMediaPath(virtualFolder.Name, {
|
|
|
|
Path: path,
|
|
|
|
NetworkPath: networkSharePath
|
2020-06-09 22:37:34 +03:00
|
|
|
}).then(() => {
|
2019-05-06 17:26:18 -07:00
|
|
|
hasChanges = true;
|
|
|
|
refreshLibraryFromServer(page);
|
2020-06-09 22:37:34 +03:00
|
|
|
}, () => {
|
|
|
|
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) {
|
2020-06-09 22:37:34 +03:00
|
|
|
const button = btnRemovePath;
|
|
|
|
const virtualFolder = currentOptions.library;
|
2020-01-26 19:02:37 +03:00
|
|
|
|
2020-06-09 22:37:34 +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'
|
2020-06-09 22:37:34 +03:00
|
|
|
}).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-06-09 22:37:34 +03:00
|
|
|
}, () => {
|
|
|
|
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) {
|
2020-06-09 22:37:34 +03:00
|
|
|
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) {
|
2020-06-09 22:37:34 +03:00
|
|
|
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) {
|
2020-06-09 22:37:34 +03:00
|
|
|
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) {
|
2020-06-09 22:37:34 +03:00
|
|
|
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>';
|
2020-06-09 22:37:34 +03:00
|
|
|
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) {
|
2020-06-09 22:37:34 +03:00
|
|
|
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) {
|
2020-06-09 22:37:34 +03:00
|
|
|
let pathInfos = (options.library.LibraryOptions || {}).PathInfos || [];
|
2020-01-26 19:02:37 +03:00
|
|
|
|
|
|
|
if (!pathInfos.length) {
|
2020-06-09 22:37:34 +03:00
|
|
|
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-06-09 22:37:34 +03:00
|
|
|
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() {
|
2020-06-09 22:37:34 +03:00
|
|
|
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);
|
2020-06-09 22:37:34 +03:00
|
|
|
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-09 22:37:34 +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-09 22:37:34 +03:00
|
|
|
// TODO: remove require
|
|
|
|
require(['text!./components/mediaLibraryEditor/mediaLibraryEditor.template.html'], 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');
|
2020-03-29 19:45:19 +02:00
|
|
|
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);
|
2020-06-09 22:37:34 +03:00
|
|
|
dlg.querySelector('.btnCancel').addEventListener('click', () => {
|
2019-05-06 17:26:18 -07:00
|
|
|
dialogHelper.close(dlg);
|
|
|
|
});
|
|
|
|
refreshLibraryFromServer(dlg);
|
2020-06-09 22:37:34 +03:00
|
|
|
});
|
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
|
|
|
}
|
2020-06-09 22:37:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let currentDeferred;
|
|
|
|
let currentOptions;
|
|
|
|
let hasChanges = false;
|
|
|
|
let isCreating = false;
|
2019-05-06 17:26:18 -07:00
|
|
|
|
2020-06-09 22:37:34 +03:00
|
|
|
/* eslint-enable indent */
|
|
|
|
export default editor;
|