MediaWiki:Common.js

From PS:1 Wiki Dev
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)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* Any JavaScript here will be loaded for all users on every page load. */
$('.smw-feed-furtherresults > a').addClass('feedlink');

var wikiTimers = {};

function formatTime(secs, format) {
    format = format || 'hms';

    var unitSeconds = {
        d: 86400,
        h: 3600,
        m: 60,
        s: 1
    };

    var labels = {
        d: 'd',
        h: 'h',
        m: 'm',
        s: 's'
    };

    // Determine the largest unit actually used
    var formatUnits = format.split('');
    var largestUnit = formatUnits.reduce(function (a, b) {
        return unitSeconds[a] > unitSeconds[b] ? a : b;
    });

    var remaining = secs;
    var parts = [];

    for (var i = 0; i < formatUnits.length; i++) {
        var unit = formatUnits[i];
        var value;

        if (unit === largestUnit) {
            // Largest unit gets the full value
            value = Math.floor(remaining / unitSeconds[unit]);
            remaining -= value * unitSeconds[unit];
        } else {
            value = Math.floor(remaining / unitSeconds[unit]);
            remaining -= value * unitSeconds[unit];
        }

        parts.push(value + labels[unit]);
    }

    return parts.join(' ');
}



function createTimer(id, container) {
    var startAttr = container.data('start');
    var startTime = startAttr ? new Date(startAttr).getTime() : null;

    var format = container.data('format') || 'hms';
    var autoStart = container.data('autostart') !== false;

    var buttonsAttr = container.data('buttons');
var buttons;

if (!buttonsAttr) {
    buttons = ['stop', 'reset']; // default
} else if (buttonsAttr === 'none') {
    buttons = [];
} else {
    buttons = buttonsAttr
        .split(',')
        .map(function (b) { return b.trim(); })
        .filter(Boolean);
}


    wikiTimers[id] = {
        seconds: 0,
        startTime: startTime,
        interval: null,
        display: container.find('.wikiTimerDisplay')[0],
        format: format
    };

    function tick() {
        if (wikiTimers[id].startTime !== null) {
            var now = Date.now();
            wikiTimers[id].seconds = Math.floor(
                (now - wikiTimers[id].startTime) / 1000
            );
        } else {
            wikiTimers[id].seconds++;
        }

        wikiTimers[id].display.innerText =
            formatTime(wikiTimers[id].seconds, wikiTimers[id].format);
    }

    function start() {
        if (wikiTimers[id].interval !== null) return;

        if (wikiTimers[id].startTime !== null) {
            tick();
        }

        wikiTimers[id].interval = setInterval(tick, 1000);
    }

    function stop() {
        clearInterval(wikiTimers[id].interval);
        wikiTimers[id].interval = null;
    }

    function reset() {
        stop();

        if (wikiTimers[id].startTime !== null) {
            wikiTimers[id].startTime = Date.now();
        } else {
            wikiTimers[id].seconds = 0;
        }

        wikiTimers[id].display.innerText =
            formatTime(wikiTimers[id].seconds, wikiTimers[id].format);

        if (autoStart) {
            start();
        }
    }

    if (buttons.includes('start')) {
        container.find('.start').on('click', start);
    }
    if (buttons.includes('stop')) {
        container.find('.stop').on('click', stop);
    }
    if (buttons.includes('reset')) {
        container.find('.reset').on('click', reset);
    }

    if (autoStart) {
        start();
    }
}

$(function () {
    $('.wikiTimer').each(function (index) {
        var container = $(this);
        var id = 'wikiTimer_' + index;

        var buttonsAttr = container.data('buttons');
var buttons;

if (!buttonsAttr) {
    buttons = ['stop', 'reset']; // default
} else if (buttonsAttr === 'none') {
    buttons = [];
} else {
    buttons = buttonsAttr
        .split(',')
        .map(function (b) { return b.trim(); })
        .filter(Boolean);
}


        var html = '<div class="wikiTimerDisplay">0</div>';

        if (buttons.includes('start')) {
            html += '<button class="start">Start</button> ';
        }
        if (buttons.includes('stop')) {
            html += '<button class="stop">Stop</button> ';
        }
        if (buttons.includes('reset')) {
            html += '<button class="reset">Reset</button>';
        }

        container.html(html);
        createTimer(id, container);
    });
});