Difference between revisions of "MediaWiki:Common.js"

From The Infosphere, the Futurama Wiki
Jump to navigation Jump to search
m (hl)
Line 2: Line 2:
var reasonCaptionShow = "Show reason";
var reasonCaptionShow = "Show reason";
var reasonCaptionHide = "Hide reason";
var reasonCaptionHide = "Hide reason";
var currentTranscriptTarget = null;
var currentTranscriptHl = null;


var hasClass = (function () {
var hasClass = (function () {
Line 87: Line 85:


addOnloadHook(columnAreaImage);
addOnloadHook(columnAreaImage);
function getClientHeight() {
return document.documentElement.clientHeight;
}


//The following is for transcript articles.
//The following is for transcript articles.
var currentTranscriptTarget = null;
var currentTranscriptHl = null;
var writtenTranscriptLineLink = false;


function transcriptStart() {
function transcriptStart() {
if(!document.getElementById("transcript-field")) //check if it is a transcript article, otherwise jump out.
if(!document.getElementById("transcript-field")) //check if it is a transcript article, otherwise jump out.
return;
return;
tf = document.getElementById("transcript-field");
var tf = document.getElementById("transcript-field");
while(tf.firstChild)
while(tf.firstChild)
tf.removeChild(tf.firstChild);
tf.removeChild(tf.firstChild);
activateText = document.createTextNode("Activate edit mode");
var activateText = document.createTextNode("Activate edit mode");
activateAnchor = document.createElement("a");
var activateAnchor = document.createElement("a");
activateAnchor.setAttribute("href", "javascript:activateTranscript();");
activateAnchor.setAttribute("href", "javascript:activateTranscript();");
activateAnchor.appendChild(activateText);
activateAnchor.appendChild(activateText);
tf.appendChild(activateAnchor);
tf.appendChild(activateAnchor);
//Let's check if someone linked to a specific line in the transcript.
var fullURL = document.URL;
if(fullURL.search("#")!=-1) {
var bookmark = fullURL.subString(fullURL.indexOf("#"), fullURL.length);
var ypos = parseInt(bookmark.split("-")[1]) - (getClientHeight()/2);
window.scrollBy(0, ypos);
}
}
}


Line 106: Line 118:


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


function deactivateTranscript() {
function deactivateTranscript() {
te = document.getElementById("transcript-edit");
var te = document.getElementById("transcript-edit");
while(te.firstChild)
while(te.firstChild)
te.removeChild(te.firstChild);
te.removeChild(te.firstChild);
tf = document.getElementById("transcript-field");
var tf = document.getElementById("transcript-field");
te.style.display = "none";
te.style.display = "none";
activateText = document.createTextNode("Activate edit mode");
var activateText = document.createTextNode("Activate edit mode");
activateAnchor = document.createElement("a");
var activateAnchor = document.createElement("a");
activateAnchor.setAttribute("href", "javascript:activateTranscript();");
activateAnchor.setAttribute("href", "javascript:activateTranscript();");
activateAnchor.appendChild(activateText);
activateAnchor.appendChild(activateText);
Line 146: Line 163:
if(currentTranscriptTarget == event.target) {
if(currentTranscriptTarget == event.target) {
//Since we are here, it must be assumed this is the second time we clicked it.
//Since we are here, it must be assumed this is the second time we clicked it.
el = event.target;
var el = event.target;
if(el.tagName.toLowerCase()=="i")
if(el.tagName.toLowerCase()=="i")
el = el.parentNode.parentNode;
el = el.parentNode.parentNode;
Line 157: Line 174:
}
}
currentTranscriptHl = el;
currentTranscriptHl = el;
transcriptLineLink();
} else {
} else {
currentTranscriptTarget = event.target;
currentTranscriptTarget = event.target;
}
}
function transcriptLineLink() {
if(!writtenTranscriptLineLink) {
writtenTranscriptLineLink = true;
var tll = document.getElementById("transcript-linelink");
var strong = document.createElement("strong");
var text1 = document.createTextNode("Link to this line:");
var text2 = document.createTextNode("Direct URL:");
var text3 = document.createTextNode("In other articles:");
var label1 = document.createElement("label");
var label2 = document.createElement("label");
label1.setAttribute("for", "transcript-directlinelink");
label2.setAttribute("for", "transcript-articlelinelink");
var input1 = document.createElement("input");
var input2 = document.createElement("input");
input1.setAttribute("id", "transcript-directlinelink");
input1.setAttribute("size", "8");
input1.setAttribute("type", "text");
input1.setAttribute("value", wgServer + wgPageName + "#link-" + currentTranscriptHl.style.top);
input2.setAttribute("id", "transcript-articlelinelink");
input2.setAttribute("size", "8");
input2.setAttribute("type", "text");
input2.setAttribute("value", "{{transline|" + wgTitle.split(" (")[0] + "|" + currentTranscriptHl.style.top + "}}");
strong.appendChild(text1);
label1.appendChild(text2);
label2.appendChild(text3);
tll.appendChild(strong);
tll.appendChild(label1);
tll.appendChild(input1);
tll.appendChild(label2);
tll.appendChild(input2);
} else {
var input1 = document.getElementById("transcript-directlinelink");
var input1 = document.getElementById("transcript-articlelinelink");
input1.setAttribute("value", wgServer + wgPageName + "#link-" +  currentTranscriptHl.style.top);
input2.setAttribute("value", "{{transline|" + wgTitle.split(" (")[0] + "|" + currentTranscriptHl.style.top + "}}");
}
}
}
}

Revision as of 18:31, 5 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);

function getClientHeight() {
	return document.documentElement.clientHeight;
}

//The following is for transcript articles.
var currentTranscriptTarget = null;
var currentTranscriptHl = null;
var writtenTranscriptLineLink = false;

function transcriptStart() {
	if(!document.getElementById("transcript-field")) //check if it is a transcript article, otherwise jump out.
		return;
	var tf = document.getElementById("transcript-field");
	while(tf.firstChild)
		tf.removeChild(tf.firstChild);
	var activateText = document.createTextNode("Activate edit mode");
	var activateAnchor = document.createElement("a");
	activateAnchor.setAttribute("href", "javascript:activateTranscript();");
	activateAnchor.appendChild(activateText);
	tf.appendChild(activateAnchor);
	//Let's check if someone linked to a specific line in the transcript.
	var fullURL = document.URL;
	if(fullURL.search("#")!=-1) {
		var bookmark = fullURL.subString(fullURL.indexOf("#"), fullURL.length);
		var ypos = parseInt(bookmark.split("-")[1]) - (getClientHeight()/2);
		window.scrollBy(0, ypos);
	}
}

addOnloadHook(transcriptStart);

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

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

function transcriptLine(event) {
	if(!event) event = window.event;
	if(currentTranscriptTarget == event.target) {
		//Since we are here, it must be assumed this is the second time we clicked it.
		var el = event.target;
		if(el.tagName.toLowerCase()=="i")
			el = el.parentNode.parentNode;
		if(el.tagName.toLowerCase()=="dd")
			el = el.parentNode;
		el.style.backgroundColor = "#888888";
		if(currentTranscriptHl) {
			currentTranscriptHl.style.background = "transparent";
			currentTranscriptHl.style.backgroundColor = "";
		}
		currentTranscriptHl = el;
		transcriptLineLink();
	} else {
		currentTranscriptTarget = event.target;
	}
}

function transcriptLineLink() {
	if(!writtenTranscriptLineLink) {
		writtenTranscriptLineLink = true;
		var tll = document.getElementById("transcript-linelink");
		var strong = document.createElement("strong");
		var text1 = document.createTextNode("Link to this line:");
		var text2 = document.createTextNode("Direct URL:");
		var text3 = document.createTextNode("In other articles:");
		var label1 = document.createElement("label");
		var label2 = document.createElement("label");
		label1.setAttribute("for", "transcript-directlinelink");
		label2.setAttribute("for", "transcript-articlelinelink");
		var input1 = document.createElement("input");
		var input2 = document.createElement("input");
		input1.setAttribute("id", "transcript-directlinelink");
		input1.setAttribute("size", "8");
		input1.setAttribute("type", "text");
		input1.setAttribute("value", wgServer + wgPageName + "#link-" + currentTranscriptHl.style.top);
		input2.setAttribute("id", "transcript-articlelinelink");
		input2.setAttribute("size", "8");
		input2.setAttribute("type", "text");
		input2.setAttribute("value", "{{transline|" + wgTitle.split(" (")[0] + "|" + currentTranscriptHl.style.top + "}}");
		strong.appendChild(text1);
		label1.appendChild(text2);
		label2.appendChild(text3);
		tll.appendChild(strong);
		tll.appendChild(label1);
		tll.appendChild(input1);
		tll.appendChild(label2);
		tll.appendChild(input2);
	} else {
		var input1 = document.getElementById("transcript-directlinelink");
		var input1 = document.getElementById("transcript-articlelinelink");
		input1.setAttribute("value", wgServer + wgPageName + "#link-" +  currentTranscriptHl.style.top);
		input2.setAttribute("value", "{{transline|" + wgTitle.split(" (")[0] + "|" + currentTranscriptHl.style.top + "}}");		
	}
}