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 09:13, 3 April 2013 (Copy from User:Peterwhy/BSiconTooltips/sandbox.js). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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() {
	// Define the attribute name of the data- attribute, from base icon to highest icon
	var dataName = ["o0", "o1", "o2", "o3", "o4", "o5"];
	
	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;
		
		//
		// Load BS IDs given by data- attributes to an array
		//
		var givenBsIds = new Array(dataName.length);
		for (var i = 0; i < givenBsIds.length; i++) {
			var data = $(this).data(dataName[i]);
			if (data) {
				givenBsIds[i] = data;
			}
		}
		
		//
		// 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 = unescape(stringToParse.replace(/_/g," "));
			$(this).data("parsed-bsid", bsId);
			
			// Lookup given BSicon id
			var layer;
			if (index == iconElements.length - 1) {
				layer = 0;
			} else {
				var tempIndex = index;
				for (var layer = 1; layer < givenBsIds.length; layer++) {
					if (givenBsIds[layer] === undefined) {
						continue;
					}
					if (tempIndex == 0) {
						break;
					}
					tempIndex--
				}
				if (tempIndex != 0) {
					// Wrong information given by data
				}
			}
			
			$(this).data("given-bsid", 	givenBsIds[layer]);
			$(this).data("layer", layer);
			
			// Attach the label element
			var bsQuoteTooltip = $("<div/>").addClass("bs-quote-tooltip").hide();
			if (givenBsIds[layer] === bsId) {
				bsQuoteTooltip.text(givenBsIds[layer]);
			} else {
				bsQuoteTooltip.text(givenBsIds[layer] + " (" + bsId + ")").addClass("bs-inconsistent");
			}
			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);
})();