Files
danbooru/app/assets/javascripts/post_tooltips.js.erb
evazion 0dcd303546 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.
2018-04-28 23:49:03 -05:00

70 lines
1.9 KiB
Plaintext

Danbooru.PostTooltip = {};
Danbooru.PostTooltip.render_tooltip = function (event, qtip) {
var post_id = $(event.target).parents("[data-id]").data("id");
$.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
Danbooru.PostTooltip.QTIP_OPTIONS = {
style: "qtip-light post-tooltip",
content: Danbooru.PostTooltip.render_tooltip,
overwrite: false,
position: {
my: "top left",
at: "bottom left",
target: "mouse",
// viewport: $(window), // XXX broken on jquery-ui 1.11; add back after upgrading to jquery-ui 1.12.
adjust: {
mouse: false,
y: 25,
method: "shift",
},
},
show: {
solo: true,
delay: 450,
effect: false,
ready: true,
event: "mouseenter",
},
hide: {
delay: 50,
fixed: true,
effect: false,
event: "unfocus mouseleave",
},
events: {
show: Danbooru.PostTooltip.on_show,
},
};
Danbooru.PostTooltip.initialize = function () {
$(document).on("mouseenter", Danbooru.PostTooltip.POST_SELECTOR, function (event) {
$(this).qtip(Danbooru.PostTooltip.QTIP_OPTIONS, event);
});
// Hide tooltips when clicking thumbnails.
$(document).on("click", Danbooru.PostTooltip.POST_SELECTOR, Danbooru.PostTooltip.hide);
};
Danbooru.PostTooltip.hide = function (event) {
$(".post-tooltip:visible").qtip("hide");
};
$(document).ready(Danbooru.PostTooltip.initialize);