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 (1st half). 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.
/*
	GARY KING'S LAYOUT

	FIXME When action=purge, icons are moved up higher.
	
	TODO Change wikEd icon to text.
	TODO Add a link to disable all (or also some) of the functions here.
	TODO Separate page into functions.
	TODO Consider templates (Template:) short pages? (In monobook.js)
	TODO Automatically enlarge lead images to 300px when a size has not been set?
*/

/*
	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);
	}
	
	// adjust references
	references = document.getElementsByClassName('references-small');
	for (var i = 0; i < references.length; i++)
	{
		ref = references[i];
		colCount = parseInt(ref.style.MozColumnCount || 1);
		colWidth = ref.style.MozColumnWidth || '30em';
		if (colCount == 2 || (colWidth.substr(colWidth.length - 2, 2) == 'em' && parseInt(colWidth) >= 30)) ref.style.MozColumnCount = ref.style.MozColumnWidth = 'auto';
		if (ref.scrollHeight > 250) 
		{
			ref.addClass('grey-border');
			ref.style.clear = 'both';
		}
	}
	
	// changes for non-discussion and discussion pages; covers discussion pages not in a "talk:" namespace, such as many noticeboards
	isDiscussionPage = ($('ca-addsection'));
	
	// don't add fancy text changes on pages with short paragraphs (discussions), AND better separate discussions
	if (isDiscussionPage)
	{
		importStylesheet('User:Gary King/short paragraphs.css');
		importStylesheet('User:Gary King/discussions.css');
	}
	
	// insert clear: right; after h3, h4, h5
	function addClears(elements)
	{
		for (var i = 0; i < elements.length; i++)
		{
			h = elements[i];
			if (!h.nextSibling) continue;
			div = document.createElement('div');
			div.addClass('clear-right');
			h.parentNode.insertBefore(div, h.nextSibling);
		}
	}
	
	h3 = content.getElementsByTagName('h3');
	h4 = content.getElementsByTagName('h4');
	h5 = content.getElementsByTagName('h5');
	
	/*addClears(h3);
	addClears(h4);
	addClears(h5);*/
	
	// adjust top icons distance from right
	topIcons = document.getElementsByClassName('topicon');
	numberOfTopIcons = topIcons.length;
	
	function getRightDist(element)
	{
		return parseInt(element.style.right);
	}
	
	distanceRight = 0;
	
	// ordered from left to right
	firstIcon = $('protected-icon') || $('status-top');
	secondIcon = $('spoken-icon');
	thirdIcon = $('featured-star') || $('good-star') || $('rollback-icon') || $('script-icon');
	
	if (firstIcon) distanceRight = getRightDist(firstIcon) + ($('status-top') ? 150 : 25);
	else if (secondIcon) distanceRight = getRightDist(secondIcon) + 25;
	else if (thirdIcon) distanceRight = getRightDist(thirdIcon) + 30;
	
	// move QuickEdit section-0 link to top-right corner of page
	sectionLink0 = $('sectionlink-0');
	
	// QE is adding an edit link to the lead section, so move it to the corner
	if (sectionLink0)
	{
		link = sectionLink0.parentNode;
		link.id = 'editsection-0';
		link.addClass('lead-qe-link');
		link.style.marginRight = distanceRight + 'px';
	
		// add new edit link into QE edit link
		newLink = document.createElement('a');
		newLink.href = wgScript + '?title=' + encodeURIComponent(wgPageName) + '&action=edit&section=0';
		newLink.title = 'Edit section';
		
		newLink.appendChild(document.createTextNode('edit'));
	
		link.insertBefore(newLink, sectionLink0);
		link.insertBefore(document.createTextNode('/'), sectionLink0);
		
		// move it
		content.insertBefore(link, section0);
	}
}