MediaWiki:Gadget-tooltip.js: Difference between revisions
Jump to navigation
Jump to search
m Added Logging for tracing an error |
Changed to new pattern |
||
Line 12: | Line 12: | ||
*/ | */ | ||
( | if (!window.tooltipGadget) { | ||
var _options = { | var tooltipEngine = {}; | ||
tooltipEngine._options = { | |||
tooltipId: "tooltip", | tooltipId: "tooltip", | ||
offsetDefault: 15 | offsetDefault: 15 | ||
}; | }; | ||
tooltipEngine._tooltips = []; | |||
tooltipEngine._tooltipsTemp = null; | |||
function | tooltipEngine._bindTooltips = function(resetTooltips) { | ||
console.log("Booting up tooltips."); | console.log("Booting up tooltips."); | ||
if (resetTooltips) { | if (resetTooltips) { | ||
_tooltipsTemp = _tooltips.concat(); | tooltipEngine._tooltipsTemp = tooltipEngine._tooltips.concat(); | ||
_tooltips = []; | tooltipEngine._tooltips = []; | ||
} | } | ||
Line 44: | Line 46: | ||
var options; | var options; | ||
if (resetTooltips && _tooltipsTemp.length && _tooltipsTemp[idx] && _tooltipsTemp[idx].text) { | if (resetTooltips && tooltipEngine._tooltipsTemp.length && tooltipEngine._tooltipsTemp[idx] && tooltipEngine._tooltipsTemp[idx].text) { | ||
if (tooltipText.length === 0) { | if (tooltipText.length === 0) { | ||
tooltipText = _tooltipsTemp[idx].text; | tooltipText = _tooltipsTemp[idx].text; | ||
} | } | ||
elm.removeEventListener("mousemove", _onElementMouseMove); | elm.removeEventListener("mousemove", tooltipEngine._onElementMouseMove); | ||
elm.removeEventListener("mouseout", _onElementMouseOut); | elm.removeEventListener("mouseout", tooltipEngine._onElementMouseOut); | ||
elm.removeEventListener("mouseover", _onElementMouseOver); | elm.removeEventListener("mouseover", tooltipEngine._onElementMouseOver); | ||
} | } | ||
if (tooltipText) { | if (tooltipText) { | ||
elm.setAttribute("data-tooltip-id", idx); | elm.setAttribute("data-tooltip-id", idx); | ||
options = _parseOptions(elm.getAttribute("data-tooltip")); | options = tooltipEngine._parseOptions(elm.getAttribute("data-tooltip")); | ||
_tooltips[idx] = { | tooltipEngine._tooltips[idx] = { | ||
text: tooltipText, | text: tooltipText, | ||
options: options | options: options | ||
}; | }; | ||
elm.addEventListener("mousemove", _onElementMouseMove); | elm.addEventListener("mousemove", tooltipEngine._onElementMouseMove); | ||
elm.addEventListener("mouseout", _onElementMouseOut); | elm.addEventListener("mouseout", tooltipEngine._onElementMouseOut); | ||
elm.addEventListener("mouseover", _onElementMouseOver); | elm.addEventListener("mouseover", tooltipEngine._onElementMouseOver); | ||
} | } | ||
}); | }); | ||
if (resetTooltips) { | if (resetTooltips) { | ||
_tooltipsTemp = null; | tooltipEngine._tooltipsTemp = null; | ||
} | } | ||
} | }; | ||
function | tooltipEngine._createTooltip = function(text, tooltipId) { | ||
var tooltipElm = document.createElement("div"); | var tooltipElm = document.createElement("div"); | ||
var tooltipText = text; | var tooltipText = text; | ||
var options = tooltipId && _tooltips[tooltipId] && _tooltips[tooltipId].options; | var options = tooltipId && tooltipEngine._tooltips[tooltipId] && tooltipEngine._tooltips[tooltipId].options; | ||
if (options && options["class"]) { | if (options && options["class"]) { | ||
Line 83: | Line 85: | ||
} | } | ||
tooltipElm.setAttribute("id", _options.tooltipId); | tooltipElm.setAttribute("id", tooltipEngine._options.tooltipId); | ||
tooltipElm.appendChild(tooltipText); | tooltipElm.appendChild(tooltipText); | ||
document.querySelector("body").appendChild(tooltipElm); | document.querySelector("body").appendChild(tooltipElm); | ||
} | }; | ||
function | tooltipEngine._getTooltipElm = function() { | ||
return document.querySelector("#" + _options.tooltipId); | return document.querySelector("#" + tooltipEngine._options.tooltipId); | ||
} | }; | ||
function | tooltipEngine._onElementMouseMove = function(evt) { | ||
var tooltipId = this.getAttribute("data-tooltip-id"); | var tooltipId = this.getAttribute("data-tooltip-id"); | ||
var tooltipElm = _getTooltipElm(); | var tooltipElm = _getTooltipElm(); | ||
var options = tooltipId && _tooltips[tooltipId] && _tooltips[tooltipId].options; | var options = tooltipId && tooltipEngine._tooltips[tooltipId] && tooltipEngine._tooltips[tooltipId].options; | ||
var offset = options && options.offset || _options.offsetDefault; | var offset = options && options.offset || tooltipEngine._options.offsetDefault; | ||
var scrollY = window.scrollY || window.pageYOffset; | var scrollY = window.scrollY || window.pageYOffset; | ||
var scrollX = window.scrollX || window.pageXOffset; | var scrollX = window.scrollX || window.pageXOffset; | ||
Line 110: | Line 112: | ||
tooltipElm.style.left = tooltipLeft + "px"; | tooltipElm.style.left = tooltipLeft + "px"; | ||
} | } | ||
} | }; | ||
function | tooltipEngine._onElementMouseOut = function(evt) { | ||
var tooltipElm = _getTooltipElm(); | var tooltipElm = tooltipEngine._getTooltipElm(); | ||
if (tooltipElm) { | if (tooltipElm) { | ||
document.querySelector("body").removeChild(tooltipElm); | document.querySelector("body").removeChild(tooltipElm); | ||
} | } | ||
} | }; | ||
function | tooltipEngine._onElementMouseOver = function(evt) { | ||
var tooltipId = this.getAttribute("data-tooltip-id"); | var tooltipId = this.getAttribute("data-tooltip-id"); | ||
var tooltipText = tooltipId && _tooltips[tooltipId] && _tooltips[tooltipId].text; | var tooltipText = tooltipId && tooltipEngine._tooltips[tooltipId] && tooltipEngine._tooltips[tooltipId].text; | ||
if (tooltipText) { | if (tooltipText) { | ||
_createTooltip(tooltipText, tooltipId); | tooltipEngine._createTooltip(tooltipText, tooltipId); | ||
} | } | ||
} | }; | ||
function | tooltipEngine._parseOptions = function(options) { | ||
var optionsObj; | var optionsObj; | ||
Line 141: | Line 143: | ||
return optionsObj; | return optionsObj; | ||
} | }; | ||
function | tooltipEngine._init = function() { | ||
console.log("Loading tooltips..." | console.log("Loading tooltips..."); | ||
try { | try { | ||
document.addEventListener("DOMContentLoaded", _bindTooltips); | document.addEventListener("DOMContentLoaded", tooltipEngine._bindTooltips); | ||
} catch(err) { | } catch(err) { | ||
console.log(err); | console.log(err); | ||
Line 152: | Line 154: | ||
} | } | ||
window.tooltipGadget = tooltipEngine; | |||
} | |||
window.tooltipGadget._init(); | |||
Revision as of 12:38, 7 November 2019
/**
* Tooltip.js
* A basic script that applies a mouseover tooltip functionality to all elements of a page that have a data-tooltip attribute
* Matthias Schuetz, http://matthiasschuetz.com
*
* Copyright (C) Matthias Schuetz
* Free to use under the MIT license
*
* --------
*
* This is a modified version not using text but spans inside so I can display some more complex nodes.
*/
if (!window.tooltipGadget) {
var tooltipEngine = {};
tooltipEngine._options = {
tooltipId: "tooltip",
offsetDefault: 15
};
tooltipEngine._tooltips = [];
tooltipEngine._tooltipsTemp = null;
tooltipEngine._bindTooltips = function(resetTooltips) {
console.log("Booting up tooltips.");
if (resetTooltips) {
tooltipEngine._tooltipsTemp = tooltipEngine._tooltips.concat();
tooltipEngine._tooltips = [];
}
var tooltippedElements = document.querySelectorAll("[data-tooltip]");
console.log("Tooltip element count: ", tooltippedElements.length);
Array.prototype.forEach.call(tooltippedElements, function(elm, idx) {
var tooltipText = null;
var contents = elm.getElementsByClassName("tooltip-content");
if (contents.length > 0) {
tooltipText = contents[0];
elm.removeChild(contents[0]);
console.log("Tooltip set up for", elm);
} else {
console.log("No content found for tooltipped element:", elm);
}
var options;
if (resetTooltips && tooltipEngine._tooltipsTemp.length && tooltipEngine._tooltipsTemp[idx] && tooltipEngine._tooltipsTemp[idx].text) {
if (tooltipText.length === 0) {
tooltipText = _tooltipsTemp[idx].text;
}
elm.removeEventListener("mousemove", tooltipEngine._onElementMouseMove);
elm.removeEventListener("mouseout", tooltipEngine._onElementMouseOut);
elm.removeEventListener("mouseover", tooltipEngine._onElementMouseOver);
}
if (tooltipText) {
elm.setAttribute("data-tooltip-id", idx);
options = tooltipEngine._parseOptions(elm.getAttribute("data-tooltip"));
tooltipEngine._tooltips[idx] = {
text: tooltipText,
options: options
};
elm.addEventListener("mousemove", tooltipEngine._onElementMouseMove);
elm.addEventListener("mouseout", tooltipEngine._onElementMouseOut);
elm.addEventListener("mouseover", tooltipEngine._onElementMouseOver);
}
});
if (resetTooltips) {
tooltipEngine._tooltipsTemp = null;
}
};
tooltipEngine._createTooltip = function(text, tooltipId) {
var tooltipElm = document.createElement("div");
var tooltipText = text;
var options = tooltipId && tooltipEngine._tooltips[tooltipId] && tooltipEngine._tooltips[tooltipId].options;
if (options && options["class"]) {
tooltipElm.setAttribute("class", options["class"]);
}
tooltipElm.setAttribute("id", tooltipEngine._options.tooltipId);
tooltipElm.appendChild(tooltipText);
document.querySelector("body").appendChild(tooltipElm);
};
tooltipEngine._getTooltipElm = function() {
return document.querySelector("#" + tooltipEngine._options.tooltipId);
};
tooltipEngine._onElementMouseMove = function(evt) {
var tooltipId = this.getAttribute("data-tooltip-id");
var tooltipElm = _getTooltipElm();
var options = tooltipId && tooltipEngine._tooltips[tooltipId] && tooltipEngine._tooltips[tooltipId].options;
var offset = options && options.offset || tooltipEngine._options.offsetDefault;
var scrollY = window.scrollY || window.pageYOffset;
var scrollX = window.scrollX || window.pageXOffset;
var tooltipTop = evt.pageY + offset;
var tooltipLeft = evt.pageX + offset;
if (tooltipElm) {
tooltipTop = (tooltipTop - scrollY + tooltipElm.offsetHeight + 20 >= window.innerHeight ? (tooltipTop - tooltipElm.offsetHeight - 20) : tooltipTop);
tooltipLeft = (tooltipLeft - scrollX + tooltipElm.offsetWidth + 20 >= window.innerWidth ? (tooltipLeft - tooltipElm.offsetWidth - 20) : tooltipLeft);
tooltipElm.style.top = tooltipTop + "px";
tooltipElm.style.left = tooltipLeft + "px";
}
};
tooltipEngine._onElementMouseOut = function(evt) {
var tooltipElm = tooltipEngine._getTooltipElm();
if (tooltipElm) {
document.querySelector("body").removeChild(tooltipElm);
}
};
tooltipEngine._onElementMouseOver = function(evt) {
var tooltipId = this.getAttribute("data-tooltip-id");
var tooltipText = tooltipId && tooltipEngine._tooltips[tooltipId] && tooltipEngine._tooltips[tooltipId].text;
if (tooltipText) {
tooltipEngine._createTooltip(tooltipText, tooltipId);
}
};
tooltipEngine._parseOptions = function(options) {
var optionsObj;
if (options.length) {
try {
optionsObj = JSON.parse(options.replace(/'/ig, "\""));
} catch(err) {
console.log(err);
}
}
return optionsObj;
};
tooltipEngine._init = function() {
console.log("Loading tooltips...");
try {
document.addEventListener("DOMContentLoaded", tooltipEngine._bindTooltips);
} catch(err) {
console.log(err);
}
}
window.tooltipGadget = tooltipEngine;
}
window.tooltipGadget._init();