MediaWiki

Gadget-breadcrumbs.js

From Dogcraft Wiki

No edit summary
mNo edit summary
 
(32 intermediate revisions by the same user not shown)
Line 1: Line 1:
$(document).ready(function() {
$(document).ready(function() {
var config =  mw.config.get([
"wgIsArticle",
"wgNamespaceNumber",
"wgCategories",
"wgTitle"
]);
function worldCat( pageCats ) {
function worldCat( pageCats ) {
var world;
for (var i = 0; i < pageCats.length; i++) {
for (var i = 0; i < pageCats.length; i++) {
if ( pageCats[i].indexOf('Survival ') > -1) {
if ( pageCats[i].indexOf('Survival ') > -1) {
    world = pageCats[i] + '';
    return pageCats[i].toString();
        return world;
}
}
}
}
return false;
return '';
}
}
var config = mw.config.get([
function simpleAddCat( categories, breadcrumb, links ) {
"wgIsArticle",
if (categories.includes( breadcrumb )) {
"wgNamespaceNumber",
links.push( breadcrumb );
"wgCategories",
}
"wgTitle"
}
]);
function htmlElementBuilder( type, config ) {
var elem = document.createElement( type.toString() );
if (typeof config.id !== 'undefined') { elem.id = config.id; }
if (typeof config.classList !== 'undefined') { elem.classList = config.classList; }
if (typeof config.textContent !== 'undefined') { elem.textContent = config.textContent; }
if (type === 'a' && typeof config.href !== 'undefined') { elem.href = config.href; }
return elem;
}
/* creates a single breadcrumb link */
function crumbBuilder( target, name ) {
var crumb = htmlElementBuilder('a', { href: '/wiki/' + target });
crumb.appendChild( htmlElementBuilder('span', { textContent: name.toString() }) );
return crumb;
}
function breadcrumbsBuilder( crumbs, fullcrumbs ) {
var container = htmlElementBuilder('div', {id: 'breadcrumbs'});
container.appendChild( crumbBuilder('Main_Page', 'Dogcraft Wiki') );
if ( fullcrumbs ) {
crumbs.forEach( function( elem ) { container.appendChild( elem ); });
}
else {
// creates <a> elements from the links list
crumbs.forEach( function( elem ) {
var crumb = crumbBuilder( 'Category:' + elem, elem.toString() );
container.appendChild( crumb );
});
}
container.appendChild( htmlElementBuilder('span', {textContent: config.wgTitle}) );
return container;
}
/* special pages */
if ( config.wgNamespaceNumber == -1) {
$('.top .indicators').prepend( breadcrumbsBuilder( [crumbBuilder( 'Special:SpecialPages', 'Special pages')], true ) );
}
/* news pages */
if ( config.wgNamespaceNumber == 3004) {
var typeCategories = [
"Newsletter"
],
pageCategories = config.wgCategories,
links = [];
var typesOnPage = pageCategories.filter(function(x){ return typeCategories.includes(x) });
if ( typesOnPage.length == 1) {
links.push( typesOnPage[0] + "" );
}
else if (typesOnPage.length > 1) {
console.log('Breadcrumbs: Page has more than one type category');
links.push( typesOnPage[0] + "" );
}
/* creates the breadcrumbs and adds them to the page */
if ( links !== "undefined") {
var breadcrumbs = htmlElementBuilder('div', {id: 'breadcrumbs'});
links.forEach( function( elem ) {
var crumb = crumbBuilder( 'Category:' + elem, elem.toString() );
breadcrumbs.appendChild( crumb );
});
 
$('.top .indicators .namespace').after( breadcrumbs );
}
else { console.log(' Breadcrumbs: links is undefined '); }
}
if ( config.wgIsArticle && ( config.wgNamespaceNumber == 0 /*&& config.wgTitle !== "Main Page"*/)) {
/* articles */
//var namespace = mw.config.get('wgFormattedNamespaces')[mw.config.get('wgNamespaceNumber')];
if ( config.wgIsArticle && ( config.wgNamespaceNumber == 0 && config.wgTitle !== "Main Page")) {
/* creates and fills links[] based on page categories */
var serverInfo = "",
var serverInfo = "",
siteContent = "",
siteContent = "",
type = "",
type = "",
list = "",
typeCategories = [
typeCategories = [
"Stations",
"Builds",
"Events",
"Events",
"Settlements",
"Settlements",
Line 36: Line 115:
],
],
pageCategories = config.wgCategories,
pageCategories = config.wgCategories,
links = [],
links = [];
crumbs = "";
links.push( "Main Page" );
simpleAddCat( pageCategories, "Site Content", links);
simpleAddCat( pageCategories, "Server Features", links);
simpleAddCat( pageCategories, "Patreon", links);
if (pageCategories.includes('Site Content')) {
if ( !pageCategories.includes('Worlds') && worldCat( pageCategories ) !== "") {  
links.push( "Site Content" );
//^ doesnt make much sense to include the specific world on the world's page,
}
//eg. Main Page > Survival 4 > Worlds > Survival 4 is weird
if (pageCategories.includes('Server Information')) {
links.push( "Server Information" );
}
var world = '';
if ( worldCat( pageCategories ) != false) {
links.push( worldCat( pageCategories ) );
links.push( worldCat( pageCategories ) );
}
}
if (pageCategories.includes('Bases')) {
if (pageCategories.includes('Builds')) {
links.push( "Builds" );
links.push( "Builds" );
links.push( "Bases" );
for (var i = 0; i < pageCategories.length; i++) {
switch (pageCategories[i]) {
case 'Bases':
links.push( "Bases" );
break;
case 'Stations':
links.push( "Stations" );
break;
case 'Shelters':
links.push( "Shelters" );
break;
default:
break;
}
}
}
}
else
else
{
{
var typesOnPage = pageCategories.filter(function(x){ return typeCategories.includes(x) });
var typesOnPage = pageCategories.filter(function(x){ return typeCategories.includes(x) });
if ( typesOnPage.lenght > 1) {
if ( typesOnPage.length == 1) {
console.log('Page has more then one type category');
links.push( typesOnPage[0] + "" );
}
else if (typesOnPage.length > 1) {
console.log('Breadcrumbs: Page has more than one type category');
links.push( typesOnPage[0] + "" );
}
}
links.push( typesOnPage[0] + "" );
}
}
if (pageCategories.includes('Lists')) {
/* creates the breadcrumbs and adds them to the page */
links.push( "Lists" );
if ( links !== "undefined") {
var breadcrumbs = breadcrumbsBuilder( links, false );
$('.top .indicators').prepend( breadcrumbs );
}
}
else { console.log(' Breadcrumbs: links is undefined '); }
links.forEach( function( elem ) {
crumbs += '<a href="/wiki/Category:' + elem + '">' + '<span>' + elem + '</span>' + '</a>';
}
});
/* this probably should be in Common.js or the Helppage gadget (if thats still a thing) */
if( crumbs.length > 0 && links.length > 0) {
if (mw.config.get('wgPageName') === "Rules" || mw.config.get('wgPageName') === "Help_Resources")
{
$('.top .indicators').prepend('<div id="breadcrumbs"></div>');
$('#tagline').hide();
$('#breadcrumbs').html('' + crumbs + '<span>' + config.wgTitle + '</span>' );
$('#breadcrumbs > a').slice(1).hide();
$('#breadcrumbs a:first-child').attr('href', '//dogcraft.net');
}
$('#breadcrumbs a:first-child').text('Dogcraft.net');
else { console.log(' crumbs or links is undefined '); }
}
if (mw.config.get('wgNamespaceNumber') === 3004) {
$("#pagecount").append($(".mw-indicators.mw-body-content"));
}
mw.hook( 'userjs.breadcrumbs.done' ).fire();
});
mw.hook( 'userjs.pagecount.done' ).add( function() {
if (mw.config.get('wgNamespaceNumber') === 3004) {
$("#pagecount").append($(".mw-indicators.mw-body-content"));
}
}
});
});

Latest revision as of 18:11, 2 August 2024

$(document).ready(function() {
	var config =  mw.config.get([
		"wgIsArticle",
		"wgNamespaceNumber",
		"wgCategories",
		"wgTitle"
		]);
	
	
	function worldCat( pageCats ) {
		for (var i = 0; i < pageCats.length; i++) {
			if ( pageCats[i].indexOf('Survival ') > -1) {
	    		return pageCats[i].toString();
			}
		}
		return '';
	}
	
	function simpleAddCat( categories, breadcrumb, links ) {
		if (categories.includes( breadcrumb )) {
			links.push( breadcrumb );
		}		
	}
	
	function htmlElementBuilder( type, config ) {
		var elem = document.createElement( type.toString() );
		
		if (typeof config.id !== 'undefined') { elem.id = config.id; }
		if (typeof config.classList !== 'undefined') { elem.classList = config.classList; }
		if (typeof config.textContent !== 'undefined') { elem.textContent = config.textContent; }
		if (type === 'a' && typeof config.href !== 'undefined') { elem.href = config.href; }
		
		return elem;
	}
	
	/* creates a single breadcrumb link */
	function crumbBuilder( target, name ) {
		var crumb = htmlElementBuilder('a', { href: '/wiki/' + target });
		crumb.appendChild( htmlElementBuilder('span', { textContent: name.toString() }) );
		
		return crumb;
	}
	
	function breadcrumbsBuilder( crumbs, fullcrumbs ) {
		var container = htmlElementBuilder('div', {id: 'breadcrumbs'});
			
			container.appendChild( crumbBuilder('Main_Page', 'Dogcraft Wiki') );
			
			if ( fullcrumbs ) {
				crumbs.forEach( function( elem ) { container.appendChild( elem ); });
			} 
			else {
				// creates <a> elements from the links list 
				crumbs.forEach( function( elem ) {
					var crumb = crumbBuilder( 'Category:' + elem, elem.toString() );
					container.appendChild( crumb );
				});
			}
			
			container.appendChild( htmlElementBuilder('span', {textContent: config.wgTitle}) );
				
			return container;
	}
		
	/* special pages */	
	if ( config.wgNamespaceNumber == -1) {
		$('.top .indicators').prepend( breadcrumbsBuilder( [crumbBuilder( 'Special:SpecialPages', 'Special pages')], true ) );
	}
	
	/* news pages */	
	if ( config.wgNamespaceNumber == 3004) {
		var typeCategories = [
				"Newsletter"
				],
			pageCategories = config.wgCategories,
			links = [];
			
		var typesOnPage = pageCategories.filter(function(x){ return typeCategories.includes(x) });
		if ( typesOnPage.length == 1) {
			links.push( typesOnPage[0] + "" );
		}
		else if (typesOnPage.length > 1) {
			console.log('Breadcrumbs: Page has more than one type category');
			links.push( typesOnPage[0] + "" );
		}
			
		/* creates the breadcrumbs and adds them to the page */
		if ( links !== "undefined") {
			var breadcrumbs = htmlElementBuilder('div', {id: 'breadcrumbs'});
			
			links.forEach( function( elem ) {
					var crumb = crumbBuilder( 'Category:' + elem, elem.toString() );
					breadcrumbs.appendChild( crumb );
				});

				
			$('.top .indicators .namespace').after( breadcrumbs );
		}
		else { console.log(' Breadcrumbs: links is undefined '); }
	}
	
	/* articles */
	if ( config.wgIsArticle && ( config.wgNamespaceNumber == 0 && config.wgTitle !== "Main Page")) {
		/* creates and fills links[] based on page categories */
		var serverInfo = "",
			siteContent = "",
			type = "",
			typeCategories = [
				"Events",
				"Settlements",
				"Groups",
				"Phenomenon",
				"Projects",
				"Worlds"
				],
			pageCategories = config.wgCategories,
			links = [];
		
		simpleAddCat( pageCategories, "Site Content", links);
		simpleAddCat( pageCategories, "Server Features", links);
		simpleAddCat( pageCategories, "Patreon", links);
		
		if ( !pageCategories.includes('Worlds') && worldCat( pageCategories ) !== "") { 
			//^ doesnt make much sense to include the specific world on the world's page,
			//eg. Main Page > Survival 4 > Worlds > Survival 4 is weird
			links.push( worldCat( pageCategories ) );
		}
		
		if (pageCategories.includes('Builds')) {
			links.push( "Builds" );
			for (var i = 0; i < pageCategories.length; i++) {
				switch (pageCategories[i]) {
					case 'Bases':
						links.push( "Bases" );
						break;
					case 'Stations':
						links.push( "Stations" );
						break;
					case 'Shelters':
						links.push( "Shelters" );
						break;
					default:
						break;
				}
			}
		}
		else
		{
			var typesOnPage = pageCategories.filter(function(x){ return typeCategories.includes(x) });
			if ( typesOnPage.length == 1) {
				links.push( typesOnPage[0] + "" );
			}
			else if (typesOnPage.length > 1) {
				console.log('Breadcrumbs: Page has more than one type category');
				links.push( typesOnPage[0] + "" );
			}
		}
		
		/* creates the breadcrumbs and adds them to the page */
		if ( links !== "undefined") {
			var breadcrumbs = breadcrumbsBuilder( links, false );
				
			$('.top .indicators').prepend( breadcrumbs );
		}
		else { console.log(' Breadcrumbs: links is undefined '); }
	
	}
	
	/* this probably should be in Common.js or the Helppage gadget (if thats still a thing) */
	if (mw.config.get('wgPageName') === "Rules" || mw.config.get('wgPageName') === "Help_Resources")
	{
		$('#tagline').hide();
		$('#breadcrumbs > a').slice(1).hide();
		$('#breadcrumbs a:first-child').attr('href', '//dogcraft.net');
		$('#breadcrumbs a:first-child').text('Dogcraft.net');
	}
	
	if (mw.config.get('wgNamespaceNumber') === 3004) {
		$("#pagecount").append($(".mw-indicators.mw-body-content")); 
	}
	mw.hook( 'userjs.breadcrumbs.done' ).fire();
});

mw.hook( 'userjs.pagecount.done' ).add( function() {
	if (mw.config.get('wgNamespaceNumber') === 3004) {
		$("#pagecount").append($(".mw-indicators.mw-body-content")); 
	}
});