2019-10-31 12:54:46 -04:00
|
|
|
import {
|
|
|
|
failedCache,
|
|
|
|
localCache,
|
|
|
|
normalize,
|
|
|
|
resetFailedCache,
|
|
|
|
resetLocalCache,
|
|
|
|
setFailedCache,
|
|
|
|
setLocalCache,
|
|
|
|
} from "pretty-text/oneboxer-cache";
|
2019-10-30 09:48:24 -04:00
|
|
|
import { later } from "@ember/runloop";
|
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) {
|
|
|
|
$(img).addClass("size-resolved");
|
|
|
|
|
|
|
|
if (img.width > 0 && img.width === img.height) {
|
|
|
|
$(img).addClass("onebox-avatar");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect square images and apply smaller onebox-avatar class
|
2020-05-07 10:36:15 -04:00
|
|
|
function applySquareGenericOnebox($elem) {
|
2020-07-26 20:23:54 -04:00
|
|
|
if (!$elem.hasClass("allowlistedgeneric")) {
|
2017-11-12 19:19:06 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let $img = $elem.find(".onebox-body img.thumbnail");
|
|
|
|
let img = $img[0];
|
|
|
|
|
|
|
|
// already resolved... skip
|
|
|
|
if ($img.length !== 1 || $img.hasClass("size-resolved")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (img.complete) {
|
2020-05-07 10:36:15 -04:00
|
|
|
resolveSize(img);
|
2017-11-12 19:19:06 -05:00
|
|
|
} else {
|
|
|
|
$img.on("load.onebox", () => {
|
2020-05-07 10:36:15 -04:00
|
|
|
resolveSize(img);
|
2017-11-12 19:19:06 -05:00
|
|
|
$img.off("load.onebox");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-19 18:31:10 -05:00
|
|
|
function loadNext(ajax) {
|
|
|
|
if (loadingQueue.length === 0) {
|
|
|
|
timeout = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let timeoutMs = 150;
|
|
|
|
let removeLoading = true;
|
2018-02-19 16:40:14 -05: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
|
|
|
cache: true,
|
|
|
|
})
|
|
|
|
.then(
|
|
|
|
(html) => {
|
2017-11-12 19:19:06 -05:00
|
|
|
let $html = $(html);
|
2019-10-31 12:54:46 -04:00
|
|
|
setLocalCache(normalize(url), $html);
|
2017-11-12 19:19:06 -05:00
|
|
|
$elem.replaceWith($html);
|
2020-05-08 03:19:48 -04:00
|
|
|
applySquareGenericOnebox($html);
|
2016-12-19 18:31:10 -05:00
|
|
|
},
|
|
|
|
(result) => {
|
|
|
|
if (result && result.jqXHR && result.jqXHR.status === 429) {
|
|
|
|
timeoutMs = 2000;
|
|
|
|
removeLoading = false;
|
2018-02-19 16:40:14 -05: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(() => {
|
2019-10-30 09:48:24 -04:00
|
|
|
timeout = later(() => loadNext(ajax), timeoutMs);
|
2016-12-19 18:31:10 -05:00
|
|
|
if (removeLoading) {
|
2018-11-27 03:00:31 -05:00
|
|
|
$elem.removeClass(LOADING_ONEBOX_CSS_CLASS);
|
2016-12-20 05:18:03 -05:00
|
|
|
$elem.data("onebox-loaded");
|
2016-12-19 18:31:10 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-20 05:18:03 -05: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
|
|
|
}) {
|
2018-02-13 18:39:44 -05:00
|
|
|
const $elem = $(elem);
|
2016-06-14 14:31:51 -04:00
|
|
|
|
2016-10-24 06:46:22 -04:00
|
|
|
// If the onebox has loaded or is loading, return
|
2020-09-22 10:28:28 -04:00
|
|
|
if ($elem.data("onebox-loaded")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($elem.hasClass(LOADING_ONEBOX_CSS_CLASS)) {
|
|
|
|
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.
|
2017-06-12 12:13:05 -04:00
|
|
|
const cached = localCache[normalize(url)];
|
2020-09-22 10:28:28 -04:00
|
|
|
if (cached) {
|
|
|
|
return cached.prop("outerHTML");
|
|
|
|
}
|
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
|
2018-11-27 03:00:31 -05:00
|
|
|
$elem.addClass(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
|
2018-02-19 16:40:14 -05: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 {
|
2019-10-30 09:48:24 -04:00
|
|
|
timeout = timeout || later(() => loadNext(ajax), 150);
|
2016-12-19 21:26:53 -05:00
|
|
|
}
|
2016-06-14 14:31:51 -04:00
|
|
|
}
|