Difference between revisions of "MediaWiki:Common.js"

From The Infosphere, the Futurama Wiki
Jump to navigation Jump to search
(Now with event listener... let's see if it works.)
m (Hm... it appears to be a mouse event!)
Line 139: Line 139:
}
}


function transcriptLine(where) {
function transcriptLine(event) {
alert(where);
if(!event) event = window.event;
alert(event.relatedTarget);
}
}

Revision as of 14:06, 4 April 2008

/* Any JavaScript here will be loaded for all users on every page load. */
var reasonCaptionShow = "Show reason";
var reasonCaptionHide = "Hide reason";

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

function collapseReason(id) {
  var content = document.getElementById("reasonContent-" + id);
  var rSwitch = document.getElementById("reasonSwitch-" + id);
  if(rSwitch.firstChild.data==reasonCaptionShow) {
    content.style.display = "block";
    rSwitch.firstChild.data = reasonCaptionHide;
  } else {
    content.style.display = "none";
    rSwitch.firstChild.data = reasonCaptionShow;
  }
}

function createAllReasonButtons() {
  var reasonIndex = 0;
  var divs = document.getElementsByTagName("div");
  for(var i = 0; i < divs.length; i++) {
    if(hasClass(divs[i], "reasonContainer")) {
      var rSwitch = divs[i].childNodes[0].lastChild;
      var content = divs[i].childNodes[1];

      content.setAttribute("id", "reasonContent-" + reasonIndex);

      var link = document.createElement("a");
      var linkCaption = document.createTextNode(reasonCaptionShow);
      link.setAttribute("id", "reasonSwitch-" + reasonIndex);
      link.setAttribute("href", "javascript:collapseReason(" + reasonIndex + ");");
      link.appendChild(linkCaption);
      
      rSwitch.appendChild(link);
      
      reasonIndex++;
    }
  }
}

addOnloadHook(createAllReasonButtons);

function columnAreaImage(){
	var divS;
	var divC;
	if (document.getElementById ) {
		divS = document.getElementById("column-content");
		divC = document.getElementById("content");
	} else if (document.all ) {
		divS = document.all["column-content"];
		divC = document.all["content"];
	} else {
		return;
	}
	if ( divS && divS.style ){
		var divSHval;
		if (divS.style.pixelHeight) { 
			divSHval= divS.style.pixelHeight;
		} else {
			divSHval=  divS.offsetHeight;
		}
		
		if(Number(divSHval) >1136){
		//1136=768(number bigger than background image and side buttons)+imgheight(370)
			divS.style.backgroundImage = "url(/images/0/02/Infosphere_lower_Background.png)";
			divS.style.backgroundPosition = "bottom left";
			divS.style.backgroundRepeat = "no-repeat";
			if(hasClass(document.body, "ns-8"))
				return;
			if(divC && divC.style ) {
				//Uhm... apparently this is working now!  Woo!
				divC.style.backgroundImage = "url(/images/4/44/Infosphere_content_Background.png)";
				divC.style.backgroundPosition = "bottom left";
				divC.style.backgroundRepeat = "no-repeat";
			}
		}
	}
}

addOnloadHook(columnAreaImage);

//The following is for transcript articles.

function transcriptStart() {
	if(!document.getElementById("transcript-field")) //check if it is a transcript article, otherwise jump out.
		return;
	tf = document.getElementById("transcript-field");
	while(tf.firstChild)
		tf.removeChild(tf.firstChild);
	activateText = document.createTextNode("Activate edit mode");
	activateAnchor = document.createElement("a");
	activateAnchor.setAttribute("href", "javascript:activateTranscript();");
	activateAnchor.appendChild(activateText);
	tf.appendChild(activateAnchor);
}

addOnloadHook(transcriptStart);

function activateTranscript() {
	te = document.getElementById("transcript-edit");
	while(te.firstChild)
		te.removeChild(te.firstChild);
	tf = document.getElementById("transcript-field");
	while(tf.firstChild)
		tf.removeChild(tf.firstChild);
	editAnchor = document.createElement("a");
	deactivateAnchor = document.createElement("a");
	editText = document.createTextNode("Edit this line");
	deactivateText = document.createTextNode("Deactivate edit mode");
	separator = document.createTextNode(" | ");
	editAnchor.setAttribute("href", "javascript:transcriptEditThisLine();");
	deactivateAnchor.setAttribute("href", "javascript:deactivateTranscript();");
	editAnchor.appendChild(editText);
	deactivateAnchor.appendChild(deactivateText);
	te.appendChild(editAnchor);
	te.appendChild(separator);
	te.appendChild(deactivateAnchor);
	te.setAttribute("style", "display:block; position: fixed; top: " + (document.documentElement.clientHeight/2) + "px; right: 10px;");
	document.getElementById("content").addEventListener('click', transcriptLine, true);
}

function deactivateTranscript() {
	te = document.getElementById("transcript-edit");
	while(te.firstChild)
		te.removeChild(te.firstChild);
	tf = document.getElementById("transcript-field");
	te.style.display = "none";
	activateText = document.createTextNode("Activate edit mode");
	activateAnchor = document.createElement("a");
	activateAnchor.setAttribute("href", "javascript:activateTranscript();");
	activateAnchor.appendChild(activateText);
	tf.appendChild(activateAnchor);
}

function transcriptLine(event) {
	if(!event) event = window.event;
	alert(event.relatedTarget);
}