Jump to content

User:Chicocvenancio/HelpDeskTBLinks.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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.
if ((mw.config.get('wgPageName') === "Wikipedia:Help_desk") || (mw.config.get('wgPageName') === "Wikipedia:Media_copyright_questions") || (mw.config.get('wgPageName') === "Wikipedia:New_contributors'_help_page/questions") || (mw.config.get('wgPageName') === "Wikipedia:Editor_assistance/Requests") && mw.config.get('wgAction') === 'view') {
    mw.loader.using(['mediawiki.util', 'mediawiki.api', 'jquery.tipsy', 'jquery.ui'], 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 = $('<a href="#' + $link[0].title.replace(pdne, "") + '" style="font-size:x-small; display:inline-block;" title="Send a talkback!" noPopup=1 id="TBsubmit' + i + '">TB</a>');
                    $($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 talkback (<a href="' +
                            mw.util.getUrl( page ) + '">Open</a>) (<a href="' +
                            mw.util.getUrl( page ) + '?diff=0">diff</a>)', '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';
        $('<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());
                },
                Cancel: function() {
                    TBLinks.useDialog('Oh!', 'You have canceled!', 'nprompt-dialog');
                }
            }         
        });
    };
    TBLinks.useDialog = function (title, message, id) {
        'use strict';
        $('#' + id).empty();
        $('#' + id).dialog("option", "title", title);
        $('#' + id).append($('<div>' + message + '</div>'));
        $('#' + id).dialog("option", "buttons", { "Close": function() {
            $(this).dialog("close");
        } } );
    };
    mw.TBLinks = TBLinks;
}( mediaWiki, jQuery ) );