Fix #4180: Tooltip requests can be spammed.

* Cancel pending ajax requests when mousing out of the thumbnail.
  Prevents multiple requests from piling up if the user moves in and out
  of the thumbnail before the first request completes. This normally
  isn't possible except during slowbooru.

* Show an error message if the ajax request fails unexpectedly.
This commit is contained in:
evazion
2019-10-06 02:03:55 -05:00
parent 61619b719e
commit d69e95a539

View File

@@ -6,18 +6,20 @@ require('qtip2/dist/jquery.qtip.css');
let PostTooltip = {};
PostTooltip.render_tooltip = function (event, qtip) {
PostTooltip.render_tooltip = async function (event, qtip) {
var post_id = $(this).parents("[data-id]").data("id");
$.get("/posts/" + post_id, { variant: "tooltip" }).then(function (html) {
try {
qtip.cache.request = $.get(`/posts/${post_id}?variant=tooltip`);
let html = await qtip.cache.request;
qtip.set("content.text", html);
qtip.elements.tooltip.removeClass("post-tooltip-loading");
// Hide the tooltip if the user stopped hovering before the ajax request completed.
if (PostTooltip.lostFocus) {
qtip.hide();
} catch (error) {
if (error.status !== 0 && error.statusText !== "abort") {
Utility.error(`Error displaying tooltip for post #${post_id} (error: ${error.status} ${error.statusText})`);
}
});
}
};
// Hide the tooltip the first time it is shown, while we wait on the ajax call to complete.
@@ -70,12 +72,15 @@ PostTooltip.initialize = function () {
} else {
$(this).qtip(PostTooltip.QTIP_OPTIONS, event);
}
PostTooltip.lostFocus = false;
});
// Cancel pending ajax requests when we mouse out of the thumbnail.
$(document).on("mouseleave.danbooru.postTooltip", PostTooltip.POST_SELECTOR, function (event) {
PostTooltip.lostFocus = true;
let qtip = $(event.target).qtip("api");
if (qtip.cache.request && qtip.cache.request.state() === "pending") {
qtip.cache.request.abort();
}
});
$(document).on("click.danbooru.postTooltip", ".post-tooltip-disable", PostTooltip.on_disable_tooltips);