From 0dcd3035462610299d046738d30ce2d16dc73a8b Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 28 Apr 2018 22:30:31 -0500 Subject: [PATCH] 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. --- app/assets/javascripts/post_tooltips.js.erb | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/post_tooltips.js.erb b/app/assets/javascripts/post_tooltips.js.erb index a167e97e4..14229c46e 100644 --- a/app/assets/javascripts/post_tooltips.js.erb +++ b/app/assets/javascripts/post_tooltips.js.erb @@ -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, }, };