MediaWiki:Chat-js.js

/** *  * @module          Chat-js * @description    Library for FANDOM Chat. * @author         Speedit * @version        1.0.0 * @license        CC-BY-SA 3.0 * */    // Variables and double-run protection. window.dev = window.dev || {}; if (window.dev.chat) { return; } // Global variable setup. window.dev.chat = {}; /**    * Chat-js bootstrapper. * @class      Chat * @this       window.dev.chat */   function Chat { // Event conditionals this.socket = false; this.render = false; // Chat toolbar. this.$toolbar = $(' ', {               'class': 'wds-button-group chat-toolbar',                'style': 'display: none;'            }).insertBefore('.ChatHeader .User'); // Toolbar creation. this.stylesheet = $(importArticles({ type: 'style', article: 'u:dev:MediaWiki:Chat-js.css' })).attr('id', 'chat_css').get(0); // Event hooks. mw.loader.using('mediawiki.api', $.proxy(_init, this)); }   /**     * Chat-js internal event initializer * @function   _init * @this       window.dev.chat * @private */   function _init(e) { // Dispatch script hook. mw.hook('dev.chat').fire(this); // Hook for premature socket availability. $.proxy(_socket, this); // Chat bootstrapper $(document.body).on(           'animationstart',            '#ChatHeader .User',            $.proxy(_main, this)        ); }   /**     * Chat-js post-render bootstrapper. * @function   _main * @param      {Object} e jQuery 'animationstart' event data. * @this       window.dev.chat * @notes      When Chat is first rendered, .ChatHeader .User is     *              populated. The initial render will trigger this *             DOM animation callback. The animation event is    *              defined in MediaWiki:Chat-js.css. * @private */   function _main(e) { if (e.originalEvent.animationName !== 'chat_initialized') { return; }       // Socket and models now ready. $.proxy(_socket, this); // Chat view is ready. $.proxy(_render, this); // Display toolbar alongside chat initial render this.$toolbar.removeAttr('style'); }   /**     * Chat socket availability. * @function   _socket * @this       window.dev.chat * @private */   function _socket { if (           !this.socket &&            typeof window.mainRoom !== 'undefined'        ) { mw.hook('dev.chat.socket').fire(window.mainRoom); this.socket = true; }   }    /**     * Chat view availability. * @function   _render * @this       window.dev.chat * @private */   function _render { if (           !this.render &&            typeof window.mainRoom !== 'undefined' &&            window.mainRoom.model.chats.length > 0        ) { mw.hook('dev.chat.render').fire(window.mainRoom); this.render = true; }   }    /**     * UI cache for cross-script button creation. * @method     $buttons * @this       window.dev.chat * @param      {String} script Script name. * @returns    {jQuery} buttons */   Chat.prototype.$buttons = function(script) { if (typeof script !== 'string') { return; }       var $b = $('.chat-toolbar').children('[data-name="' + script + '"]'); if ($b.exists) { return $b; }   };    /**     * Button creation utility. * @method     button * @param      {Object} opts Button options. * @param      {string} opts.name Script name. * @param      {Object} opts.attr jQuery element attributes. * @constructor */   Chat.prototype.button = function(opts) { if (opts.name && !Chat.prototype.$buttons(opts.name)) { // Option setup opts.attr = opts.attr || {}; $.extend(opts.attr, {               'data-name': opts.name,                'title': opts.name            }); opts.attr.class = (typeof opts.attr.class === 'string' ? opts.attr.class + ' ' : '') + [                   'wds-button', 'wds-is-secondary', 'wds-is-squished', 'chat-toolbar__button' ].join(' '); // Button generation $.extend(this, opts); this.$el = $(' ', this.attr).appendTo('.chat-toolbar'); }   };    // Load MediaWiki API module. mw.loader.load('mediawiki.api'); // Globally expose and bootload library. window.dev.chat = new Chat; }(this, jQuery, mediaWiki)); /** **/
 * (function (window, $, mw) {