Langbahn Team – Weltmeisterschaft

User:Jeeputer/specialRandomPage.js

Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/**
 * Finds a random page in a selected namespace with preferred options
**/
mw.loader.using(["oojs-ui-core", "oojs-ui-windows", "oojs-ui-widgets"], function() {
    var namespaces = [{
        name: 'All',
        ns: '0|6|10|14|118|828'
    }, {
        name: '(Article)',
        ns: 0
    }, {
        name: 'File',
        ns: 6
    }, {
        name: 'Template',
        ns: 10
    }, {
        name: 'Category',
        ns: 14
    }, {
        name: 'Draft',
        ns: 118
    }, {
        name: 'Module',
        ns: 828
    }];

    var SPRPFieldSet = new OO.ui.FieldsetLayout({
        align: 'inline'
    });

    var dropDown = new OO.ui.DropdownWidget({
        menu: {
            items: namespaces.map(function(item) {
                return new OO.ui.MenuOptionWidget({
                    data: item.ns,
                    label: item.name
                });
            })
        }
    });
    dropDown.getMenu().selectItemByLabel(window.SPRPpreferredNamespace || 'All');

    var redirect = new OO.ui.ToggleButtonWidget({
        icon: 'articleRedirect',
        title: 'Random redirect',
        value: window.SPRPActivateRedirect || false
    }),
        edit = new OO.ui.ToggleButtonWidget({
            icon: 'edit',
            title: 'Open the edit form',
            value: window.SPRPAlwaysEdit || false
        }),
        links = new OO.ui.ToggleButtonWidget({
            icon: 'link',
            title: 'See what links to the page',
            value: window.SPRPActivateWLH || false
        });
    options = new OO.ui.ButtonGroupWidget({
        items: [redirect, edit, links]
    });

    SPRPFieldSet.addItems([
        new OO.ui.FieldLayout(dropDown, {
            align: 'top'
        }),
        new OO.ui.FieldLayout(options, {
            align: 'top'
        })
    ]);

    function SPRPProcessDialog(config) {
        SPRPProcessDialog.super.call(this, config);
    }
    OO.inheritClass(SPRPProcessDialog, OO.ui.ProcessDialog);

    SPRPProcessDialog.static.name = 'SPRPDialog';
    SPRPProcessDialog.static.title = 'Random page';
    SPRPProcessDialog.static.actions = [{
        action: 'save',
        label: 'Go',
        flags: 'primary'
    }, {
        label: 'Cancel',
        flags: 'safe'
    }];

    SPRPProcessDialog.prototype.initialize = function() {
        SPRPProcessDialog.super.prototype.initialize.apply(this, arguments);
        this.content = new OO.ui.PanelLayout({
            padded: true,
            expanded: false
        });
        this.content.$element.append(SPRPFieldSet.$element);
        this.$body.append(this.content.$element);
    };
    SPRPProcessDialog.prototype.getActionProcess = function(action) {
        var dialog = this;
        if (action) {
            mw.notify('Finding a random page...');
            var actionParam, WLH = false,
                redir = false;
            if (redirect.getValue()) {
                redir = true;
            }
            if (links.getValue()) {
                WLH = true;
                actionParam = 'view';
            } else {
                actionParam = edit.getValue() ? 'edit' : 'view';
            }
            return new OO.ui.Process(function() {
                var ns = dropDown.getMenu().findSelectedItem();
                var r = redir ? 'redirects' : 'nonredirects';
                new mw.Api().get({
                    action: 'query',
                    format: 'json',
                    list: 'random',
                    rnlimit: '1',
                    rnnamespace: ns.data,
                    rnfilterredir: r
                }).done(function(data) {
                    var title = data.query.random[0].title;
                    var url = mw.config.get('wgServer');
                    if (WLH) {
                        url += mw.util.getUrl('Special:WhatLinksHere/' + title);
                    } else {
                        url += mw.util.getUrl(title, {
                            redirect: redir ? 'no' : 'yes',
                            action: actionParam
                        })
                        if (actionParam == 'edit') {
                            url += '#editform'
                        }
                    }
                    window.location.href = url;
                });
                dialog.close({
                    action: action
                });
            });
        }
        return SPRPProcessDialog.super.prototype.getActionProcess.call(this, action);
    };

    var windowManager = new OO.ui.WindowManager();
    $(mw.util.addPortletLink('p-cactions', '#', 'Random Page', 'ca-sprp', 'Go to a random page in a selected namespace', window.SPRPAccesskey || 'z')).on('click', function(e) {
        e.preventDefault();
        $(document.body).append(windowManager.$element);
        var dialog = new SPRPProcessDialog();
        windowManager.addWindows([dialog]);
        windowManager.openWindow(dialog);
    });
});