post tooltips: cache html after first api call (#3689).

Avoid triggering the API call each time you hover over the same post.
Overwrite `content.text` to save the results after the first call.
This commit is contained in:
evazion
2018-04-28 22:30:31 -05:00
parent e5db839e54
commit 0dcd303546

View File

@@ -3,11 +3,20 @@ Danbooru.PostTooltip = {};
Danbooru.PostTooltip.render_tooltip = function (event, qtip) {
var post_id = $(event.target).parents("[data-id]").data("id");
return $.get("/posts/" + post_id, { variant: "tooltip" }).done(function () {
$.get("/posts/" + post_id, { variant: "tooltip" }).then(function (html) {
qtip.set("content.text", html);
qtip.elements.tooltip.removeClass("post-tooltip-loading");
});
};
// Hide the tooltip the first time it is shown, while we wait on the ajax call to complete.
Danbooru.PostTooltip.on_show = function (event, qtip) {
if (!qtip.cache.hasBeenShown) {
qtip.elements.tooltip.addClass("post-tooltip-loading");
qtip.cache.hasBeenShown = true;
}
};
Danbooru.PostTooltip.POST_SELECTOR = "*:not(.ui-sortable-handle) > .post-preview img";
// http://qtip2.com/options
@@ -40,7 +49,7 @@ Danbooru.PostTooltip.QTIP_OPTIONS = {
event: "unfocus mouseleave",
},
events: {
show: function (event, qtip) { qtip.elements.tooltip.addClass("post-tooltip-loading"); },
show: Danbooru.PostTooltip.on_show,
},
};