2020-09-25 13:29:36 +02:00
|
|
|
import he from 'he';
|
|
|
|
|
2020-10-15 11:46:24 +02:00
|
|
|
/**
|
|
|
|
* This script is used to retrieve bookmarks metadata asynchronously:
|
|
|
|
* - title, description and keywords while creating a new bookmark
|
|
|
|
* - thumbnails while visiting the bookmark list
|
|
|
|
*
|
|
|
|
* Note: it should only be included if the user is logged in
|
|
|
|
* and the setting general.enable_async_metadata is enabled.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes given input loaders - used in edit link template.
|
|
|
|
*
|
|
|
|
* @param {object} loaders List of input DOM element that need to be cleared
|
|
|
|
*/
|
2020-09-25 13:29:36 +02:00
|
|
|
function clearLoaders(loaders) {
|
|
|
|
if (loaders != null && loaders.length > 0) {
|
|
|
|
[...loaders].forEach((loader) => {
|
|
|
|
loader.classList.remove('loading-input');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 11:46:24 +02:00
|
|
|
/**
|
|
|
|
* AJAX request to update the thumbnail of a bookmark with the provided ID.
|
|
|
|
* If a thumbnail is retrieved, it updates the divElement with the image src, and displays it.
|
|
|
|
*
|
|
|
|
* @param {string} basePath Shaarli subfolder for XHR requests
|
|
|
|
* @param {object} divElement Main <div> DOM element containing the thumbnail placeholder
|
|
|
|
* @param {int} id Bookmark ID to update
|
|
|
|
*/
|
|
|
|
function updateThumb(basePath, divElement, id) {
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
xhr.open('PATCH', `${basePath}/admin/shaare/${id}/update-thumbnail`);
|
|
|
|
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
|
|
|
|
xhr.responseType = 'json';
|
|
|
|
xhr.onload = () => {
|
|
|
|
if (xhr.status !== 200) {
|
|
|
|
alert(`An error occurred. Return code: ${xhr.status}`);
|
|
|
|
} else {
|
|
|
|
const { response } = xhr;
|
|
|
|
|
|
|
|
if (response.thumbnail !== false) {
|
|
|
|
const imgElement = divElement.querySelector('img');
|
|
|
|
|
|
|
|
imgElement.src = response.thumbnail;
|
|
|
|
imgElement.dataset.src = response.thumbnail;
|
|
|
|
imgElement.style.opacity = '1';
|
|
|
|
divElement.classList.remove('hidden');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
xhr.send();
|
|
|
|
}
|
|
|
|
|
2020-09-25 13:29:36 +02:00
|
|
|
(() => {
|
2020-10-15 11:46:24 +02:00
|
|
|
const basePath = document.querySelector('input[name="js_base_path"]').value;
|
2020-09-25 13:29:36 +02:00
|
|
|
const loaders = document.querySelectorAll('.loading-input');
|
2020-10-15 11:46:24 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* METADATA FOR EDIT BOOKMARK PAGE
|
|
|
|
*/
|
2020-09-25 13:29:36 +02:00
|
|
|
const inputTitle = document.querySelector('input[name="lf_title"]');
|
2020-10-15 11:46:24 +02:00
|
|
|
if (inputTitle != null) {
|
|
|
|
if (inputTitle.value.length > 0) {
|
|
|
|
clearLoaders(loaders);
|
|
|
|
return;
|
|
|
|
}
|
2020-09-25 13:29:36 +02:00
|
|
|
|
2020-10-15 11:46:24 +02:00
|
|
|
const url = document.querySelector('input[name="lf_url"]').value;
|
2020-09-25 13:29:36 +02:00
|
|
|
|
2020-10-15 11:46:24 +02:00
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
xhr.open('GET', `${basePath}/admin/metadata?url=${encodeURI(url)}`, true);
|
|
|
|
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
|
|
|
|
xhr.onload = () => {
|
|
|
|
const result = JSON.parse(xhr.response);
|
|
|
|
Object.keys(result).forEach((key) => {
|
|
|
|
if (result[key] !== null && result[key].length) {
|
|
|
|
const element = document.querySelector(`input[name="lf_${key}"], textarea[name="lf_${key}"]`);
|
|
|
|
if (element != null && element.value.length === 0) {
|
|
|
|
element.value = he.decode(result[key]);
|
|
|
|
}
|
2020-09-25 13:29:36 +02:00
|
|
|
}
|
2020-10-15 11:46:24 +02:00
|
|
|
});
|
|
|
|
clearLoaders(loaders);
|
|
|
|
};
|
2020-09-25 13:29:36 +02:00
|
|
|
|
2020-10-15 11:46:24 +02:00
|
|
|
xhr.send();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* METADATA FOR THUMBNAIL RETRIEVAL
|
|
|
|
*/
|
|
|
|
const thumbsToLoad = document.querySelectorAll('div[data-async-thumbnail]');
|
|
|
|
if (thumbsToLoad != null) {
|
|
|
|
[...thumbsToLoad].forEach((divElement) => {
|
|
|
|
const { id } = divElement.closest('[data-id]').dataset;
|
|
|
|
|
|
|
|
updateThumb(basePath, divElement, id);
|
|
|
|
});
|
|
|
|
}
|
2020-09-25 13:29:36 +02:00
|
|
|
})();
|