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 => {
|
|
|
|
uploads.forEach(upload => (_cache[upload.short_url] = upload.url));
|
|
|
|
urls.forEach(url => (_cache[url] = _cache[url] || "missing"));
|
|
|
|
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
|
|
|
|
|
|
|
function _loadCachedShortUrls($images) {
|
|
|
|
$images.each((idx, image) => {
|
|
|
|
let $image = $(image);
|
2018-06-15 23:03:24 +08:00
|
|
|
let 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-06-15 23:03:24 +08:00
|
|
|
const urls = _.map($images, img => $(img).data("orig-src"));
|
|
|
|
lookupUncachedUploadUrls(urls, ajax).then(() =>
|
|
|
|
_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-06-15 23:03:24 +08:00
|
|
|
Ember.run.debounce(
|
|
|
|
null,
|
|
|
|
() => {
|
|
|
|
_loadShortUrls($shortUploadUrls, ajax);
|
|
|
|
},
|
|
|
|
450,
|
|
|
|
true
|
|
|
|
);
|
2017-11-13 09:08:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|