2017-08-23 04:40:01 +08:00
|
|
|
let _cache = {};
|
|
|
|
|
|
|
|
export function lookupCachedUploadUrl(shortUrl) {
|
|
|
|
return _cache[shortUrl];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function lookupUncachedUploadUrls(urls, ajax) {
|
2018-06-15 23:03:24 +08:00
|
|
|
return ajax("/uploads/lookup-urls", {
|
|
|
|
method: "POST",
|
|
|
|
data: { short_urls: urls }
|
|
|
|
}).then(uploads => {
|
2018-11-22 04:48:52 +08:00
|
|
|
uploads.forEach(upload =>
|
|
|
|
cacheShortUploadUrl(upload.short_url, upload.url)
|
|
|
|
);
|
|
|
|
urls.forEach(url =>
|
|
|
|
cacheShortUploadUrl(url, lookupCachedUploadUrl(url) || "missing")
|
|
|
|
);
|
2018-06-15 23:03:24 +08:00
|
|
|
return uploads;
|
|
|
|
});
|
2017-08-23 04:40:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function cacheShortUploadUrl(shortUrl, url) {
|
|
|
|
_cache[shortUrl] = url;
|
|
|
|
}
|
2017-11-13 09:08:11 +08:00
|
|
|
|
2018-11-22 04:48:52 +08:00
|
|
|
export function resetCache() {
|
|
|
|
_cache = {};
|
|
|
|
}
|
|
|
|
|
2017-11-13 09:08:11 +08:00
|
|
|
function _loadCachedShortUrls($images) {
|
|
|
|
$images.each((idx, image) => {
|
2018-11-22 04:48:52 +08:00
|
|
|
const $image = $(image);
|
|
|
|
const url = lookupCachedUploadUrl($image.data("orig-src"));
|
|
|
|
|
2017-11-13 09:08:11 +08:00
|
|
|
if (url) {
|
2018-06-15 23:03:24 +08:00
|
|
|
$image.removeAttr("data-orig-src");
|
2017-11-13 09:08:11 +08:00
|
|
|
if (url !== "missing") {
|
2018-06-15 23:03:24 +08:00
|
|
|
$image.attr("src", url);
|
2017-11-13 09:08:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function _loadShortUrls($images, ajax) {
|
2018-11-21 09:51:14 +08:00
|
|
|
const urls = $images.toArray().map(img => $(img).data("orig-src"));
|
2018-11-22 04:48:52 +08:00
|
|
|
return lookupUncachedUploadUrls(urls, ajax).then(() =>
|
2018-06-15 23:03:24 +08:00
|
|
|
_loadCachedShortUrls($images)
|
|
|
|
);
|
2017-11-13 09:08:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function resolveAllShortUrls(ajax) {
|
2018-06-15 23:03:24 +08:00
|
|
|
let $shortUploadUrls = $("img[data-orig-src]");
|
2017-11-13 09:08:11 +08:00
|
|
|
|
|
|
|
if ($shortUploadUrls.length > 0) {
|
|
|
|
_loadCachedShortUrls($shortUploadUrls);
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
$shortUploadUrls = $("img[data-orig-src]");
|
2017-11-13 09:08:11 +08:00
|
|
|
if ($shortUploadUrls.length > 0) {
|
|
|
|
// this is carefully batched so we can do a leading debounce (trigger right away)
|
2018-11-22 04:48:52 +08:00
|
|
|
return Ember.run.debounce(
|
2018-06-15 23:03:24 +08:00
|
|
|
null,
|
2018-11-22 04:48:52 +08:00
|
|
|
() => _loadShortUrls($shortUploadUrls, ajax),
|
2018-06-15 23:03:24 +08:00
|
|
|
450,
|
|
|
|
true
|
|
|
|
);
|
2017-11-13 09:08:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|