MediaWiki:Common.js
From RationalWikiWiki
Note: After saving, you may have to bypass your browser's cache to see the changes. Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh); Konqueror: click Reload or press F5; Opera: clear the cache in Tools → Preferences; Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* Any JavaScript here will be loaded for all users on every page load. */
// **********************************************************************
// ** ***WARNING GLOBAL GADGET FILE*** **
// ** changes to this file affect many users. **
// ** please discuss on the talk page before editing **
// ** **
// **********************************************************************
// Imported from: [[User:Steel359/^demon.js]]
// Version as of: 2007-07-01T00:27:43
// [[User:^demon/csd.js]] adapted for page protection use
// <pre><nowiki>
if (document.title.indexOf("Confirm protection") != -1) {
var protform = {
FillSelect:function(sel, arr){
if(arr && arr.length>0){
if(arr.length>1){
var FirstOpt = new Option("Protection reasons", "");
// catches stupid IE error
if(FirstOpt.innerHTML != "Protection reasons"){
FirstOpt.innerHTML = "Protection reasons";
}
sel.appendChild(FirstOpt);
sel.options[0].style.color = "gray"
}
sel.disabled = false;
for(var i=0;i<arr.length;i++){
var opt = new Option(arr[i].display, arr[i].value);
// catches stupid IE error
if(opt.innerHTML != arr[i].display){
opt.innerHTML = arr[i].display;
}
sel.appendChild(opt);
}
}else{
sel.options[0] = new Option("No Options Available", "");
// catches stupid IE error
if(sel.options[0].innerHTML != "No Options Available"){
sel.options[0].innerHTML = "No Options Available";
}
sel.disabled = true;
return false;
}
return true;
},
ValueArray:[
{"value":"Constant vandalism","display":"Constant vandalism"},
{"value":"Edit warring","display":"Edit warring"},
{"value":"RationalWikian editing own article","display":"RationalWikian editing own article"},
],
addEvent:function(obj, evType, fn, useCapture){
//alert(obj);
if (obj.addEventListener){
obj.addEventListener(evType, fn, useCapture);
return true;
} else if (obj.attachEvent){
var r = obj.attachEvent("on"+evType, fn);
return r;
} else {
alert("Handler could not be attached");
}
}
}
function addSelectAfter(){
var obj = document.getElementById('mwProtect-reason');
// create select using included array
var sel = document.createElement("SELECT");
protform .FillSelect(sel, protform .ValueArray);
// name it
sel.name = "proOptions";
sel.id = "proOptions";
sel.style.marginLeft = "10px";
protform .addEvent(sel, "change", protChangeBox, false);
// check for next sibling and insert before it or at the end
if(obj.nextSibling){
obj.parentNode.insertBefore(sel,obj.nextSibling);
}else{
obj.parentNode.appendChild(sel);
}
}
try{
// add one on load
protform.addEvent(window, "load", addSelectAfter, false);
}catch(err){}
}
function protChangeBox() {
var obj = document.getElementById('mwProtect-reason');
obj.value = document.getElementById('proOptions').value;
}
// ---------------------------------------------------------------------------------------------------------------
if (document.title.indexOf("Confirm protection") != -1) {
var protform2 = {
FillSelect:function(sel, arr){
if(arr && arr.length>0){
if(arr.length>1){
var FirstOpt = new Option("Protection expiries", "");
// catches stupid IE error
if(FirstOpt.innerHTML != "Protection expiries"){
FirstOpt.innerHTML = "Protection expiries";
}
sel.appendChild(FirstOpt);
sel.options[0].style.color = "gray"
}
sel.disabled = false;
for(var i=0;i<arr.length;i++){
var opt = new Option(arr[i].display, arr[i].value);
// catches stupid IE error
if(opt.innerHTML != arr[i].display){
opt.innerHTML = arr[i].display;
}
sel.appendChild(opt);
}
}else{
sel.options[0] = new Option("No Options Available", "");
// catches stupid IE error
if(sel.options[0].innerHTML != "No Options Available"){
sel.options[0].innerHTML = "No Options Available";
}
sel.disabled = true;
return false;
}
return true;
},
ValueArray:[
{"value":"1 day","display":"1 day"},
{"value":"2 days","display":"2 days"},
{"value":"3 days","display":"3 days"},
{"value":"5 days","display":"5 days"},
{"value":"1 week","display":"1 week"},
{"value":"2 weeks","display":"2 weeks"},
{"value":"3 weeks","display":"3 weeks"},
{"value":"4 weeks","display":"4 weeks"},
{"value":"6 weeks","display":"6 weeks"}
],
addEvent:function(obj, evType, fn, useCapture){
//alert(obj);
if (obj.addEventListener){
obj.addEventListener(evType, fn, useCapture);
return true;
} else if (obj.attachEvent){
var r = obj.attachEvent("on"+evType, fn);
return r;
} else {
alert("Handler could not be attached");
}
}
}
function addSelectAfter(){
var obj = document.getElementById('expires');
// create select using included array
var sel = document.createElement("SELECT");
protform2 .FillSelect(sel, protform2 .ValueArray);
// name it
sel.name = "proExpires";
sel.id = "proExpires";
sel.style.marginLeft = "10px";
protform2 .addEvent(sel, "change", protChangeBox2, false);
// check for next sibling and insert before it or at the end
if(obj.nextSibling){
obj.parentNode.insertBefore(sel,obj.nextSibling);
}else{
obj.parentNode.appendChild(sel);
}
}
try{
// add one on load
protform2.addEvent(window, "load", addSelectAfter, false);
}catch(err){}
}
function protChangeBox2() {
var obj = document.getElementById('expires');
obj.value = document.getElementById('proExpires').value;
}
/*
importScript and importStylesheet
allows easy sharing of js and css
part of wikibits.js in newer MediaWiki versions
*/
function importScript(page) {
var uri = wgScript + '?title=' +
encodeURIComponent(page.replace(/ /g,'_')).replace('%2F','/').replace('%3A',':') +
'&action=raw&ctype=text/javascript';
return importScriptURI(uri);
}
var loadedScripts = {}; // included-scripts tracker
function importScriptURI(url) {
if (loadedScripts[url]) {
return null;
}
loadedScripts[url] = true;
var s = document.createElement('script');
s.setAttribute('src',url);
s.setAttribute('type','text/javascript');
document.getElementsByTagName('head')[0].appendChild(s);
return s;
}
function importStylesheet(page) {
return importStylesheetURI(wgScript + '?action=raw&ctype=text/css&title=' + encodeURIComponent(page.replace(/ /g,'_')));
}
function importStylesheetURI(url) {
return document.createStyleSheet ? document.createStyleSheet(url) : appendCSS('@import "' + url + '";');
}
function appendCSS(text) {
var s = document.createElement('style');
s.type = 'text/css';
s.rel = 'stylesheet';
if (s.styleSheet) s.styleSheet.cssText = text //IE
else s.appendChild(document.createTextNode(text + '')) //Safari sometimes borks on null
document.getElementsByTagName('head')[0].appendChild(s);
return s;
}
// Adds a tab allowing you to edit the 0th section of a page (the top area usually used as an introduction).
//
addOnloadHook(function () {
var x;
if (!(x = document.getElementById('ca-edit') )) return;
var url;
if (!(url = x.getElementsByTagName('a')[0] )) return;
if (!(url = url.href )) return;
var y = addPortletLink('p-cactions', url+"§ion=0", '0', 'ca-edit-0',
'Edit the lead section of this page', '0', x.nextSibling);
y.className = x.className; // steal classes from the the edit tab...
x.className = 'istalk'; // ...and make the edit tab have no right margin
// exception: don't steal the "selected" class unless actually editing section 0:
if (/(^| )selected( |$)/.test(y.className)) {
if (!document.editform || !document.editform.wpSection
|| document.editform.wpSection.value != "0") {
y.className = y.className.replace(/(^| )selected( |$)/g, "$1");
x.className += ' selected';
}
}
});
/* 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
* [[Wikipedia:NavFrame]].
* Maintainers: [[User:R. Koot]]
*/
var autoCollapse = 2;
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 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 = "6em";
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] );
tableIndex++;
}
}
for ( var i = 0; i < tableIndex; i++ ) {
if ( hasClass( NavigationBoxes[i], "collapsed" ) || ( tableIndex >= autoCollapse && hasClass( NavigationBoxes[i], "autocollapse" ) ) ) {
collapseTable( i );
}
}
}
addOnloadHook( createCollapseButtons );
function collapsedCommentLinkClickHandler(e) {
e = e || window.event;
var eventSource = e.target || e.srcElement;
// firstly, collapse all tables
var tableIndex = 0;
var collapsibleButton = document.getElementById("collapseButton" + tableIndex);
while (collapsibleButton) {
if (collapsibleButton.firstChild.data == collapseCaption) {
collapseTable(tableIndex);
}
collapsibleButton = document.getElementById("collapseButton" + ++tableIndex);
}
// now, see if we can find the table to which this anchor refers
var anchorIndex = eventSource.href.indexOf('#');
if (anchorIndex != -1) {
var anchorName = eventSource.href.substring(anchorIndex+1);
var tableDiv = document.getElementById(anchorName);
if (tableDiv) {
// look for a collapsible table nested in this div, and expand it
var tables = tableDiv.getElementsByTagName("table");
for (var i = 0; i < tables.length; ++i) {
if (hasClass(tables[i], "collapsible") && tables[i].id.substring(0, 16) == "collapsibleTable") {
tableIndex = tables[i].id.substring(16);
collapseTable(tableIndex);
}
}
}
}
}
function addOnclickEventsToCollapsedCommentLinks() {
var spans = document.getElementsByTagName("span");
for (var i = 0; i < spans.length; ++i) {
if (spans[i].hasChildNodes() && hasClass(spans[i], "collapsed_comment_link")) {
// look for links amongst this element's daughter elements
var daughters = spans[i].getElementsByTagName("a");
for (var j = 0; j < daughters.length; ++j) {
// add an onclick handler to this anchor
daughters[j].onclick = collapsedCommentLinkClickHandler;
}
}
}
}
addOnloadHook(addOnclickEventsToCollapsedCommentLinks);
// </nowiki></pre>