User:Kiteretsu/AjaxUserRights.js: Difference between revisions

From Test Wiki
Jump to navigation Jump to search
Content deleted Content added
+
 
No edit summary
Line 166: Line 166:


importArticles({ type: 'script', articles: [
importArticles({ type: 'script', articles: [
'User:Infinityboy7/Modal.js',
'Test Wiki:Request for permissions',
'User:Infinityboy7/I18n-js/code.js'
'Test Wiki:Community portal'
]});
]});


mw.loader.using('mediawiki.api').then(function() {
mw.loader.using('mediawiki.api').then(function() {
mw.hook('dev.i18n').add(function(i18np) {
mw.loadMessages('AjaxUserRights').then(function{
i18np.loadMessages('AjaxUserRights').then(function(i18np) {
i18n = i18np;
i18n.useUserLang();
mw.hook('dev.modal').add(function() { createModal() });
mw.hook('dev.modal').add(function() { createModal() });
});
});

Revision as of 07:00, 3 June 2024

/**
 * @docs   [[AjaxUserRights]]
 * @name   AjaxUserRights
 * @desc   Allows to change usergroups of users without leaving the current page
 * @author Kofirs2634
 */ 

$(function() {
    if (window.AjaxUserRightsLoaded) return;
    window.AjaxUserRightsLoaded = true;

    const rightsCanon =  ['bureaucrat', 'sysop', 'interface administrator','abuse filter administrator'];
    var config = mw.config.get(['wgPageName', 'wgNamespaceNumber', 'wgUserGroups', 'wgUserName']),
        target, modal, i18n,
        api = new mw.Api();

    if (config.wgUserGroups.length <= 3) return;

    function getRights() {
        target = $('#aur-target').val();
        if (!target) return;

        api.get({
            action: 'query',
            list: 'users',
            ususers: target,
            usprop: 'groups'
        }).then(function(data) {
            rights = data.query.users[0].groups;

            // adding the reason field
            $('#aur-wrap').empty()
            .append($('<span>', {
                style: 'font-weight: bold',
                text: i18n.msg('reason').plain()
            })).append($('<input>', { id: 'aur-reason' })).append('<br>');
            // $('#aur-btn-give').removeAttr('disabled');

            // making list
            for (i = 0; i < rightsCanon.length; i++) {
                $('#aur-wrap').append($('<label>', {
                    text: i18n.msg(rightsCanon[i]).plain()
                }).prepend($('<input>', {
                    type: 'checkbox',
                    id: 'aur-group-' + rightsCanon[i],
                    disabled: true
                })).after('<br>'));
            }

            // disabling and enabling checkboxes
            if (config.wgUserGroups.includes(rightsCanon[0])) {
                if (target == config.wgUserName) {
                    $('#aur-group-bureaucrat').removeAttr('disabled');
                    $('#aur-group-sysop').removeAttr('disabled');
                    $('#aur-group-abuse filter administrator').removeAttr('disabled');
                        $('#aur-group-interface administrator').removeAttr('disabled');
                    } else {
                        $('#aur-group-bureaucrat').removeAttr('disabled');
                        $('#aur-group-sysop').removeAttr('disabled');
                    }
                }
            }
            if (config.wgUserGroups.includes(rightsCanon[1])) {
                if (target == config.wgUserName) {
                    $('#aur-group-sysop').removeAttr('disabled');
                    $('#aur-group-bureaucrate').removeAttr('disabled');
                    config.wgUserGroups.forEach(function(e) {
                        $('#aur-group-' + e).removeAttr('disabled');
                    });
                } else { // но это не ты
                    $('#aur-group-bureaucrate').removeAttr('disabled');
                }
            }
            if (!config.wgUserGroups.includes(rightsCanon[0]) && !config.wgUserGroups.includes(rightsCanon[1])) {
                if (target == config.wgUserName) {
                    config.wgUserGroups.forEach(function(e) {
                        $('#aur-group-' + e).removeAttr('disabled');
                    });
                }
            }

            // giving ticks
            rights.forEach(function(e) { if (rightsCanon.includes(e)) $('#aur-group-' + e).attr('checked', 'checked') });
        });
    }

    function giveRights() {
        // creating arrays of groups for API query
        var add = [], remove = [];
        for (i = 0; i < $('#aur-wrap input[type="checkbox"]').length; i++) {
            if ($('#aur-wrap input[type="checkbox"]')[i].checked && !$('#aur-wrap input[type="checkbox"]')[i].disabled) add[add.length] = rightsCanon[i];
            else if (!$('#aur-wrap input[type="checkbox"]')[i].checked && !$('#aur-wrap input[type="checkbox"]')[i].disabled) remove[remove.length] = rightsCanon[i];
        }
        // query itself inspired by GiveChatMod
        api.get({
            action: "query",
            format: "json",
            meta: "tokens",
            type: "userrights",
        }).done(function(data) {
            opts = {
                action: 'userrights',
                user: target,
                token: data.query.tokens.userrightstoken,
                reason: $('#aur-reason').val(),
                remove: remove.join('|'),
                add: add.join('|')
            };
            api.post(opts).done(function(result) {
                if (!result.warnings && (Object.keys(result.userrights.added).length > 0 || Object.keys(result.userrights.removed).length > 0 )) {
                    mw.notify(i18n.msg('notification-success').plain());
                } else {
                    mw.notify(i18n.msg('notification-error').plain(), {
                        type: 'error'
                    });
                    console.warn(result);
                }
            });
        });
    }

    function createModal() {
        modal = new window.dev.modal.Modal({
            content: '<div id="aur-modal-wrap"></div>',
            id: 'aur-modal',
            size: 'medium',
            title: 'AjaxUserRights',
            buttons: [{
                primary: true,
                id: 'aur-btn-get',
                text: i18n.msg('button-get').plain(),
                event: 'get'
            }, {
                primary: true,
                // HACK: [[Modal]] does not support easily re-enabling buttons yet
                // disabled: true,
                id: 'aur-btn-give',
                text: i18n.msg('button-give').plain(),
                event: 'give'
            }, {
                id: 'aur-btn-cancel',
                text: i18n.msg('button-cancel').plain(),
                event: 'close'
            }],
            events: {
                give: giveRights,
                get: getRights
            }
        });
        modal.create();
    }

    function appendButton() {
        $('#my-tools-menu').prepend($('<li>', { 'class': 'aur-open' }).append($('<a>', {
            href: '#',
            text: 'AjaxUserRights'
        })).click(function() {
            $('#aur-modal-wrap').append($('<span>', {
                style: 'font-weight: bold',
                text: i18n.msg('username').plain()
            })).append($('<input>', { id: 'aur-target' })).append('<br>')
            .append($('<div>', { id: 'aur-wrap' }));
            modal.show();
        }));
    }

    importArticles({ type: 'script', articles: [
        'Test Wiki:Request for permissions',
        'Test Wiki:Community portal'
    ]});

    mw.loader.using('mediawiki.api').then(function() {
       mw.loadMessages('AjaxUserRights').then(function{
               
                mw.hook('dev.modal').add(function() { createModal() });
            });
        });
        appendButton();
    });
});