Järjestelmäviesti:Gadget-defaultVisibilityToggles.js

Huomautus: Selaimen välimuisti pitää tyhjentää asetusten tallentamisen jälkeen, jotta muutokset tulisivat voimaan.

  • Firefox ja Safari: Napsauta Shift-näppäin pohjassa Päivitä, tai paina Ctrl-F5 tai Ctrl-R (⌘-R Macilla)
  • Google Chrome: Paina Ctrl-Shift-R (⌘-Shift-R Macilla)
  • Edge: Napsauta Ctrl-näppäin pohjassa Päivitä tai paina Ctrl-F5
/* jshint undef: true, esversion: 5 */
/* globals $, jQuery, mw, window, getComputedStyle */

(function defaultVisibilityTogglesIIFE() {
"use strict";

if (window.noDefaultVisibilityToggles) return;

/* == NavBars == */
var NavigationBarHide = "piilota ▲";
var NavigationBarShow = "näytä ▼";

// Check if an element has been activated with a toggle.
// For convenience, this has the side effect of marking the element as having
// a toggle, if it is not already marked.
// Allows the functions to avoid toggleifying elements more than once, which
// can lead to multiple "show" buttons, for instance.
// The argument must be an Element, not a jQuery object.
function checkAndSetToggleified(element) {
	if (element.isToggleified) {
		return true;
	}
	element.isToggleified = true;
}

function getToggleCategory(element, defaultCategory) {
	if ($(element).find("table").first().is(".translations"))
		return "translations";
	
	var heading = element;
	while ((heading = heading.previousElementSibling)) {
		// tagName is always uppercase:
		// https://developer.mozilla.org/en-US/docs/Web/API/Element/tagName
		var num = heading.tagName.match(/H(\d)/);
		if (num)
			num = Number(num[1]);
		else
			continue;
		if (4 <= num && num <= 6) {
			if (heading.getElementsByTagName("span")[1])
				heading = heading.getElementsByTagName("span")[0];
			var text = jQuery(heading).text()
				.toLowerCase()
				// jQuery's .text() is inconsistent about whitespace:
				.replace(/^\s+|\s+$/g, "").replace(/\s+/g, " ")
				// remove numbers added by the "Auto-number headings" pref:
				.replace(/^[1-9][0-9.]+ ?/, "");
			// Toggle category must be convertible to a valid CSS identifier so
			// that it can be used in an id selector in jQuery in
			// ToggleCategory.prototype.newSidebarToggle
			// in [[MediaWiki:Gadget-VisibilityToggles.js]].
			// Spaces must later be converted to hyphens or underscores.
			// Reference: https://drafts.csswg.org/selectors-4/#id-selectors
			if (/^[a-zA-Z0-9\s_-]+$/.test(text))
				return text;
			else
				break;
		} else if (num)
			break;
	}
	
	return defaultCategory;
}

function createNavToggle(navFrame) {
	if (checkAndSetToggleified(navFrame)) {
		return;
	}
	
	var navHead, navContent;
	for (var i = 0, children = navFrame.childNodes; i < children.length; ++i) {
		var child = children[i];
		if (child.nodeName === "DIV") {
			var classList = child.classList;
			if (classList.contains("NavHead"))
				navHead = child;
			if (classList.contains("NavContent"))
				navContent = child;
		}
	}
	if (!(navHead && navContent))
		return;
	
	// Step 1, don't react when a subitem is clicked.
	$(navHead).find("a").on("click", function (e) {
		e.stopPropagation();
	});
			
	// Step 2, toggle visibility when bar is clicked.
	// NOTE This function was chosen due to some funny behaviour in Safari.
	var $navToggle = $("<a>").attr("role", "button").attr("tabindex", "0");
	
	$("<span>").addClass("NavToggle")
		.append($navToggle)
		.prependTo(navHead);
	
	navHead.style.cursor = "pointer";
	var toggleCategory = $(navFrame).data("toggle-category")
		|| getToggleCategory(navFrame, "other boxes");
	navHead.onclick = window.VisibilityToggles.register(toggleCategory,
		function show() {
			$navToggle.html(NavigationBarHide);
			if (navContent)
				navContent.style.display = "block";
		},
		function hide() {
			$navToggle.html(NavigationBarShow);
			if (navContent)
				navContent.style.display = "none";
		});
}


/* == View Switching == */

function viewSwitching(rootElement) {
	if (checkAndSetToggleified(rootElement)) {
		return;
	}
	
	var $rootElement = $(rootElement);
	var showButtonText = $rootElement.data("vs-showtext") || "kaikki ▼";
	var hideButtonText = $rootElement.data("vs-hidetext") || "tiivistetty ▲";
	
	var toSkip = $rootElement.find(".vsSwitcher").find("*");
	var elemsToHide = $rootElement.find(".vsHide").not(toSkip);
	var elemsToShow = $rootElement.find(".vsShow").not(toSkip);
	
	// Find the element to place the toggle button in.
	var toggleElement = $rootElement.find(".vsToggleElement").not(toSkip).first();
	
	// The toggleElement becomes clickable in its entirety, but
	// we need to prevent this if a contained link is clicked instead.
	toggleElement.children("a").on("click", function (e) {
		e.stopPropagation();
	});
	
	// Add the toggle button.
	var toggleButton = $("<a>").attr("role", "button").attr("tabindex", "0");
	
	$("<span>").addClass("NavToggle").append(toggleButton).prependTo(toggleElement);
	
	// Determine the visibility toggle category (for the links in the bar on the left).
	var toggleCategory = $rootElement.data("toggle-category");
	if (!toggleCategory) {
		var classNames = $rootElement.attr("class").split(/\s+/);
		
		for (var i = 0; i < classNames.length; ++i) {
			var className = classNames[i].split("-");
			
			if (className[0] == "vsToggleCategory") {
				toggleCategory = className[1];
			}
		}
	}
	
	if (!toggleCategory)
		toggleCategory = "muu";
	
	// Register the visibility toggle.
	toggleElement.css("cursor", "pointer");
	toggleElement.on("click", window.VisibilityToggles.register(toggleCategory,
		function show() {
			toggleButton.html(hideButtonText);
			elemsToShow.hide();
			elemsToHide.show();
		},
		function hide() {
			toggleButton.html(showButtonText);
			elemsToShow.show();
			elemsToHide.hide();
		}));
}





window.createNavToggle = createNavToggle;
window.viewSwitching = viewSwitching;
window.getToggleCategory = getToggleCategory;

/* == Apply four functions defined above == */
mw.hook('wikipage.content').add(function($content) {
	// NavToggles
	$('.NavFrame', $content).each(function(){
		//createNavToggle(this);
	});

	//view switching
	$('.vsSwitcher', $content).each(function(){
		viewSwitching(this);
	});

});



})();