User:Kopcap94/badge.js

console.log('v2.4f stable'); var namespace = mw.config.get('wgNamespaceNumber'); if (namespace !== 2 && namespace !== 1200 && namespace !== 500 &&        (namespace !== 4 || mw.config.get('wgTitle') !== 'Medals' || mw.config.get('wgAction') !== 'view')) { return; }   medalFunctions = { // Default settings medalDefaultSettings: function { default_cfg = JSON.stringify({dataUser:{}, dataMedal: {}, module_title: 'User\'s reward', module_more: 'Show more', module_count_info: 'Amounts of this achievement', border: { top_left: 'http://images3.wikia.nocookie.net/siegenax/ru/images/1/13/Medal_Border_corner.png', top_right: 'http://images2.wikia.nocookie.net/siegenax/ru/images/d/de/Medal_Border_corner_right.png' }}); $('#mw-content-text').prepend('Settings not exist or broken. Reset them? '); $('#MedalResetSettings').click(function {               medalFunctions.saveSettingFunction(default_cfg);            }); }, // Switch setting's tabs function switchSettings: function(class_name) { $('.MedalSetMenu').hide; $('.' + class_name).show; }, // Link's matcher medalMatcher: function(url) { if (url.match(/https?:\/\/(images?|vignette)\d?\.wikia\.nocookie\.net\//)) { return true; }           return false; },
 * (function($,mw) {

// New medal form addMedalForm: function { $('.MedalList').append(               ' ' +                    ' ' +                        '' +                    ' ' +                    ' ' +                        ' ' +                             '✓ ' +                            '' + medalDeleteImg + ' ' +                            'Medal name : ' +                             '' +                        ' ' +                        ' ' +                            'Title : ' + '' + ' ' +                       ' ' +                            'Image link : ' + '' + ' ' +                   ' ' +                ' '            );        }, // New user form        addUserForm: function {            $('.MedalUser').append( '' + '' + '' + medalDeleteImg + ' ' + 'Medals ' + ' ' +                   medalCollectForm + ' '           );        }, // Append medal settings (using in user form)        appendMedalSettings: function($that) {            var imgLink = $that.find('.MedalListLinkImage').val;            var titleNew = $that.find('.MedalListTitle').val;            var nameNew = $that.find('.MedalListName').val;            var prevName = $that.find('.MedalListName').attr('data-prev');            $that.find('.MedalImagePreview img')                .attr('src', imgLink)                .attr('title', titleNew)                .attr('data-prev', nameNew);            if ($(medalCollectForm).find('[data-section="' + nameNew + '"]').length) {                // Name wasn't changed, nothing to do!                return;            }            var to_add = '' +                            ' ' + nameNew + ' ' + ' ';           // Changing default cfg var medalCollectSaveForm = $(medalCollectForm).append(to_add); $('.medalCollectForm').each(function {               $(this).append(to_add);            }); if (prevName !== 'undefined') { // Removing old name $('[data-section="' + prevName + '"]').remove; medalCollectSaveForm.find('[data-section="' + prevName + '"]').remove; }           // Saving cfg medalCollectForm = medalCollectSaveForm.prop('outerHTML'); }, // Delete medal settings (using in user form) deleteMedalSettings: function($that) { var medal_name = $that.find('.MedalListName').val; // If someone decide to cheat var prev_name = $that.find('.MedalListName').attr('data-prev'); // Changing default cfg var medalCollectSaveForm = $(medalCollectForm); medalCollectSaveForm.find('[data-section="' + medal_name + '"], [data-section="' + prev_name + '"]').remove; medalCollectForm = medalCollectSaveForm.prop('outerHTML'); // Doing things $that.remove; $('[data-section="' + medal_name + '"], [data-section="' + prev_name + '"]').remove; }, // Collect result function medalCollectFunction: function { result = {}; result.dataUser = {}; result.dataMedal = {}; // Main settings result.module_title = $('#MedalTitle').val; result.module_more = $('#MedalMore').val; result.module_count_info = $('#MedalCount').val; result.border = { top_left: $('#MedalBorderLeft').val, top_right: $('#MedalBorderRight').val };           // Collecting users $('.UserForm').each(function {               var user = $(this).find('.MedalUserName').val;                if (user === '') return;                var medals = [];                $(this).find('.medalCollectBox').each(function { if ($(this).find('input[type="checkbox"]').attr('checked') !== 'checked') { return; }                   var medal_name = $(this).attr('data-section'); var medal_count = $(this).find('.MedalCollectAmount').val; if (medal_count.match(/\d+/) && medal_count !== 1) { medal_name += ':' + medal_count; }                   medals.push(medal_name); });               result.dataUser[user] = medals;            }); // Collectings medals $('.MedalForm').each(function {               var medal_name = $(this).find('.MedalListName').val;                if (medal_name === '') return;                var medal_image = $(this).find('.MedalListLinkImage').val;                if (!medalFunctions.medalMatcher(medal_image)) return;                result.dataMedal[medal_name] = {                    title: $(this).find('.MedalListTitle').val,                    image_url: medal_image                };            }); return result; }, // Save config function saveSettingFunction: function(medalSet) { $.ajax({               url: '/api.php',                type: 'POST',                data: {                    action: 'edit',                    title: 'Project:Medals',                    summary: 'Settings changes',                    text: medalSet,                    bot: 1,                    token: mw.user.tokens.get('editToken'),                    format: 'json'                },                success: function(d) {                    if (d.edit.result == 'Success') {                        location.href += (location.href.indexOf('?') > -1) ? '&action=purge' : '?action=purge';                    }                }            }); }, // Main function medalMainFunction: function { if (typeof MedalSettings === 'undefined') { // Settings are broken or still not exists return; }           user = mw.config.get('wgTitle'); medalModal = false; if (typeof MedalSettings.dataUser[user] === 'undefined') { // Returning if user don't have medals return; }           // Check for non wiki's images medalBorderRight = (medalFunctions.medalMatcher(MedalSettings.border.top_right)) ? $(' ').attr('src', MedalSettings.border.top_right).prop('outerHTML') : ''; medalBorderLeft = (medalFunctions.medalMatcher(MedalSettings.border.top_left)) ? $(' ').attr('src', MedalSettings.border.top_left).prop('outerHTML') : ''; $('#WikiaRail').prepend(               '' +                    ' ' +                        // Borders: Top-Left; Top-Right; Bottom-Left; Bottom-Right;                        ' ' +                             medalBorderLeft +                         ' ' +                        ' ' +                             medalBorderRight +                         ' ' +                        '' +                             medalBorderRight +                         ' ' +                        '<div style="position:absolute; bottom:0; right:0; transform:rotate(180deg); -ms-transform:rotate(180deg); -webkit-transform:rotate(180deg); -moz-transform:rotate(180deg); -o-transform:rotate(180deg);">' + medalBorderLeft + ' ' +                       // Title $(' ').text(MedalSettings.module_title).prop('outerHTML') + // Medal's section ' ' +                            ' ' +                        ' ' +                    ' ' +                ' '            );

if (MedalSettings.dataUser[user].length > 12) { medalModal = true; medalModalForm.append(' '); $('.RewardSection').after(                   '<center style="margin:5px 0 -20px 0;">' +                         $('<button type="button" class="MedalMore" style="cursor:pointer;" />').text((MedalSettings.module_more === '' || typeof(MedalSettings.module_more) === 'undefined') ? 'Show more' : MedalSettings.module_more).prop('outerHTML') +                    ' '                ); }           $.each(MedalSettings.dataUser[user], function (i, v) {                imgBody = '';                // If user have more then 1 medal                if (v.indexOf(':') > -1) {                    count = v.replace(/.+:(.+)/g, '$1');                    v = v.replace(/(.+):.+/g, '$1');                    imgBody += $('<div class="RewardCount" style="position:absolute; bottom:0; right:0; padding:0 4px; background-color:white; border:1px solid black; border-radius:10px; cursor:help; color:black;" />').text('x' + count).attr('title', MedalSettings.module_count_info).prop('outerHTML');                }                // Additional protect (if file not from Wikia image hosting)                if (typeof MedalSettings.dataMedal[v].image_url === 'undefined' || !medalFunctions.medalMatcher(MedalSettings.dataMedal[v].image_url)) {                    return;                }                imgBody += $('<img style="cursor:help; vertical-align:middle;" width="50" />').attr('src', MedalSettings.dataMedal[v].image_url).attr('title', MedalSettings.dataMedal[v].title).prop('outerHTML'); if (medalModal) { medalModalForm.find('.ModalMedalCollection center').append(' ' + imgBody + ' '); if (i > 11) return; }               $('.RewardSection center').append(' ' + imgBody + ' '); });           $('.MedalMore').click(function { $.showCustomModal( MedalSettings.module_title + ' ' + user, medalModalForm, {width: 400}); });       }, // Settings function        medalSettingsFunction: function {            var group_list = mw.config.get('wgUserGroups');            if (group_list.indexOf('sysop') == -1 && group_list.indexOf('helper') == -1 && group_list.indexOf('staff') == -1 && group_list.indexOf('vstf') == -1) {               // No rights - nothing to do here :P                return;            }            // Variables for this function            medalCollect = [];            medalDeleteImg = '<img src="data:image/gif;base64,R0lGODlhAQABAIABAAAAAP///yH5BAEAAAEALAAAAAABAAEAQAICTAEAOw%3D%3D" class="sprite trash" />';            medalCollectForm = $('<div class="medalCollectForm" style="display:none; text-align:left; margin-top:5px; border-top:1px solid black;" />');            if (typeof MedalSettings === "undefined") {                // If settings not exists or broken: ask to create new!                medalFunctions.medalDefaultSettings;

return; }           $('#mw-content-text').prepend('<center style="padding:20px;"><button id="MedalSettings">Access settings  '); // Creating from medalModalForm.append(               ' ' +                    ' ' +                        '<button type="button" class="wikia-button medalSwitch" onclick="medalFunctions.switchSettings(\'MedalMainDialog\')">Main ' +                        '<button type="button" class="wikia-button medalSwitch" onclick="medalFunctions.switchSettings(\'MedalUserDialog\')">Users ' +                        '<button type="button" class="wikia-button medalSwitch" onclick="medalFunctions.switchSettings(\'MedalListDialog\')">Medals ' +                    ' ' +                ' ' +                ' ' +                    // Main settings menu                    ' ' +                        '<h2 style="padding:5px 0 5px 5px; background-color:rgba(0,0,0,0.1); border-bottom:solid 1px #36759c; margin-bottom:5px;">Main Settings ' +                        ' Module title: ' + $('<input id="MedalTitle" style="float:right; width:80%;" />').attr('value', MedalSettings.module_title).prop('outerHTML') + ' ' + ' Expand button: ' + $('<input id="MedalMore" style="float:right; width:80%;" />').attr('value', MedalSettings.module_more).prop('outerHTML') + ' ' + ' Medal count: ' + $('<input id="MedalCount" style="float:right; width:80%;" />').attr('value', MedalSettings.module_count_info).prop('outerHTML') + ' ' + ' Border left: ' + $('<input id="MedalBorderLeft" style="float:right; width:80%;" />').attr('value', MedalSettings.border.top_left).prop('outerHTML') + ' ' + ' Border right: ' + $('<input id="MedalBorderRight" style="float:right; width:80%;" />').attr('value', MedalSettings.border.top_right).prop('outerHTML') + ' ' + ' ' +                   // User's settings menu ' ' +                       '<h2 style="padding:5px 0 5px 5px; background-color:rgba(0,0,0,0.1); border-bottom:solid 1px #36759c;">' + 'Users' + '<button onclick="medalFunctions.addUserForm" style="padding:0 4px; margin:0 10px 0 0; float:right;">Add user form ' + ' ' +                       ' ' +                    ' ' +                    // Medal's settings menu ' ' +                       '<h2 style="padding:5px 0 5px 5px; background-color:rgba(0,0,0,0.1); border-bottom:solid 1px #36759c;">' + 'Medals' + '<button onclick="medalFunctions.addMedalForm" style="padding:0 4px; margin:0 10px 0 0; float:right;">Add medal form ' + ' ' +                       ' ' +                    ' ' +                ' '            );            // Filling up form for medals and users            $.each(MedalSettings.dataMedal, function(k,v) { // Because we need to know all kinds of medals medalCollect.push(k); medalModalForm.find('.MedalList').append(                   ' ' +                        ' ' +                            $('<img height="70" style="margin:0 5px -3px 0; cursor:help;"/>').attr('src', v.image_url).attr('title', v.title).prop('outerHTML') +                        ' ' +                        ' ' +                            ' ' +                                 '<button onclick="medalFunctions.appendMedalSettings($(this).parents(\'.MedalForm\'))" style="padding:0 4px; margin:0 6px 0 0; float:right;">✓ ' +                                '<button onclick="medalFunctions.deleteMedalSettings($(this).parents(\'.MedalForm\'))" style="padding:0 0 0 3px; margin:0 5px 0 0; float:right;">' + medalDeleteImg + ' ' +                                'Medal name : ' +                                 $('<input class="MedalListName" style="float:right; width:334px; margin-right:5px;" />').attr('value', k).attr('data-prev', k).prop('outerHTML') + ' ' +                           ' ' +                                'Title : ' + $('<input class="MedalListTitle" style="float:right; width:385px; margin-right:5px;" />').attr('value', v.title).prop('outerHTML') + ' ' +                           ' ' +                                'Image link : ' + $('<input class="MedalListLinkImage" style="float:right; width:385px; margin-right:5px;" />').attr('value', v.image_url).prop('outerHTML') + ' ' +                       ' ' +                    ' '                );            });            // Preparing default 'module' for medals $.each(medalCollect, function(i,v) {               medalCollectForm.append( $(' ' +                       ' ' +                         $(' ').text(v + ' ').prop('outerHTML') +                         '<input class="MedalCollectAmount" style="width:30px; text-align:center;"/>' +                    ' ').attr('data-section', v).prop('outerHTML') );           });            // Saving it            medalCollectForm = medalCollectForm.prop('outerHTML'); // Creating form for users $.each(MedalSettings.dataUser, function(k,v) {               var medalCollectFormNew = $(medalCollectForm);                $.each(v, function(i,val) { // If user have more then 1 medal if (val.indexOf(':') > -1) { var count = val.replace(/.+:(.+)/g, '$1'); val = val.replace(/(.+):.+/g, '$1'); medalCollectFormNew.find('.medalCollectBox[data-section="' + val + '"] .MedalCollectAmount').attr('value', count); }                   medalCollectFormNew.find('.medalCollectBox[data-section="' + val + '"] input[type="checkbox"]').attr('checked', 'checked'); });               medalModalForm.find('.MedalUser').append( '<div class="UserForm CustomForm" style="text-align:center; margin-top:5px; border-bottom:1px solid black; padding-bottom:5px;">' + $('<input class="MedalUserName" style="float:left; width:40%; margin-left:5px;" />').attr('value', k).prop('outerHTML') + '<button onclick="$(this).parents(\'.CustomForm\').remove" style="padding:0 0 0 3px; margin:0 5px; float:left;">' + medalDeleteImg + ' ' + '<button onclick="$(this).parents(\'.UserForm\').find(\'.medalCollectForm\').toggle" style="padding:0 0 0 3px; margin:0 5px; width:100px;"> Medals ' + ' ' +                       medalCollectFormNew.prop('outerHTML') + ' '               );            });            // For reset function medalModalFormCompleted = medalModalForm.prop('outerHTML'); // Modal window $('#MedalSettings').click(function {               $.showCustomModal('Settings', medalModalFormCompleted, { id: 'ModalSettingsWindow', width: 600, height: 450, buttons: [{ message: 'Purge page', handler: function { location.href += (location.href.indexOf('?') > -1) ? '&action=purge' : '?action=purge'; }                   },{                        message: 'Reset changes', handler: function { $('#ModalSettingsWindow fieldset').replaceWith(medalModalFormCompleted); }                   },{                        message: 'Save', handler: function { result_data = JSON.stringify(medalFunctions.medalCollectFunction); // Save it! medalFunctions.saveSettingFunction(result_data); }                   }]                });            });        }, // Launch function init: function { $.ajax({               url: '/index.php',                type: 'GET',                data: {                    title: 'Project:Medals',                    action: 'raw',                    nocache: 1,                    allinone: 1,                    dataType:'text',                    debug:'true'                },                success: function(data) {                    // Parsing result                    try {                        MedalSettings = JSON.parse(data);                    } catch(err) {                        if (namespace === 4) {                            medalFunctions.medalDefaultSettings;                        }                        return;                    }                    medalModalForm = $('<fieldset style="border:solid 1px #36759c; margin:0; padding:1em;" />');                    // Checking if we're on settings window or not                    if (namespace != 4) { medalFunctions.medalMainFunction; } else { medalFunctions.medalSettingsFunction; }               },                error: function { if (namespace === 4) { medalFunctions.medalDefaultSettings; }               }            });        }    };    // LAUNCH!    medalFunctions.init; })(this.jQuery,this.mediaWiki);