MediaWiki:FandomizedChat/core.js

/** * @title FandomizedChat * @description This script makes the chat more modern and *             adds more features to it. * @version 3.0 * @author Ultimate Dark Carnage **/

(function(mw, $, mainRoom, config){   // Check if the script has been disabled    if (config.disableFandomizedChat) return;    // If the script is not disabled, proceed with execution    // Extending the main room object    /**     * @method addUserTag     * @description Adds a tag on the side of the chatter's     *              username. This method is based on the Chat     *              Hacks script.     * @param chat - The chat object     **/    NodeChatController.prototype.addUserTag = function(chat){        var tag = null,            allowed = ['staff', 'helper', 'vstf', 'bureaucrat', 'sysop', 'chatmoderator', 'discussions-moderator'];        if ($.isArray(config.allowedGroups)){            allowed = $.merge(config.allowedGroups, allowed);        }        for (var cid in this.model.users.models){            if (this.model.users.models[cid].attributes.name == chat.attributes.name){ var name = this.model.users.models[cid].attributes.name, userData = this.model.users.findByName(name), groups = userData.attributes.groups; groups = groups.filter(function(group){                   return allowed.indexOf(group) > -1;                }).sort(function(a, b){                    if (allowed.indexOf(a) < allowed.indexOf(b)) return -1;                    else if (allowed.indexOf(a) > allowed.indexOf(b)) return 1;                    return 0;                }).map(function(group){                    return group.replace('sysop', 'admin');                }); tag = $(' ', {                   'class': 'chat-user-tag user-tag user-tag-' + groups[0],                    text: groups[0]                }); break; }       }        if (typeof tag !== 'undefined' && config.noTags !== true){ this.chatUL.children.last.find('.username').append(tag); }   };    mainRoom.model.chats.bind('afteradd', $.proxy(mainRoom.viewDiscussion.addUserTag, mainRoom)); /**    * @method killEmoticons * @description Converts emoticons to text. This method is also based on the *             ChatHacks script. * @param chat - The chat object **/   NodeChatController.prototype.killEmoticons = function(chat){ if (config.killEmoticons === true){ this.chatUL.children.last.find('.message img').each(function{               $(this).replaceWith(function{ var that = this, html = $(' ', {                           'title': i18n['emote'],                            text: this.title,                            css: {                                'color': 'blue',                                'cursor': 'pointer'                            },                            on: {                                'click': function{                                    this.outerHTML = $(that).prop('outerHTML');                                }                            }                        }); return html; });           });        }    };    mainRoom.model.chats.bind('afteradd', $.proxy(mainRoom.viewDiscussion.killEmoticons, mainRoom));

/**    * @description Creating the primary object for the script * @type {Object} **/   var FandomizedChat = Object.create(null); /**    * @method onsend * @description Creates an event after the message has been sent * @param {Function} callback - The callback used when the message *                             has been send **/   FandomizedChat.onsend = function(callback){ mainRoom.model.chats.bind('afteradd', callback); mainRoom.model.privateUsers.bind('add', function(user){           var roomid = user.attributes.roomId,                room = mainRoom.chats.privates[roomid];            room.model.chats.bind('afteradd', callback);        }); };   /**     * @method getEmoticons * @description Fetches the emoticon object **/   FandomizedChat.getEmoticons = function{ var emoticons = Object.create(null), mapping = new EmoticonMapping; mapping.loadFromWikiText(mw.config.get('wgChatEmoticons')); $.each(mapping._settings, function(key, value){           emoticons[value[0]] = key;        }); return emoticons; };   /**     * @method toggleEmoticonPanel * @description Creates the emoticon panel when it doesn't exist. *             Toggles the panel if it does. * @param event - The click event. **/   FandomizedChat.toggleEmoticonPanel = function(event){ var $a = $(event.target).is('#EmoticonButton') ? $(event.target) : $(event.target).parents('#EmoticonButton'), hash = $a.prop('hash'), $panelSelector = $(hash), emoticons = FandomizedChat.getEmoticons; if (!$panelSelector.length){ var $panel = $(' ', {               'class': 'EmoticonPanel emoticon-panel',                'id': 'EmoticonPanel',                html: $(' ', { 'class': 'EmoticonPanelWrapper emoticon-panel-wrapper', html: [ $(' ', {                           'class': 'EmoticonPanelContent panel-content',                            html: [                                $(' ', { 'class': 'EmoticonPanelHeader panel-header', html: [ $(' ', {                                           'class': 'EmoticonPanelHeading panel-heading',                                            text: 'Emoticons'                                        }), $(' ', {                                           'class': 'EmoticonPanelIconName panel-icon-name'                                        }), $('', {                                           'class': 'EmoticonPanelPageButton panel-page-button wds-button',                                            'href': '/wiki/MediaWiki:Emoticons',                                            text: 'Go To Emoticons Page'                                        }) ]                               }),                                $(' ', {                                    'class': 'EmoticonPanelItems panel-items', html: $.map(emoticons, function(key, value){                                       var $div = $(' ', { 'class': 'EmoticonPanelItem panel-item', 'data-emote': value, on: { 'click': function{ var text = $(document.getElementsByName('message'), $('#Write')), val = text.val; if (val){ text.val(val + ' ' + $(this).data('emote')); } else { text.val($(this).data('emote')); }                                               }                                            },                                            html: $(' ', {                                                'class': 'EmoticonPanelImage panel-image emoticon',                                                'alt': value,                                                'title': value,                                                'src': key                                            }) });                                   })                                })                            ]                        }),                        $(' ', {                            'class': 'EmoticonPanelTriangleWrapper panel-triangle-wrapper',                            html: $(' ', { 'class': 'EmoticonPanelTriangle panel-triangle' })                       })                    ]                })            });            $(document.getElementsByName('message'), $('#Write')).after($panel); } else { $panelSelector.toggle(500); }   };    /**     * @method addEmoticonButton * @description Adds a button for the emoticon panel **/   FandomizedChat.addEmoticonButton = function{ var emoticonButtonIcon = config.emoticonButtonIcon ||'https://vignette.wikia.nocookie.net/dev/images/9/97/EmoticonButtonIcon.png/revision/latest?cb=20171212213906', emoticonButton = $('', {               'href': '#EmoticonPanel',                'class': 'EmoticonButton emoticon-button',                'id': 'EmoticonButton',                on: { 'click': FandomizedChat.toggleEmoticonPanel },                html: $(' ', { 'class': 'EmoticonButtonIcon emoticon-icon', 'alt': 'Emoticons', 'src': emoticonButtonIcon })           });        if (!$('#EmoticonButton').length){ $(document.getElementsByName('message'), $('#Write')).after(emoticonButton); }   }; }(mediaWiki, jQuery, mainRoom, window.FandomizedChatSettings = window.FandomizedChatSettings || {}));