Difference between revisions of "MediaWiki:Common.js"
From Game Logs
m (don't self-close tags) |
m (fix loading indicator for Grimoire) |
||
Line 32: | Line 32: | ||
var wrapper = $("<div></div>").addClass("dnd5-grimoire-wrapper").appendTo(link), | var wrapper = $("<div></div>").addClass("dnd5-grimoire-wrapper").appendTo(link), | ||
− | embed = $("<div></div>").addClass("dnd5-grimoire-embed dnd5-grimoire- | + | embed = $("<div></div>").addClass("dnd5-grimoire-embed dnd5-grimoire-loader").appendTo(wrapper); |
// Show the block first, better with loader. | // Show the block first, better with loader. | ||
Line 47: | Line 47: | ||
if (!article.length) | if (!article.length) | ||
return console.log("Failed to find the DOM chunk with spell description"); | return console.log("Failed to find the DOM chunk with spell description"); | ||
− | embed.removeClass(" | + | embed.removeClass("dnd5-grimoire-loader").append(article); |
}) | }) | ||
.fail(function() { | .fail(function() { |
Revision as of 23:54, 18 February 2018
/* Any JavaScript here will be loaded for all users on every page load. */ /* TEMPLATE:GRIMOIRE STARTS */ $(function() { // Activate only on 5ed pages. if ((wgCategories || []).indexOf("DnD 5") === -1) return; $(".dnd5-grimoire").on("mouseover", function(e) { // Only one popup allowed. $(".dnd5-grimoire .dnd5-grimoire-wrapper").hide(); var link = $(e.target).closest(".dnd5-grimoire"), embed = link.find(".dnd5-grimoire-embed"); // Toggle visibility of the info div if it's already been populated. if (embed.length) { if (embed.not(":visible")) embed.parent().show(); return; } // Otherwise load data externally. var spell = link.text().trim().toLowerCase() .replace(/[^\w\d\ ]/g, '') .replace(/\ +/g, '-'); if (!spell.length) return; var wrapper = $("<div></div>").addClass("dnd5-grimoire-wrapper").appendTo(link), embed = $("<div></div>").addClass("dnd5-grimoire-embed dnd5-grimoire-loader").appendTo(wrapper); // Show the block first, better with loader. // no Promises, damn. $.ajax({ "url": "//thebombzen.com/grimoire/spells/" + spell, "type": "get", "cache": true }) .then(function(data) { var doc = $(data); var article = doc.find("article"); if (!article.length) return console.log("Failed to find the DOM chunk with spell description"); embed.removeClass("dnd5-grimoire-loader").append(article); }) .fail(function() { embed.removeClass("loading").html("<p>Failed to look up this spell :(</p>"); }); }); $(".dnd5-grimoire").on("mouseout", function(e) { var target = $(e.target).closest(".dnd5-grimoire-wrapper"); if (!target.length) return; target.hide(); }); }); /* TEMPLATE:GRIMOIRE ENDS */