Usuário:Chicocvenancio/linksDeRepostas.js

Origem: Wikipédia, a enciclopédia livre.

Nota: Depois de publicar, poderá ter de contornar a cache do seu navegador para ver as alterações.

  • Firefox / Safari: Pressione Shift enquanto clica Recarregar, ou pressione Ctrl-F5 ou Ctrl-R (⌘-R no Mac)
  • Google Chrome: Pressione Ctrl-Shift-R (⌘-Shift-R no Mac)
  • Internet Explorer / Edge: Pressione Ctrl enquanto clica Recarregar, ou pressione Ctrl-F5
  • Opera: Pressione Ctrl-F5.
if ( ((mw.config.get('wgNamespaceNumber') % 2 ===  1) &&
	mw.config.get('wgNamespaceNumber') > 0 ||
	mw.config.get('wgNamespaceNumber') === 4) &&
	mw.config.get('wgAction') === 'view'
) {
	mw.loader.using(['jquery.tipsy', 'jquery.ui', 'mediawiki.util'])
	.done( function () {
		$( mw.TBLinks );
	} );
}

( function ( mw, $ ) {
    'use strict';
    var TBLinks;
    TBLinks = function () {
        if ( $("#mw-content-text").length ) {
            var $nodeList = $('a[title*="Discussão:"][title^="Usuári"]'), pdne = ' (página inexistente)', i, $link, $newLink;
            if ($nodeList !== null) {
                for (i = 0; i < $nodeList.length; i += 1) {
                    $link = $($nodeList[i]);
                    $newLink = $('<a href="#' + $link[0].title.replace(pdne, "") + '" style="font-size:x-small; display:inline-block;" title="Mande um aviso de resposta!" noPopup=1 id="TBsubmit' + i + '">R</a>');
                    $($link).after($newLink);
                }
            }
            $('a[title="Mande um aviso de resposta!"]').tipsy({html: true}).before("|").after("|").click(function (e) {
                TBLinks.submit($(this).attr('href').substr(1), this.id);
            });
        }
    };
    TBLinks.nicePrompt = function (title, label, callback) {
        $('<div id="nprompt-dialog" class="ui-widget">' + (label ? '<label for="nprompt-input">' + label + '</label>' : '') + '<input type="text" id="nprompt-input"> </input></div>').dialog({
            title: title,
            open: function () {
                $(".ui-dialog-titlebar-close").hide();
                $('#nprompt-input').autocomplete({source: $('.toctext').map(function() { return $(this).text(); }).get()});
            },
            close: function () {
            $('#nprompt-dialog').dialog('destroy');
            $('#nprompt-dialog').remove();
            },
            buttons: {
                "OK": function () {
                    callback($('#nprompt-input').val());
                },
                Cancelar: function() {
                    TBLinks.useDialog('Ah!', 'Você cancelou!', 'nprompt-dialog');
                }
            }         
        });
    };
    TBLinks.useDialog = function (title, message, id) {
        $('#' + id).empty();
        $('#' + id).dialog("option", "title", title);
        $('#' + id).append($('<div>' + message + '</div>'));
        $('#' + id).dialog("option", "buttons", { "Fechar": function() {
            $(this).dialog("close");
        } } );
    };
    TBLinks.submit = function  (page, id) {
        TBLinks.edit = function (section) {
            var sectionTitle = "{{Resposta|" + mw.config.get('wgPageName').replace(/_/g, ' ') + "|", data;
            if (section === "") {
                sectionTitle += "ts=~~" + "~~}}";
            } else {
                sectionTitle += section + "|ts=~~" + "~~}}";
            }
            data = {
                format : 'json',
                action : 'edit',
                minor : false,
                title : page,
                text : sectionTitle,
                section : 'new',
                sectiontitle: "Aviso de resposta em [[" + mw.config.get('wgPageName').replace(/_/g, ' ') + ']]',
                summary : "Aviso de resposta em [[" + mw.config.get('wgPageName').replace(/_/g, ' ') + ']]. Feito com [[Usuário:Chicocvenancio/linksDeRepostas|linksDeRepostas]]',
                token : mw.user.tokens.get('csrfToken')
            };
            $.ajax({
                url : mw.util.wikiScript('api'),
                type : 'POST',
                dataType : 'json',
                data : data,
                success : function (data) {
                    if (data && data.edit && data.edit.result && data.edit.result === 'Success') {
                        TBLinks.useDialog('Sucesso', 'Sucesso em mandar o aviso de resposta! (<a href="' +
                            mw.util.getUrl( page ) + '">Abrir</a>) <a href="' +
                            mw.util.getUrl( page ) + '?diff=0">(diff)</a>', 'nprompt-dialog');
                    } else {
                        TBLinks.useDialog('Erro!!', 'Houve um erro ao requisitar a edição da página. Código: ' + data.error.code + '": ' + data.error.info, 'nprompt-dialog');
                    }
                },
                error : function () {
                    TBLinks.useDialog('Erro!!', 'Houve um erro ao usar AJAX para editar a página.', 'nprompt-dialog');
                }
            });
        };
        TBLinks.nicePrompt("Por favor coloque o título da seção aonde está a resposta (ou deixe em branco):", "Seção:", TBLinks.edit);
    };
    mw.TBLinks = TBLinks;
}( mediaWiki, jQuery ) );