MediaWiki:PreferenceLinks.js

/** * @Name           PreferenceLinks * @Version        v1.0 * @Author         TheGoldenPatrik1 * @Description    Creates a WDS dropdown menu with links to Preferences. */ mw.loader.using('mediawiki.api').then(function {   'use strict';    // Config    var config = mw.config.get([ 'wgUserName', 'wgUserLanguage' ]);   // Load Protection    if ( window.PreferenceLinksLoaded || config.wgUserName === null ) {       return;    }    window.PreferenceLinksLoaded = true;    // Variables    var i18n,        wds,        place = '.wds-list > li > a[data-tracking-label="account.preferences"]';    /**     * @class PreferenceLinks     * @classdesc Main PreferenceLinks class     */    var PreferenceLinks = {};    // Messages    PreferenceLinks.messages = [        'prefs-personal',        'prefs-emailv2',        'prefs-editing',        'prefs-under-the-hood',        'prefs-auth-prefstext',        'prefs-gadgets',        'global-navigation-user-my-preferences'    ];    // Buttons    PreferenceLinks.buttons = [        'personal',        'emailv2',        'editing',        'under-the-hood',        'auth-prefstext',        'gadgets'    ];    /**     * @method init     * @description Initiates the script     */    PreferenceLinks.init = function  {        mw.hook('dev.wds').add($.proxy(this.wds, this)); importArticle({           type: 'script',            article: 'u:dev:WDSIcons/code.js'        }); };   /**     * @method wds * @description Handles WDSIcons * @param {String} wdsIcon - Variable for WDSIcons */   PreferenceLinks.wds = function (wdsIcon) { wds = wdsIcon; this.lang; };   /**     * @method lang * @description Gets system messages and gadgets */   PreferenceLinks.lang = function  { new mw.Api.get({           action: 'query',            meta: 'allmessages',            ammessages: this.messages.join('|'),            amlang: config.wgUserLanguage,            list: 'gadgets'        }).done($.proxy(this.i18no, this)); };   /**     * @method i18no * @description Loades the system messages and main script * @param {String} d - Data returned from lang */   PreferenceLinks.i18no = function (d) { i18n = (function(m) {           return d.query.allmessages[m]['*'];        }); this.main(d); };   /**     * @method main * @description Creates the dropdown * @param {String} d - Data returned from lang */   PreferenceLinks.main = function (d) { var list = $('', {'class': 'wds-list wds-is-linked'}), uri = new mw.Uri(mw.util.getUrl('Special:Preferences')); $.each(this.buttons, function(k, v) {           if (v === 'gadgets' && d.warnings) {                return;            }            uri.fragment = 'mw-prefsection-' + v;            list.append( $('').append(                   $('', { 'href': uri.toString, 'text': i18n(k) })               )            );        });        $('.wds-global-navigation__user-menu') .children('.wds-dropdown__content') .addClass('wds-is-not-scrollable') .find(place).parent.after(           $('', { 'id': 'preference-links-dropdown', 'class': 'wds-dropdown-level-2' }).append( $('', {                   'href': mw.util.getUrl('Special:Preferences'),                    'class': 'wds-dropdown-level-2__toggle preference-links'                }).append(                    $(' ', { text: i18n(6) }),                   wds.icon('menu-control-tiny', { 'class': 'wds-dropdown-chevron' })               ),                $(' ', {                    'class': 'wds-dropdown-level-2__content wds-is-not-scrollable preference-links-menu',                    'append': list                }) )       );        $(place).parent.remove; };   // Load Script PreferenceLinks.init; });