2019-10-05 02:09:52 +02:00
|
|
|
/**
|
|
|
|
* Fetch and display preview images for various image upload forms.
|
|
|
|
*/
|
|
|
|
|
2024-08-30 00:35:20 +02:00
|
|
|
import { assertNotNull } from './utils/assert';
|
2019-10-05 02:09:52 +02:00
|
|
|
import { fetchJson, handleError } from './utils/requests';
|
2021-10-10 00:50:57 +02:00
|
|
|
import { $, $$, clearEl, hideEl, makeEl, showEl } from './utils/dom';
|
2019-10-05 02:09:52 +02:00
|
|
|
import { addTag } from './tagsinput';
|
|
|
|
|
2021-10-06 03:31:50 +02:00
|
|
|
const MATROSKA_MAGIC = 0x1a45dfa3;
|
|
|
|
|
2019-10-05 02:09:52 +02:00
|
|
|
function scrapeUrl(url) {
|
2019-11-29 00:19:47 +01:00
|
|
|
return fetchJson('POST', '/images/scrape', { url })
|
2019-10-05 02:09:52 +02:00
|
|
|
.then(handleError)
|
|
|
|
.then(response => response.json());
|
|
|
|
}
|
|
|
|
|
2021-10-16 21:52:12 +02:00
|
|
|
function elementForEmbeddedImage({ camo_url, type }) {
|
2021-10-06 03:31:50 +02:00
|
|
|
// The upload was fetched from the scraper and is a path name
|
|
|
|
if (typeof camo_url === 'string') {
|
|
|
|
return makeEl('img', { className: 'scraper-preview--image', src: camo_url });
|
|
|
|
}
|
|
|
|
|
|
|
|
// The upload was fetched from a file input and is an ArrayBuffer
|
2021-10-16 21:52:12 +02:00
|
|
|
const objectUrl = URL.createObjectURL(new Blob([camo_url], { type }));
|
2021-10-06 03:31:50 +02:00
|
|
|
const tagName = new DataView(camo_url).getUint32(0) === MATROSKA_MAGIC ? 'video' : 'img';
|
|
|
|
return makeEl(tagName, { className: 'scraper-preview--image', src: objectUrl });
|
|
|
|
}
|
|
|
|
|
2019-10-05 02:09:52 +02:00
|
|
|
function setupImageUpload() {
|
|
|
|
const imgPreviews = $('#js-image-upload-previews');
|
|
|
|
if (!imgPreviews) return;
|
|
|
|
|
|
|
|
const form = imgPreviews.closest('form');
|
2021-10-10 00:50:57 +02:00
|
|
|
const [fileField, remoteUrl, scraperError] = $$('.js-scraper', form);
|
|
|
|
const descrEl = $('.js-image-descr-input', form);
|
|
|
|
const tagsEl = $('.js-image-tags-input', form);
|
2023-10-12 15:14:30 +02:00
|
|
|
const sourceEl = $$('.js-source-url', form).find(input => input.value === '');
|
2019-10-05 02:09:52 +02:00
|
|
|
const fetchButton = $('#js-scraper-preview');
|
2019-12-19 00:51:02 +01:00
|
|
|
if (!fetchButton) return;
|
2019-10-05 02:09:52 +02:00
|
|
|
|
|
|
|
function showImages(images) {
|
|
|
|
clearEl(imgPreviews);
|
|
|
|
|
|
|
|
images.forEach((image, index) => {
|
2021-10-06 03:31:50 +02:00
|
|
|
const img = elementForEmbeddedImage(image);
|
2019-10-05 02:09:52 +02:00
|
|
|
const imgWrap = makeEl('span', { className: 'scraper-preview--image-wrapper' });
|
|
|
|
imgWrap.appendChild(img);
|
|
|
|
|
|
|
|
const label = makeEl('label', { className: 'scraper-preview--label' });
|
|
|
|
const radio = makeEl('input', {
|
|
|
|
type: 'radio',
|
|
|
|
className: 'scraper-preview--input',
|
|
|
|
});
|
|
|
|
if (image.url) {
|
|
|
|
radio.name = 'scraper_cache';
|
|
|
|
radio.value = image.url;
|
|
|
|
}
|
|
|
|
if (index === 0) {
|
|
|
|
radio.checked = true;
|
|
|
|
}
|
|
|
|
label.appendChild(radio);
|
|
|
|
label.appendChild(imgWrap);
|
|
|
|
imgPreviews.appendChild(label);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
function showError() {
|
|
|
|
clearEl(imgPreviews);
|
|
|
|
showEl(scraperError);
|
|
|
|
enableFetch();
|
|
|
|
}
|
2024-07-04 02:27:59 +02:00
|
|
|
function hideError() {
|
|
|
|
hideEl(scraperError);
|
|
|
|
}
|
|
|
|
function disableFetch() {
|
|
|
|
fetchButton.setAttribute('disabled', '');
|
|
|
|
}
|
|
|
|
function enableFetch() {
|
|
|
|
fetchButton.removeAttribute('disabled');
|
|
|
|
}
|
2019-10-05 02:09:52 +02:00
|
|
|
|
|
|
|
const reader = new FileReader();
|
|
|
|
|
|
|
|
reader.addEventListener('load', event => {
|
2024-07-04 02:27:59 +02:00
|
|
|
showImages([
|
|
|
|
{
|
|
|
|
camo_url: event.target.result,
|
|
|
|
type: fileField.files[0].type,
|
|
|
|
},
|
|
|
|
]);
|
2019-10-05 02:09:52 +02:00
|
|
|
|
|
|
|
// Clear any currently cached data, because the file field
|
|
|
|
// has higher priority than the scraper:
|
|
|
|
remoteUrl.value = '';
|
2020-04-19 02:14:40 +02:00
|
|
|
disableFetch();
|
2019-10-05 02:09:52 +02:00
|
|
|
hideError();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Watch for files added to the form
|
2024-07-04 02:27:59 +02:00
|
|
|
fileField.addEventListener('change', () => {
|
|
|
|
fileField.files.length && reader.readAsArrayBuffer(fileField.files[0]);
|
|
|
|
});
|
2019-10-05 02:09:52 +02:00
|
|
|
|
|
|
|
// Watch for [Fetch] clicks
|
|
|
|
fetchButton.addEventListener('click', () => {
|
|
|
|
if (!remoteUrl.value) return;
|
|
|
|
|
|
|
|
disableFetch();
|
|
|
|
|
2024-07-04 02:27:59 +02:00
|
|
|
scrapeUrl(remoteUrl.value)
|
|
|
|
.then(data => {
|
|
|
|
if (data === null) {
|
|
|
|
scraperError.innerText = 'No image found at that address.';
|
|
|
|
showError();
|
|
|
|
return;
|
|
|
|
} else if (data.errors && data.errors.length > 0) {
|
|
|
|
scraperError.innerText = data.errors.join(' ');
|
|
|
|
showError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
hideError();
|
|
|
|
|
|
|
|
// Set source
|
|
|
|
if (sourceEl) sourceEl.value = sourceEl.value || data.source_url || '';
|
|
|
|
// Set description
|
|
|
|
if (descrEl) descrEl.value = descrEl.value || data.description || '';
|
|
|
|
// Add author
|
|
|
|
if (tagsEl && data.author_name) addTag(tagsEl, `artist:${data.author_name.toLowerCase()}`);
|
|
|
|
// Clear selected file, if any
|
|
|
|
fileField.value = '';
|
|
|
|
showImages(data.images);
|
|
|
|
|
|
|
|
enableFetch();
|
|
|
|
})
|
|
|
|
.catch(showError);
|
2019-10-05 02:09:52 +02:00
|
|
|
});
|
2020-04-19 02:14:40 +02:00
|
|
|
|
2021-02-20 20:24:18 +01:00
|
|
|
// Fetch on "enter" in url field
|
2021-10-09 21:58:07 +02:00
|
|
|
remoteUrl.addEventListener('keydown', event => {
|
2024-07-04 02:27:59 +02:00
|
|
|
if (event.keyCode === 13) {
|
|
|
|
// Hit enter
|
2021-02-20 20:24:18 +01:00
|
|
|
fetchButton.click();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-19 02:14:40 +02:00
|
|
|
// Enable/disable the fetch button based on content in the image scraper. Fetching with no URL makes no sense.
|
2024-04-23 00:43:36 +02:00
|
|
|
function setFetchEnabled() {
|
2020-06-09 07:56:33 +02:00
|
|
|
if (remoteUrl.value.length > 0) {
|
2020-04-19 02:14:40 +02:00
|
|
|
enableFetch();
|
2024-07-04 02:27:59 +02:00
|
|
|
} else {
|
2020-04-19 02:14:40 +02:00
|
|
|
disableFetch();
|
|
|
|
}
|
2020-05-13 20:05:47 +02:00
|
|
|
}
|
2020-08-11 07:43:19 +02:00
|
|
|
|
2024-04-23 00:43:36 +02:00
|
|
|
remoteUrl.addEventListener('input', () => setFetchEnabled());
|
|
|
|
setFetchEnabled();
|
|
|
|
|
2020-08-11 07:43:19 +02:00
|
|
|
// Catch unintentional navigation away from the page
|
|
|
|
|
|
|
|
function beforeUnload(event) {
|
|
|
|
// Chrome requires returnValue to be set
|
|
|
|
event.preventDefault();
|
|
|
|
event.returnValue = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
function registerBeforeUnload() {
|
|
|
|
window.addEventListener('beforeunload', beforeUnload);
|
|
|
|
}
|
|
|
|
|
|
|
|
function unregisterBeforeUnload() {
|
|
|
|
window.removeEventListener('beforeunload', beforeUnload);
|
|
|
|
}
|
|
|
|
|
2024-08-27 20:58:27 +02:00
|
|
|
function createTagError(message) {
|
|
|
|
const buttonAfter = $('#tagsinput-save');
|
2024-08-30 00:35:20 +02:00
|
|
|
const errorElement = makeEl('span', { className: 'help-block tag-error', innerText: message });
|
2024-08-27 23:26:59 +02:00
|
|
|
|
2024-08-28 00:24:03 +02:00
|
|
|
buttonAfter.insertAdjacentElement('beforebegin', errorElement);
|
2024-08-27 20:58:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function clearTagErrors() {
|
2024-08-28 00:24:03 +02:00
|
|
|
$$('.tag-error').forEach(el => el.remove());
|
2024-08-27 20:58:27 +02:00
|
|
|
}
|
|
|
|
|
2024-08-28 00:38:14 +02:00
|
|
|
const ratingsTags = ['safe', 'suggestive', 'questionable', 'explicit', 'semi-grimdark', 'grimdark', 'grotesque'];
|
|
|
|
|
2024-08-27 20:58:27 +02:00
|
|
|
// populate tag error helper bars as necessary
|
|
|
|
// return true if all checks pass
|
|
|
|
// return false if any check fails
|
|
|
|
function validateTags() {
|
2024-08-28 00:24:03 +02:00
|
|
|
const tagInput = $('textarea.js-taginput');
|
2024-08-27 23:26:59 +02:00
|
|
|
|
2024-08-28 00:24:03 +02:00
|
|
|
if (!tagInput) {
|
|
|
|
return true;
|
2024-08-27 20:58:27 +02:00
|
|
|
}
|
|
|
|
|
2024-08-28 00:24:03 +02:00
|
|
|
const tagsArr = tagInput.value.split(',').map(t => t.trim());
|
2024-08-27 20:58:27 +02:00
|
|
|
|
2024-08-27 21:41:45 +02:00
|
|
|
const errors = [];
|
2024-08-27 20:58:27 +02:00
|
|
|
|
|
|
|
let hasRating = false;
|
|
|
|
let hasSafe = false;
|
|
|
|
let hasOtherRating = false;
|
2024-08-27 23:26:59 +02:00
|
|
|
|
2024-08-27 21:41:45 +02:00
|
|
|
tagsArr.forEach(tag => {
|
|
|
|
if (ratingsTags.includes(tag)) {
|
2024-08-27 20:58:27 +02:00
|
|
|
hasRating = true;
|
2024-08-27 21:41:45 +02:00
|
|
|
if (tag === 'safe') {
|
2024-08-27 20:58:27 +02:00
|
|
|
hasSafe = true;
|
|
|
|
} else {
|
|
|
|
hasOtherRating = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!hasRating) {
|
2024-08-27 21:41:45 +02:00
|
|
|
errors.push('Tag input must contain at least one rating tag');
|
2024-08-27 20:58:27 +02:00
|
|
|
} else if (hasSafe && hasOtherRating) {
|
2024-08-27 21:41:45 +02:00
|
|
|
errors.push('Tag input may not contain any other rating if safe');
|
2024-08-27 20:58:27 +02:00
|
|
|
}
|
|
|
|
|
2024-08-27 21:41:45 +02:00
|
|
|
if (tagsArr.length < 3) {
|
|
|
|
errors.push('Tag input must contain at least 3 tags');
|
2024-08-27 20:58:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
errors.forEach(msg => createTagError(msg));
|
|
|
|
|
2024-08-27 21:41:45 +02:00
|
|
|
return errors.length === 0; // true: valid if no errors
|
2024-08-27 20:58:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function disableUploadButton() {
|
2024-08-28 01:16:13 +02:00
|
|
|
const submitButton = $('.button.input--separate-top');
|
2024-08-27 20:58:27 +02:00
|
|
|
if (submitButton !== null) {
|
|
|
|
submitButton.disabled = true;
|
2024-08-27 21:41:45 +02:00
|
|
|
submitButton.innerText = 'Please wait...';
|
2024-08-27 20:58:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// delay is needed because Safari stops the submit if the button is immediately disabled
|
|
|
|
requestAnimationFrame(() => submitButton.setAttribute('disabled', 'disabled'));
|
|
|
|
}
|
|
|
|
|
|
|
|
function submitHandler(event) {
|
2024-08-28 00:38:14 +02:00
|
|
|
// Remove any existing tag error elements
|
|
|
|
clearTagErrors();
|
2024-08-27 20:58:27 +02:00
|
|
|
|
2024-08-28 00:38:14 +02:00
|
|
|
if (validateTags()) {
|
|
|
|
// Disable navigation check
|
2024-08-27 20:58:27 +02:00
|
|
|
unregisterBeforeUnload();
|
|
|
|
|
2024-08-28 00:38:14 +02:00
|
|
|
// Prevent duplicate attempts to submit the form
|
2024-08-27 20:58:27 +02:00
|
|
|
disableUploadButton();
|
2024-08-27 21:41:45 +02:00
|
|
|
|
2024-08-28 00:38:14 +02:00
|
|
|
// Let the form submission complete
|
|
|
|
} else {
|
2024-08-28 00:52:11 +02:00
|
|
|
// Scroll to view validation errors
|
2024-08-30 00:35:20 +02:00
|
|
|
assertNotNull($('.fancy-tag-upload')).scrollIntoView();
|
2024-08-28 00:38:14 +02:00
|
|
|
|
|
|
|
// Prevent the form from being submitted
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
2024-08-27 20:58:27 +02:00
|
|
|
}
|
|
|
|
|
2020-08-11 07:43:19 +02:00
|
|
|
fileField.addEventListener('change', registerBeforeUnload);
|
|
|
|
fetchButton.addEventListener('click', registerBeforeUnload);
|
2024-08-27 20:58:27 +02:00
|
|
|
form.addEventListener('submit', submitHandler);
|
2019-10-05 02:09:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export { setupImageUpload };
|