MediaWiki:Common.js

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

/* Redirects 'Add a photo' button to Special:Upload */

$(function { // Restores the Special:Upload functionality. This does not block core functionality	if (window.UploadPhotos && window.UploadPhotos.showDialog) {		$('a.wikia-button.upphotos').unbind('click',UploadPhotos.showDialog);	} });

/** * Start upload form customisations. Copied from Wookieepedia. * @author Green tentacle */ function setupUploadForm{ // Check if cookie has been set for form style. Overrides URL parameter if set. var formstyle = getCookie("uploadform"); $("#uploadBasicLinkJS").show; $("#uploadTemplateNoJS").hide; var wpLicense = $('#wpLicense'); if ( wpLicense.length && window.location.search.indexOf('wpForReUpload=1') == -1){ if (formstyle == "guided" || (formstyle == "" && window.location.search.indexOf('basic=true') == -1)){ // Add link to basic form $("#uploadtext").prepend(' Switch to basic upload form '); // Stretch table to full width $('#mw-htmlform-description').css('width', '100%'); // Bind upload button to verify function $('#mw-upload-form').bind('submit', verifySummary); // Hide existing rows var rows = $('#mw-htmlform-description').find('tr'); $('tr.mw-htmlform-field-HTMLTextAreaField').hide; $('tr.mw-htmlform-field-HTMLTextAreaField').next.detach; $('#mw-htmlform-description').addClass('hidable start-hidden'); // Add new required rows rows.eq(1).after(' Source:  '); $('#mw-htmlform-description').append(' '); var tbody1 = $('#mw-htmlform-description').children('tbody').eq(0); tbody1.append(' Description:  '); tbody1.append(' Optional fields  '); // Add new optional rows var tbody2 = $('#mw-htmlform-description').children('tbody').eq(1); tbody2.append(' Attention:  '); tbody2.append(' Original designer / artist:  '); tbody2.append(' <td class="mw-label" style="width: 125px;">Conversion / editing / upload information: <td class="mw-input"><textarea id="filespecsBox" cols="60" rows="2" style="overflow: auto;"> '); tbody2.append(' <td class="mw-label" style="width: 125px;">Other versions / source images: <td class="mw-input"><textarea id="versionsBox" cols="60" rows="2" style="overflow: auto;"> '); tbody2.append(' <td class="mw-label" style="width: 125px;">Artist categories: <td class="mw-input"><textarea id="catartistBox" cols="60" rows="2" style="overflow: auto;"> '); tbody2.append(' <td class="mw-label" style="width: 125px;">Subject categories: <td class="mw-input"><textarea id="catsubjectBox" cols="60" rows="2" style="overflow: auto;"> '); tbody2.append(' <td class="mw-label" style="width: 125px;">Type categories: <td class="mw-input"><textarea id="cattypeBox" cols="60" rows="2" style="overflow: auto;"> '); } else { // Old style form just needs Information template in the summary box $('#wpUploadDescription').val('==Summary==\r\n'); // Add link to guided form $("#uploadtext").prepend(' Switch to guided upload form</a> '); }	} } function verifySummary{ var wpLicense = document.getElementById('wpLicense'); // Check for licensing if ( wpLicense.value == "" ){ alert('Licensing must be completed.'); return false; }	// Check for source if ( document.getElementById('sourceBox').value == "" ){ alert('Source must be completed.'); return false; }	var strBuilder = '==Summary==\r\n'; document.getElementById('wpUploadDescription').value = strBuilder; wpLicense.selectedIndex = 0; return true; }

addOnloadHook(function {	if ( wgCanonicalSpecialPageName === 'Upload' ) {		setupUploadForm;		addHideButtons;	} }); /** * End upload form customisations */

/** * Sets the cookie. Copied from Wookieepedia. * @param c_name string Name of the cookie * @param value string 'on' or 'off' * @param expiredays integer Expiry time of the cookie in days */ function setCookie( c_name, value, expiredays ) { var exdate = new Date; exdate.setDate( exdate.getDate + expiredays); document.cookie = c_name + "=" + escape(value) + ( ( expiredays === null ) ? "" : ";expires=" + exdate.toGMTString ); }

/** * Gets the cookie. Copied from Wookieepedia. * @param c_name string Cookie name * @return The cookie name or empty string */ function getCookie( c_name ) { if ( document.cookie.length > 0 ) { var c_start = document.cookie.indexOf( c_name + "=" ) if ( c_start !== -1 ) { c_start = c_start + c_name.length + 1; var c_end = document.cookie.indexOf( ";", c_start ); if ( c_end === -1 ) { c_end = document.cookie.length; }			return unescape( document.cookie.substring( c_start, c_end ) ); } 	}	return ""; }

/** * Hide buttons. Copied from Wookieepedia. */ function addHideButtons { var hidables = getElementsByClass('hidable'); for( var i = 0; i < hidables.length; i++ ) { var box = hidables[i]; var button = getElementsByClass('hidable-button', box, 'span'); if( button != null && button.length > 0 ) { button = button[0]; button.onclick = toggleHidable; button.appendChild( document.createTextNode('[Hide]') ); if( new ClassTester('start-hidden').isMatch(box) ) button.onclick('bypass'); }	} } function toggleHidable(bypassStorage) { var parent = getParentByClass('hidable', this); var content = getElementsByClass('hidable-content', parent); var nowShown; if( content != null && content.length > 0 ) { content = content[0]; if( content.style.display == 'none' ) { content.style.display = content.oldDisplayStyle; this.firstChild.nodeValue = '[Hide]'; nowShown = true; } else { content.oldDisplayStyle = content.style.display; content.style.display = 'none'; this.firstChild.nodeValue = '[Show]'; nowShown = false; }		if( window.storagePresent && ( typeof( bypassStorage ) == 'undefined' || bypassStorage != 'bypass' ) ) { var page = window.pageName.replace(/\W/g, '_'); var items = getElementsByClass('hidable'); var item = -1; for( var i = 0; i < items.length; i++ ) { if( items[i] == parent ) { item = i;					break; }			}			if( item == -1 ) { return; }			var storage = globalStorage[window.location.hostname]; storage.setItem('hidableshow-' + item + '_' + page, nowShown); }	} }

/** * End of hide buttons. */

/************************************************************ * Functions.js stuff * Copied from Wookieepedia * Could be rewritten with jQuery ************************************************************/ /*   Source: http://www.dustindiaz.com/getelementsbyclass/ getElementsByClass, which complements getElementById and getElementsByTagName, returns an array of all subelements of node that are tagged with a specific CSS class (searchClass) and are of the tag name tag. If tag is null, it searches for any suitable elements regardless of the tag name. Example: getElementsByClass('infobox', document.getElementById('content'), 'div') selects the same elements as the CSS declaration #content div.infobox function getElementsByClass(searchClass, node, tag) {	var classElements = new Array; if(node == null) node = document; if(tag == null) tag = '*'; var els = node.getElementsByTagName(tag); var elsLen = els.length; var tester = new ClassTester(searchClass); for(i = 0, j = 0; i < elsLen; i++) {		if(tester.isMatch(els[i])) {			classElements[j] = els[i]; j++; }	}	return classElements; } function ClassTester(className) {	this.regex = new RegExp("(^|\\s)" + className + "(\\s|$)"); } ClassTester.prototype.isMatch = function(element) {	return this.regex.test(element.className); } /*   end getElementsByClass

/*   Returns the element's nearest parent that has the specified CSS class. function getParentByClass(className, element) { var tester = new ClassTester(className); var node = element.parentNode; while(node != null && node != document) {		if(tester.isMatch(node)) return node; node = node.parentNode; }	return null; }