MediaWiki:Gadget-Awards.js

From the Super Mario Wiki, the Mario encyclopedia
Revision as of 09:51, June 10, 2023 by Lakituthequick (talk | contribs) (Crowdsignal bug monkeypatch)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* Main Page poll spinner */

function pollspinner_boxSetup(nextPoll) {
	window.answerOtherHTML = undefined; // Crowdsignal bug monkeypatch
    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.bind(null, nextPoll);

    var pdscript = document.createElement("script");
    pdscript.addEventListener("error", pollspinner_reportLoadingError.bind(null, nextPoll));
    pdscript.type = "text/javascript";
    pdscript.language = "javascript";
    pdscript.src = "https://secure.polldaddy.com/p/" + pollSubstitute.dataset.pollid + ".js";
    pollSubstitute.appendChild(pdscript);
}

function pollspinner_reportLoadingError(nextPoll) {
    var pollSubstitute = nextPoll.getElementsByClassName("pollspinner_poll")[0];
    var errorMessage = document.createTextNode("It looks like the poll failed to load. This could be the result of a network error, or your browser might have denied the request. Please make sure that scripts from the domain secure.polldaddy.com are allowed, then click the button below to try again.");
    pollSubstitute.appendChild(errorMessage);

    var reload = document.createElement("div");
    reload.className = "pollspinner_begin";
    reload.appendChild(document.createTextNode("Reload"));
    reload.addEventListener("click", pollspinner_boxSetup.bind(null, nextPoll));
    pollSubstitute.appendChild(reload);

    pollspinner_activate(nextPoll);
}

function pollspinner_getNext_random(unansweredPolls) {
    return Math.floor(Math.random() * unansweredPolls.length);
}

function pollspinner_getNext_sequential(unansweredPolls) {
    return 0;
}

function pollspinner_appendSpecialButtons(nextPoll) {
        var spinner = document.getElementsByClassName("pollspinner")[0];
        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);
}

function pollspinner_resize() {
    var spinner = document.getElementsByClassName("pollspinner")[0];
    var element = spinner.querySelectorAll("[data-status='active']")[0];
    spinner.style.height = element.offsetHeight + "px";
    $(window).resize();
}

function pollspinner_activate(element) {
    var spinner = document.getElementsByClassName("pollspinner")[0];
    var activeBoxes = spinner.querySelectorAll("[data-status='active']");
    for (var i = 0; i < activeBoxes.length; i++) {
        activeBoxes[i].dataset.status = "unanswered";
    }
    
    var visibleBoxes = spinner.getElementsByClassName("pollspinnerbox visible");
    while (visibleBoxes.length > 0) {
        visibleBoxes[0].removeEventListener("resize", pollspinner_resize);
        visibleBoxes[0].className = visibleBoxes[0].className.replace(/ *visible */g, " ") + " hidden";
    }

    element.dataset.status = "active";
    pollspinner_resize();
    element.className = element.className.replace(/ *hidden */g, " ") + " visible";
}

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

    var unansweredPolls = spinner.querySelectorAll("[data-status='unanswered']");
    var skippedPolls = spinner.querySelectorAll("[data-status='skipped']");
    
    if (unansweredPolls.length > 0) {
        var nextPoll;

        switch (spinner.dataset.order) {
            case "sequential":
                nextPoll = pollspinner_getNext_sequential(unansweredPolls);
                break;
            case "random":
            default:
                nextPoll = pollspinner_getNext_random(unansweredPolls);
                break;
        }

        if (unansweredPolls[nextPoll].dataset.loaded) {
            pollspinner_activate(unansweredPolls[nextPoll]);
        } else {
            pollspinner_boxSetup(unansweredPolls[nextPoll]);
        }
    } else if (skippedPolls.length > 0) {
        for (var i = 0; i < skippedPolls.length; i++) {
            skippedPolls[i].dataset.status = "unanswered";
        }

        pollspinner_getNext();
    } else {
        var end = spinner.getElementsByClassName("pollspinnerbox end");
        
        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") + "!";
        }
        pollspinner_activate(end[0]);
    }
}

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

        var active = document.querySelectorAll("[data-status='active']")[0];
        active.dataset.status = "answered";
        var pdsBoxTop = active.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();        
    } else if (obj.result == 'view-results') {
        pollspinner_resize();
    } else if (obj.result == 'load') {
        var spinner = document.getElementsByClassName("pollspinner")[0];
        spinner.className = "pollspinner animated";

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

        thisPoll.dataset.loaded = true;
        pollspinner_appendSpecialButtons(thisPoll);
        pollspinner_activate(thisPoll);
    }
}

function pollspinner_skip (spinner) {
    var activePoll = spinner.querySelectorAll("[data-status='active']");
    if (activePoll.length > 0) {
        activePoll[0].dataset.status = "skipped";
        pollspinner_getNext();
    }
}

function pollspinner_begin (intro){
    this.className += " clicked"; 
    intro.dataset.status = "answered";
    pollspinner_getNext();
}

function pollspinner_setup () {
    var spinner = document.getElementsByClassName("pollspinner")[0];
    
    if (spinner != null) {
        spinner.className = "pollspinner animated";
        var pollSpinnerBoxes = spinner.getElementsByClassName("pollspinnerbox");
        
        for (var i = 0; i < pollSpinnerBoxes.length; i++) {
            pollSpinnerBoxes[i].dataset.status = "unanswered";
        }

        var end = document.createElement("div");
        end.className = "pollspinnerbox end";
        end.dataset.status = "end";
        spinner.appendChild(end);
        
        var intro = spinner.getElementsByClassName("pollspinnerbox intro")[0];
        if (intro != null) {
            var begin = intro.getElementsByClassName("pollspinner_begin")[0];
            begin.addEventListener("click", pollspinner_begin.bind(begin, intro));
            pollspinner_activate(intro);
        } else {
            pollspinner_getNext();
        }
    }
}

$(pollspinner_setup);

/* Community awards synchronized table */

function setUpSynchTable () {
  var tables = document.getElementsByClassName("awardsSynchronisedTable");
  for (var k = 0; k < tables.length; k++) {
    var rows = tables[k].rows;
    for (var i = 0; i < rows.length; i++) {
      var cells = rows[i].cells;
      for (var j = 0; j < cells.length; j++) {
        cells[j].style.verticalAlign = "top";
        var synchDiv = document.createElement("DIV");
        synchDiv.style.position = "relative";
        synchDiv.className = "synchDiv";
        var childs = cells[j].childNodes;
        while (childs.length > 0) {
          synchDiv.appendChild(childs[0]);
        }
        cells[j].appendChild(synchDiv);
      }
    }
  }
  if (tables.length > 0) {
    document.onscroll = synchTableOnScroll;
  }
}

function synchTableOnScroll() {
  var divs = document.getElementsByClassName("synchDiv");
  for (var i = 0; i < divs.length; i++) {
    var parent = divs[i].parentNode;
    var parentRect = parent.getBoundingClientRect();
    var offset;
    if (window.innerHeight > divs[i].offsetHeight) {
      offset = Math.min(Math.max(-1 * parentRect.top, 0), parent.clientHeight - divs[i].offsetHeight);
    } else {
      offset = Math.min(Math.max(-1 * parentRect.top / (parent.clientHeight - window.innerHeight) * (parent.clientHeight - divs[i].offsetHeight), 0), parent.clientHeight - divs[i].offsetHeight);
    }
    divs[i].style.top = offset + "px";
  }
}

$(setUpSynchTable);