2020-11-18 09:11:12 +00:00
|
|
|
/**
|
|
|
|
* Module handling diff page reloading and the RevisionSlider browser history
|
|
|
|
*
|
|
|
|
* @class Diffpage
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
function DiffPage() {
|
|
|
|
this.lastRequest = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$.extend( DiffPage.prototype, {
|
2016-06-17 13:06:12 +00:00
|
|
|
/**
|
2020-11-18 09:11:12 +00:00
|
|
|
* Refreshes the diff view with two given revision IDs
|
2016-06-17 13:06:12 +00:00
|
|
|
*
|
2020-11-18 09:11:12 +00:00
|
|
|
* @param {number} diff
|
|
|
|
* @param {number} oldid
|
|
|
|
* @param {SliderView} sliderView
|
|
|
|
* @param {number} [retryAttempt=0]
|
2016-06-17 13:06:12 +00:00
|
|
|
*/
|
2020-11-18 09:11:12 +00:00
|
|
|
refresh: function ( diff, oldid, sliderView, retryAttempt ) {
|
2023-06-22 09:41:37 +00:00
|
|
|
const self = this,
|
2020-11-18 09:11:12 +00:00
|
|
|
retryLimit = 2,
|
|
|
|
data = {
|
|
|
|
diff: diff,
|
|
|
|
oldid: oldid
|
|
|
|
},
|
|
|
|
params = this.getExtraDiffPageParams();
|
2016-05-10 18:53:31 +00:00
|
|
|
|
2020-11-18 09:11:12 +00:00
|
|
|
retryAttempt = retryAttempt || 0;
|
2016-08-15 18:15:30 +00:00
|
|
|
|
2020-11-18 09:11:12 +00:00
|
|
|
if ( Object.keys( params ).length > 0 ) {
|
|
|
|
$.extend( data, params );
|
|
|
|
}
|
2016-08-15 18:15:30 +00:00
|
|
|
|
2020-11-18 09:11:12 +00:00
|
|
|
if ( this.lastRequest ) {
|
|
|
|
this.lastRequest.abort();
|
|
|
|
}
|
2016-08-15 18:15:30 +00:00
|
|
|
|
2020-11-18 09:11:12 +00:00
|
|
|
$( 'table.diff[data-mw="interface"]' ).addClass( 'mw-revslider-diff-loading' );
|
|
|
|
|
|
|
|
this.lastRequest = $.ajax( {
|
|
|
|
url: mw.util.wikiScript( 'index' ),
|
|
|
|
data: data,
|
|
|
|
tryCount: 0
|
|
|
|
} );
|
|
|
|
// Don't chain, so lastRequest is a jQuery.jqXHR object
|
|
|
|
this.lastRequest.then( function ( data2 ) {
|
2023-06-22 09:41:37 +00:00
|
|
|
const $data = $( data2 ),
|
2020-11-18 09:11:12 +00:00
|
|
|
$container = $( '.mw-revslider-container' ),
|
|
|
|
scrollLeft = $container.find( '.mw-revslider-revisions-container' ).scrollLeft();
|
|
|
|
|
|
|
|
// Add our current rendered slider into the newly loaded container
|
|
|
|
$data.find( '.mw-revslider-container' ).replaceWith( $container );
|
2023-06-22 09:41:37 +00:00
|
|
|
const $contentText = $data.find( '#mw-content-text' );
|
2020-11-18 09:11:12 +00:00
|
|
|
|
|
|
|
// Replace elements on the page with the newly loaded elements, from top to bottom
|
|
|
|
$( '#mw-content-text' ).replaceWith( $contentText );
|
|
|
|
$( '.printfooter' ).replaceWith( $data.find( '.printfooter' ) );
|
|
|
|
$( '#catlinks' ).replaceWith( $data.find( '#catlinks' ) );
|
|
|
|
$( '#mw-panel' ).replaceWith( $data.find( '#mw-panel' ) );
|
|
|
|
// Update edit link
|
|
|
|
$( '#ca-edit a, #ca-ve-edit a' ).each( function () {
|
2023-06-22 09:41:37 +00:00
|
|
|
const uri = new mw.Uri( $( this ).attr( 'href' ) );
|
2020-11-18 09:11:12 +00:00
|
|
|
uri.query.oldid = diff;
|
|
|
|
$( this ).attr( 'href', uri.toString() );
|
2016-08-15 18:15:30 +00:00
|
|
|
} );
|
2020-11-18 09:11:12 +00:00
|
|
|
// Update wgRevisionId (T161257), and wgDiffOldId/NewId
|
|
|
|
mw.config.set( {
|
|
|
|
wgRevisionId: diff,
|
|
|
|
wgDiffOldId: oldid,
|
|
|
|
wgDiffNewId: diff
|
2016-05-10 18:53:31 +00:00
|
|
|
} );
|
2020-11-18 09:11:12 +00:00
|
|
|
|
|
|
|
$( '.mw-revslider-revisions-container' ).scrollLeft( scrollLeft );
|
|
|
|
|
|
|
|
self.addHandlersToCoreLinks( sliderView );
|
|
|
|
|
|
|
|
mw.hook( 'wikipage.content' ).fire( $contentText );
|
2023-04-21 07:03:07 +00:00
|
|
|
mw.hook( 'wikipage.diff' ).fire( $( document.body ).find( 'table.diff' ) );
|
2020-11-18 09:11:12 +00:00
|
|
|
|
|
|
|
}, function ( xhr ) {
|
|
|
|
$( 'table.diff[data-mw="interface"]' ).removeClass( 'mw-revslider-diff-loading' );
|
|
|
|
if ( xhr.statusText !== 'abort' ) {
|
|
|
|
this.tryCount++;
|
|
|
|
mw.track( 'counter.MediaWiki.RevisionSlider.error.refresh' );
|
|
|
|
if ( retryAttempt <= retryLimit ) {
|
|
|
|
self.refresh( diff, oldid, sliderView, retryAttempt + 1 );
|
2016-05-11 10:01:44 +00:00
|
|
|
}
|
2020-11-18 09:11:12 +00:00
|
|
|
// TODO notify the user that we failed to update the diff?
|
|
|
|
// This could also attempt to reload the page with the correct diff loaded without ajax?
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces the current state in the history stack
|
|
|
|
*
|
|
|
|
* @param {number} diff
|
|
|
|
* @param {number} oldid
|
|
|
|
* @param {SliderView} sliderView
|
|
|
|
*/
|
|
|
|
replaceState: function ( diff, oldid, sliderView ) {
|
2023-08-25 14:19:14 +00:00
|
|
|
history.replaceState(
|
|
|
|
this.getStateObject( diff, oldid, sliderView ),
|
|
|
|
$( document ).find( 'title' ).text(),
|
|
|
|
this.getStateUrl( diff, oldid )
|
|
|
|
);
|
2020-11-18 09:11:12 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pushes the current state onto the history stack
|
|
|
|
*
|
|
|
|
* @param {number} diff
|
|
|
|
* @param {number} oldid
|
|
|
|
* @param {SliderView} sliderView
|
|
|
|
*/
|
|
|
|
pushState: function ( diff, oldid, sliderView ) {
|
2023-08-25 14:19:14 +00:00
|
|
|
history.pushState(
|
|
|
|
this.getStateObject( diff, oldid, sliderView ),
|
|
|
|
$( document ).find( 'title' ).text(),
|
|
|
|
this.getStateUrl( diff, oldid )
|
|
|
|
);
|
2020-11-18 09:11:12 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a state object to be used with history.replaceState and history.pushState
|
|
|
|
*
|
2023-10-24 13:45:31 +00:00
|
|
|
* @private
|
2020-11-18 09:11:12 +00:00
|
|
|
* @param {number} diff
|
|
|
|
* @param {number} oldid
|
|
|
|
* @param {SliderView} sliderView
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
|
|
|
getStateObject: function ( diff, oldid, sliderView ) {
|
|
|
|
return {
|
|
|
|
diff: diff,
|
|
|
|
oldid: oldid,
|
|
|
|
pointerOlderPos: sliderView.pointerOlder.getPosition(),
|
|
|
|
pointerNewerPos: sliderView.pointerNewer.getPosition(),
|
|
|
|
sliderPos: sliderView.slider.getOldestVisibleRevisionIndex()
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a URL to be used with history.replaceState and history.pushState
|
|
|
|
*
|
2023-10-24 13:45:31 +00:00
|
|
|
* @private
|
2020-11-18 09:11:12 +00:00
|
|
|
* @param {number} diff
|
|
|
|
* @param {number} oldid
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
getStateUrl: function ( diff, oldid ) {
|
2023-06-22 09:41:37 +00:00
|
|
|
let url = mw.util.wikiScript( 'index' ) + '?diff=' + diff + '&oldid=' + oldid;
|
|
|
|
const params = this.getExtraDiffPageParams();
|
|
|
|
for ( const key in params ) {
|
2023-06-15 10:16:59 +00:00
|
|
|
url += '&' + key + '=' + params[ key ];
|
2016-05-10 18:53:31 +00:00
|
|
|
}
|
2020-11-18 09:11:12 +00:00
|
|
|
return url;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an object containing all possible parameters that should be included in diff URLs
|
|
|
|
* when selected revisions change, e.g. uselang
|
|
|
|
*
|
2023-10-24 13:45:31 +00:00
|
|
|
* @private
|
2020-11-18 09:11:12 +00:00
|
|
|
* @return {Object}
|
|
|
|
*/
|
|
|
|
getExtraDiffPageParams: function () {
|
2023-06-22 09:41:37 +00:00
|
|
|
const params = {},
|
2022-01-13 12:15:32 +00:00
|
|
|
paramArray = location.search.slice( 1 ).split( '&' ).filter( function ( elem ) {
|
2020-11-18 09:11:12 +00:00
|
|
|
return elem.indexOf( '=' ) > 0 && elem.match( /^(diff|oldid)=/ ) === null;
|
|
|
|
} );
|
|
|
|
paramArray.forEach( function ( elem ) {
|
2023-06-22 09:41:37 +00:00
|
|
|
const pair = elem.split( '=', 2 );
|
2020-11-18 09:11:12 +00:00
|
|
|
params[ pair[ 0 ] ] = pair[ 1 ];
|
|
|
|
} );
|
|
|
|
return params;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {SliderView} sliderView
|
|
|
|
*/
|
|
|
|
initOnPopState: function ( sliderView ) {
|
|
|
|
window.addEventListener( 'popstate', function ( event ) {
|
|
|
|
if ( event.state === null ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mw.track( 'counter.MediaWiki.RevisionSlider.event.historyChange' );
|
|
|
|
sliderView.slider.setFirstVisibleRevisionIndex( event.state.sliderPos );
|
|
|
|
sliderView.updatePointersAndDiffView(
|
|
|
|
event.state.pointerNewerPos,
|
|
|
|
event.state.pointerOlderPos,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {SliderView} sliderView
|
|
|
|
*/
|
|
|
|
addHandlersToCoreLinks: function ( sliderView ) {
|
|
|
|
$( '#differences-nextlink' ).on( 'click', function () {
|
|
|
|
sliderView.showNextDiff();
|
|
|
|
return false;
|
|
|
|
} );
|
|
|
|
$( '#differences-prevlink' ).on( 'click', function () {
|
|
|
|
sliderView.showPrevDiff();
|
|
|
|
return false;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
} );
|
2016-05-10 18:53:31 +00:00
|
|
|
|
2020-03-29 20:36:10 +00:00
|
|
|
module.exports = DiffPage;
|