User:Chicocvenancio/teahouseTalkbackLink.js

if (mw.config.get('wgPageName') === "Wikipedia:Teahouse/Questions" && mw.config.get('wgAction') === 'view') { mw.loader.using(['jquery.tipsy', 'jquery.ui', 'jquery'], function {    $(mw.TBLinks); }); }; ( function ( mw, $ ) {   var TBLinks;    TBLinks = function  {        "use strict";        if ($("#mw-content-text").length > 0) {            var $nodeList = $('a[title^="User talk:"]'), pdne = ' (page does not exist)', i, $link, $newLink;            if ($nodeList !== null) {                for (i = 0; i < $nodeList.length; i += 1) {                    $link = $($nodeList[i]);                    $newLink = $('TB');                   $($link).after($newLink);                }            }            $('a[title="Send a talkback!"]').tipsy({html: true}).before("|").after("|").click(function (e) { TBLinks.submit($(this).attr('href').substr(1), this.id); });       };    };    TBLinks.submit = function  (page, id) {        'use strict';        TBLinks.edit = function (section) {            var sectionTitle = "{{Talkback|" + 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',                summary : "Talkback: you've got messages!",                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('Success', 'Success in sending your talkbakc (Open) (diff)', 'nprompt-dialog'); } else { TBLinks.useDialog('Error!!', 'There was an error requesting the edit. Code: ' + data.error.code + '": ' + data.error.info, 'nprompt-dialog');                   }                },                error : function  {                    TBLinks.useDialog('Error!!', 'There was an error in using AJAX to edit the page.', 'nprompt-dialog');                }            });        }        TBLinks.nicePrompt("Please enter the title of the question you're replying to (or just leave it blank):", "Section:", TBLinks.edit);    };    TBLinks.nicePrompt = function (title, label, callback) {        'use strict';        $(' ' + (label ? '' + label + ' ' : '') + ' ').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('Oh!', 'You have canceld!', 'nprompt-dialog'); }           }                 });    };    TBLinks.useDialog = function (title, message, id) {        'use strict';        $('#' + id).empty;        $('#' + id).dialog("option", "title", title);        $('#' + id).append($(' ' + message + ' '));        $('#' + id).dialog("option", "buttons", { "Close": function { $(this).dialog("close"); } } );   };    mw.TBLinks = TBLinks; }( mediaWiki, jQuery ) );