MediaWiki:ProtectionIcons.js

/** * @Name           ProtectionIcons * @Version        v1.0 * @Author         TheGoldenPatrik1 * @Author         Eizen * @Description    Adds icons based on the page's level of protection. */ (function ($, mw) {   'use strict';    // Main variables    var config = mw.config.get([ 'wgPageName', 'wgArticleId', 'wgNamespaceNumber' ]);   // Load protection    if ( config.wgArticleId === 0 || [-1, 8, 500, 501, 502, 1200, 1201, 2000, 2001].indexOf(config.wgNamespaceNumber) !== -1 || window.ProtectionIconsLoaded ) {       return;    }    window.ProtectionIconsLoaded = true;    /**     * @class Main     */    var Main = {        wdsIsLoaded: $.Deferred,        i18nIsLoaded: $.Deferred,        /**         * @method getJSON         * @description Gets the JSON protection data for the current page.         */        getJSON: function {            var that = this;            $.getJSON('/api.php', { format: 'json', action: 'query', prop: 'info', inprop: 'protection', titles: config.wgPageName }, function (d) { if(!d.error) { that.handleJSON(d); }           });        },        /**         * @method handleJSON         * @description Analyzes protection JSON data and adds the icons.         * @param {String} results - The protection data         */        handleJSON: function(results) {            // Declarations            var data,                protection,                editSysop,                editAutoconfirmed,                moveSysop,                moveAutoconfirmed;            // Definitions            data = results.query.pages;            editSysop = editAutoconfirmed = moveSysop = moveAutoconfirmed = false;            for (var i = 0; i < data[config.wgArticleId].protection.length; i++) {                protection = data[config.wgArticleId].protection[i];                if (protection.type === 'edit') {                    if (protection.level === 'sysop') {                        editSysop = true;                    } else if (protection.level === 'autoconfirmed') { editAutoconfirmed = true; }               }                if (protection.type === 'move') { if (protection.level === 'sysop') { moveSysop = true; } else if (protection.level === 'autoconfirmed') { moveAutoconfirmed = true; }               }            }            // Edit element if (editSysop || editAutoconfirmed) { $(                   ($('.UserProfileActionButton').exists ? '.UserProfileActionButton' : '.page-header__contribution-buttons')               ).prepend(                    this.assembleSpanElement('edit-protection', editSysop)                ); }           // Move element if (               $('#ca-move').exists &&                (moveSysop || moveAutoconfirmed)            ) { $('#ca-move').append(                   this.assembleSpanElement('move-protection', moveSysop)                ); }       },        /**         * @method assembleSpanElement * @description Creates the icon element. * @param {String} spanId - The icon name * @param {String} canSysopAct - Determines if the page is sysop           *                               protected */       assembleSpanElement: function (spanId, canSysopAct) { // Declarations var that, actionType; // Definitions that = this; actionType = spanId.split('-')[0]; return $(' ', {               'class': 'protection-level',                'id': spanId,                'title':                    that.i18n.msg( (canSysopAct ?                       actionType + 'Sysop' :                        actionType + 'Autoconfirmed') ).plain           }).append(                this.wds.icon( (canSysopAct ?                   'lock-small' :                    'unlock-small') )           );        },        /**         * @name i18nHandler * @description Loads I18n-js * @param {String} i18no - Variable for loading I18n-js */       i18nHandler: function (i18no) { var that = this; i18no.loadMessages('ProtectionIcons').done(function (i18n) {               that.i18n = i18n;                that.i18nIsLoaded.resolve;            }); },       /**         * @name wdsHandler * @description Loads WDSIcons * @param {String} wds - Variable for loading WDSIcons */       wdsHandler: function (wds) { this.wds = wds; this.wdsIsLoaded.resolve; },       /**         * @name init * @descrition Initiates script */       init: function  { mw.hook('dev.i18n').add($.proxy(Main.i18nHandler, Main)); mw.hook('dev.wds').add($.proxy(Main.wdsHandler, Main)); $.when(this.i18nIsLoaded, this.wdsIsLoaded).done(               $.proxy(Main.getJSON, Main)            ); // Imports importArticles(               {                    type: 'style',                    articles: [                        'u:dev:MediaWiki:ProtectionIcons.css'                    ]                },                {                    type: 'script',                    articles: [                        'u:dev:MediaWiki:I18n-js/code.js',                        'u:dev:MediaWiki:WDSIcons.js'                    ]                }            ); }   };    // Load script Main.init; }(jQuery, mediaWiki));