mediawiki-extensions-Visual.../modules/ve-mw/init/ve.init.mw.DiffPage.init.js
Ed Sanders 3cf074c6ea Move historical diff loading to DiffLoader util
Change-Id: I318752ede2e5c90f9d0ecd26b39e997fff2d60bd
2017-08-13 15:31:42 -04:00

76 lines
2.9 KiB
JavaScript

/*!
* VisualEditor MediaWiki DiffPage init.
*
* @copyright 2011-2017 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
( function () {
var $visualDiff,
reviewModeButtonSelect,
uri = new mw.Uri(),
mode = uri.query.diffmode || 'source',
conf = mw.config.get( 'wgVisualEditorConfig' ),
pluginModules = conf.pluginModules.filter( mw.loader.getState );
function onReviewModeButtonSelectSelect( item ) {
var modulePromise, progress, oldPageName, newPageName,
$revSlider = $( '.mw-revslider-container' ),
$wikitextDiff = $( 'table.diff[data-mw="interface"]' ),
oldId = mw.config.get( 'wgDiffOldId' ),
newId = mw.config.get( 'wgDiffNewId' );
if ( mw.config.get( 'wgCanonicalSpecialPageName' ) !== 'ComparePages' ) {
oldPageName = newPageName = mw.config.get( 'wgRelevantPageName' );
} else {
oldPageName = uri.query.page1;
newPageName = uri.query.page2;
}
mode = item.getData();
if ( mode === 'visual' ) {
progress = new OO.ui.ProgressBarWidget( { classes: [ 've-init-mw-diffPage-loading' ] } );
$wikitextDiff.addClass( 'oo-ui-element-hidden' ).before( progress.$element );
// TODO: Load a smaller subset of VE for computing the visual diff
modulePromise = mw.loader.using( [ 'ext.visualEditor.desktopArticleTarget' ].concat( pluginModules ) );
mw.libs.ve.diffLoader.getVisualDiffGeneratorPromise( oldId, newId, modulePromise, oldPageName, newPageName ).then( function ( visualDiffGenerator ) {
var diffElement = new ve.ui.DiffElement( visualDiffGenerator(), { classes: [ 've-init-mw-diffPage-diff' ] } );
progress.$element.remove();
$wikitextDiff.before( diffElement.$element );
$visualDiff = diffElement.$element;
$revSlider.addClass( 've-init-mw-diffPage-revSlider-visual' );
diffElement.positionDescriptions();
} );
} else {
if ( $visualDiff ) {
$visualDiff.addClass( 'oo-ui-element-hidden' );
}
$wikitextDiff.removeClass( 'oo-ui-element-hidden' );
$revSlider.removeClass( 've-init-mw-diffPage-revSlider-visual' );
}
if ( history.replaceState ) {
uri.query.diffmode = mode;
history.replaceState( '', document.title, uri );
}
}
mw.hook( 'wikipage.diff' ).add( function () {
// The PHP widget was a ButtonGroupWidget, so replace with a
// ButtonSelectWidget instead of infusing.
reviewModeButtonSelect = new OO.ui.ButtonSelectWidget( {
items: [
new OO.ui.ButtonOptionWidget( { data: 'visual', icon: 'eye', label: mw.msg( 'visualeditor-savedialog-review-visual' ) } ),
new OO.ui.ButtonOptionWidget( { data: 'source', icon: 'wikiText', label: mw.msg( 'visualeditor-savedialog-review-wikitext' ) } )
]
} );
reviewModeButtonSelect.on( 'select', onReviewModeButtonSelectSelect );
$( '.ve-init-mw-diffPage-diffMode' ).empty().append( reviewModeButtonSelect.$element );
reviewModeButtonSelect.selectItemByData( mode );
} );
}() );