MediaWiki:LastEdited/beta.js

require(['wikia.ui.factory'], function(ui) {   'use strict';    importArticle({ type: 'script', article: 'u:dev:MediaWiki:I18n-js/code.js' });   importArticle({ type: 'style', article: 'u:dev:MediaWiki:LastEdited/code.css' });   var lastEdited = {        title: mw.config.get('wgPageName'),        id: mw.config.get('wgArticleId'),        canRollback: /(bureaucrat|sysop|helper|vstf|staff|content-moderator|rollback)/.test(mw.config.get('wgUserGroups').join(' ')),        userLink: function(prefix, user, text) {            return mw.html.element('a', { href: mw.util.wikiGetlink(prefix + user) }, text);        },        renderUserTime: function(data, i18n, $content) {            // Build user links            var user = data.user,            links = lastEdited.userLink('User:', user, user) +                    ' (' + lastEdited.userLink('User talk:', user, i18n.msg('talk').plain) + ' | ' +                   lastEdited.userLink('Special:Contributions/', user, i18n.msg('contribs').plain); if (/(bureaucrat|sysop|helper|vstf|staff)/.test(mw.config.get('wgUserGroups').join(' '))) { links += ' | ' + lastEdited.userLink('Special:Block/', user, i18n.msg('block').plain); }           if (lastEdited.options.avatar) { links = mw.html.element('img', {                   src: 'https://services.wikia.com/user-avatar/user/' + data.userid + '/avatar',                    width: lastEdited.options.avatarsize,                    height: lastEdited.options.avatarsize                }) + links; }           links += ')';            // Build time            var $time = $(' ', { 'class': 'lastEdited-timeago', title: data.timestamp });           if (lastEdited.options.time === 'timestamp') {                var date = new Date(data.timestamp).toString;                if (lastEdited.options.timezone && lastEdited.options.timezone === 'UTC') {                    date = new Date(data.timestamp).toUTCString;                }                $time.text(date.slice(0, 3) + ', ' + date.slice(4, 15) + ', ' + date.slice(16, 24));            } else {                $time.timeago;            }            $content.html( i18n.msg('lastEdited').escape .replace('$1', links) .replace('$2', $time.prop('outerHTML')) );       },        renderDiff: function(data, i18n, $content) {            if (lastEdited.options.diff && data.diff.from) {                var link = $('', { id: 'lastEdited-diff-link', href: mw.util.wikiGetlink('Special:Diff/' + data.diff.to), text: i18n.msg('diff').plain });               if (lastEdited.options.diffModal) {                    link.click(function(e) { e.preventDefault; if (lastEdited.modal) { lastEdited.modal.show; }                   });                }                $content.append( ' (',                   link,                    ')' );           }        },        renderMinor: function(data, i18n, $content) {            if (data.minor === '') {                $content.append( ' ',                   $(' ', {                        id: 'lastEdited-minor',                        text: i18n.msg('minor').plain                    }) );           }        },        renderComment: function(data, i18n, $content) {            var comment = data.parsedcomment;            if (lastEdited.options.comment && comment) {                $content.append( ' ',                   i18n.msg('comment').escape, ': ',                   comment.indexOf('Created page with') === -1 ? comment : i18n.msg('created').escape );           }        },        renderSize: function(data, i18n, $content, prev) {            if (lastEdited.options.size) {                var bytes = data.size - prev.size,                    classes = 'mw-plusminus-' + (bytes > 0 ? 'pos' : bytes < 0 ? 'neg' : 'null');               if (Math.abs(bytes) > 500) {                    classes += ' lastEdited-diff-major';                }                $content.append( ' ',                   i18n.msg('size').escape, ': ',                   data.size, ' ',                   i18n.msg('bytes').escape, ' ',                   $(' ', {                        text: '(' + (bytes > 0 ? '+' : '') + bytes.toString.replace(/\B(?=(\d{3})+(?!\d))/g, ',') + ')',                        'class': classes                    }) );           }        },        add: function(data, i18n, modal) {            i18n.useUserLang;            var $content = lastEdited.$content;            data = data[0].query.pages[lastEdited.id].revisions;            if (!data[1]) {                return;            }            var prev = data[1];            data = data[0];            ['UserTime', 'Diff', 'Minor', 'Comment', 'Size'].forEach(function(el) { lastEdited['render' + el](data, i18n, $content, prev); });           lastEdited.generateModal(i18n, data, modal);        },        generateModal: function(i18n, data, modal) {            var config = {                vars: {                    id: 'lastEdited-diff',                    size: 'large',                    title: i18n.msg('changes').escape + ': ' + lastEdited.title.replace(/_/g, ' '),                    content: ' ',                    buttons: [{                        vars: {                            value: i18n.msg('cancel').plain,                            data: [{                                key: 'event',                                value: 'close'                            }]                        }                    }, {                        vars: {                            value: i18n.msg('link').plain,                            classes: ['normal', 'primary'],                            data: [{ key: 'event', value: 'link' }]                       }                    }, {                        vars: { value: i18n.msg('undo').plain, classes: ['normal', 'primary'], data: [{ key: 'event', value: 'undo' }]                       }                    }]                },                confirmCloseModal: function { lastEdited.modal = null; lastEdited.generateModal(i18n, data, modal); return true; }           };            if (lastEdited.canRollback && mw.config.get('wgUserName') !== data.user) { config.vars.buttons.push({                   vars: {                        value: i18n.msg('rollback').plain,                        classes: ['normal', 'primary'],                        data: [{                            key: 'event',                            value: 'rollback'                        }]                    }                }); }           modal.createComponent(config, function(diffModal) {                diffModal.bind('link', function { diffModal.trigger('close'); window.open('/?diff=' + data.diff.to, '_blank'); });               diffModal.bind('undo', function { diffModal.trigger('close'); window.open('/wiki/' + mw.util.wikiUrlencode(lastEdited.title) +                               '?action=edit&undoafter=' + data.diff.from +                                '&undo=' + data.diff.to,                    '_blank'); });               diffModal.bind('rollback', function { $.post(mw.util.wikiScript('api'), {                       action: 'rollback',                        title: lastEdited.title,                        user: data.user,                        token: data.rollbacktoken,                        format: 'json'                    }).done(function(d) {                        if (!d.error) {                            window.location.reload;                        }                    }); });               lastEdited.modal = diffModal;            }); },       fetch: function { var query = { action: 'query', titles: lastEdited.title, prop: 'revisions', rvprop: 'timestamp|user|userid|size|parsedcomment|flags', rvlimit: 2, rvdiffto: 'prev', format: 'json' };           if (lastEdited.canRollback) { query.rvtoken = 'rollback'; }           return $.get(mw.util.wikiScript('api'), query); },       insert: function { var $loader = $(' ', {               id: 'lastEdited',                'class': 'lastEdited'            }).append($(' ', { id: 'lastEdited-loading', src: mw.config.get('stylepath') + '/common/images/ajax.gif' }));           if (mw.config.get('skin') === 'oasis') { var pos = lastEdited.options.position; if (pos.element && pos.method) { var $el = $(pos.element), m = pos.method; if ($el.length && (m === 'append' || m === 'prepend')) { $el[m]($loader); }               } else { $loader.insertAfter('#PageHeader .page-header__title, .UserProfileActionButton'); }           } else { $loader.insertBefore('#bodyContent'); }           lastEdited.$content = $loader; },       shouldRun: function { var allowed = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14, 15, 110, 111, 828, 829, 1202], ns = lastEdited.options.namespaces; if (ns.include instanceof Array) { allowed = allowed.concat(ns.include); }           if (ns.exclude instanceof Array) { allowed = allowed.filter(function(elem) {                   return lastEdited.options.namespaces.exclude.indexOf(elem) < 0;                }); }           return allowed.indexOf(mw.config.get('wgNamespaceNumber')) !== -1 && lastEdited.options.pages.indexOf(lastEdited.title) === -1 && !mw.config.get('wgIsMainPage') && mw.config.get('wgAction') === 'view' && !window.lastEditedLoaded && lastEdited.id !== 0; },       init: function { lastEdited.options = $.extend({               avatar: true,                avatarsize: 15,                size: true,                diff: true,                diffModal: true,                comment: true,                time: 'timeago',                position: {                     element: ,                     method:                 },                namespaces: {                    include: [],                    exclude: []                },                pages: []            }, window.lastEdited); if (lastEdited.shouldRun) { window.lastEditedLoaded = true; lastEdited.insert; mw.hook('dev.i18n').add(function(i18no) {                   $.when( lastEdited.fetch, i18no.loadMessages('LastEdited'), ui.init(['modal']), mw.loader.using('mediawiki.action.history.diff') ).then(lastEdited.add);               }); }       }    };    lastEdited.init; });