MediaWiki:!pm.js

/** * !pm * Creates a command that users can use to open PMs in the chat * @Author Mario&LuigiBowser'sInsideStory */ require(['wikia.window', 'jquery'], function(context, $) {   // Scoping + double run protection    if ( mw.config.get('wgCanonicalSpecialPageName') !== "Chat" || (context.dev || {}).pmc ) {       return;    }    // i18n (feel free to add your own translation)    var i18n = {        en: {            'pm-command': 'pm',            'user-not-present': 'User not in chat.',            'room-exists': 'Private room already exists',            'user-modal-title': 'Users'        }    };    var config = mw.config.get([ 'wgUserName', 'wgUserLanguage' ]);   i18n = i18n[i18n.hasOwnProperty(config.wgUserLanguage) ? config.wgUserLanguage : 'en'];    function init {        (window.dev || {}).pmc = new Pmc;    }    function Pmc {        $('textarea[name="message"]').on('keydown', $.proxy(this.handler, this));    }    Pmc.prototype.handler = function(e) {        var val = $('textarea[name="message"]').val;        if (e.which == 13 && val.startsWith('!' + i18n['pm-command'])) {            e.preventDefault;            var user = val.split("!" + i18n['pm-command'] + " ")[1];           if (user) {                if (!context.mainRoom.model.users.findByName(user)) {                    context.mainRoom.model.chats.add(new models.InlineAlert({                        text: i18n['user-not-present']                    }));                }                 else if (context.mainRoom.model.privateUsers.findByName(user)) {                    context.mainRoom.model.chats.add(new models.InlineAlert({                        text: i18n['room-exists']                    }));                }                else {                    this.openPm(user);                }            }            else {                this.showUsers;            }            // Clear textarea            $('textarea[name="message"]').val('');        }    };     Pmc.prototype.openPm = function(user) {        context.mainRoom.openPrivateChat(Array(user));    };    Pmc.prototype.showUsers = function { var $modalHtml = $(''); context.mainRoom.model.users.filter(function(usr) {           return usr.attributes.name !== config.wgUserName;        }).forEach(function(i) {            $modalHtml.append( $('', {                   'class': 'pmc-users-user',                    'data-user': i.attributes.name,                    click: $.proxy(function { if (context.mainRoom.model.privateUsers.findByName(i.attributes.name)) { context.mainRoom.model.chats.add(new models.InlineAlert({ text: i18n['room-exists'] }));                       }                        else { context.mainRoom.openPrivateChat(Array(i.attributes.name)); }                       $('#pmc-users').closeModal; }, this)               }).html(                    $(' ', { src: i.attributes.avatarSrc.replace('/scale-to-width-down/28', '') }).after(i.attributes.name)               ) );       });        $.showCustomModal(i18n['user-modal-title'], $modalHtml, {            id: 'pmc-users'        }); };   mw.hook('dev.chat.render').add(init); importArticles({       type: 'script',        articles: ['u:dev:Chat-js.js']    }, {        type: 'style',        articles: ['u:dev:MediaWiki:!pm.css']    }); });