Jump to content

User:Peterwhy/BSiconTooltips.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Peterwhy (talk | contribs) at 17:25, 23 August 2013 (Copy from User:Peterwhy/BSiconTooltips/sandbox.js: remove support for custom data attributes). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// [[User:Peterwhy/BSiconTooltips]]
// with stylesheet [[User:Peterwhy/BSiconTooltips.css]]
importStylesheet("User:Peterwhy/BSiconTooltips.css");

(function() {
	var load = function() {
		// Do setup for each bs-overlap cell
		$(".bs-overlap").each(eachSetup);
		// Attach listener for future addition of .bs-overlap
		$("#content").on("mouseenter", ".bs-overlap:not(bs-setup-done)", eachSetup);
	};
	
	var eachSetup = function (event) {
		// check if this table element has been set up
		if ($(this).hasClass("bs-setup-done")) {
			return;
		}
		$(this).addClass("bs-setup-done");
		
		// if first time run this
		var _this = this;
		var showTimer, hideTimer;
		var labelElements, iconElements;
		var iconHeight, iconWidth;
		
		//
		// Add label boxes
		//
		iconElements = $(this).find("img,a.new");
		iconElements.each(function(index) {
			// Tag elements with class
			$(this).addClass("bs-icon");
			if (index == iconElements.length - 1) {
				$(this).addClass("bs-base");
			} else {
				$(this).addClass("bs-superimpose");
				$(this).closest("div").addClass("bs-superimpose-wrapper");
			}
			
			// Get BSicon id from img src or a href (if icon name is not valid)
			var tagName = $(this).prop("tagName").toLowerCase()
			var stringToParse, bsId;
			if (tagName == "a") {
				stringToParse = $(this).attr("href");
			} else if (tagName == "img") {
				stringToParse = $(this).attr("src");
			}
			stringToParse = stringToParse.split("BSicon_",2)[1].split(".svg",1)[0];
			bsId = decodeURIComponent(stringToParse.replace(/_/g," "));
			$(this).data("parsed-bsid", bsId);
			
			// Attach the label element
			var bsQuoteTooltip = $("<div/>").addClass("bs-quote-tooltip").hide().text(bsId);
			if (tagName == "a") {
				bsQuoteTooltip.addClass("bs-no-file");
			}
			
			var bsQuoteLink = $("<a/>", {
				href:"/wiki/File:BSicon_" + stringToParse + ".svg",
				title:"File:BSicon " + bsId + ".svg"
			}).addClass("bs-quote-link").append(bsQuoteTooltip);
			
			if ($(this).parent().prop("tagName").toLowerCase() == "a") {
				$(this).addClass("bs-has-link");
				$(this).parent().before(bsQuoteLink);
			} else {
				$(this).before(bsQuoteLink);
			}
		});
		
		// Tag for empty icon cell, i.e. no icon
		if (iconElements.length == 0) {
			$(this).addClass("bs-empty");
		}
		
		labelElements = $(this).find(".bs-quote-tooltip");
		
		iconHeight = iconElements.last().height();
		iconWidth = iconElements.last().width();
		
		//
		// Set up show/hide behaviours
		//
		var show = function () {
			$(_this).find(".bs-superimpose").each(function(index) {
				var thisIconWidth = $(this).width();
				var transformOriginX = Math.round(1300*iconHeight/thisIconWidth) + "%";
				$(this).closest(".bs-superimpose-wrapper").css({
					"transform-origin": (transformOriginX + " 50%"),
					"-ms-transform-origin": (transformOriginX + " 50%"),
					"-moz-transform-origin": (transformOriginX + " 50%"),
					"-o-transform-origin": (transformOriginX + " 50%"),
					"-webkit-transform-origin": (transformOriginX + " 50%")
				});
			});
			labelElements.fadeIn(200);
			$(_this).addClass("selected");
			clearTimeout(hideTimer);
		}
		var hide = function () {
			$(_this).removeClass("selected");
			labelElements.fadeOut(100);
		}
		
		//
		// Set up mouse event handlers
		//
		var mouseEnter = function(event) {
			clearTimeout(showTimer);
			clearTimeout(hideTimer);
			
			showTimer = setTimeout(show, 200);
		};
		var mouseLeave = function(event) {
			clearTimeout(showTimer);
			hideTimer = setTimeout(hide, 500);
		}
		$(this).hover(mouseEnter, mouseLeave);
		
		//
		// Directly run mouseEnter after first hover
		//
		if (event && event.type == "mouseenter") {
			mouseEnter(event);
		}
	};
	
	$(document).ready(load);
})();