2019-06-11 09:13:39 +08:00
|
|
|
const _cache = {};
|
2018-11-27 16:00:31 +08:00
|
|
|
|
2020-02-12 18:11:28 +08:00
|
|
|
export function applyInlineOneboxes(inline, ajax, opts) {
|
|
|
|
opts = opts || {};
|
|
|
|
|
2018-01-30 05:39:41 +08:00
|
|
|
Object.keys(inline).forEach(url => {
|
|
|
|
// cache a blank locally, so we never trigger a lookup
|
|
|
|
_cache[url] = {};
|
|
|
|
});
|
|
|
|
|
2017-07-20 03:08:54 +08:00
|
|
|
return ajax("/inline-onebox", {
|
2020-02-12 18:11:28 +08:00
|
|
|
data: {
|
|
|
|
urls: Object.keys(inline),
|
|
|
|
category_id: opts.categoryId,
|
|
|
|
topic_id: opts.topicId
|
|
|
|
}
|
2017-07-20 03:08:54 +08:00
|
|
|
}).then(result => {
|
2018-06-15 23:03:24 +08:00
|
|
|
result["inline-oneboxes"].forEach(onebox => {
|
2018-11-13 04:14:20 +08:00
|
|
|
if (onebox.title) {
|
|
|
|
_cache[onebox.url] = onebox;
|
|
|
|
let links = inline[onebox.url] || [];
|
|
|
|
links.forEach(link => {
|
2019-06-11 09:13:39 +08:00
|
|
|
$(link)
|
|
|
|
.text(onebox.title)
|
2020-05-08 04:08:48 +08:00
|
|
|
.addClass("inline-onebox")
|
|
|
|
.removeClass("inline-onebox-loading");
|
2018-11-13 04:14:20 +08:00
|
|
|
});
|
|
|
|
}
|
2017-07-20 03:08:54 +08:00
|
|
|
});
|
|
|
|
});
|
2018-06-15 23:03:24 +08:00
|
|
|
}
|
2017-07-20 03:08:54 +08:00
|
|
|
|
|
|
|
export function cachedInlineOnebox(url) {
|
|
|
|
return _cache[url];
|
|
|
|
}
|
2019-06-11 09:13:39 +08:00
|
|
|
|
|
|
|
export function applyCachedInlineOnebox(url, onebox) {
|
|
|
|
return (_cache[url] = onebox);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function deleteCachedInlineOnebox(url) {
|
|
|
|
return delete _cache[url];
|
|
|
|
}
|