Jump to content

User:Gary Queen/layout.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Gary Queen (talk | contribs) at 17:39, 13 August 2010 (some more). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
/*
	hook
*/
addOnloadHook(function()
{
	/*
		variables
	*/
	content = $('content');
	
	bodyContent = $('bodyContent');
	cactions = $('p-cactions');
	caEdit = $('ca-edit');
	caMain = $('ca-nstab-main');
	contentSub = $('contentSub');
	diff = content.getElementsByClassName('diff')[0];
	globalWrapper = $('globalWrapper');
	h2 = content.getElementsByTagName('h2');
	jumpToNav = $('jump-to-nav');
	afterJumpToNav = jumpToNav.nextSibling.nextSibling.nextSibling.nextSibling;
	paragraphs = content.getElementsByTagName('p');
	pBody = $('p-personal').getElementsByClassName('pBody')[0];
	pendingChanges = $('mw-fr-revisiontag');
	pPersonal = $('p-personal')
	relLinks = content.getElementsByClassName('rellink');
	section0 = $('section-0');
	siteSub = $('siteSub');
	toc = $('toc');
	tPrint = $('t-print');
	userMessages = content.getElementsByClassName('usermessage');
	wikiPreview = $('wikiPreview');
	wikitables = content.getElementsByClassName('wikitable');
	
	pagesLayout();
});

/*
	useful functions
*/
function $(element)
{
	return document.getElementById(element);
}

Object.prototype.addClass = function(newClass)
{
	element = this;
	
	if (element.className)
	{
		classes = element.className.split(' ');
		classes.push(newClass);
		return element.className = classes.join(' ');
	}
	else return element.className = newClass;	
}

Object.prototype.hasClass = function(classToCheck)
{
	element = this;
	
	if (!element.className) return false;
	
	classes = element.className.split(' ');
	for (var i = 0; i < classes.length; i++)
	{
		if (classes[i] == classToCheck)
			return true;
	}
	
	return false;	
}

Object.prototype.removeClass = function(oldClass)
{
	element = this;
	
	if (!element.className) return false;
	classes = element.className.split(' ');
	newClasses = [];
	for (var i = 0; i < classes.length; i++)
	{
		if (classes[i] != oldClass)
			newClasses.push(classes[i]);
	}
	
	return element.className = newClasses;	
}

String.prototype.trim = function()
{
	return this.replace(/^[\s|\n]+|[\s|\n]+$/g, '');	
}

String.prototype.ltrim = function()
{
	return this.replace(/^[\s|\n]+/, '');
}

String.prototype.rtrim = function()
{
	return this.replace(/[\s|\n]+$/, '');
}

/*
	do page layout
*/
function pagesLayout()
{
	/*
		Layout
	*/
	// Fatter pages except when it would exceed page width
	if (window.innerWidth > 1425) globalWrapper.style.width = cactions.style.width = pBody.style.width = '1400px';
	else globalWrapper.style.width = cactions.style.width = pBody.style.width = (window.innerWidth - 25) + 'px';
	
	// Thinner page width for articles (1000 pixels wide)
	var thinnerPage = false;
	var fatterPage = false;
	
	// TODO fatterPages
	
	// fatterPageTerms
	if (typeof(fatterPageTerms) == 'object' && fatterPageTerms.length > 0)
	{
		for (var i = 0; i < fatterPageTerms.length; i++)
		{
			if (wgPageName.replace(/_/g, ' ').indexOf(fatterPageTerms[i]) != -1)
			{
				fatterPage = true;
				break;
			}
		}
	}
	
	// thinnerPages
	if (typeof(thinnerPages) == 'object' && thinnerPages.length > 0)
	{
		for (var i = 0; i < thinnerPages.length; i++)
		{
			if (wgPageName.indexOf(thinnerPages[i].replace(/ /g, '_')) == 0)
			{
				thinnerPage = true;
				break;
			}
		}
	}
	
	// TODO thinnerPageTerms
	
	// Thinner pages for articles
	if (window.innerWidth > 1025 && fatterPage == false && (wgCanonicalNamespace == '' || thinnerPage) && (wgAction == 'view' || wgAction == 'submit' || wgAction == 'edit' || wgAction == 'purge') && (location.href.indexOf('title=') && location.href.indexOf('diff=')) == -1 && !(wgCanonicalNamespace == '' && wgTitle == wgMainPageTitle))
		globalWrapper.style.width = cactions.style.width = pBody.style.width = '1000px';
		
	// Shorter search text (searchGoButton, mw-searchButton)
	$('searchGoButton').value = 'G';
	$('mw-searchButton').value = 'S';
	
	// remove the extra space after editsections
	var editSections = document.getElementsByClassName('editsection');
	var nextSibling;
	for (var i = 0; i < editSections.length; i++)
	{
		nextSibling = editSections[i].nextSibling;
		if (nextSibling && nextSibling.nodeType == 3 && nextSibling.nodeValue == ' ')
			nextSibling.parentNode.removeChild(nextSibling);
	}
}