MediaWiki:QuickDiff.js

/*   QuickDiff - quickly view a diff! - by OneTwoThreeFall   */ /* This is a derivative of 'DiffAnchored' script by Gguigui1  */ /* This is old version: http://dev.wikia.com/wiki/MediaWiki:QuickDiff/code.js?oldid=38893  */ /* New version not work well with mobile browser */ /*global window, jQuery, mediaWiki */ (function ($, mw) {   'use strict';    // double-run protection    if (window.quickDiffLoaded) {        return;    }    window.quickDiffLoaded = true;    var msg = {        en: {            error: 'Something went wrong while getting the page at "%url".',            loading: 'Loading…',            title: 'QuickDiff: %pagename'        }    };    // use user language, with English as fallback    msg = $.extend(msg.en, msg[mw.config.get('wgUserLanguage')]);    function showModal {        var $modalDiv = $(' ').attr({ // these classes make for better Oasis styling 'class': 'WikiaArticle AdminDashboardChromedArticle', 'id': 'quickdiff' }).css({ // scroll within the modal dialog rather than the page 'min-height': '150px', 'max-height': 'calc(100vh - 175px)', 'overflow-y': 'auto', // better MonoBook styling 'font-size': '13px', 'line-height': '21px', 'position': 'relative' });       var $modal = $modalDiv.makeModal({ width: $(window).width - 100 });       // MonoBook-specific fixes        if (mw.config.get('skin') === 'monobook') {            $modal.css({ // re-set top, else modal will be in middle of screen 'top': $(window).scrollTop + $modal.data('settings').topOffset, // use wiki background colour in modal 'background-color': $('#content').css('background-color') });       }    }    function updateModal(content, title) {        // show modal if it doesn't exist        if (!$('#quickdiffWrapper').length) {            if (!title) {                title = msg.loading;            }            showModal;        }        if (typeof title === 'string') {            $('#quickdiffWrapper > h1').text(msg.title.replace('%pagename', title));        }        $('#quickdiff').html(content);    }    function loadDiff(url) {        updateModal(' ');        // add 'action=render' and 'diffonly' params to save some bytes on each request        url.extend({ action: 'render', diffonly: '1' });       var urlString = url.toString;        $.get(urlString)            .done(function (content) { if ($(content).hasClass('diff')) { var title = $(content).find('#mw-diff-ntitle1 > strong > a').attr('title'); mw.loader.using('mediawiki.action.history.diff', function {                        updateModal(content, title);                    }); return; }               updateModal(msg.error.replace('%url', urlString)); })           .fail(function  { updateModal(msg.error.replace('%url', urlString)); });   }    function init {        // allow modal blackout to work in MonoBook        mw.util.addCSS('#positioned_elements .blackout{height:100%;width:100%;position:fixed}');        // attach to body for compatibility with ajax-loaded content        // also, one attached event handler is better than hundreds!        $('body').on('click', 'a', function (event) { var url = new mw.Uri(event.currentTarget.href); if (               url.host === location.hostname &&  // same domain                url.query.diff &&  // has diff param                !url.fragment      // no fragment (prevents triggering by section links on diff pages)            ) { event.preventDefault; loadDiff(url); }       });    }    $(init); }(jQuery, mediaWiki));