MediaWiki:Gadget-defaultVisibilityToggles.js: diferenças entre revisões

Fonte: Wikinotícias
Conteúdo apagado Conteúdo adicionado
novo
 
m - emitindo alerta
Linha 2: Linha 2:
"use strict";
"use strict";


var NavigationBarHide = "ocultar";
var NavigationBarHide = "ocultar";
var NavigationBarShow = "exibir";
var NavigationBarShow = "exibir";


function checkAndSetToggleified(element) {
function checkAndSetToggleified(element) {
Linha 28: Linha 28:
var text = jQuery(heading).text()
var text = jQuery(heading).text()
.toLowerCase()
.toLowerCase()
// jQuery's .text() is inconsistent about whitespace:
.replace(/^\s+|\s+$/g, "").replace(/\s+/g, " ")
.replace(/^\s+|\s+$/g, "").replace(/\s+/g, " ")
// remove numbers added by the "Auto-number headings" pref:
.replace(/^[1-9][0-9.]+ ?/, "");
.replace(/^[1-9][0-9.]+ ?/, "");
if (/^[a-zA-Z0-9\s_-]+$/.test(text))
if (/^[a-zA-Z0-9\s_-]+$/.test(text))
Linha 61: Linha 59:
if (!(navHead && navContent))
if (!(navHead && navContent))
return;
return;

// Step 1, don't react when a subitem is clicked.
$(navHead).find("a").on("click", function (e) {
$(navHead).find("a").on("click", function (e) {
e.stopPropagation();
e.stopPropagation();
});
});

// Step 2, toggle visibility when bar is clicked.
var $navToggle = $("<a>").attr("role", "button").attr("tabindex", "0");
var $navToggle = $("<a>").attr("role", "button").attr("tabindex", "0");
Linha 90: Linha 86:
}
}


function setupHiddenQuotes(li) {
/* == View Switching == */
if (checkAndSetToggleified(li)) {
return;
}
var HQToggle, liComp, dl;
var HQShow = "citações";
var HQHide = "citações";
function show() {
HQToggle.html(HQHide);
$(li).children("ul").show();
}
function hide() {
HQToggle.html(HQShow);
$(li).children("ul").hide();
}
for (var k = 0; k < li.childNodes.length; k++) {
// Look at each component of the definition.
liComp = li.childNodes[k];
if (liComp.nodeName.toLowerCase() === "dl" && !dl) {
dl = liComp;
}

if (/^(ul|UL)$/.test(liComp.nodeName)) {
HQToggle = $("<a>").attr("role", "button").attr("tabindex", "0");
$(dl || liComp).before($("<span>").addClass("HQToggle").append(HQToggle));
HQToggle.on("click", window.VisibilityToggles.register("quotations", show, hide));
break;
}
}
}


function viewSwitching(rootElement) {
function viewSwitching(rootElement) {
Linha 98: Linha 125:
var $rootElement = $(rootElement);
var $rootElement = $(rootElement);
var showButtonText = $rootElement.data("vs-showtext") || "mais";
var showButtonText = $rootElement.data("vs-showtext") || "mais";
var hideButtonText = $rootElement.data("vs-hidetext") || "menos";
var hideButtonText = $rootElement.data("vs-hidetext") || "menos";
var toSkip = $rootElement.find(".vsSwitcher").find("*");
var toSkip = $rootElement.find(".vsSwitcher").find("*");
Linha 113: Linha 140:
e.stopPropagation();
e.stopPropagation();
});
});

// Add the toggle button.
var toggleButton = $("<a>").attr("role", "button").attr("tabindex", "0");
var toggleButton = $("<a>").attr("role", "button").attr("tabindex", "0");
$("<span>").addClass("NavToggle").append(toggleButton).prependTo(toggleElement);
$("<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");
var toggleCategory = $rootElement.data("toggle-category");
if (!toggleCategory) {
if (!toggleCategory) {
Linha 135: Linha 160:
if (!toggleCategory)
if (!toggleCategory)
toggleCategory = "others";
toggleCategory = "others";

// Register the visibility toggle.
toggleElement.css("cursor", "pointer");
toggleElement.css("cursor", "pointer");
toggleElement.on("click", window.VisibilityToggles.register(toggleCategory,
toggleElement.on("click", window.VisibilityToggles.register(toggleCategory,
Linha 151: Linha 175:
}
}


/* ==List switching== */
window.listSwitcherRowCount = window.listSwitcherRowCount || 3;
window.listSwitcherRowCount = window.listSwitcherRowCount || 3;


Linha 177: Linha 200:
var $rootElement = $(rootElement);
var $rootElement = $(rootElement);
var $termList = $rootElement.find(".term-list");
var $termList = $rootElement.find(".term-list");
// Find the element to place the toggle button in.
var $toggleElement = $rootElement.find(".list-switcher-element");
var $toggleElement = $rootElement.find(".list-switcher-element");
var columnCount = parseInt($termList.data("column-count"), 10);
var columnCount = parseInt($termList.data("column-count"), 10);
if (!(columnCount && columnCount > 0)) {
if (!(columnCount && columnCount > 0)) {
Linha 189: Linha 209:
var $listItems = $rootElement.find("ul").first().find("li");
var $listItems = $rootElement.find("ul").first().find("li");
var $toHide = getListItemsToHide($listItems, columnCount, rowsInShowState);
var $toHide = getListItemsToHide($listItems, columnCount, rowsInShowState);

// Don't do anything if there aren't any items to hide.
if ($toHide.length === 0) {
if ($toHide.length === 0) {
$toggleElement.hide();
$toggleElement.hide();
Linha 197: Linha 216:
$toggleElement.css("cursor", "pointer");
$toggleElement.css("cursor", "pointer");
// Add the toggle button.
var $toggleButton = $("<a>").attr("role", "button").attr("tabindex", "0");
var $toggleButton = $("<a>").attr("role", "button").attr("tabindex", "0");
Linha 207: Linha 224:
$navToggle.css(rootBackgroundColor ? "background-color" : "background",
$navToggle.css(rootBackgroundColor ? "background-color" : "background",
rootBackgroundColor || rootBackground);
rootBackgroundColor || rootBackground);

// 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) {
$toggleElement.children("a").on("click", function (e) {
e.stopPropagation();
e.stopPropagation();
});
});

var toggleCategory = $rootElement.data("toggle-category")
var toggleCategory = $rootElement.data("toggle-category")
|| getToggleCategory($rootElement[0], "other lists");
|| getToggleCategory($rootElement[0], "other lists");
var showButtonText = $toggleElement.data("showtext") || "mais";
var hideButtonText = $toggleElement.data("hidetext") || "menos";
// Determine the text for the $toggleButton.

var showButtonText = $toggleElement.data("showtext") || "mais";
var hideButtonText = $toggleElement.data("hidetext") || "menos";
// Register the visibility toggle.
$toggleElement.on("click", window.VisibilityToggles.register(toggleCategory,
$toggleElement.on("click", window.VisibilityToggles.register(toggleCategory,
function show() {
function show() {
Linha 231: Linha 243:
$toHide.hide();
$toHide.hide();
}));
}));

// Add the toggle button to the DOM tree.
$navToggle.append($toggleButton).prependTo($toggleElement);
$navToggle.append($toggleButton).prependTo($toggleElement);
$toggleElement.show();
$toggleElement.show();
Linha 242: Linha 253:
window.getToggleCategory = getToggleCategory;
window.getToggleCategory = getToggleCategory;


/* == Apply four functions defined above == */
mw.hook('wikipage.content').add(function($content) {
mw.hook('wikipage.content').add(function($content) {
// NavToggles
// NavToggles
Linha 248: Linha 258:
createNavToggle(this);
createNavToggle(this);
});
});

//quotes
if (mw.config.get('wgNamespaceNumber') === 0) {
if (mw.config.get('wgNamespaceNumber') === 0) {
// First, find all the ordered lists, i.e. all the series of definitions.
$('ol > li', $content).each(function(){
$('ol > li', $content).each(function(){
setupHiddenQuotes(this);
setupHiddenQuotes(this);
Linha 257: Linha 265:
}
}


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


// list switching
$(".list-switcher", $content).each(function () {
$(".list-switcher", $content).each(function () {
enableListSwitch(this, window.listSwitcherRowCount);
enableListSwitch(this, window.listSwitcherRowCount);

Revisão das 14h16min de 26 de janeiro de 2022

(function defaultVisibilityTogglesIIFE() {
"use strict";

var NavigationBarHide = "ocultar";
var NavigationBarShow = "exibir";

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)) {
		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()
				.replace(/^\s+|\s+$/g, "").replace(/\s+/g, " ")
				.replace(/^[1-9][0-9.]+ ?/, "");
			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;

	$(navHead).find("a").on("click", function (e) {
		e.stopPropagation();
	});

	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";
		});
}

function setupHiddenQuotes(li) {
	if (checkAndSetToggleified(li)) {
		return;
	}
	
	var HQToggle, liComp, dl;
	var HQShow = "citações";
	var HQHide = "citações";
	function show() {
		HQToggle.html(HQHide);
		$(li).children("ul").show();
	}
	function hide() {
		HQToggle.html(HQShow);
		$(li).children("ul").hide();
	}
	
	for (var k = 0; k < li.childNodes.length; k++) {
		// Look at each component of the definition.
		liComp = li.childNodes[k];
		if (liComp.nodeName.toLowerCase() === "dl" && !dl) {
			dl = liComp;
		}

		if (/^(ul|UL)$/.test(liComp.nodeName)) {
			HQToggle = $("<a>").attr("role", "button").attr("tabindex", "0");
			$(dl || liComp).before($("<span>").addClass("HQToggle").append(HQToggle));
			HQToggle.on("click", window.VisibilityToggles.register("quotations", show, hide));
			break;
		}
	}
}

function viewSwitching(rootElement) {
	if (checkAndSetToggleified(rootElement)) {
		return;
	}
	
	var $rootElement = $(rootElement);
	var showButtonText = $rootElement.data("vs-showtext") || "mais";
	var hideButtonText = $rootElement.data("vs-hidetext") || "menos";
	
	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();
	});

	var toggleButton = $("<a>").attr("role", "button").attr("tabindex", "0");
	
	$("<span>").addClass("NavToggle").append(toggleButton).prependTo(toggleElement);

	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 = "others";

	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.listSwitcherRowCount = window.listSwitcherRowCount || 3;

function getListItemsToHide($listItems, columnCount, rowsInShowState) {
	var count = $listItems.length;
	var itemsPerColumn = Math.ceil(count / columnCount);
	
	var $elemsToHide = $();
	if (itemsPerColumn > rowsInShowState) {
		for (var i = 0; i < columnCount; ++i) {
			var columnStart = i * itemsPerColumn;
			$elemsToHide = $elemsToHide
				.add($listItems.slice(columnStart + rowsInShowState, columnStart + itemsPerColumn));
		}
	}
	
	return $elemsToHide;
}

function enableListSwitch (rootElement, rowsInShowState) {
	if (checkAndSetToggleified(rootElement)) {
		return;
	}
	
	var $rootElement = $(rootElement);
	var $termList = $rootElement.find(".term-list");
	var $toggleElement = $rootElement.find(".list-switcher-element");
	var columnCount = parseInt($termList.data("column-count"), 10);
	if (!(columnCount && columnCount > 0)) {
		$toggleElement.hide();
		return;
	}
	
	var $listItems = $rootElement.find("ul").first().find("li");
	var $toHide = getListItemsToHide($listItems, columnCount, rowsInShowState);

	if ($toHide.length === 0) {
		$toggleElement.hide();
		return;
	}
	
	$toggleElement.css("cursor", "pointer");
	var $toggleButton = $("<a>").attr("role", "button").attr("tabindex", "0");
	
	var rootBackgroundColor = $termList.css("background-color"),
		rootBackground = $termList.css("background");
	var $navToggle = $("<span>").addClass("NavToggle");
	if (rootBackgroundColor || rootBackground)
		$navToggle.css(rootBackgroundColor ? "background-color" : "background",
			rootBackgroundColor || rootBackground);

	$toggleElement.children("a").on("click", function (e) {
		e.stopPropagation();
	});

	var toggleCategory = $rootElement.data("toggle-category")
		|| getToggleCategory($rootElement[0], "other lists");
	var showButtonText = $toggleElement.data("showtext") || "mais";
	var hideButtonText = $toggleElement.data("hidetext") || "menos";

	$toggleElement.on("click", window.VisibilityToggles.register(toggleCategory,
		function show() {
			$toggleButton.html(hideButtonText);
			$toHide.show();
		},
		function hide() {
			$toggleButton.html(showButtonText);
			$toHide.hide();
		}));

	$navToggle.append($toggleButton).prependTo($toggleElement);
	$toggleElement.show();
}

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

mw.hook('wikipage.content').add(function($content) {
	// NavToggles
	$('.NavFrame', $content).each(function(){
		createNavToggle(this);
	});
	
	if (mw.config.get('wgNamespaceNumber') === 0) {
		$('ol > li', $content).each(function(){
			setupHiddenQuotes(this);
		});
	}

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

	$(".list-switcher", $content).each(function () {
		enableListSwitch(this, window.listSwitcherRowCount);
	});
});

jQuery(mw).on("LivePreviewDone", function (ev, sels) {
	var ols = jQuery(sels.join(",")).find("ol");
	for (var i = 0; i < ols.length; i++) {
		for (var j = 0; j < ols[i].childNodes.length; j++) {
			var li = ols[i].childNodes[j];
			if (li.nodeName.toUpperCase() == "LI") {
				setupHiddenQuotes(li);
			}
		}
	}
});

})();