MediaWiki:Common.js

/* */

/* ######################################################################## */ /* ### JavaScript here is loaded for all users and all skins. ### */ /* ######################################################################## */

/* ######################################################################## */ /* ### TITLE ICONS (Template:Games)                                    ### */ /* ### ### */ /* ### Description: Add icons to article title                          ### */ /* ### Credit:     User:Porter21                                       ### */ /* ######################################################################## */

function addTitleIcons { var iconBar = $('#va-titleicons'); var previewBar = $('#va-titleicons-preview');

if (skin != 'monobook' && skin != 'oasis' && skin != 'wikia') { return; }

if (iconBar.length > 0 && $('a', previewBar).length > 0) { if (skin == 'oasis' || skin == 'wikia') { var articleDiv = $('#WikiaArticle');

if (articleDiv.length > 0) { iconBar.css('display', 'block').prependTo(articleDiv); }     } else if (skin == 'monobook') { var firstHeading = $('#firstHeading').css('position', 'relative');

if (firstHeading.length > 0) { iconBar.css('display', 'block').appendTo(firstHeading.css('padding-right', previewBar.width + 25)); }     }

$('#va-titleicons-more').append('');

iconBar.hover(        function  {            $(this).addClass('va-titleicons-hover');         }, function  {            $(this).removeClass('va-titleicons-hover');         }); } }

/* ######################################################################## */ /* ### COLLAPSIBLE TABLES                                              ### */ /* ### ### */ /* ### Description: Allow tables to be collapsible                      ### */ /* ### Credit:     http://en.wikipedia.org/wiki/MediaWiki:Common.js    ### */ /* ###             Customized for Fallout Wiki by User:Porter21        ### */ /* ######################################################################## */

/* Test if an element has a certain class ************************************** * * Description: Uses regular expressions and caching for better performance. * Maintainers: User:Mike Dillon, User:R. Koot, User:SG */

var hasClass = (function {   var reCache = {};   return function (element, className) {      return (reCache[className] ? reCache[className] : (reCache[className] = new RegExp("(?:\\s|^)" + className + "(?:\\s|$)"))).test(element.className);  }; });

/** Collapsible tables ********************************************************* * * Description: Allows tables to be collapsed, showing only the header. See *              NavFrame. * Maintainers: User:R. Koot */

var autoCollapse = 1; var collapseCaption = "hide"; var expandCaption = "show";

function collapseTable(tableIndex) { var Button = document.getElementById( "collapseButton" + tableIndex ); var Table = document.getElementById( "collapsibleTable" + tableIndex );

if ( !Table || !Button ) { return false; }

var Rows = Table.rows;

if ( Button.firstChild.data == collapseCaption ) { for ( var i = 1; i < Rows.length; i++ ) { Rows[i].style.display = "none"; }     Button.firstChild.data = expandCaption; } else { for ( var i = 1; i < Rows.length; i++ ) { Rows[i].style.display = Rows[0].style.display; }     Button.firstChild.data = collapseCaption; } }

function createCollapseButtons { var tableIndex = 0; var collapseIndex = 0; var NavigationBoxes = new Object; var Tables = document.getElementsByTagName( "table" );

for ( var i = 0; i < Tables.length; i++ ) { if ( hasClass( Tables[i], "collapsible" ) ) {

/* only add button and increment count if there is a header row to work with */ var HeaderRow = Tables[i].getElementsByTagName( "tr" )[0]; if (!HeaderRow) continue; var Header = HeaderRow.getElementsByTagName( "th" )[0]; if (!Header) continue;

NavigationBoxes[ tableIndex ] = Tables[i]; Tables[i].setAttribute( "id", "collapsibleTable" + tableIndex );

var Button    = document.createElement( "span" ); var ButtonLink = document.createElement( "a" ); var ButtonText = document.createTextNode( collapseCaption );

Button.style.styleFloat = "right"; Button.style.cssFloat = "right"; Button.style.fontWeight = "normal"; Button.style.textAlign = "right"; Button.style.width = "4em"; Button.className = "t_show_hide";

ButtonLink.style.color = Header.style.color; ButtonLink.setAttribute( "id", "collapseButton" + tableIndex ); ButtonLink.setAttribute( "href", "javascript:collapseTable(" + tableIndex + ");" ); ButtonLink.appendChild( ButtonText );

Button.appendChild( document.createTextNode( "[" ) ); Button.appendChild( ButtonLink ); Button.appendChild( document.createTextNode( "]" ) );

Header.insertBefore( Button, Header.childNodes[0] );

if ( !hasClass( Tables[i], "nocount" ) ) { collapseIndex++; }        tableIndex++; }  }

for ( var i = 0; i  ').appendTo(bc); var ul = $(' ').appendTo(table); for ( var l = 0; l < lines.length; l++ ) { var line = lines[l]; $(' ').toggleClass('noarchive', (new RegExp('^\\{\\{'+archiveListRegex+'\}\}')).test(line)) .attr({line:line}) .append( $(' ').text(line).append(' ') ).appendTo(ul); }              var sections = []; var sectionEnd = lines.length-1; for ( var l = lines.length-1; l >= 0; l-- ) { var line = lines[l]; if ( /^=.+?=/.test(line) || l === 0 ) { var section = { start: l, end: sectionEnd }; section.length = section.end - section.start + 1; sections.unshift(section); sectionEnd = l-1; }              }               var section; while( section = sections.shift ) { var tr = ul.children.eq(section.start); $(' ').attr({rowspan: section.length}).appendTo(tr); }              $(' ').append(                  $(skinConfig.buttonOpenSec + msg('buttonSelectAll') + skinConfig.buttonClose).click(function(e) { e.preventDefault; ul.children('tr').addClass('archive'); }), ' ',                 $(skinConfig.buttonOpenSec + msg('buttonDeselectAll') + skinConfig.buttonClose).click(function(e) { e.preventDefault; ul.children('tr').removeClass('archive'); }), ' ',                 $(skinConfig.buttonOpenPri + msg('buttonSaveArchive') + skinConfig.buttonClose).click(function(e) { archive; }), ' ',                 $(skinConfig.buttonOpenPri + msg('buttonAbort') + skinConfig.buttonClose).click(function(e) { bc.find('.ajax').remove; location = wgServer+wgScript+'?title='+encodeURI(wgPageName)+'&action=purge'; })              ).prependTo(bc).clone(true).appendTo(bc); var click = false; var add; table.mousedown(function(e) {                 e.preventDefault;                  var $li = $(e.target).closest('tr');                  if(!$li.length) return;                  var $section = $(e.target).closest('.section');                  if ( $section.length ) {                     var slist = $li.nextAll(':lt('+(parseInt($section.attr('rowspan'),10)-1)+')').andSelf;                     var sadd = slist.filter(function { return !$(this).hasClass('archive') }).length;                     slist.toggleClass('archive', !!sadd);                     return;                  }                  click = true;                  add = !$li.hasClass('archive');                  $li.toggleClass('archive', !!add);               }); table.mouseover(function(e) {                 if (!click) return;                  var $li = $(e.target).closest('tr');                  if(!$li.length) return;                  $li.toggleClass('archive', !!add);               }); $('body').mouseup(function(e) {                 click = false;               }); function archive { var talkLines = []; var archiveLines = []; ul.children.each(function {                    var arr = $(this).hasClass('noarchive') || !$(this).hasClass('archive')                        ? talkLines : archiveLines;                     arr.push( $(this).attr('line') );                  }); if ( !(new RegExp('^\\{\\{'+archiveListRegex+'\}\}')).test(talkLines[0]) ) talkLines = [, ].concat(talkLines); archiveLines = [, ].concat(archiveLines); bc.empty; $('') .attr({src: stylepath+'/common/progress-wheel.gif'}).appendTo(bc); runArchive(talkLines.join('\n'), archiveLines.join('\n')); }              var archiveTitle; function runArchive(talkContent, archiveContent) { var archiveNo; function findArchives { var m = $(' Finding archive id: ').appendTo(bc); api({                       action: 'query',                        list: 'allpages',                        apnamespace: wgNamespaceNumber,                        apprefix: wgTitle+'/'+config.archiveSubpage,                        aplimit: 1,                        apdir: 'descending'                     }, function(q) {                        archiveNo = q.query.allpages.length ?                           parseInt(q.query.allpages[0].title.substr(wgPageName.length+("/"+config.archiveSubpage).length),10)+1 :                           1;                        archiveTitle = wgPageName+'/'+config.archiveSubpage+' '+archiveNo;                        m.append('done... (using '+archiveNo+')');                       saveArchive;                     }); }                 function saveArchive { var m = $(' Finding token for '+archiveTitle+': ').appendTo(bc); token(archiveTitle, function(p) {                       m.append('done...');                        m = $(' Saving archive page: ').appendTo(bc);                        api({ action: 'edit', title: archiveTitle, text: archiveContent, token: p.edittoken, summary: ""+ msg('summaryArchiveFrom') + " "+wgPageName+".", minor: true, createonly: true }, function(q) { if ( q.error && q.error.code === "articleexists" ) { m.append('failed...'); bc.append(" The archive page we tried to create already exists. "); return abort; }                          m.append('done...'); saveTalk; });                    });                  }                  function saveTalk { var m = $(' Finding token for '+wgPageName+': ').appendTo(bc); m.append('done...'); m = $(' Updating talk page: ').appendTo(bc); api({                       action: 'edit',                        title: wgPageName,                        text: talkContent,                        token: talkToken,                        summary: "" + msg('summaryArchiveTo') + " "+archiveTitle+".",                        minor: true,                        basetimestamp: time,                        starttimestamp: tokenTime                     }, function(q) {                        if ( q.edit.result === "Success" ) {                           m.append('done...');                           bc.find('.ajax').remove;                           location = wgServer+wgScript+'?title='+encodeURI(wgPageName)+'&action=purge';                        } else {                           m.append('failed...');                           bc.append(" Failed to update talkpage, you may wish to have the archive subpage we just created deleted. ");                          return abort;                        }                     });                  }                  function abort {                     bc.find('.ajax').remove;                     bc.append(" Aborting... ");                     $(" You may want to ")                        .append( $('refresh').attr({href: wgServer+wgArticlePath.replace('$1', encodeURI(wgPageName))}) )                        .append(' and try again.')                        .appendTo(bc);                  }                  // start                  findArchives;               }            });         }         $(skinConfig.controlsMarkup)            .click(startArchiving)            .appendTo(skinConfig.pageControls);      });   } }

/* ######################################################################## */ /* ### AJAX RC                                                         ### */ /* ### ### */ /* ### Description: Automatically refresh "Recent changes" via AJAX     ### */ /* ### Credit:     User:pcj (http://www.wowpedia.org)                  ### */ /* ######################################################################## */

var ajaxIndicator = stylepath + '/common/progress-wheel.gif'; var ajaxPages = new Array("Special:RecentChanges", "Special:WikiActivity", "The_Vault:WikiActivity"); var ajaxTimer; var ajaxRefresh = 60000; var refreshText = 'Auto-refresh'; var refreshHover = 'Enable auto-refreshing page loads'; var doRefresh = true;

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) } function getCookie(c_name) { if (document.cookie.length>0) { c_start=document.cookie.indexOf(c_name + "=") if (c_start!=-1) { c_start=c_start + c_name.length+1 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 "" } function preloadAJAXRL { ajaxRLCookie = (getCookie("ajaxload-"+wgPageName)=="on") ? true:false; appTo = ($("#WikiaPageHeader").length)?$("#WikiaPageHeader > h1"):$(".firstHeading"); appTo.append('&#160; ' + refreshText + ':   '); $("#ajaxLoadProgress").ajaxSend(function (event, xhr, settings){     if (location.href == settings.url) $(this).show;   }).ajaxComplete (function (event, xhr, settings){      if (location.href == settings.url) $(this).hide;   }); $("#ajaxToggle").click(toggleAjaxReload); $("#ajaxToggle").attr("checked", ajaxRLCookie); if (getCookie("ajaxload-"+wgPageName)=="on") loadPageData; } function toggleAjaxReload { if ($("#ajaxToggle").attr("checked") == true) { setCookie("ajaxload-"+wgPageName, "on", 30); doRefresh = true; loadPageData; } else { setCookie("ajaxload-"+wgPageName, "off", 30); doRefresh = false; clearTimeout(ajaxTimer); } }

function loadPageData { cC = ($("#WikiaArticle").length)?"#WikiaArticle":"#bodyContent"; $(cC).load(location.href + " " + cC + " > *", function (data) {      if (doRefresh) ajaxTimer = setTimeout("loadPageData;", ajaxRefresh);   }); }

/* ######################################################################## */ /* ### DISABLE ARCHIVE EDIT                                            ### */ /* ### ### */ /* ### Description: Remove section edit links and gray out main edit    ### */ /* ###             button on archived talk pages                       ### */ /* ### Credit:     User:Porter21                                       ### */ /* ######################################################################## */

function disableArchiveEdit { // Configuration var userconfig = (window.DisableArchiveEditConfig) ? window.DisableArchiveEditConfig : {}; var config = $.extend(true, {     archiveSubpage: 'Archive',      disableCompletely: false,      textColor: '#D9D9D9',      userLang: false,      // English      en: {         archived: "Archived",         archivedTooltip: "This page is an archive and should not be edited."      }   }, userconfig); // Function for multi-language support (by Daniel Friesen aka User:Dantman) function msg(name) { if ( config.userLang && wgUserLanguage in config && name in config[wgUserLanguage] ) return config[wgUserLanguage][name]; if ( wgContentLanguage in config && name in config[wgContentLanguage] ) return config[wgContentLanguage][name]; return config.en[name]; }  // Check whether page is an archive if ((new RegExp('\\/\[' + config.archiveSubpage.substr(0,1).toUpperCase     + config.archiveSubpage.substr(0,1).toLowerCase + '\]' + config.archiveSubpage.substr(1)      + '\\s\*\\d\*')).test(wgTitle)) { // Remove "add new section" links and prepare altering "edit" page control switch (skin) { case 'monaco': $('#control_addsection').remove; $('#fe_newsection').remove; editlink = $('#ca-edit'); break; case 'monobook': $('#ca-addsection').remove; editlink = $('#ca-edit > a'); break; case 'oasis': case 'wikia': var oasisButton = $(((wgNamespaceNumber == 3) ? '#WikiaUserPagesHeader' : '#WikiaPageHeader') + ' > ul.wikia-menu-button > li'); oasisButton.children("a:first").prependTo($('ul:first', oasisButton)).wrap(' ').children('img').remove; oasisButton.prepend(''); $('a[data-id="addtopic"]', oasisButton).parent.remove; editlink = $('a:first', oasisButton); break; }     // Remove "edit section" links $('span.editsection').remove; // Alter "edit" page control if (config.disableCompletely) { editlink.remove; } else { editlink.attr('title', msg('archivedTooltip')).css('color', config.textColor).text(msg('archived')); }  } } /* ######################################################################## */ /* ### COUNTDOWN                                                        ### */ /* ### ### */ /* ### Description: Adds countdown to specific date to a page. ### */ /* ### Credit:     User:Splarka (http://community.wikia.com)           ### */ /* ######################################################################## */

// Usage example: // //  Only January 01 2007 00:00:00 PST until New years. // //  Javascript disabled.

function updatetimer(i) { var now = new Date; var then = timers[i].eventdate; var diff = count=Math.floor((then.getTime-now.getTime)/1000);

// catch bad date strings if(isNaN(diff)) { timers[i].firstChild.nodeValue = '** ' + timers[i].eventdate + ' **' ; return; }

// determine plus/minus if(diff<0) { diff = -diff; var tpm = ''; } else { var tpm = ''; }

// calcuate the diff var left = (diff%60) + ' seconds'; diff=Math.floor(diff/60); if(diff > 0) left = (diff%60) + ' minutes ' + left; diff=Math.floor(diff/60); if(diff > 0) left = (diff%24) + ' hours ' + left; diff=Math.floor(diff/24); if(diff > 0) left = diff + ' days ' + left timers[i].firstChild.nodeValue = tpm + left;

// a setInterval is more efficient, but calling setTimeout // makes errors break the script rather than infinitely recurse timeouts[i] = setTimeout('updatetimer(' + i + ')',1000); }

function checktimers { //hide 'nocountdown' and show 'countdown' var nocountdowns = getElementsByClassName(document, 'span', 'nocountdown'); for(var i in nocountdowns) nocountdowns[i].style.display = 'none' var countdowns = getElementsByClassName(document, 'span', 'countdown'); for(var i in countdowns) countdowns[i].style.display = 'inline'

//set up global objects timers and timeouts. timers = getElementsByClassName(document, 'span', 'countdowndate'); //global timeouts = new Array; // generic holder for the timeouts, global if(timers.length == 0) return; for(var i in timers) { timers[i].eventdate = new Date(timers[i].firstChild.nodeValue); updatetimer(i); //start it up  } }

/* ######################################################################## */ /* ### DUPLICATE FILE LIST                                             ### */ /* ### ### */ /* ### Description: Finds duplicate files on the wiki. ### */ /* ### Credit:     User:pcj (http://www.wowpedia.org)                  ### */ /* ######################################################################## */

function findDupFiles(gf) { var fileDiv = $('#mw-dupfiles');

if (fileDiv.length) { dil = new Array; ajaxIndicator = stylepath + '/common/progress-wheel.gif'; output = ''; url = '/api.php?action=query&generator=allimages&prop=duplicatefiles&gailimit=500&format=json';

if (!($('#dupFilesProgress').length)) { fileDiv.prepend('  '); }

if (gf) { url += "&gaifrom=" + gf; }

$.getJSON( url, function (data) {        if ( data.query ) {            pages = data.query.pages;

for (pageID in pages) { dils = "," + dil.join;

if ( dils.indexOf("," + pages[pageID].title) == -1                  && pages[pageID].title.indexOf("File::") == -1 && pages[pageID].duplicatefiles ) { output += " " + pages[pageID].title + " \n\n";

for ( x = 0; x < pages[pageID].duplicatefiles.length; x++ ) { output += "File:" + pages[pageID].duplicatefiles[x].name + "\n"; dil.push("File:" + pages[pageID].duplicatefiles[x].name.replace(/_/g, " ")); }                 output += "</ul>\n\n" }           }

fileDiv.append(output);

if (data["query-continue"]) { setTimeout("findDupFiles('" + encodeURIComponent(data["query-continue"].allimages.gaifrom).replace(/'/g, "%27") + "');", 5000); } else { $('#dupFilesProgress').hide; }        }      } );   } }

/* ######################################################################## */ /* ### SCRIPT LOADER                                                   ### */ /* ### ### */ /* ### Description: Loads all the other scripts                         ### */ /* ### Credit:     User:Porter21                                       ### */ /* ######################################################################## */

function vaultScriptLoader { // Always loaded checktimers; createCollapseButtons;

// Only loaded for specific namespaces if ((wgNamespaceNumber == 0 || wgNamespaceNumber == 4 || wgNamespaceNumber == 110 || wgNamespaceNumber == 502) &&     !wgIsMainpage) { addTitleIcons; }

if (wgNamespaceNumber%2 != 0 && wgNamespaceNumber != 501) { archiveTool; disableArchiveEdit; }

// Only loaded for specific pages if (wgPageName == 'The_Vault:Duplicate_files') { findDupFiles; }  for (x in ajaxPages) { if (wgPageName == ajaxPages[x] && $("#ajaxToggle").length==0) preloadAJAXRL; } }

jQuery(function($) {  vaultScriptLoader; });

/* */