Editing has been locked on December 31st, 2017. All content and images will keep being available. Thank you for 10 years!

MediaWiki:Common.js

From Userpedia
Revision as of 09:02, 9 May 2017 by Anton (talk | contribs)
Jump to: navigation, search
/* Any JavaScript here will be loaded for all users on every page load. */

/** 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 );
	};
})();


/** rotating logo ***************************************************************
 *
 * description: i dont know how to describe it aaa
 * maintainers: 
 */

UPLogo2012Urls = [

]

/** pollspinner *****************************************************************
 *
 * description: this shows every awards poll to a user on the main page
 * maintainers: 2257
 */

function pollspinner_validateVote(pd_votefunction, otherBox, click) {

    if (otherBox.value == "none") {
        alert("Hey! It looks like you might be stupid!\n\nYour vote, " + otherBox.value + ", was denied for the following reason:\n\n");
    } else {
        pd_votefunction(click);
    }

}

function pollspinner_boxSetup(nextPoll) {

    var spinner = document.getElementsByClassName("pollspinner")[0];
    spinner.className = "pollspinner animated throbbing";

    var pollSubstitute = nextPoll.getElementsByClassName("pollspinner_poll")[0];
    pollSubstitute.id = "PDI_container" + pollSubstitute.dataset.pollid;

    while (pollSubstitute.childNodes.length > 0) {
        pollSubstitute.removeChild(pollSubstitute.childNodes[0]);
    }

    this['pd_callback_' + pollSubstitute.dataset.pollid] = pollspinner_callback;

    var pdscript = document.createElement("script");
    pdscript.type = "text/javascript";
    pdscript.language = "javascript";
    pdscript.src = "http://s3.polldaddy.com/p/" + pollSubstitute.dataset.pollid + ".js";
    pdscript.onload = function() {

        var spinner = document.getElementsByClassName("pollspinner")[0];
        spinner.className = "pollspinner animated";
        pollspinner_hideCurrent();

        var votebuttonOuter = nextPoll.getElementsByClassName("pds-votebutton-outer")[0];        
        var skip = document.createElement("a");
        skip.className = "pds-vote-button pollspinner_skip";
        skip.appendChild(document.createTextNode("Skip"));
        skip.addEventListener("click", pollspinner_skip.bind(null, spinner));
        votebuttonOuter.insertBefore(skip, votebuttonOuter.getElementsByClassName("pds-vote-button")[0].nextSibling);

        var voteButton = nextPoll.getElementsByClassName("pds-vote-button")[0]; 
        var otherBox = nextPoll.getElementsByClassName("pds-textfield")[0];
        var voteButtonOnclick = pollspinner_validateVote.bind(null, window['PD_prevote' + pollSubstitute.dataset.pollid], otherBox);
        voteButton.addEventListener("click", voteButtonOnclick);
        voteButton.removeEventListener("click", window['PD_prevote' + pollSubstitute.dataset.pollid]);

        var pdsBoxTop = nextPoll.getElementsByClassName("pds-box-top")[0];
        var throbber = document.createElement("div");
        throbber.className = "throbber";
        throbber.appendChild(document.createTextNode("Loading next poll..."));
        pdsBoxTop.appendChild(throbber);

        nextPoll.dataset.loaded = true;
        spinner.style.height = nextPoll.offsetHeight + "px";
        nextPoll.className = "pollspinnerbox active";
    }
    pollSubstitute.appendChild(pdscript);

}

function pollspinner_hideCurrent() {
    
        var spinner = document.getElementsByClassName("pollspinner")[0];
        var activeBoxes = spinner.getElementsByClassName("pollspinnerbox active");
        if (activeBoxes.length > 0) {
            activeBoxes[0].className = "pollspinnerbox answered";
        }
}

function pollspinner_getNext() {
    
    var spinner = document.getElementsByClassName("pollspinner")[0];

    var unansweredPolls = spinner.getElementsByClassName("pollspinnerbox unanswered");
    var skippedPolls = spinner.getElementsByClassName("pollspinnerbox skipped");
    
    if (unansweredPolls.length > 0) {
        
        var nextPoll = Math.floor(Math.random() * unansweredPolls.length);
        if (unansweredPolls[nextPoll].dataset.loaded) {
            pollspinner_hideCurrent();
            spinner.style.height = unansweredPolls[nextPoll].offsetHeight + "px";
            unansweredPolls[nextPoll].className = "pollspinnerbox active";
        } else {
            pollspinner_boxSetup(unansweredPolls[nextPoll]);
        }
        
    } else if (skippedPolls.length > 0) {
        
        while (skippedPolls.length > 0) {
            skippedPolls[0].className = "pollspinnerbox unanswered";
        }

        pollspinner_getNext();
        
    } else {

        pollspinner_hideCurrent();
        var end = spinner.getElementsByClassName("pollspinnerend");
        end[0].className = "pollspinnerend active";
        
        if (mw.config.get("wgUserName") == null) {
            
            end[0].innerHTML = "The power of the Votes is restored to the poll... and it's all thanks to you! Thank you!";
            
        } else {
            
            end[0].innerHTML = mw.config.get("wgUserName") + "! The power of the Votes is restored to the poll... and it's all thanks to you! Thank you, " + mw.config.get("wgUserName") + "!";
            
        }
        
        spinner.style.height = end[0].offsetHeight + "px";
    }
    
}

function pollspinner_callback( json ) {
    
    var obj = jQuery.parseJSON( json );
    
    if ( obj.result == 'registered' || obj.result == 'already-registered' ) {
        
        var spinner = document.getElementsByClassName("pollspinner")[0];

        var pdsBoxTop = document.getElementsByClassName("pollspinnerbox active")[0].getElementsByClassName("pds-box-top")[0];
        var throbber = document.createElement("div");
        throbber.className = "throbber";
        throbber.appendChild(document.createTextNode("Loading next poll..."));
        pdsBoxTop.appendChild(throbber);

        pollspinner_getNext();        
    }
}

function pollspinner_skip (spinner) {

    var activePoll = spinner.getElementsByClassName("pollspinnerbox active");
    if (activePoll.length > 0) {
        activePoll[0].className = "pollspinnerbox skipped";
        pollspinner_getNext();
    }
    
}


function pollspinner_setup () {
    
    var spinner = document.getElementsByClassName("pollspinner")[0];
    
    if (spinner != null) {
        
        var preload = new Image();
        preload.src = "http://wiki.userpedia.net/images/8/82/Pollthrobber.png";
        
        spinner.className = "pollspinner animated";
        var pollSpinnerBoxes = spinner.getElementsByClassName("pollspinnerbox");
        
        for (var i = 0; i < pollSpinnerBoxes.length; i++) {
            pollSpinnerBoxes[i].className = "pollspinnerbox unanswered" 
        }

        var end = document.createElement("div");
        end.className = "pollspinnerend";
        spinner.appendChild(end);
        
        pollspinner_getNext();

    }
}

pollspinner_setup();