2021-12-17 03:25:34 -05:00
|
|
|
import domFromString from "discourse-common/lib/dom-from-string";
|
2019-10-31 12:54:46 -04:00
|
|
|
import {
|
|
|
|
failedCache,
|
2021-10-22 07:15:46 -04:00
|
|
|
lookupCache,
|
2019-10-31 12:54:46 -04:00
|
|
|
normalize,
|
|
|
|
resetFailedCache,
|
|
|
|
resetLocalCache,
|
|
|
|
setFailedCache,
|
|
|
|
setLocalCache,
|
|
|
|
} from "pretty-text/oneboxer-cache";
|
2022-07-16 18:50:49 -04:00
|
|
|
import discourseLater from "discourse-common/lib/later";
|
2019-10-31 12:54:46 -04:00
|
|
|
|
2016-12-19 18:31:10 -05:00
|
|
|
let timeout;
|
|
|
|
const loadingQueue = [];
|
2018-11-27 03:00:31 -05:00
|
|
|
|
|
|
|
export const LOADING_ONEBOX_CSS_CLASS = "loading-onebox";
|
|
|
|
|
|
|
|
export function resetCache() {
|
|
|
|
loadingQueue.clear();
|
2019-10-31 12:54:46 -04:00
|
|
|
resetLocalCache();
|
|
|
|
resetFailedCache();
|
2018-11-27 03:00:31 -05:00
|
|
|
}
|
2016-06-14 14:31:51 -04:00
|
|
|
|
2017-11-12 19:19:06 -05:00
|
|
|
function resolveSize(img) {
|
2021-10-22 07:15:46 -04:00
|
|
|
img.classList.add("size-resolved");
|
2017-11-12 19:19:06 -05:00
|
|
|
|
|
|
|
if (img.width > 0 && img.width === img.height) {
|
2021-10-22 07:15:46 -04:00
|
|
|
img.classList.add("onebox-avatar");
|
2017-11-12 19:19:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect square images and apply smaller onebox-avatar class
|
2021-10-22 07:15:46 -04:00
|
|
|
function applySquareGenericOnebox(elem) {
|
|
|
|
if (!elem.classList.contains("allowlistedgeneric")) {
|
2017-11-12 19:19:06 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-22 07:15:46 -04:00
|
|
|
let img = elem.querySelector(".onebox-body img.thumbnail");
|
2017-11-12 19:19:06 -05:00
|
|
|
|
|
|
|
// already resolved... skip
|
2021-10-22 07:15:46 -04:00
|
|
|
if (!img || img.classList.contains("size-resolved")) {
|
2017-11-12 19:19:06 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (img.complete) {
|
2020-05-07 10:36:15 -04:00
|
|
|
resolveSize(img);
|
2017-11-12 19:19:06 -05:00
|
|
|
} else {
|
2021-10-22 07:15:46 -04:00
|
|
|
img.addEventListener("load", _handleLoadingOneboxImages);
|
2017-11-12 19:19:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-22 07:15:46 -04:00
|
|
|
function _handleLoadingOneboxImages() {
|
|
|
|
resolveSize(this);
|
|
|
|
this.removeEventListener("load", _handleLoadingOneboxImages);
|
|
|
|
}
|
|
|
|
|
2016-12-19 18:31:10 -05:00
|
|
|
function loadNext(ajax) {
|
|
|
|
if (loadingQueue.length === 0) {
|
|
|
|
timeout = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let timeoutMs = 150;
|
|
|
|
let removeLoading = true;
|
2021-10-22 07:15:46 -04:00
|
|
|
const { url, refresh, elem, categoryId, topicId } = loadingQueue.shift();
|
2016-12-19 18:31:10 -05:00
|
|
|
|
|
|
|
// Retrieve the onebox
|
|
|
|
return ajax("/onebox", {
|
|
|
|
dataType: "html",
|
2018-02-19 16:40:14 -05:00
|
|
|
data: {
|
|
|
|
url,
|
|
|
|
refresh,
|
|
|
|
category_id: categoryId,
|
|
|
|
topic_id: topicId,
|
|
|
|
},
|
2016-12-19 18:31:10 -05:00
|
|
|
})
|
|
|
|
.then(
|
2021-12-17 03:25:34 -05:00
|
|
|
(template) => {
|
2022-01-12 06:49:24 -05:00
|
|
|
const node = domFromString(template)[0];
|
2021-10-22 07:15:46 -04:00
|
|
|
setLocalCache(normalize(url), node);
|
|
|
|
elem.replaceWith(node);
|
|
|
|
applySquareGenericOnebox(node);
|
2016-12-19 18:31:10 -05:00
|
|
|
},
|
|
|
|
(result) => {
|
2021-10-22 07:15:46 -04:00
|
|
|
if (result?.jqXHR?.status === 429) {
|
2016-12-19 18:31:10 -05:00
|
|
|
timeoutMs = 2000;
|
|
|
|
removeLoading = false;
|
2021-10-22 07:15:46 -04:00
|
|
|
loadingQueue.unshift({ url, refresh, elem, categoryId, topicId });
|
2016-12-19 18:31:10 -05:00
|
|
|
} else {
|
2019-11-26 00:49:58 -05:00
|
|
|
setFailedCache(normalize(url), true);
|
2016-12-19 18:31:10 -05:00
|
|
|
}
|
|
|
|
}
|
2018-06-15 11:03:24 -04:00
|
|
|
)
|
2016-12-19 18:31:10 -05:00
|
|
|
.finally(() => {
|
2022-07-16 18:50:49 -04:00
|
|
|
timeout = discourseLater(() => loadNext(ajax), timeoutMs);
|
2016-12-19 18:31:10 -05:00
|
|
|
if (removeLoading) {
|
2021-10-22 07:15:46 -04:00
|
|
|
elem.classList.remove(LOADING_ONEBOX_CSS_CLASS);
|
|
|
|
elem.dataset.oneboxLoaded = "";
|
2016-12-19 18:31:10 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-22 07:15:46 -04:00
|
|
|
// Perform a lookup of a onebox based an anchor element.
|
2016-10-24 06:46:22 -04:00
|
|
|
// It will insert a loading indicator and remove it when the loading is complete or fails.
|
2018-02-19 16:40:14 -05:00
|
|
|
export function load({
|
|
|
|
elem,
|
|
|
|
ajax,
|
|
|
|
topicId,
|
2021-02-18 09:07:26 -05:00
|
|
|
categoryId,
|
|
|
|
refresh = true,
|
|
|
|
offline = false,
|
|
|
|
synchronous = false,
|
2018-02-19 16:40:14 -05:00
|
|
|
}) {
|
2016-10-24 06:46:22 -04:00
|
|
|
// If the onebox has loaded or is loading, return
|
2021-10-22 07:15:46 -04:00
|
|
|
|
|
|
|
if (elem.dataset.oneboxLoaded) {
|
2020-09-22 10:28:28 -04:00
|
|
|
return;
|
|
|
|
}
|
2021-10-22 07:15:46 -04:00
|
|
|
|
|
|
|
if (elem.classList.contains(LOADING_ONEBOX_CSS_CLASS)) {
|
2020-09-22 10:28:28 -04:00
|
|
|
return;
|
|
|
|
}
|
2016-06-14 14:31:51 -04:00
|
|
|
|
2018-02-13 18:39:44 -05:00
|
|
|
const url = elem.href;
|
2016-06-14 14:31:51 -04:00
|
|
|
|
|
|
|
// Unless we're forcing a refresh...
|
|
|
|
if (!refresh) {
|
|
|
|
// If we have it in our cache, return it.
|
2021-10-22 07:15:46 -04:00
|
|
|
const cached = lookupCache(url);
|
2020-09-22 10:28:28 -04:00
|
|
|
if (cached) {
|
2021-10-22 07:15:46 -04:00
|
|
|
return cached;
|
2020-09-22 10:28:28 -04:00
|
|
|
}
|
2016-06-14 14:31:51 -04:00
|
|
|
|
|
|
|
// If the request failed, don't do anything
|
2017-06-12 12:13:05 -04:00
|
|
|
const failed = failedCache[normalize(url)];
|
2020-09-22 10:28:28 -04:00
|
|
|
if (failed) {
|
|
|
|
return;
|
|
|
|
}
|
2021-02-18 09:07:26 -05:00
|
|
|
|
|
|
|
if (offline) {
|
|
|
|
return;
|
|
|
|
}
|
2016-06-14 14:31:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the loading CSS class
|
2021-10-22 07:15:46 -04:00
|
|
|
elem.classList.add(LOADING_ONEBOX_CSS_CLASS);
|
2016-06-14 14:31:51 -04:00
|
|
|
|
2016-12-19 18:31:10 -05:00
|
|
|
// Add to the loading queue
|
2021-10-22 07:15:46 -04:00
|
|
|
loadingQueue.push({ url, refresh, elem, categoryId, topicId });
|
2016-12-19 18:31:10 -05:00
|
|
|
|
|
|
|
// Load next url in queue
|
2016-12-20 05:18:03 -05:00
|
|
|
if (synchronous) {
|
2016-12-19 21:26:53 -05:00
|
|
|
return loadNext(ajax);
|
2016-12-20 05:18:03 -05:00
|
|
|
} else {
|
2022-07-16 18:50:49 -04:00
|
|
|
timeout = timeout || discourseLater(() => loadNext(ajax), 150);
|
2016-12-19 21:26:53 -05:00
|
|
|
}
|
2016-06-14 14:31:51 -04:00
|
|
|
}
|