Welcome to IOPWiki, Commander.
We are searching for new editors to keep track of Girls' Frontline 2 content, as well as veteran players to complete existing Girls' Frontline and Project Neural Cloud articles.
You can contribute without an account. Learn how to contribute and join our Discord server.

MediaWiki:Gadget-tooltip.js: Difference between revisions

Welcome to IOP Wiki. This website is maintained by the Girls' Frontline community and is free to edit by anyone.
Jump to navigation Jump to search
Initial setup
 
m Changed div to span to try something
 
(14 intermediate revisions by the same user not shown)
Line 6: Line 6:
  * Copyright (C) Matthias Schuetz
  * Copyright (C) Matthias Schuetz
  * Free to use under the MIT license
  * 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.
  */
  */


(function (root, factory) {
if (!window.tooltipEngine) {
if (typeof define === "function" && define.amd) {
window.tooltipEngine = {};
// AMD. Register as an anonymous module.
 
define(factory);
window.tooltipEngine._options = {
} else if (!root.tooltip) {
// Browser globals
root.tooltip = factory(root);
}
}(this, function() {
var _options = {
tooltipId: "tooltip",
tooltipId: "tooltip",
offsetDefault: 15
offsetDefault: 15
};
};


var _tooltips = [];
window.tooltipEngine._tooltips = [];
var _tooltipsTemp = null;
window.tooltipEngine._tooltipsTemp = null;


function _bindTooltips(resetTooltips) {
window.tooltipEngine._bindTooltips = function(resetTooltips) {
console.log("Booting up tooltips.");
if (resetTooltips) {
if (resetTooltips) {
_tooltipsTemp = _tooltips.concat();
window.tooltipEngine._tooltipsTemp = window.tooltipEngine._tooltips.concat();
_tooltips = [];
window.tooltipEngine._tooltips = [];
}
}
var tooltippedElements = document.querySelectorAll("[data-tooltip]");
console.log("Tooltip element count: ", tooltippedElements.length);


Array.prototype.forEach.call(document.querySelectorAll("[data-tooltip]"), function(elm, idx) {
Array.prototype.forEach.call(tooltippedElements, function(elm, idx) {
var tooltipText = elm.getAttribute("title").trim();
var tooltipText = null;
var contents = elm.getElementsByClassName("tooltip-content");
if (contents.length > 0) {
tooltipText = contents[0];
$(contents[0]).removeClass("uninitialized");
elm.removeChild(contents[0]);
console.log("Tooltip set up for", elm);
} else {
console.log("No content found for tooltipped element:", elm);
}
var options;
var options;


if (resetTooltips && _tooltipsTemp.length && _tooltipsTemp[idx] && _tooltipsTemp[idx].text) {
if (resetTooltips && window.tooltipEngine._tooltipsTemp.length && window.tooltipEngine._tooltipsTemp[idx] && window.tooltipEngine._tooltipsTemp[idx].text) {
if (tooltipText.length === 0) {
if (tooltipText.length === 0) {
elm.setAttribute("title", _tooltipsTemp[idx].text);
tooltipText = _tooltipsTemp[idx].text;
tooltipText = _tooltipsTemp[idx].text;
}
}


elm.removeEventListener("mousemove", _onElementMouseMove);
elm.removeEventListener("mousemove", window.tooltipEngine._onElementMouseMove);
elm.removeEventListener("mouseout", _onElementMouseOut);
elm.removeEventListener("mouseout", window.tooltipEngine._onElementMouseOut);
elm.removeEventListener("mouseover", _onElementMouseOver);
elm.removeEventListener("mouseover", window.tooltipEngine._onElementMouseOver);
}
}


if (tooltipText) {
if (tooltipText) {
elm.setAttribute("title", "");
elm.setAttribute("data-tooltip-id", idx);
elm.setAttribute("data-tooltip-id", idx);
options = _parseOptions(elm.getAttribute("data-tooltip"));
options = window.tooltipEngine._parseOptions(elm.getAttribute("data-tooltip"));
_tooltips[idx] = {
window.tooltipEngine._tooltips[idx] = {
text: tooltipText,
text: tooltipText,
options: options
options: options
};
};


elm.addEventListener("mousemove", _onElementMouseMove);
elm.addEventListener("mousemove", window.tooltipEngine._onElementMouseMove);
elm.addEventListener("mouseout", _onElementMouseOut);
elm.addEventListener("mouseout", window.tooltipEngine._onElementMouseOut);
elm.addEventListener("mouseover", _onElementMouseOver);
elm.addEventListener("mouseover", window.tooltipEngine._onElementMouseOver);
}
}
});
});


if (resetTooltips) {
if (resetTooltips) {
_tooltipsTemp = null;
window.tooltipEngine._tooltipsTemp = null;
}
}
}
};


function _createTooltip(text, tooltipId) {
window.tooltipEngine._createTooltip = function(text, tooltipId) {
var tooltipElm = document.createElement("div");
var tooltipElm = document.createElement("span");
var tooltipText = document.createTextNode(text);
var tooltipText = text;
var options = tooltipId && _tooltips[tooltipId] && _tooltips[tooltipId].options;
var options = tooltipId && window.tooltipEngine._tooltips[tooltipId] && window.tooltipEngine._tooltips[tooltipId].options;


if (options && options["class"]) {
if (options && options["class"]) {
Line 76: Line 86:
}
}


tooltipElm.setAttribute("id", _options.tooltipId);
tooltipElm.setAttribute("id", window.tooltipEngine._options.tooltipId);
tooltipElm.appendChild(tooltipText);
tooltipElm.appendChild(tooltipText);


document.querySelector("body").appendChild(tooltipElm);
document.querySelector("body").appendChild(tooltipElm);
}
};


function _getTooltipElm() {
window.tooltipEngine._getTooltipElm = function() {
return document.querySelector("#" + _options.tooltipId);
return document.querySelector("#" + window.tooltipEngine._options.tooltipId);
}
};


function _onElementMouseMove(evt) {
window.tooltipEngine._onElementMouseMove = function(evt) {
var tooltipId = this.getAttribute("data-tooltip-id");
var tooltipId = this.getAttribute("data-tooltip-id");
var tooltipElm = _getTooltipElm();
var tooltipElm = window.tooltipEngine._getTooltipElm();
var options = tooltipId && _tooltips[tooltipId] && _tooltips[tooltipId].options;
var options = tooltipId && window.tooltipEngine._tooltips[tooltipId] && window.tooltipEngine._tooltips[tooltipId].options;
var offset = options && options.offset || _options.offsetDefault;
var offset = options && options.offset || window.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 103: Line 113:
tooltipElm.style.left = tooltipLeft + "px";
tooltipElm.style.left = tooltipLeft + "px";
}
}
}
};


function _onElementMouseOut(evt) {
window.tooltipEngine._onElementMouseOut = function(evt) {
var tooltipElm = _getTooltipElm();
var tooltipElm = window.tooltipEngine._getTooltipElm();


if (tooltipElm) {
if (tooltipElm) {
document.querySelector("body").removeChild(tooltipElm);
document.querySelector("body").removeChild(tooltipElm);
}
}
}
};


function _onElementMouseOver(evt) {
window.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 && window.tooltipEngine._tooltips[tooltipId] && window.tooltipEngine._tooltips[tooltipId].text;


if (tooltipText) {
if (tooltipText) {
_createTooltip(tooltipText, tooltipId);
window.tooltipEngine._createTooltip(tooltipText, tooltipId);
}
}
}
};


function _parseOptions(options) {
window.tooltipEngine._parseOptions = function(options) {
var optionsObj;
var optionsObj;


Line 134: Line 144:


return optionsObj;
return optionsObj;
}
};
 
}
function _init() {
window.addEventListener("load", _bindTooltips);
}
 
_init();


return {
RLQ.push(['jquery', function () {
setOptions: function(options) {
  $(document).ready(function() {
for (var option in options) {
console.log("Loading tooltips...");
if (_options.hasOwnProperty(option)) {
  window.tooltipEngine._bindTooltips();
_options[option] = options[option];
  });
}
}]);
}
},
refresh: function() {
_bindTooltips(true);
}
};
}));

Latest revision as of 19:33, 11 December 2024

/**
 * 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.tooltipEngine) {
	window.tooltipEngine = {};

	window.tooltipEngine._options = {
		tooltipId: "tooltip",
		offsetDefault: 15
	};

	window.tooltipEngine._tooltips = [];
	window.tooltipEngine._tooltipsTemp = null;

	window.tooltipEngine._bindTooltips = function(resetTooltips) {
		console.log("Booting up tooltips.");
		
		if (resetTooltips) {
			window.tooltipEngine._tooltipsTemp = window.tooltipEngine._tooltips.concat();
			window.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];
				$(contents[0]).removeClass("uninitialized");
				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 && window.tooltipEngine._tooltipsTemp.length && window.tooltipEngine._tooltipsTemp[idx] && window.tooltipEngine._tooltipsTemp[idx].text) {
				if (tooltipText.length === 0) {
					tooltipText = _tooltipsTemp[idx].text;
				}

				elm.removeEventListener("mousemove", window.tooltipEngine._onElementMouseMove);
				elm.removeEventListener("mouseout", window.tooltipEngine._onElementMouseOut);
				elm.removeEventListener("mouseover", window.tooltipEngine._onElementMouseOver);
			}

			if (tooltipText) {
				elm.setAttribute("data-tooltip-id", idx);
				options = window.tooltipEngine._parseOptions(elm.getAttribute("data-tooltip"));
				
				window.tooltipEngine._tooltips[idx] = {
					text: tooltipText,
					options: options
				};

				elm.addEventListener("mousemove", window.tooltipEngine._onElementMouseMove);
				elm.addEventListener("mouseout", window.tooltipEngine._onElementMouseOut);
				elm.addEventListener("mouseover", window.tooltipEngine._onElementMouseOver);
			}
		});

		if (resetTooltips) {
			window.tooltipEngine._tooltipsTemp = null;
		}
	};

	window.tooltipEngine._createTooltip = function(text, tooltipId) {
		var tooltipElm = document.createElement("span");
		var tooltipText = text;
		var options = tooltipId && window.tooltipEngine._tooltips[tooltipId] && window.tooltipEngine._tooltips[tooltipId].options;

		if (options && options["class"]) {
			tooltipElm.setAttribute("class", options["class"]);
		}

		tooltipElm.setAttribute("id", window.tooltipEngine._options.tooltipId);
		tooltipElm.appendChild(tooltipText);

		document.querySelector("body").appendChild(tooltipElm);
	};

	window.tooltipEngine._getTooltipElm = function() {
		return document.querySelector("#" + window.tooltipEngine._options.tooltipId);
	};

	window.tooltipEngine._onElementMouseMove = function(evt) {
		var tooltipId = this.getAttribute("data-tooltip-id");
		var tooltipElm = window.tooltipEngine._getTooltipElm();
		var options = tooltipId && window.tooltipEngine._tooltips[tooltipId] && window.tooltipEngine._tooltips[tooltipId].options;
		var offset = options && options.offset || window.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";
		}
	};

	window.tooltipEngine._onElementMouseOut = function(evt) {
		var tooltipElm = window.tooltipEngine._getTooltipElm();

		if (tooltipElm) {
			document.querySelector("body").removeChild(tooltipElm);
		}
	};

	window.tooltipEngine._onElementMouseOver = function(evt) {
		var tooltipId = this.getAttribute("data-tooltip-id");
		var tooltipText = tooltipId && window.tooltipEngine._tooltips[tooltipId] && window.tooltipEngine._tooltips[tooltipId].text;

		if (tooltipText) {
			window.tooltipEngine._createTooltip(tooltipText, tooltipId);	
		}
	};

	window.tooltipEngine._parseOptions = function(options) {
		var optionsObj;

		if (options.length) {
			try {
				optionsObj = JSON.parse(options.replace(/'/ig, "\""));
			} catch(err) {
				console.log(err);
			}
		}

		return optionsObj;
	};
}

RLQ.push(['jquery', function () {
  $(document).ready(function() {
	console.log("Loading tooltips...");
  	window.tooltipEngine._bindTooltips();
  });
}]);