Jump to content

User:This, that and the other/ratemath.js

From Wikipedia, the free encyclopedia
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.
// <nowiki>

mw.loader.using(['ext.gadget.Twinkle'], function() {
    /**
        CONFIGURATION
        ratemathWatchRatedMathPages (boolean)
        If, when applying rating template to page, watch it
        Default true
        */
    if( typeof( ratemathWatchRatedMathPages ) === 'undefined' ) {
        ratemathWatchRatedMathPages = true;
    }
    
    function ratemath() {
        if( (mw.config.get('wgNamespaceNumber') !== 0 && mw.config.get('wgNamespaceNumber') !== 1) ||
            !mw.config.get('wgCurRevisionId') ) {
            return;
        }
        $(mw.util.addPortletLink( 'p-cactions', "#", "Rate math", "tw-rate", "Rate article for WikiProject Mathematics", "")).click(ratemath.callback);
    }
    window.ratemath = ratemath;
    $(document).ready(ratemath);
    
    ratemath.callback = function ratemathCallback() {
        var Window = new Morebits.simpleWindow( 600, 250 );
        Window.setTitle( "Rate mathematics article" );
        var form = new Morebits.quickForm( ratemath.callback.evaluate );
        form.append( {
                type: 'select',
                name: 'quality',
                label: 'Quality: ',
                list: [
                    { label: 'Stub class', value: 'Stub' },
                    { label: 'Start class', value: 'Start' },
                    { label: 'C class', value: 'C' },
                    { label: 'B class', value: 'B' },
                    { label: 'B+ class', value: 'Bplus' },
                    { label: 'Good article', value: 'GA' },
                    { label: 'A class', value: 'A' },
                    { label: 'Featured article', value: 'FA' },
                    { label: 'List', value: 'List' },
                    { label: 'Featured list', value: 'FL' }
                ]
            } );
        form.append( {
                type: 'select',
                name: 'priority',
                label: 'Priority: ',
                list: [
                    { label: 'Low', value: 'Low' },
                    { label: 'Mid', value: 'Mid' },
                    { label: 'High', value: 'High' },
                    { label: 'Top', value: 'Top' },
                ]
            } );
        form.append( {
                type: 'select',
                name: 'field',
                label: 'Field: ',
                list: [
                    { label: 'Analysis', value: 'analysis' },
                    { label: 'Algebra', value: 'algebra' },
                    { label: 'Applied mathematics', value: 'applied' },
                    { label: 'Basic topics (elementary material and common facts)', value: 'basics' },
                    { label: 'Discrete mathematics', value: 'discrete' },
                    { label: 'Foundations (logic and set theory)', value: 'foundations' },
                    { label: 'General', value: 'general' },
                    { label: 'Geometry', value: 'geometry' },
                    { label: 'History', value: 'history' },
                    { label: 'Mathematical Physics', value: 'mathematical physics' },
                    { label: 'Mathematicians', value: 'mathematicians' },
                    { label: 'Number theory', value: 'number theory' },
                    { label: 'Probability and statistics', value: 'probability and statistics' },
                    { label: 'Topology', value: 'topology' }    
                ]   
            } );
        form.append( { type:'submit' } );
    
        var result = form.render();
        Window.setContent( result );
        Window.display();
    };

    ratemath.callbacks = {
        main: function( pageobj ) {
            var text = pageobj.getPageText();
            var statelem = pageobj.getStatusElement();
            var params = pageobj.getCallbackParameters();

            if( text.search(/\{\{\s*[mM]ath(s)?[ _]?rating/) != -1 ) {
                Morebits.status.info('Info', 'Removing previous rating templates');        
                text = text.replace(/\{\{\s*[mM]aths?[ _]?rating([a-zA-Z |= \n])*\}\}/g,  '');  
                if( text.search(/\{\{\s*[mM]ath(s)?[ _]?rating/) != -1 ) {
                    statelem.error('Failed to remove previous templates, aborting');        
                    return;
                }
            }    
            
            pageobj.setPageText("{{maths rating|class=" + params.quality +
                 "|priority=" + params.priority + "|field=" + params.field + "}}\n" + text);
            pageobj.setWatchlist(ratemathWatchRatedMathPages);
            pageobj.setEditSummary("Rating article for [[WP:MATH|WikiProject Mathematics]]. Quality: " + params.quality +
                 " / Priority: " + params.priority + " / Field: " + params.field + " (script assisted)");
            pageobj.save();
        }
    };

    ratemath.callback.evaluate = function ratemathCallbackEvaluate(e) {
        var form = e.target;
        var quality = form.quality.value;
        var priority = form.priority.value;
        var field = form.field.value;

        mw.config.set('wgPageName', mw.config.get('wgPageName').replace(/_/g, ' '));  // for queen/king/whatever and country!

        var pagename = mw.config.get('wgPageName');
        if (mw.config.get('wgNamespaceNumber') === 0) {
            pagename = 'Talk:' + mw.config.get('wgPageName');
        }

        Morebits.status.init( form );
        Morebits.simpleWindow.setButtonsEnabled(false);

        Morebits.wiki.actionCompleted.redirect = pagename;
        Morebits.wiki.actionCompleted.notice = "Tagging complete, reloading talk page in some seconds";

        var wikipedia_page = new Morebits.wiki.page(pagename, 'Tagging talk page');
        wikipedia_page.setCallbackParameters({ quality: quality, priority: priority, field: field });
        wikipedia_page.setFollowRedirect(false);
        wikipedia_page.load(ratemath.callbacks.main);
    };
});

// </nowiki>