User:KockaAdmiralac/common.js

(function {   if (mw.config.get('wgServer') !== 'http://redwall.wikia.com') {        return;    }    var config = mw.config.get([ 'wgArticlePath' ]),   Bloglist = {        init: function {            mw.loader.using('mediawiki.api').then($.proxy(function {                this.api = new mw.Api;                this.api.get({ action: 'query', list: 'categorymembers', cmtitle: 'Category:News posts', cmsort: 'timestamp', cmdir: 'desc', cmlimit: 5 }).done($.proxy(function(d) {                   if(!d.error) {                        this.fetchData(d.query.categorymembers.map(function(el) {                            return el.title;                        }));                    }                }, this));            }, this));            ['list', 'image', 'time'].forEach(this.initElement, this);        },        initElement: function(t) {            t = 'blog' + t;            this[t] = {};            $('.' + t).each($.proxy(function(_, el) {                el = $(el);                this[t][el.text] = el;            }, this));        },        fetchData: function(d) {            this.api.get({ action: 'query', titles: d.join('|'), prop: 'images|revisions', rvprop: 'timestamp' }).done($.proxy(function(d) {               if(!d.error) {                    this.processData(d.query.pages);                }            }, this));        },        processData: function(d) {            var i = 0;            $.each(d, $.proxy(function(k, v) {                ++i;                if(this.bloglist[i]) {                    this.bloglist[i].html(mw.html.element('a', {                        href: config.wgArticlePath.replace('$1', v.title)                    }, v.title.split(':').splice(1).join(':')));                }                if(this.blogimage[i] && v.images && v.images.length > 0) {                    this.blogimage[i].html(mw.html.element('img', {                        src: config.wgArticlePath.replace('$1', 'Special:FilePath/' + v.images[0].title)                    }));                }                if(this.blogtime[i]) {                    this.blogtime[i].text(new Date(v.revisions[0].timestamp).toLocaleString); }           }, this)); }   };    $($.proxy(Bloglist.init, Bloglist)); });