MediaWiki:LangSelect.js

/** *  * @module                  LangSelectEdit * @description            Amends editor UX of. * @author                 Speedit * @version                1.4.1 * @license                CC-BY-SA 3.0 * */    'use strict'; // Variable caching and script scoping window.dev = window.dev || {}; var $tn = $('.transclude-notice'), $ph = $('.page-header__languages'), conf = mw.config.get([           'wgIsArticle',            'wgAction',            'wgRelevantPageName',            'wgVisualEditorPreferred',            'wgUserLanguage',            'wgContentLanguage'        ]); if (       window.dev.langSelect ||        !conf.wgIsArticle ||        conf.wgAction !== 'view' ||        !$tn.exists ||        $('.template-documentation').exists    ) { return; }   window.dev.langSelectEdit = {}; /**    * Script initialiser. * @function           langSelectEditInit */   function langSelectEditInit { window.dev.langSelectEdit = new LangSelectEdit; }   /**     * @class               LangSelectEdit * @classdesc          The main LangSelectEdit class. *                     Method retrieves language variable from the *                     DOM, passes the appropriate subpage wikilink *                     and fetches i18n. */   function LangSelectEdit { mw.hook('dev.langselectedit').fire(this); this.path = new mw.Uri($tn.children('.text').attr('href')).path; this.lang = $tn.attr('data-lang'); this.name = $tn.attr('data-lang-name'); $(this.elements) .each($.proxy(this.edit, this)); if (           this.path !== window.location.pathname &&            this.base !== $.noop        ) { window.dev.i18n.loadMessages('LangSelectEdit') .done($.proxy(this.base, this)); }       if ($ph.exists) { $.proxy(this.interwiki, this); }   }    /**     * @method          edit * @description    Method assigns the wikilink variable from the init *                 logic to any edit buttons. */   LangSelectEdit.prototype.edit = function(i, link) { var $link = $(link); var uri = new mw.Uri($link.attr('href')); uri.path = this.path; $link.attr('href', uri.toString).off('click'); };   /**     * @method          base * @description    Method creates buttons for the base page *                 and appends them to the page header. * @param          {i18n} object * @notes          Please do not remove Monobook support *                 (cross-platform portability). */   LangSelectEdit.prototype.base = function(i18n) { // URI building from base page. var uri = {}; ['edit', 'history'].forEach($.proxy(function(a) { uri[a] = new mw.Uri(mw.util.getUrl(conf.wgRelevantPageName)); }, this)); uri.edit.extend(           conf.wgVisualEditorPreferred ?                { 'veaction': 'edit' } :                { 'action':   'edit' }        ); uri.history.extend({ 'action': 'history' }); // Create buttons. $.each(uri, function(a, u) {           var ID = 'ca-base-' + a,                PATH = u.getRelativePath,                TXT = i18n.msg(a + 'Base').plain;            if (mw.config.get('skin') !== 'oasis') {                mw.util.addPortletLink(ID, PATH, TXT);            } else {                $('.page-header__contribution-buttons .wds-list')                .find('a[id$="-' + a + '"]').parent                .after($('', {                    append: $('', { 'href': PATH, 'id': ID, text: TXT })                }));            }        }); };   /**     * @method          interwiki * @description    Method updates and extends interwiki, then *                 appends new links to the language dropdown. * @param          {jQuery} $ph page language dropdown */   LangSelectEdit.prototype.interwiki = function { var userLangName = $tn.attr('data-userlang-name'); // Add content language link and text override. if (this.lang !== conf.wgContentLanguage) { this.addInterwikiLink({               'class': 'i18ndoc-parent',                'path': mw.util.getUrl(conf.wgRelevantPageName),                'lang': conf.wgContentLanguage,                'text': $ph.find('.wds-dropdown__toggle span').text            }); $ph.find('.wds-dropdown__toggle span').text(this.name); }       // Add preload link if user language is missing. var userLangRgx = new RegExp('/' + this.lang + '$'), userLangPath = mw.util.getUrl(this.path.replace(userLangRgx, '') + '/' + conf.wgUserLanguage), userLangUri = new mw.Uri(userLangPath).extend({               'action': 'edit',                'preload': 'Template:I18ndoc',                'summary': mw.util.wikiUrlencode(this.preloadText)                    .replace(/_/g, '+')            }).toString; this.addInterwikiLink({           'class': 'i18ndoc-preload',            'path': userLangUri,            'lang': conf.wgUserLanguage,            'text': userLangName        }); };   /**     * @method          addInterwikiLink * @description    Utility method for adding interwiki links. * @param          {Object} opts Link configuration options. * @param          {String} opts.class Link class. * @param          {String} opts.path Link target path. * @param          {String} opts.path Interwiki language. * @param          {String} opts.text Link text. * @returns        {jQuery} Language dropdown list item. */   LangSelectEdit.prototype.addInterwikiLink = function(opts) { var $old = $ph.find('.wds-list a[data-tracking="top-' + opts.lang + '"]'); return ($old.exists               ? $old.parent                : $('', { 'class': opts.class, append: $('', {                       'href': opts.path,                        'id': opts.lang,                        'data-tracking': 'top-' + opts.lang,                        'text': opts.text                    }) })           ).insertBefore($ph.find('.wds-list li:not([class]):first-of-type')); };   /**     * @member          {String} preloadText * @description    Text to use when preloading the page. */   LangSelectEdit.prototype.preloadText = 'Automatic generation of i18n documentation'; /**    * @member          {Object} string * @description    Selectors to apply subpage override to. */   LangSelectEdit.prototype.elements = [ '#ca-edit', '#ca-ve-edit', '#ca-viewsource', '#ca-history' ].join(', ');
 * (function (window, mw, $) {

mw.hook('dev.i18n').add(langSelectEditInit); importArticle({       type: 'script',        article: 'u:dev:MediaWiki:I18n-js/code.js'    });

}(window, mediaWiki, jQuery));