Difference between revisions of "MediaWiki:Common.js"
From Pandora Wiki
EvilDragon (talk | contribs) (New page: →Any JavaScript here will be loaded for all users on every page load.: /* Test if an element has a certain class ************************************** * Note: this is straight out ...) |
EvilDragon (talk | contribs) |
||
(3 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
/* Any JavaScript here will be loaded for all users on every page load. */ | /* Any JavaScript here will be loaded for all users on every page load. */ | ||
− | + | ||
− | + | ||
/* Test if an element has a certain class ************************************** | /* Test if an element has a certain class ************************************** | ||
* Note: this is straight out of Wikipedia's Common.js (http://en.wikipedia.org/wiki/MediaWiki:Common.js) | * Note: this is straight out of Wikipedia's Common.js (http://en.wikipedia.org/wiki/MediaWiki:Common.js) | ||
*/ | */ | ||
− | + | ||
var hasClass = (function () { | var hasClass = (function () { | ||
var reCache = {}; | var reCache = {}; | ||
Line 12: | Line 12: | ||
}; | }; | ||
})(); | })(); | ||
− | + | ||
− | + | ||
/** Hideable notes ********************************************************* | /** Hideable notes ********************************************************* | ||
* This is based off of code from Wikipedia's Common.js (http://en.wikipedia.org/wiki/MediaWiki:Common.js) | * This is based off of code from Wikipedia's Common.js (http://en.wikipedia.org/wiki/MediaWiki:Common.js) | ||
*/ | */ | ||
− | + | ||
function showNotes( spanIndex ) | function showNotes( spanIndex ) | ||
{ | { | ||
var Button = document.getElementById( "showNoteLink" + spanIndex ); | var Button = document.getElementById( "showNoteLink" + spanIndex ); | ||
var Span = document.getElementById( "showNotes" + spanIndex ); | var Span = document.getElementById( "showNotes" + spanIndex ); | ||
− | + | ||
if (!Span || !Button) | if (!Span || !Button) | ||
{ | { | ||
return false; | return false; | ||
} | } | ||
− | + | ||
if (Button.firstChild.data == "[show]") | if (Button.firstChild.data == "[show]") | ||
{ | { | ||
Line 39: | Line 39: | ||
} | } | ||
} | } | ||
− | + | ||
function createShowNotesLinks() | function createShowNotesLinks() | ||
{ | { | ||
Line 45: | Line 45: | ||
var Spans = document.getElementsByTagName( "span" ); | var Spans = document.getElementsByTagName( "span" ); | ||
var CurrentSpan = new Object(); | var CurrentSpan = new Object(); | ||
− | + | ||
for ( var i = 0; i < Spans.length; i++ ) | for ( var i = 0; i < Spans.length; i++ ) | ||
{ | { | ||
Line 52: | Line 52: | ||
CurrentSpan = Spans[i]; | CurrentSpan = Spans[i]; | ||
CurrentSpan.setAttribute( "id", "showNotes" + spanIndex ); | CurrentSpan.setAttribute( "id", "showNotes" + spanIndex ); | ||
− | + | ||
var Button = document.createElement( "span" ); | var Button = document.createElement( "span" ); | ||
var ButtonLink = document.createElement( "a" ); | var ButtonLink = document.createElement( "a" ); | ||
var ButtonText = document.createTextNode( "[show]" ); | var ButtonText = document.createTextNode( "[show]" ); | ||
− | + | ||
ButtonLink.setAttribute( "id", "showNoteLink" + spanIndex ); | ButtonLink.setAttribute( "id", "showNoteLink" + spanIndex ); | ||
− | ButtonLink.setAttribute( "href", " | + | ButtonLink.setAttribute( "href", "javascript:" ); |
addHandler( ButtonLink, "click", new Function( "evt", "showNotes(" + spanIndex + " ); return killEvt( evt );") ); | addHandler( ButtonLink, "click", new Function( "evt", "showNotes(" + spanIndex + " ); return killEvt( evt );") ); | ||
+ | ButtonLink.appendChild( ButtonText ); | ||
+ | |||
+ | Button.appendChild(document.createTextNode( " " )); | ||
+ | Button.appendChild(ButtonLink); | ||
+ | |||
+ | CurrentSpan.parentNode.insertBefore( Button, CurrentSpan.nextSibling ); | ||
+ | CurrentSpan.style.display="none"; | ||
+ | |||
+ | spanIndex++; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | addOnloadHook( createShowNotesLinks ); | ||
+ | |||
+ | |||
+ | function mouseOver(e, imgUrl) | ||
+ | { | ||
+ | var body = document.getElementById('bodyContent'); | ||
+ | |||
+ | var image = new Image(); | ||
+ | image.src = imgUrl; | ||
+ | |||
+ | var previewImage = document.createElement('img'); | ||
+ | previewImage.setAttribute('src', imgUrl); | ||
+ | previewImage.setAttribute('id', 'previewImage'); | ||
+ | |||
+ | var scrolledHeight = 0; | ||
+ | |||
+ | if (document.documentElement.scrollTop) | ||
+ | scrolledHeight = document.documentElement.scrollTop; | ||
+ | else if (document.body.scrollTop) | ||
+ | scrolledHeight = document.body.scrollTop; | ||
+ | else | ||
+ | scrolledHeight = window.pageYOffset; | ||
+ | |||
+ | previewStyle = "position: absolute; left: " + (e.clientX - image.width - 200) + "px; top: " + (e.clientY + scrolledHeight - 30) + "px; border: 1px solid grey;"; | ||
+ | previewImage.setAttribute('style', previewStyle); | ||
+ | |||
+ | body.appendChild(previewImage); | ||
+ | } | ||
+ | |||
+ | |||
+ | function mouseOut() | ||
+ | { | ||
+ | var body = document.getElementById('bodyContent'); | ||
+ | var previewImage = document.getElementById('previewImage'); | ||
+ | |||
+ | body.removeChild(previewImage); | ||
+ | } | ||
+ | |||
+ | function createScreenshotLinks() | ||
+ | { | ||
+ | var spanIndex = 0; | ||
+ | var Spans = document.getElementsByTagName( "span" ); | ||
+ | var CurrentSpan = new Object(); | ||
+ | |||
+ | for ( var i = 0; i < Spans.length; i++ ) | ||
+ | { | ||
+ | if ( hasClass( Spans[i], "previewScreenshot" ) ) | ||
+ | { | ||
+ | CurrentSpan = Spans[i]; | ||
+ | |||
+ | var wikiLinks = new Object(); // This is needed because the wiki automatically turns urls into links. | ||
+ | wikiLinks = CurrentSpan.getElementsByTagName( "a" ); | ||
+ | var imgURL = wikiLinks[0].innerHTML; | ||
+ | |||
+ | var Button = document.createElement( "span" ); | ||
+ | var ButtonLink = document.createElement( "a" ); | ||
+ | var ButtonText = document.createTextNode( "[ss]" ); | ||
+ | |||
+ | ButtonLink.setAttribute("href", imgURL); | ||
+ | ButtonLink.setAttribute("target", "_" + imgURL); | ||
ButtonLink.appendChild( ButtonText ); | ButtonLink.appendChild( ButtonText ); | ||
Button.appendChild(document.createTextNode( " " )); | Button.appendChild(document.createTextNode( " " )); | ||
Button.appendChild(ButtonLink); | Button.appendChild(ButtonLink); | ||
+ | Button.setAttribute( "onmouseover", "mouseOver(event, '" + imgURL + "');"); | ||
+ | Button.setAttribute( "onmouseout", "mouseOut();"); | ||
CurrentSpan.parentNode.insertBefore( Button, CurrentSpan.nextSibling ); | CurrentSpan.parentNode.insertBefore( Button, CurrentSpan.nextSibling ); | ||
Line 73: | Line 148: | ||
} | } | ||
− | + | addOnloadHook( createScreenshotLinks ); |
Latest revision as of 12:28, 25 January 2011
/* Any JavaScript here will be loaded for all users on every page load. */
/* Test if an element has a certain class **************************************
* Note: this is straight out of Wikipedia's Common.js (http://en.wikipedia.org/wiki/MediaWiki:Common.js)
*/
var hasClass = (function () {
var reCache = {};
return function (element, className) {
return (reCache[className] ? reCache[className] : (reCache[className] = new RegExp("(?:\\s|^)" + className + "(?:\\s|$)"))).test(element.className);
};
})();
/** Hideable notes *********************************************************
* This is based off of code from Wikipedia's Common.js (http://en.wikipedia.org/wiki/MediaWiki:Common.js)
*/
function showNotes( spanIndex )
{
var Button = document.getElementById( "showNoteLink" + spanIndex );
var Span = document.getElementById( "showNotes" + spanIndex );
if (!Span || !Button)
{
return false;
}
if (Button.firstChild.data == "[show]")
{
Span.style.display = "inline";
Button.firstChild.data = " [hide]";
}
else
{
Span.style.display = "none";
Button.firstChild.data = "[show]";
}
}
function createShowNotesLinks()
{
var spanIndex = 0;
var Spans = document.getElementsByTagName( "span" );
var CurrentSpan = new Object();
for ( var i = 0; i < Spans.length; i++ )
{
if ( hasClass( Spans[i], "hiddenNotes" ) )
{
CurrentSpan = Spans[i];
CurrentSpan.setAttribute( "id", "showNotes" + spanIndex );
var Button = document.createElement( "span" );
var ButtonLink = document.createElement( "a" );
var ButtonText = document.createTextNode( "[show]" );
ButtonLink.setAttribute( "id", "showNoteLink" + spanIndex );
ButtonLink.setAttribute( "href", "javascript:" );
addHandler( ButtonLink, "click", new Function( "evt", "showNotes(" + spanIndex + " ); return killEvt( evt );") );
ButtonLink.appendChild( ButtonText );
Button.appendChild(document.createTextNode( " " ));
Button.appendChild(ButtonLink);
CurrentSpan.parentNode.insertBefore( Button, CurrentSpan.nextSibling );
CurrentSpan.style.display="none";
spanIndex++;
}
}
}
addOnloadHook( createShowNotesLinks );
function mouseOver(e, imgUrl)
{
var body = document.getElementById('bodyContent');
var image = new Image();
image.src = imgUrl;
var previewImage = document.createElement('img');
previewImage.setAttribute('src', imgUrl);
previewImage.setAttribute('id', 'previewImage');
var scrolledHeight = 0;
if (document.documentElement.scrollTop)
scrolledHeight = document.documentElement.scrollTop;
else if (document.body.scrollTop)
scrolledHeight = document.body.scrollTop;
else
scrolledHeight = window.pageYOffset;
previewStyle = "position: absolute; left: " + (e.clientX - image.width - 200) + "px; top: " + (e.clientY + scrolledHeight - 30) + "px; border: 1px solid grey;";
previewImage.setAttribute('style', previewStyle);
body.appendChild(previewImage);
}
function mouseOut()
{
var body = document.getElementById('bodyContent');
var previewImage = document.getElementById('previewImage');
body.removeChild(previewImage);
}
function createScreenshotLinks()
{
var spanIndex = 0;
var Spans = document.getElementsByTagName( "span" );
var CurrentSpan = new Object();
for ( var i = 0; i < Spans.length; i++ )
{
if ( hasClass( Spans[i], "previewScreenshot" ) )
{
CurrentSpan = Spans[i];
var wikiLinks = new Object(); // This is needed because the wiki automatically turns urls into links.
wikiLinks = CurrentSpan.getElementsByTagName( "a" );
var imgURL = wikiLinks[0].innerHTML;
var Button = document.createElement( "span" );
var ButtonLink = document.createElement( "a" );
var ButtonText = document.createTextNode( "[ss]" );
ButtonLink.setAttribute("href", imgURL);
ButtonLink.setAttribute("target", "_" + imgURL);
ButtonLink.appendChild( ButtonText );
Button.appendChild(document.createTextNode( " " ));
Button.appendChild(ButtonLink);
Button.setAttribute( "onmouseover", "mouseOver(event, '" + imgURL + "');");
Button.setAttribute( "onmouseout", "mouseOut();");
CurrentSpan.parentNode.insertBefore( Button, CurrentSpan.nextSibling );
CurrentSpan.style.display="none";
spanIndex++;
}
}
}
addOnloadHook( createScreenshotLinks );