FANDOM


// HighlightUsers
require(['wikia.window', 'mw'], function (script, mw) {
    // Script variables
    if (
        window.highlightUsersLoaded ||
        $('#highlightUserStyles').exists()
    ) {
        return;
    }
    window.highlightUsersLoaded = true;
    var config = window.highlightUsersConfig || {},
        groups = config.length ?
            Object.keys(window.highlightUsersConfig.colors).reverse() :
            [
                'bot',
                'vanguard',
                'voldev',
                'global-discussions-moderator',
                'vstf',
                'helper',
                'staff'
            ].reverse(),
        colors = config.length ?
            window.highlightUsersConfig.colors :
            {
                'staff': '#da0da0',
                'helper': '#4c5f1d',
                'vstf': '#f77f77',
                'global-discussions-moderator': '#4286f4',
                'voldev': '#4286f4',
                'vanguard': '#1eaf7a',
                'bot': '#a400a4'
            },
        cache = { /* users */ }

    // Script configuration
    var userGroups = {
            global: [
                'staff',
                'helper',
                'vstf',
                'global-discussions-moderator',
                'voldev',
                'vanguard',
                'council',
                'bot-global'
            ],
            local: [
                'bureaucrat',
                'sysop',
                'content-moderator',
                'threadmoderator',
                'chatmoderator',
                'rollback',
                'bot'
            ]
        },
        userGroupList = userGroups.global.concat(userGroups.local);
    groups.forEach(function(g) {
        var groupIsValid = userGroupList.some(function(t) {
            return userGroups[t].includes(g);
        });
        if (!groupIsValid) {
            delete colors[g];
            groups.splice(groups.indexOf(g), 1);
        }
    });
    var mode = {
        check: function(ug) {
            return groups.some(function(g) {
                return userGroup[ug].includes(g);
            })
        },
        groups = {
            global: mode.check('global'),
            local: mode.check('local')
        };
    }

    // User highlighting modules
    var highlightUsers = {
        // Script dependencies
        dependencies: [
            'mediawiki.util',
            'mediawiki.api',
            'user'
        ],
        // Usergroup initialiser
        init: function() {
            groups.forEach(function(g) { cache[g] = [] });
            var l = groups.join('|'),
                t = new Date().getTime(),
                cb = Number(localStorage.highlightUsersAge) || t,
                s = (typeof localStorage.highlightUsersCache === 'undefined');
            if (s || (t > +cb+21600000)) {
                highlightUsers.call(l);
            } else {
                var d = JSON.parse(localStorage.highlightUsersCache);
                highlightUsers.style(d);
            }
        },
        // API userlist callback
        call: function(l) {
            var query = {
                action: 'query',
                list: 'groupmembers',
                gmgroups: l,
                gmlimit: 'max'
            };
            new mw.Api().get(query).done(highlightUsers.store);  
        },
        // Usergroup data handler
        store: function(d) {
            if (d.error) { return; }
            d.users.forEach(function(u) {
                u.groups.forEach(function(g) {
                    cache[g].push(u.name);
                });
            });
            groups.map(function(g) {
                cache[g] = cache[g].sort();
            });
            localStorage.highlightUsersCache = JSON.stringify(cache);
            localStorage.highlightUsersAge = new Date().getTime();
            highlightUsers.style(cache);
        },
        // Style highlighter
        style: function(c) {
            var highlightUserStyles = mw.util.addCSS('/* HighlightUsers styling */');
            highlightUserStyles.ownerNode.id = 'highlightUserStyles';
            groups.forEach(function(g) {
                var style = 'color: ' + colors[g] + ' !important;';
                if (mode.groups.local && userGroups.local.indexOf(g) > -1) {
                    style += ' font-weight: bold !important;'
                }
                $('#highlightUserStyles').append(
                    'a[href$=":' +
                    c[g].map(function(u) {
                        return mw.util.wikiUrlencode(u)
                    }).join('"],\na[href$=":') +
                    '"] {' + style + '}'
                );
            });
        }
    };

    // Script initialiser
    mw.loader.using(highlightUsers.dependencies).then(highlightUsers.init);
});