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/imageOptionsEditor/imageOptionsEditor.js

118 lines
4.6 KiB
JavaScript
Raw Normal View History

/* eslint-disable indent */
/**
* Module for image Options Editor.
* @module components/imageOptionsEditor/imageOptionsEditor
*/
import globalize from 'globalize';
import dom from 'dom';
import dialogHelper from 'dialogHelper';
import 'emby-checkbox';
import 'emby-select';
import 'emby-input';
2018-10-23 01:05:09 +03:00
function getDefaultImageConfig(itemType, type) {
return {
Type: type,
MinWidth: 0,
2020-05-04 12:44:12 +02:00
Limit: 'Primary' === type ? 1 : 0
2019-10-09 18:59:17 +03:00
};
2018-10-23 01:05:09 +03:00
}
function findImageOptions(imageOptions, type) {
return imageOptions.filter(i => {
2019-10-09 18:59:17 +03:00
return i.Type == type;
})[0];
2018-10-23 01:05:09 +03:00
}
function getImageConfig(options, availableOptions, imageType, itemType) {
2019-10-09 18:59:17 +03:00
return findImageOptions(options.ImageOptions || [], imageType) || findImageOptions(availableOptions.DefaultImageOptions || [], imageType) || getDefaultImageConfig(itemType, imageType);
2018-10-23 01:05:09 +03:00
}
function setVisibilityOfBackdrops(elem, visible) {
2019-10-09 18:59:17 +03:00
if (visible) {
2020-05-04 12:44:12 +02:00
elem.classList.remove('hide');
elem.querySelector('input').setAttribute('required', 'required');
2019-10-09 18:59:17 +03:00
} else {
2020-05-04 12:44:12 +02:00
elem.classList.add('hide');
elem.querySelector('input').setAttribute('required', '');
elem.querySelector('input').removeAttribute('required');
2019-10-09 18:59:17 +03:00
}
2018-10-23 01:05:09 +03:00
}
function loadValues(context, itemType, options, availableOptions) {
const supportedImageTypes = availableOptions.SupportedImageTypes || [];
setVisibilityOfBackdrops(context.querySelector('.backdropFields'), supportedImageTypes.includes('Backdrop'));
setVisibilityOfBackdrops(context.querySelector('.screenshotFields'), supportedImageTypes.includes('Screenshot'));
Array.prototype.forEach.call(context.querySelectorAll('.imageType'), i => {
const imageType = i.getAttribute('data-imagetype');
const container = dom.parentWithTag(i, 'LABEL');
2019-10-09 18:59:17 +03:00
if (!supportedImageTypes.includes(imageType)) {
2020-05-04 12:44:12 +02:00
container.classList.add('hide');
2019-10-09 18:59:17 +03:00
} else {
2020-05-04 12:44:12 +02:00
container.classList.remove('hide');
2019-10-09 18:59:17 +03:00
}
if (getImageConfig(options, availableOptions, imageType, itemType).Limit) {
i.checked = true;
} else {
i.checked = false;
}
2018-10-23 01:05:09 +03:00
});
const backdropConfig = getImageConfig(options, availableOptions, 'Backdrop', itemType);
2020-05-04 12:44:12 +02:00
context.querySelector('#txtMaxBackdrops').value = backdropConfig.Limit;
context.querySelector('#txtMinBackdropDownloadWidth').value = backdropConfig.MinWidth;
const screenshotConfig = getImageConfig(options, availableOptions, 'Screenshot', itemType);
2020-05-04 12:44:12 +02:00
context.querySelector('#txtMaxScreenshots').value = screenshotConfig.Limit;
context.querySelector('#txtMinScreenshotDownloadWidth').value = screenshotConfig.MinWidth;
2018-10-23 01:05:09 +03:00
}
function saveValues(context, options) {
options.ImageOptions = Array.prototype.map.call(context.querySelectorAll('.imageType:not(.hide)'), c => {
2018-10-23 01:05:09 +03:00
return {
2020-05-04 12:44:12 +02:00
Type: c.getAttribute('data-imagetype'),
2018-10-23 01:05:09 +03:00
Limit: c.checked ? 1 : 0,
MinWidth: 0
2019-10-09 18:59:17 +03:00
};
});
options.ImageOptions.push({
2020-05-04 12:44:12 +02:00
Type: 'Backdrop',
Limit: context.querySelector('#txtMaxBackdrops').value,
MinWidth: context.querySelector('#txtMinBackdropDownloadWidth').value
2019-10-09 18:59:17 +03:00
});
options.ImageOptions.push({
2020-05-04 12:44:12 +02:00
Type: 'Screenshot',
Limit: context.querySelector('#txtMaxScreenshots').value,
MinWidth: context.querySelector('#txtMinScreenshotDownloadWidth').value
2019-10-09 18:59:17 +03:00
});
2018-10-23 01:05:09 +03:00
}
2020-06-26 21:15:00 +03:00
export class showEditor {
constructor(itemType, options, availableOptions) {
return import('text!./components/imageOptionsEditor/imageOptionsEditor.template.html').then(({default: template}) => {
return new Promise((resolve) => {
const dlg = dialogHelper.createDialog({
size: 'small',
removeOnClose: true,
scrollY: false
});
dlg.classList.add('formDialog');
dlg.innerHTML = globalize.translateDocument(template);
dlg.addEventListener('close', () => {
saveValues(dlg, options);
});
loadValues(dlg, itemType, options, availableOptions);
dialogHelper.open(dlg).then(resolve, resolve);
dlg.querySelector('.btnCancel').addEventListener('click', () => {
dialogHelper.close(dlg);
});
2019-10-09 18:59:17 +03:00
});
2020-06-26 21:15:00 +03:00
});
2018-10-23 01:05:09 +03:00
}
}
2019-10-09 18:59:17 +03:00
/* eslint-enable indent */
2020-06-26 21:15:00 +03:00
export default showEditor;