MediaWiki:DiscussionsRailModule/code.js

// Discussions rail module with Forum styling, by Speedit // rail wrapper and avatar stack by KockaAdmiralac require(['wikia.window', 'mw'], function (script, mw) {   // Scope limiting    var $rail = $('#WikiaRail'),        $content = $('#mw-content-text');    if( window.discussionsModuleLoaded || !$('.wds-community-header .wds-tabs__tab:last-child a[data-tracking="discuss"]').exists ) {       return;    }    window.discussionsModuleLoaded = true;

// Module styling importArticle({       type: 'style',        article: 'u:dev:MediaWiki:DiscussionsRailModule/code.css'    }); mw.util.addCSS(       '.discussions-rail-theme .embeddable-discussions-module .embeddable-discussions-title {' +            'color: ' + wgSassParams["color-links"] + ';' +        '}' +        '.discussions-rail-theme .embeddable-discussions-module .embeddable-discussions-show-all a {' +            'color: ' + wgSassParams["color-buttons"] + ';' +        '}' +        '.discussions-rail-theme .embeddable-discussions-module .avatar-container,' +        '.discussions-rail-theme .wds-avatar-stack .wds-avatar {' +            'border: 2px solid ' + $('.page-header__separator').css('background-color') + ';' +        '}'    );

// Module configuration var config = window.discussionsModuleConfig || {}; if(config.columns && [1, 2].indexOf(config.columns) === -1) { delete config.columns; }   if(config.size && [3, 4, 5, 6].indexOf(config.size) === -1) { delete config.size; }   config.mostrecent = Boolean(config.mostrecent).toString; if(config.catid) { config.catid = config.catid.join.replace(/\s/g, ''); }   var discussionsModule = mw.html.element('discussions', config), $module = $(' ', { 'class': 'discussions-rail-theme rail-module' });

// Fetching Discussions information function init { new mw.Api.get({           action: 'parse',            disablepp: true,            text: discussionsModule        }).done(callback); }

// Callback function after fetch function callback(d) { if(!d.error) { $module.html(d.parse.text['*'].replace(/<\/?p>/g, '')); if($rail.hasClass('loaded')) { addModule; } else { $rail.on('afterLoad.rail', addModule); }       }    }

// Avatar stack $.get('https://services.wikia.com/discussion/' + wgCityId + '/threads', function(d) {       // Fetching avatar stack        window.discussionsModuleStack = $(' ', { 'class': 'wds-avatar-stack', html: d._embedded.contributors[0].userInfo.splice(0, 5) .map(function(usercard) {                   return $('', { href: mw.util.wikiGetlink('User:' + usercard.name), class: 'wds-avatar-stack__avatar', title: usercard.name, html: $(' ', {                           class: 'wds-avatar',                            src: usercard.avatarUrl ? usercard.avatarUrl.replace(/100(?!.*100)/, '30') : 'https://vignette4.wikia.nocookie.net/messaging/images/1/19/Avatar.jpg/revision/latest/scale-to-width-down/30'                        }) });               })        });    }).done(function {        script.discussionsStackLoaded = true;        addStack;    });

// Module function function addModule { // Todo - configurable position above Insights or below WA       $rail.append($module); script.discussionsModuleAdded = true; // Sideloading module contents mw.hook('wikipage.content').fire($module); addStack; }

// Avatar stack function function addStack { var $dModule = $('.discussions-rail-theme'); // Rail stack addition if (typeof discussionsModuleAdded !== "undefined" && typeof discussionsStackLoaded !== "undefined" && typeof discussionsStackAdded == "undefined") { script.discussionsStackAdded = true; $rail.find($dModule).append(discussionsModuleStack); }       // Content stack addition if (wgIsArticle && $content.find($dModule).length && typeof contentStackAdded == "undefined") { script.contentStackAdded = true; $content.find($dModule).append(discussionsModuleStack); }   }

// Initialization mw.loader.using('mediawiki.api').then(init); });