Benutzer:Wiegels/js/votes.js

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen

Hinweis: Leere nach dem Veröffentlichen den Browser-Cache, um die Änderungen sehen zu können.

  • Firefox/Safari: Umschalttaste drücken und gleichzeitig Aktualisieren anklicken oder entweder Strg+F5 oder Strg+R (⌘+R auf dem Mac) drücken
  • Google Chrome: Umschalttaste+Strg+R (⌘+Umschalttaste+R auf dem Mac) drücken
  • Internet Explorer/Edge: Strg+F5 drücken oder Strg drücken und gleichzeitig Aktualisieren anklicken
  • Opera: Strg+F5
// Kandidaturen auswerten
(function() {
    var roles = {
            A: { min: 50, diff: 0, quote: 2/1 }, // Administrator
            B: { min: 50, diff: 0, quote: 7/3 }, // Bürokrat
            C: { min: 25, diff: 0, quote: 7/3 }, // Checkuser
            M: { min:  0, diff: 0, quote: 2/1 }, // Mentor
            O: { min: 50, diff: 0, quote: 7/3 }, // Oversighter
            S: { min:  0, diff: 1, quote:   0 }, // Schiedsrichter
        },
        wgPageName = mw.config.get('wgPageName');
    if ($('.jw-votes').length==0 && wgPageName.match(
        RegExp('^Wikipedia:('+[
            'Kandidaturen$',
            'Adminkandidaturen/',
            'Bürokratenkandidaturen/',
            'Checkuser/',
            // 'Mentorenprogramm/Abstimmungen',
            'Oversightkandidaturen/',
            'Schiedsgericht/'
        ].join('|').replace(/\//, '\/')+')')
    )) {
        $('head').append(
            '<link href="/w/index.php?title=Benutzer:Wiegels/css/indicator.css&action=raw&ctype=text/css" rel="stylesheet" type="text/css"/>'+
            '<link href="/w/index.php?title=Benutzer:Wiegels/css/votes.css&action=raw&ctype=text/css" rel="stylesheet" type="text/css"/>');
        if (wgPageName.match(/^Wikipedia:Mentorenprogramm\/Abstimmungen(|\/.*)$/)) {
            $('#mw-content-text').html(
                $('#mw-content-text').html().
                    replace(/>Aktuelle Kandidaturen</g, '>Mentorenkandidaturen<').
                    replace(/h3>/g, 'h4>').replace(/h2>/g, 'h3>').replace(/h1>/g, 'h2>'));
            $('#mw-content-text h3').each(function() { $(this).parent().after($(this)).remove(); });
        }
        $('.mw-parser-output').
            prepend('<h2 style="display:none;"><span class="mw-headline">'+wgPageName.replace(/^.*:(.*?)(\/.*|)$/, '$1')+'</span></h2>').
            children('h2, .mw-heading2').
            each(function() {
                var key = $(this).find('.mw-headline').text()[0];
                if (key in roles) {
                    $(this).nextUntil('h2, .mw-heading2', 'h3').each(function() {
                        var h3 = $(this);
                        var counts = [];
                        var votes = $('<div class="jw-votes jw-indicator"><span class="jw-name">'+$(this).find('.mw-headline').text()+'</span></div>');
                        $([/^Pro/, /^[CK]ontra/, /^Enthaltung(en|)/]).each(function(index, pattern) {
                            h3.nextUntil('h3', 'h4').each(function() {
                                if (matches = $(this).find('.mw-headline').text().match(pattern)) {
                                    var count = 0;
                                    $(this).nextUntil('h4', 'ol').each(function() {
                                        count = $(this).children(':visible:has(:parent)').length;
                                    });
                                    counts.push(count);
                                    votes.append('<span class="jw-count jw-count-'+index+'" title="'+matches[0]+'">'+count+'</span>');
                                }
                            });
                        });
	                    var sum = (counts[0] ?? 0)+(counts[1] ?? 0);
                        if (sum>0) {
                            var lists = $(this).nextUntil('h3', 'ol');
                            var names = lists.children().filter(function() { return $(this).text()!='…'; }).
                                    map(function() {
                                        var name = $(this).html().
                                            replace(/(^.*?\(CES?T\))[^]*$/, '$1').
                                            replace(/^[^]*<a [^>]*?title="Benutzer(?:|in)(?:| Diskussion):([^"/:]+?)(?:|: [^"]*| \(Seite nicht vorhanden\))"[^>]*?>.+?<\/a>(.*?\(CES?T\)|)[^]*?$/i, '$1').
                                            replace(/^.*?-- *(.*?) \d\d:\d\d, [^,]+? \(CES?T\)[^]*?$/i, '$1');
                                        $(this).attr('data-voter', name);
                                        return name;
                                    }).
                                    toArray().sort();
                            var multi = names.join('|').replace(/^(.*)$/, '|$1|').match(/\|([^|]+)(?=\|\1\|)/g);
                            var role = roles[key];
                            var index = (counts[0]<Math.max(role.min, counts[1]+role.diff, counts[1]*role.quote) ? 0 : (key.match(/[AB]/) ? 2 : 1));
                            var title = '= '+counts[0]+'/'+sum+' ('+['nicht gewählt', 'bestanden', 'gewählt'][index]+')';
                            votes.append(
                                (!multi ?
                                    '<span class="jw-multi jw-multi-0" title="Keine mehrfachen Stimmabgaben">0</span>' :
                                    '<span class="jw-multi jw-multi-1" title="Mehrfache Stimmabgaben:\n'+multi.join("\n").replace(/\|/g, '')+'">'+multi.length+'</span>')+
                                '<span class="jw-quote jw-quote-'+index+'" title="'+title+'">'+(''+Math.round(counts[0]/sum*10000)).replace(/(\d\d)$/, ',$1 %')+'</span>');
                            if (multi) {
                                $(multi).each(function() {
                                    lists.find('li[data-voter="'+this.substr(1)+'"]').addClass('jw-multi-vote');
                                });
                            }
                        }
                        $('.mw-indicators').append(votes);
                    });
                }
            });
        if ($('.jw-votes').length>1) {
            $('.jw-votes').find('.jw-name, .jw-count, .jw-quote').addClass('jw-sortable').on('click', function() {
                var index = $(this).index();
                var value = function(element, index) {
                        var value = $(element).children().eq(index).text();
                        return (index==0 ? value : -('0000'+value.replace(/[ %,]/g, '')).substr(-5));
                    };
                $($('.jw-votes').get().sort((a, b) => value(a, index)>value(b, index))).each(function() {
                    $('.mw-indicators').append($(this));
                });
            });
        }
    }
})();