MediaWiki:Gadget-closeDiscussion.js

Վիքիպեդիայից՝ ազատ հանրագիտարանից

Ծանուցում. Հիշելուց հետո կատարված փոփոխությունները տեսնելու համար մաքրեք ձեր զննարկիչի հիշապահեստը. Mozilla / Firefox / Safari՝ Ctrl+Shift+R (Cmd+Shift+R Mac OS X-ում) Konqueror՝ F5 Opera՝ Tools→Preferences ընտրացանկից։ Internet Explorer՝ Ctrl+F5

//<nowiki>
$(function() {
    mw.loader.using( [ 'mediawiki.util' , 'oojs-ui'], function() {
        var options = {};
        options['Վիքիպեդիա:Ջնջման_առաջադրված_հոդվածներ'] = [{
                    data: 'ջնջել',
                    label: 'Ջնջել'
                }, {
                    data: 'թողնել',
                    label: 'Թողնել'
            }];
        //TODO: Ավելացնել նաև անվանափոխման էջը
        
        if (!options[mw.config.get('wgPageName')]) {
            return;
        }
        var selectedSection = null;
        var api = new mw.Api();
        function editSection(section, title, func) {
            return api.get({ 
                    action: "query",
                    prop: "revisions",
                    rvprop: "content",
                    rvsection: section,
                    titles: title,
                    rvslots: "main" }
                ).then(function(data){
                    for (var i in data.query.pages) {
                        var content = data.query.pages[i].revisions[0].slots.main["*"];
                        return api.post({
                            action: 'edit',
                            title: title,
                            section: section,
                            text: func(content),
                            token: mw.user.tokens.get('csrfToken')
                        });
                    }
                });
        }

        function addTag(text, reason, title) {
            return api.post({
                action: 'edit',
                title: (title ? title : mw.config.get('wgPageName')),
                prependtext: text,
                summary: reason,
                token: mw.user.tokens.get('csrfToken')
            });
        }

        function deleteHandler(section, decision, message) {
            var pageTitle = '';
            function helper() {
                api.get({
                        titles: 'Վիքիպեդիա:Ջնջման_առաջադրված_հոդվածներ',
                        action: 'query',
                        prop: 'revisions',
                        rvprop: 'ids',
                    }).then(function(data) {
                        rev = data.query.pages["17381"].revisions[0].revid;
                        if ( decision == 'թողնել' && pageTitle !== '' ) {
                            editSection(0, pageTitle, function(content) {
                                content = content.replace(/{{ *ջնջել(\|[^}]*)? *}}\n?/i, '');
                                return content;
                            });  
                            var url = 'https://hy.wikipedia.org/w/index.php?oldid=' + rev + '#' + pageTitle;
                            addTag('{{ՋԵԷ թողնված թեկնածու|url=' + url + '}}', 'ըստ քննարկման', "Քննարկում:" + pageTitle);
                        } else if ( decision == 'ջնջել' ) {
                            if ( mw.config.get('wgUserGroups').includes("sysop") ) {
                                api.post({
                                    action: 'delete',
                                    title: 'Քննարկում:' + pageTitle,
                                    reason: 'ջնջված էջի քննարկում',
                                    token: mw.user.tokens.get('csrfToken')
                                });
                                api.post({
                                    action: 'delete',
                                    title: pageTitle,
                                    reason: '[[Special:Permalink/' +  rev + '#' + pageTitle + '|ըստ քննարկման]]',
                                    token: mw.user.tokens.get('csrfToken')
                                });
                            } else {
                                api.post({
                                    action: 'edit',
                                    title: 'Քննարկում:' + pageTitle,
                                    nocreate: true,
                                    prependtext: '{{արագ|ըստ քննարկման}}\n',
                                    summary: 'ներկայացնում եմ ջնջման ըստ քննարկման',
                                    token: mw.user.tokens.get('csrfToken')
                                });
                                addTag('{{արագ|[[Special:Permalink/' +  rev + '#' + pageTitle + '|ըստ քննարկման]]}}', 'ներկայացնում եմ ջնջման ըստ քննարկման', pageTitle);
                            }
                        }
                    });
            }

            editSection(section, 'Վիքիպեդիա:Ջնջման_առաջադրված_հոդվածներ', function(content) {
                content = content.replace(/(==[^\n]+==)/, '$1\n{{ՔՓ|' + message + '--~~~~}}') + '\n{{ՔՎ}}';
                var findArticle = /{{[Ll]a\| *([^}]+) *}}/g;
                var match = findArticle.exec(content);
                if ( match ) {
                    pageTitle = match[1];
                }
                return content;
            }).then(function() {
                if ( pageTitle === '' ) {
                    OO.ui.prompt( 'Հոդվածը չի գտնվել, խնդրում ենք նշել անվանումը կամ քննարկումը փակել ձեռքով։', { textInput: { placeholder: 'Հոդվածի անվանում' } } ).done( function ( result ) {
                        if ( result !== null ) {
                            pageTitle = result;
                            helper();
                        }
                    } );
                } else {
                    helper();
                }
            });
        }

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

        // Specify a name for .addWindows()
        ProcessDialog.static.name = 'dataDrainerDialog';
        // Specify a static title and actions.
        ProcessDialog.static.title = 'Փակել քննարկումը';
        ProcessDialog.static.actions = [
            { 
                action: 'save',
                label: 'Պահպանել',
                flags: [ 'primary', 'destructive' ] 
            },
            {
                label: 'Փակել',
                flags: 'safe'
            }
        ];

        // Use the initialize() method to add content to the dialog's $body, 
        // to initialize widgets, and to set up event handlers. 
        ProcessDialog.prototype.initialize = function () {
            ProcessDialog.super.prototype.initialize.apply( this, arguments );
            
            this.content = new OO.ui.PanelLayout( { padded: true, expanded: false } );
            this.content.$element.append("<div>Դուք պատրաստվում եք փակել այս քննարկումը։ Խնդրում ենք նախ ծանոթանալ քննարկման կանոնակարգի հետ և համոզվել, որ համաձայնություն կա։</div>");
            
            this.option1 = new OO.ui.ButtonOptionWidget( options[mw.config.get('wgPageName')][0] );
            this.option2 = new OO.ui.ButtonOptionWidget( options[mw.config.get('wgPageName')][1] );
            this.buttonSelect = new OO.ui.ButtonSelectWidget( { 
                items: [ this.option1, this.option2 ] 
            } );
            this.multilineTextInput = new OO.ui.MultilineTextInputWidget( );
            this.$body.append( this.content.$element );
            this.content.$element.append( this.buttonSelect.$element );
            this.content.$element.append( this.multilineTextInput.$element );
        };

        // Use the getActionProcess() method to specify a process to handle the 
        // actions (for the 'save' action, in this example).
        ProcessDialog.prototype.getActionProcess = function ( action ) {
            var dialog = this;
            if ( action ) {
                var selectedTypes = [];
                if ( action === 'save' && this.buttonSelect.findSelectedItems() ) {
                    var selectedOption = this.buttonSelect.findSelectedItems().getData();
                    var text = this.multilineTextInput.getValue();
                    if ( mw.config.get('wgPageName') == 'Վիքիպեդիա:Ջնջման_առաջադրված_հոդվածներ' ) {
                        if ( text === '' ) {
                            text = selectedOption[0].toUpperCase() + selectedOption.slice(1) + '։';
                        }
                        deleteHandler(selectedSection, selectedOption, text);
                    }
                }

                if ( !this.buttonSelect.findSelectedItems() ) {
                    OO.ui.alert( 'Դուք տարբերակ չեք ընտրել։ Խնդրում ենք ընտրել «' + options[mw.config.get('wgPageName')][0].label + '» կամ «' + options[mw.config.get('wgPageName')][1].label + '»'  );
                } else {
                    // Close the window
                    return new OO.ui.Process( function () {
                        dialog.close( { action: action } );
                    });
                }
            }
            
            // Fallback to parent handler.
            return ProcessDialog.super.prototype.getActionProcess.call( this, action );
        };

        // Get dialog height.
        ProcessDialog.prototype.getBodyHeight = function () {
            return 230;
        };

        // Create a new dialog window.
        var processDialog = new ProcessDialog({ });    

        // Create and append the window manager.
        var windowManager = new OO.ui.WindowManager();
        $( 'body' ).append( windowManager.$element );
        windowManager.addWindows( [ processDialog ] );

        var headlines = $('.mw-headline');
        for(var i = 0; i < headlines.length; i++) {
            $(headlines[i]).prepend('<a class="gadget-close-section" sectionn=' + (i+1) + ' href="#">[Փակել քննարկումը] </a> ');
        }

        $('.gadget-close-section').on('click', function(e) {
            e.preventDefault();
            selectedSection = $(e.target).attr('sectionn');
            windowManager.openWindow( processDialog, [e] );
        });
    });
});
//</nowiki>