MediaWiki:AjaxDelete/code.js

/* BETA */ $(function {   var ug = mw.config.get('wgUserGroups');    if (ug.indexOf('sysop') + ug.indexOf('vstf') + ug.indexOf('staff') + ug.indexOf('helper') > -4) {        // Creates the object        var obj = window.AjaxDelete || {};        // Function for finding URI variables in strings        String.prototype.getURIVariable = function(item) {            var str = '?' + this.split(/\?(.+)/)[1];            var svalue = str.match(new RegExp('[\\?\\&]' + item + '=([^\\&]*)(\\&?)', 'i'));            return svalue ? svalue[1] : svalue;        };        $(document).click(function(e) { var target = $(e.target); var targetHref = target.attr('href'); // Catching delete links if (targetHref.getURIVariable('action') == 'delete') { if (e.ctrlKey || e.shiftKey) return; e.preventDefault; var isImg = target.is('a'); var isRevImg = targetHref.getURIVariable('oldimage'); var page = decodeURIComponent(targetHref.match(/\/wiki\/(.*?)\?/)[1]).replace(/_/g, ' '); var token = mw.user.tokens.get('editToken'); var Api = new mw.Api; $.get(targetHref, function(c) {                   var pContent = isImg ? $(c).find('#mw-img-deleteconfirm') : $(c).find('#deleteconfirm');                    var selectHtml = pContent.find('#wpDeleteReasonList').html;                    // Show the block modal                    $.showCustomModal('Ajax Delete', 'You are deleting ' + page + '. \                   Reason: ' + selectHtml + ' \                    \ \ Watch this page ', { id: 'ajaxDeleteModal', callback: function { if (isImg && !obj.imageDeleteReasons) return; if (!isImg && !obj.deleteReasons) return; var $reasonSelect = $('#AjaxDeleteReasonSelect'); $.each(isImg ? obj.imgDeleteReasons : obj.deleteReasons, function(key, value) {                               $reasonSelect                                    .append($('  ') .attr('value', key) .text(value));                           }); },                       buttons: [{ // Delete button id: 'ajaxDeleteButton', defaultButton: true, message: 'Delete', handler: function { var deleteReason = $('#AjaxDeleteReasonSelect').val == 'other' ? $('#AjaxDeleteCustomReason').val : $('#AjaxDeleteReasonSelect').val + ($('#AjaxDeleteCustomReason').val.trim !==  ? ': ' + $('#AjaxDeleteCustomReason').val : ); // Creates the base delete object var config = { action: 'delete', title: page, reason: deleteReason, bot: true, token: token };                               if (isRevImg) config.oldimage = isRevImg; if ($('#accountCreation').prop('checked')) config.watchlist = 'watch'; Api.post(config).done(function(d) {                                   if (!d.error) {                                        new BannerNotification(isRevImg ? 'The revision for ' : '' + page + ' has been deleted successfully!', 'confirm').show;                                   } else {                                        new BannerNotification('Failed while deleting ' + isRevImg ? 'revision for' : '' + page + ': ' + d.error.code, 'error').show;                                   }                                }).fail(function {                                    new BannerNotification('Failed while deleting ' + isRevImg ? 'revision for' : '' + page, 'error').show;                               }); $('#ajaxDeleteModal').closeModal; // Close the modal }                       }, { // Cancel button id: 'ajaxBlockCancel', defaultButton: true, message: 'Cancel', handler: function { $('#ajaxBlockModal').closeModal; }                       }]                    });                });            }        });    } });