MediaWiki

Common.js

From Dogcraft Wiki

mNo edit summary
m (Changing top bar image source to external location, maintaining designated image. This edit is in line with the edit moratorium as it is done to keep the logo variant, per wiki admin decision)
 
(155 intermediate revisions by 3 users not shown)
Line 2: Line 2:


/* Navigation Popups */
/* Navigation Popups */
/* Currently Disabled
/* Currently Disabled ü
mw.loader.load('/load.php?modules=ext.gadget.Navigation_popups');
mw.loader.load('/load.php?modules=ext.gadget.Navigation_popups');*/
window.popupAdminLinks = false;
window.popupAdminLinks = false;
window.popupActionsMenu = false;
window.popupActionsMenu = false;
Line 9: Line 9:
window.popupSubpopups = false;
window.popupSubpopups = false;
window.popupSummaryData = false;
window.popupSummaryData = false;
*/
 
function randomPageImage( category ) {
params = {
        action: 'query',
        list: 'categorymembers',
        cmtitle: 'Category:' + category,
        cmprop: 'ids',
        cmtype: 'page',
        cmlimit: '5000',
        format: 'json'
    },
    api = new mw.Api();
return new Promise(function(resolve,reject) {
api.get( params ).done( function ( data ) {
    var pages = data.query.categorymembers,
        page,
        cat_random = Math.floor(Math.random() * pages.length),
        id_random = pages[cat_random].pageid;
    var paramsPI = {
        action: 'query',
        prop: 'pageimages',
        pageids: id_random,
        piprop: 'original',
        format: 'json'
    };
    api.get( paramsPI ).done( function (data ) {
        resolve(data.query.pages[id_random].original);
    });
} );
});
}


$(document).ready(function() {
$(document).ready(function() {
var goToDogcraft = document.querySelectorAll("#p-Go_to_Dogcraft > a");
/* pride logo varian */
goToDogcraft[0].setAttribute("href", "//dogcraft.net");
document.querySelector('#main > nav:first-child .nav-link.wiki-link img').src = "https://i.imgur.com/mXX3XMq.png";
var pagename = mw.config.get('wgPageName'),
namespace = mw.config.get('wgNamespaceNumber');
// var indicator = document.querySelectorAll(".mw-indicators");
// var indicator1 = indicator[0];
// if (indicator1.childNodes.length !== 1) {document.getElementById("tagline").innerHTML = indicator1.innerHTML;}
/*== Shows hidden categories when users select that option in their User Preferences (this is technically a bugfix) ==*/
if ( mw.user.options.get( 'showhiddencats' ) == 1 ) {
var hiddenCatsContainer = document.getElementById('mw-hidden-catlinks');
if (hiddenCatsContainer) hiddenCatsContainer.style.display = 'block';
}
var indicator = document.querySelectorAll(".mw-indicators");
/*== Hides the actions button and indicators while in the Visual editor ==*/
var indicator1 = indicator[0];
if (indicator1.childNodes.length !== 1) {document.getElementById("tagline").innerHTML = indicator1.innerHTML;}
mw.hook( 've.activationComplete' ).add( function () {
$('.content .top').hide();
});
var mainNamespaceMarker = document.createElement("h4");
mw.hook( 've.deactivationComplete' ).add( function () {
mainNamespaceMarker.classList.add("namespace");
$('.content .top').show();
mainNamespaceMarker.classList.add("label");
});
mainNamespaceMarker.textContent = "Main";
var pageContentDiv = document.getElementById("content"),
/*== Dogcraft.net topbar: Automatically enabled to display the Dogcraft.net topbar on the Help Resources(WIP) and Rules pages. (moved from gadget) ==*/
pagePcactions = document.getElementById("p-cactions");
$(document).ready(function() {
if (mw.config.get('wgPageName') === "Rules" || mw.config.get('wgPageName') === "Help_Resources")
if (wgNamespaceNumber == "Main")  
{
{
pagePcactions.insertBefore(mainNamespaceMarker,pageContentDiv);
$('.nav .nav-title a span').text('Dogcraft');
$('#wikiNav').removeClass('expanded');
}
}
});
/*== Shows edit button on Rules and Help Resources for those who can edit it ==*/
if (pagename === 'Rules' || pagename === 'Help_Resources') {
if (mw.config.get('wgIsProbablyEditable')) {
$('#wikiActions').show();
}
}
/* pagecount */
var pagecount = document.createElement('div'),
link = document.createElement('a'),
pages = document.createElement('span'),
text = document.createElement('span'),
indicators = document.querySelector('#main > main:first-of-type > div.content:first-child > div.top:first-child > .indicators'),
api = new mw.Api(),
num = api.get( {
action:"query",
meta:"siteinfo",
siprop:"statistics",
formatversion:"2"
});
pagecount.id = 'pagecount';
link.href = "/wiki/Special:AllPages?namespace=0&hideredirects=1";
text.textContent = 'pages';
$.when( num ).then( function ( data ) {
pages.textContent = data.query.statistics.articles;
link.append(pages, text);
pagecount.append(link);
indicators.after(pagecount);
mw.hook( 'userjs.pagecount.done' ).fire();
});
/*= Top header banner =*/
if (namespace === 14 || namespace === 3004 || namespace == 2 || namespace == 0) {
var headbanner = document.getElementById("headbanner");
if (headbanner) {
var firstHeading = document.querySelectorAll("#content h1:first-of-type")[0];//document.getElementById("firstHeading");
if (headbanner.hasAttribute("title")) {
if (headbanner.title == "auto") {
randomPageImage(pagename.split(":")[1].split("/")[0]).then(function(result) {
firstHeading.style.setProperty('--banner-padding', "300px");
firstHeading.style.setProperty('--banner-image', 'url(' + result.source + ')');
});
} else {
firstHeading.style.setProperty('--banner-padding', headbanner.title);
firstHeading.style.setProperty('--banner-image', 'url(' + headbanner.textContent + ')');
}
}
firstHeading.classList.add('banner');
$("#contentSub").prepend($("main > .content > .top"));
}
/** custom category page marker class
* added to firstHeading because requesting the page <body> (which normally has marker classes) on every category page load is
* not a good idea - its the parent for the whole page. The firstHeading is siblings with every other 1st child of the page content,
* which includes the div.top on pages with a top banner image set. (But this can be used on non-bannered cat pages as well)
*/
if ( mw.config.get('wgCategories').includes('Custom category pages') ) {
var title = document.querySelectorAll("#content h1:first-of-type")[0];
title.classList.add("category-custom");
/* remove /Portal from the displayed title (This could also be achieved with enabling {{DISPLAYTITLE}} */
if (title.textContent.includes("/Portal")) {
    title.textContent = title.textContent.substr(0, title.textContent.indexOf("/"));
}
}
}
//temp: move news-info indicator into pagecount's place
if ((mw.config.get('wgNamespaceNumber') === 3004) && (mw.config.get('wgUserName') == null)) {
$('#siteSub > .top')[0].classList.add('indicator-right');
}
});
//temp: move news-info indicator into pagecount's place
mw.hook( 'userjs.pagecount.done' ).add( function() {
if ((mw.config.get('wgNamespaceNumber') === 3004) && ($(window).width() > 700) && (mw.config.get('wgUserName') != null)) {
$("#pagecount").append($(".mw-indicators.mw-body-content"));
}
});
});

Latest revision as of 02:56, 16 June 2025

/* Any JavaScript here will be loaded for all users on every page load. */

/* Navigation Popups */
/* Currently Disabled ü
mw.loader.load('/load.php?modules=ext.gadget.Navigation_popups');*/
window.popupAdminLinks = false;
window.popupActionsMenu = false;
window.popupStructure = 'lite';
window.popupSubpopups = false;
window.popupSummaryData = false;

function randomPageImage( category ) {
	params = {
	        action: 'query',
	        list: 'categorymembers',
	        cmtitle: 'Category:' + category,
	        cmprop: 'ids',
	        cmtype: 'page',
	        cmlimit: '5000',
	        format: 'json'
    },
    api = new mw.Api();
	
	return new Promise(function(resolve,reject) { 
		api.get( params ).done( function ( data ) {
		    var pages = data.query.categorymembers,
		        page,
		        cat_random = Math.floor(Math.random() * pages.length),
		        id_random = pages[cat_random].pageid;
		
		    var paramsPI = {
		        action: 'query',
		        prop: 'pageimages',
		        pageids: id_random,
		        piprop: 'original',
		        format: 'json'
		    };
		    api.get( paramsPI ).done( function (data ) {
		        resolve(data.query.pages[id_random].original);
		    });
		} );
	});
}

$(document).ready(function() {
	/* pride logo varian */
	document.querySelector('#main > nav:first-child .nav-link.wiki-link img').src = "https://i.imgur.com/mXX3XMq.png";
	
	var pagename = mw.config.get('wgPageName'),
		namespace = mw.config.get('wgNamespaceNumber');
	// var indicator = document.querySelectorAll(".mw-indicators");
	// var indicator1 = indicator[0];
	
	// if (indicator1.childNodes.length !== 1) {document.getElementById("tagline").innerHTML = indicator1.innerHTML;}
	
	/*== Shows hidden categories when users select that option in their User Preferences (this is technically a bugfix) ==*/
	if ( mw.user.options.get( 'showhiddencats' ) == 1 ) {
		var hiddenCatsContainer = document.getElementById('mw-hidden-catlinks');
		if (hiddenCatsContainer) hiddenCatsContainer.style.display = 'block';
	}
	
	/*== Hides the actions button and indicators while in the Visual editor ==*/
	
	mw.hook( 've.activationComplete' ).add( function () {
		$('.content .top').hide();
	});
	
	mw.hook( 've.deactivationComplete' ).add( function () {
			$('.content .top').show();
	});
	
	/*== Dogcraft.net topbar: Automatically enabled to display the Dogcraft.net topbar on the Help Resources(WIP) and Rules pages. (moved from gadget) ==*/
	$(document).ready(function() {
		if (mw.config.get('wgPageName') === "Rules" || mw.config.get('wgPageName') === "Help_Resources")
		{
			$('.nav .nav-title a span').text('Dogcraft');
			$('#wikiNav').removeClass('expanded');
		}
	});
	
	/*== Shows edit button on Rules and Help Resources for those who can edit it ==*/
	if (pagename === 'Rules' || pagename === 'Help_Resources') {
		if (mw.config.get('wgIsProbablyEditable')) {
			$('#wikiActions').show();
		}
	}
	
	/* pagecount */
	var pagecount = document.createElement('div'),
		link = document.createElement('a'),
		pages = document.createElement('span'),
		text = document.createElement('span'),
		indicators = document.querySelector('#main > main:first-of-type > div.content:first-child > div.top:first-child > .indicators'),
		api = new mw.Api(),
		num = api.get( {
			action:"query",
			meta:"siteinfo",
			siprop:"statistics",
			formatversion:"2"
		});
	
	pagecount.id = 'pagecount';
	link.href = "/wiki/Special:AllPages?namespace=0&hideredirects=1";
	text.textContent = 'pages';
	
	$.when( num ).then( function ( data ) {
		pages.textContent = data.query.statistics.articles;
		link.append(pages, text);
		pagecount.append(link);
	
		indicators.after(pagecount);
		mw.hook( 'userjs.pagecount.done' ).fire();
	});
	
	/*= Top header banner =*/
	if (namespace === 14 || namespace === 3004 || namespace == 2 || namespace == 0) {
		var headbanner = document.getElementById("headbanner");
		if (headbanner) {
			var firstHeading = document.querySelectorAll("#content h1:first-of-type")[0];//document.getElementById("firstHeading");
			if (headbanner.hasAttribute("title")) { 
				if (headbanner.title == "auto") {
					randomPageImage(pagename.split(":")[1].split("/")[0]).then(function(result) {
						firstHeading.style.setProperty('--banner-padding', "300px");
						firstHeading.style.setProperty('--banner-image', 'url(' + result.source + ')');
					});
				} else {
					firstHeading.style.setProperty('--banner-padding', headbanner.title);
					firstHeading.style.setProperty('--banner-image', 'url(' + headbanner.textContent + ')');
				}
			}
			firstHeading.classList.add('banner');
			$("#contentSub").prepend($("main > .content > .top"));
		}
		
		/** custom category page marker class 
		* added to firstHeading because requesting the page <body> (which normally has marker classes) on every category page load is 
		* not a good idea - its the parent for the whole page. The firstHeading is siblings with every other 1st child of the page content,
		* which includes the div.top on pages with a top banner image set. (But this can be used on non-bannered cat pages as well)
		*/
		if ( mw.config.get('wgCategories').includes('Custom category pages') ) {
			var title = document.querySelectorAll("#content h1:first-of-type")[0];
			title.classList.add("category-custom");
			
			/* remove /Portal from the displayed title (This could also be achieved with enabling {{DISPLAYTITLE}} */
			if (title.textContent.includes("/Portal")) {
			    title.textContent = title.textContent.substr(0, title.textContent.indexOf("/"));
			} 
		}
	}

	//temp: move news-info indicator into pagecount's place
	if ((mw.config.get('wgNamespaceNumber') === 3004) && (mw.config.get('wgUserName') == null)) {
		$('#siteSub > .top')[0].classList.add('indicator-right');
	}

});

//temp: move news-info indicator into pagecount's place
mw.hook( 'userjs.pagecount.done' ).add( function() {
	if ((mw.config.get('wgNamespaceNumber') === 3004) && ($(window).width() > 700) && (mw.config.get('wgUserName') != null)) {
		$("#pagecount").append($(".mw-indicators.mw-body-content")); 
	}
});