MediaWiki:VoteBoostToggle.js

From Roat Pkz
Jump to navigation Jump 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.
/* -------------------------------------------------------
   Vote Boost Toggle for drop rate tables
   Auto-inserts a checkbox above each boosted drop table.
   Buffs stack multiplicatively per sidebar calc.
------------------------------------------------------- */
(function () {
    var VOTE_BOOST = 0.20;

	/*
	colours and rates should match Module:DropRateCalc
	*/
    function getColor(rate) {
        if (rate <= 1)   return '#0b5884';
        if (rate <= 16)  return '#3c780a';
        if (rate <= 64)  return '#a48900';
        if (rate <= 256) return '#b55e0c';
        if (rate <= 999) return '#9f261e';
        return '#5c1a1a';
    }

    function updateRates(voteActive) {
        document.querySelectorAll('td[data-baserate]').forEach(function (td) {
            var base      = parseInt(td.getAttribute('data-baserate'), 10);
            var reduction = parseFloat(td.getAttribute('data-reduction'));
            var noboost   = td.getAttribute('data-noboost') === '1';

            var rate;
            if (noboost) {
                rate = base;
            } else {
                rate = Math.floor(base * (1 - reduction));
                if (voteActive) {
                    rate = Math.floor(rate * (1 - VOTE_BOOST));
                }
            }

            if (rate < 1) rate = 1;
            td.textContent = '1/' + rate;
            td.style.backgroundColor = getColor(rate);
        });
    }

    function makeToggle() {
        var wrapper = document.createElement('div');
        wrapper.style.cssText = 'margin:6px 0; padding:5px 10px; background:#1a1a2e; border:1px solid #444; display:inline-block; border-radius:4px;';
        var label = document.createElement('label');
        label.style.cssText = 'cursor:pointer; color:#ffffff; font-size:0.9em;';
        var cb = document.createElement('input');
        cb.type = 'checkbox';
        cb.className = 'vote-boost-toggle';
        cb.style.cssText = 'margin-right:6px; cursor:pointer;';
        cb.addEventListener('change', function () {
            document.querySelectorAll('.vote-boost-toggle').forEach(function (other) {
                other.checked = cb.checked;
            });
            updateRates(cb.checked);
        });
        label.appendChild(cb);
        label.appendChild(document.createTextNode(' Apply vote boost (+20%)'));
        wrapper.appendChild(label);
        return wrapper;
    }

    mw.hook('wikipage.content').add(function () {
        var seen = [];
        document.querySelectorAll('td[data-baserate]').forEach(function (td) {
            var table = td.closest('table');
            if (table && seen.indexOf(table) === -1) {
                seen.push(table);
                table.parentNode.insertBefore(makeToggle(), table);
            }
        });
    });

}());