jellyfish-web/src/components/mediaLibraryCreator/mediaLibraryCreator.js

237 lines
8.2 KiB
JavaScript
Raw Normal View History

/* eslint-disable indent */
/**
* Module for media library creator.
* @module components/mediaLibraryCreator/mediaLibraryCreator
*/
import loading from 'loading';
import dialogHelper from 'dialogHelper';
import dom from 'dom';
import $ from 'jQuery';
import libraryoptionseditor from 'components/libraryoptionseditor/libraryoptionseditor';
import globalize from 'globalize';
import 'emby-toggle';
import 'emby-input';
import 'emby-select';
import 'paper-icon-button-light';
import 'listViewStyle';
import 'formDialogStyle';
import 'emby-button';
import 'flexStyles';
2018-10-23 01:05:09 +03:00
function onAddLibrary() {
2020-01-26 18:53:40 +03:00
if (isCreating) {
return false;
}
if (pathInfos.length == 0) {
import('alert').then(({default: alert}) => {
alert({
2020-05-04 12:44:12 +02:00
text: globalize.translate('PleaseAddAtLeastOneFolder'),
type: 'error'
2020-01-26 18:53:40 +03:00
});
});
2020-01-26 18:53:40 +03:00
2019-05-06 19:47:02 -07:00
return false;
}
2019-05-06 19:47:02 -07:00
isCreating = true;
loading.show();
const dlg = dom.parentWithClass(this, 'dlg-librarycreator');
const name = $('#txtValue', dlg).val();
let type = $('#selectCollectionType', dlg).val();
2020-01-26 18:53:40 +03:00
2020-05-04 12:44:12 +02:00
if (type == 'mixed') {
2020-01-26 18:53:40 +03:00
type = null;
}
const libraryOptions = libraryoptionseditor.getLibraryOptions(dlg.querySelector('.libraryOptions'));
2019-05-06 19:47:02 -07:00
libraryOptions.PathInfos = pathInfos;
ApiClient.addVirtualFolder(name, type, currentOptions.refresh, libraryOptions).then(() => {
2019-05-06 19:47:02 -07:00
hasChanges = true;
isCreating = false;
loading.hide();
dialogHelper.close(dlg);
}, () => {
import('toast').then(({default: toast}) => {
2020-05-04 12:44:12 +02:00
toast(globalize.translate('ErrorAddingMediaPathToVirtualFolder'));
});
2020-01-26 18:53:40 +03:00
2019-05-06 19:47:02 -07:00
isCreating = false;
loading.hide();
});
return false;
2018-10-23 01:05:09 +03:00
}
function getCollectionTypeOptionsHtml(collectionTypeOptions) {
return collectionTypeOptions.map(i => {
return `<option value="${i.value}">${i.name}</option>`;
2020-05-04 12:44:12 +02:00
}).join('');
2018-10-23 01:05:09 +03:00
}
function initEditor(page, collectionTypeOptions) {
2020-05-04 12:44:12 +02:00
$('#selectCollectionType', page).html(getCollectionTypeOptionsHtml(collectionTypeOptions)).val('').on('change', function () {
const value = this.value;
const dlg = $(this).parents('.dialog')[0];
2020-05-04 12:44:12 +02:00
libraryoptionseditor.setContentType(dlg.querySelector('.libraryOptions'), value == 'mixed' ? '' : value);
2020-01-26 18:53:40 +03:00
2019-05-06 19:47:02 -07:00
if (value) {
2020-05-04 12:44:12 +02:00
dlg.querySelector('.libraryOptions').classList.remove('hide');
2019-05-06 19:47:02 -07:00
} else {
2020-05-04 12:44:12 +02:00
dlg.querySelector('.libraryOptions').classList.add('hide');
2019-05-06 19:47:02 -07:00
}
2020-05-04 12:44:12 +02:00
if (value != 'mixed') {
const index = this.selectedIndex;
2020-01-26 18:53:40 +03:00
if (index != -1) {
const name = this.options[index].innerHTML.replace('*', '').replace('&amp;', '&');
2020-05-04 12:44:12 +02:00
$('#txtValue', dlg).val(name);
const folderOption = collectionTypeOptions.filter(i => {
2020-01-26 18:53:40 +03:00
return i.value == value;
2018-10-23 01:05:09 +03:00
})[0];
2020-05-04 12:44:12 +02:00
$('.collectionTypeFieldDescription', dlg).html(folderOption.message || '');
2018-10-23 01:05:09 +03:00
}
}
});
2020-05-04 12:44:12 +02:00
page.querySelector('.btnAddFolder').addEventListener('click', onAddButtonClick);
page.querySelector('.btnSubmit').addEventListener('click', onAddLibrary);
page.querySelector('.folderList').addEventListener('click', onRemoveClick);
page.querySelector('.chkAdvanced').addEventListener('change', onToggleAdvancedChange);
2018-10-23 01:05:09 +03:00
}
function onToggleAdvancedChange() {
const dlg = dom.parentWithClass(this, 'dlg-librarycreator');
2020-05-04 12:44:12 +02:00
libraryoptionseditor.setAdvancedVisible(dlg.querySelector('.libraryOptions'), this.checked);
2018-10-23 01:05:09 +03:00
}
function onAddButtonClick() {
const page = dom.parentWithClass(this, 'dlg-librarycreator');
2020-01-26 18:53:40 +03:00
import('directorybrowser').then(({default: directoryBrowser}) => {
const picker = new directoryBrowser();
2018-10-23 01:05:09 +03:00
picker.show({
enableNetworkSharePath: true,
2020-01-26 18:53:40 +03:00
callback: function (path, networkSharePath) {
if (path) {
addMediaLocation(page, path, networkSharePath);
}
2019-05-06 19:47:02 -07:00
picker.close();
2018-10-23 01:05:09 +03:00
}
2020-01-26 18:53:40 +03:00
});
});
2018-10-23 01:05:09 +03:00
}
function getFolderHtml(pathInfo, index) {
let html = '';
2020-01-26 18:53:40 +03:00
html += '<div class="listItem listItem-border lnkPath" style="padding-left:.5em;">';
html += `<div class="${pathInfo.NetworkPath ? 'listItemBody two-line' : 'listItemBody'}">`;
html += `<div class="listItemBodyText">${pathInfo.Path}</div>`;
2020-01-26 18:53:40 +03:00
if (pathInfo.NetworkPath) {
html += `<div class="listItemBodyText secondary">${pathInfo.NetworkPath}</div>`;
2020-01-26 18:53:40 +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>';
2020-01-26 18:53:40 +03:00
return html;
2018-10-23 01:05:09 +03:00
}
function renderPaths(page) {
const foldersHtml = pathInfos.map(getFolderHtml).join('');
const folderList = page.querySelector('.folderList');
2019-05-06 19:47:02 -07:00
folderList.innerHTML = foldersHtml;
2020-01-26 18:53:40 +03:00
if (foldersHtml) {
2020-05-04 12:44:12 +02:00
folderList.classList.remove('hide');
2020-01-26 18:53:40 +03:00
} else {
2020-05-04 12:44:12 +02:00
folderList.classList.add('hide');
2020-01-26 18:53:40 +03:00
}
2018-10-23 01:05:09 +03:00
}
function addMediaLocation(page, path, networkSharePath) {
const pathLower = path.toLowerCase();
const pathFilter = pathInfos.filter(p => {
2019-05-06 19:47:02 -07:00
return p.Path.toLowerCase() == pathLower;
});
2020-01-26 18:53:40 +03:00
if (!pathFilter.length) {
const pathInfo = {
2018-10-23 01:05:09 +03:00
Path: path
};
2020-01-26 18:53:40 +03:00
if (networkSharePath) {
pathInfo.NetworkPath = networkSharePath;
}
2019-05-06 19:47:02 -07:00
pathInfos.push(pathInfo);
renderPaths(page);
2018-10-23 01:05:09 +03:00
}
}
function onRemoveClick(e) {
const button = dom.parentWithClass(e.target, 'btnRemovePath');
const index = parseInt(button.getAttribute('data-index'));
const location = pathInfos[index].Path;
const locationLower = location.toLowerCase();
pathInfos = pathInfos.filter(p => {
2019-05-06 19:47:02 -07:00
return p.Path.toLowerCase() != locationLower;
});
2020-05-04 12:44:12 +02:00
renderPaths(dom.parentWithClass(button, 'dlg-librarycreator'));
2018-10-23 01:05:09 +03:00
}
function onDialogClosed() {
currentResolve(hasChanges);
2018-10-23 01:05:09 +03:00
}
function initLibraryOptions(dlg) {
libraryoptionseditor.embed(dlg.querySelector('.libraryOptions')).then(() => {
2020-05-04 12:44:12 +02:00
$('#selectCollectionType', dlg).trigger('change');
onToggleAdvancedChange.call(dlg.querySelector('.chkAdvanced'));
2020-01-26 18:53:40 +03:00
});
2018-10-23 01:05:09 +03:00
}
2020-06-26 19:30:44 +03:00
export class showEditor {
constructor(options) {
return new Promise((resolve) => {
currentOptions = options;
currentResolve = resolve;
hasChanges = false;
import('text!./components/mediaLibraryCreator/mediaLibraryCreator.template.html').then(({default: template}) => {
const dlg = dialogHelper.createDialog({
size: 'small',
modal: false,
removeOnClose: true,
scrollY: false
});
2020-06-26 19:30:44 +03:00
dlg.classList.add('ui-body-a');
dlg.classList.add('background-theme-a');
dlg.classList.add('dlg-librarycreator');
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
initEditor(dlg, options.collectionTypeOptions);
dlg.addEventListener('close', onDialogClosed);
dialogHelper.open(dlg);
dlg.querySelector('.btnCancel').addEventListener('click', () => {
dialogHelper.close(dlg);
});
pathInfos = [];
renderPaths(dlg);
initLibraryOptions(dlg);
});
2020-06-26 19:30:44 +03:00
});
2018-10-23 01:05:09 +03:00
}
}
let pathInfos = [];
let currentResolve;
let currentOptions;
let hasChanges = false;
let isCreating = false;
2019-05-06 19:47:02 -07:00
/* eslint-enable indent */
2020-06-26 19:30:44 +03:00
export default showEditor;