2020-06-16 14:13:36 +00:00
|
|
|
/**
|
|
|
|
* @external SliderView
|
|
|
|
*/
|
2018-11-16 16:33:52 +00:00
|
|
|
( function () {
|
2016-06-17 13:06:12 +00:00
|
|
|
/**
|
|
|
|
* Module handling diff page reloading and the RevisionSlider browser history
|
|
|
|
*
|
2020-06-16 14:13:36 +00:00
|
|
|
* @class Diffpage
|
2016-06-17 13:06:12 +00:00
|
|
|
* @constructor
|
|
|
|
*/
|
2016-05-10 18:53:31 +00:00
|
|
|
var DiffPage = function () {
|
2016-08-15 18:15:30 +00:00
|
|
|
this.lastRequest = null;
|
2016-05-10 18:53:31 +00:00
|
|
|
};
|
|
|
|
|
2020-01-24 16:21:23 +00:00
|
|
|
/**
|
|
|
|
* @class mw.libs.revisionSlider.DiffPage
|
|
|
|
*/
|
2016-05-10 18:53:31 +00:00
|
|
|
$.extend( DiffPage.prototype, {
|
2016-06-17 13:06:12 +00:00
|
|
|
/**
|
|
|
|
* Refreshes the diff view with two given revision IDs
|
|
|
|
*
|
2017-05-08 15:45:07 +00:00
|
|
|
* @param {number} diff
|
|
|
|
* @param {number} oldid
|
2017-02-02 17:24:51 +00:00
|
|
|
* @param {SliderView} sliderView
|
2016-08-15 18:15:30 +00:00
|
|
|
* @param {number} [retryAttempt=0]
|
2016-06-17 13:06:12 +00:00
|
|
|
*/
|
2017-05-08 15:45:07 +00:00
|
|
|
refresh: function ( diff, oldid, sliderView, retryAttempt ) {
|
2016-08-15 18:15:30 +00:00
|
|
|
var self = this,
|
|
|
|
retryLimit = 2,
|
|
|
|
data = {
|
2016-11-22 16:59:15 +00:00
|
|
|
diff: diff,
|
|
|
|
oldid: oldid
|
2016-08-15 18:15:30 +00:00
|
|
|
},
|
2016-08-01 11:33:36 +00:00
|
|
|
params = this.getExtraDiffPageParams();
|
2016-08-15 18:15:30 +00:00
|
|
|
|
|
|
|
retryAttempt = retryAttempt || 0;
|
|
|
|
|
2016-08-01 11:33:36 +00:00
|
|
|
if ( Object.keys( params ).length > 0 ) {
|
|
|
|
$.extend( data, params );
|
|
|
|
}
|
2016-08-15 18:15:30 +00:00
|
|
|
|
|
|
|
if ( this.lastRequest ) {
|
|
|
|
this.lastRequest.abort();
|
|
|
|
}
|
|
|
|
|
2016-07-02 20:17:40 +00:00
|
|
|
$( 'table.diff[data-mw="interface"]' ).addClass( 'mw-revslider-diff-loading' );
|
2016-08-15 18:15:30 +00:00
|
|
|
|
|
|
|
this.lastRequest = $.ajax( {
|
2016-05-10 18:53:31 +00:00
|
|
|
url: mw.util.wikiScript( 'index' ),
|
2016-08-01 11:33:36 +00:00
|
|
|
data: data,
|
2016-08-15 18:15:30 +00:00
|
|
|
tryCount: 0
|
|
|
|
} );
|
|
|
|
// Don't chain, so lastRequest is a jQuery.jqXHR object
|
2020-09-11 15:28:52 +00:00
|
|
|
this.lastRequest.then( function ( data2 ) {
|
|
|
|
var $data = $( data2 ),
|
2018-09-04 08:52:16 +00:00
|
|
|
$contentText,
|
2016-08-15 18:15:30 +00:00
|
|
|
$container = $( '.mw-revslider-container' ),
|
|
|
|
scrollLeft = $container.find( '.mw-revslider-revisions-container' ).scrollLeft();
|
2016-05-19 12:52:36 +00:00
|
|
|
|
2016-10-24 08:53:08 +00:00
|
|
|
// Add our current rendered slider into the newly loaded container
|
2016-08-15 18:15:30 +00:00
|
|
|
$data.find( '.mw-revslider-container' ).replaceWith( $container );
|
2018-09-04 08:52:16 +00:00
|
|
|
$contentText = $data.find( '#mw-content-text' );
|
2016-10-24 08:53:08 +00:00
|
|
|
|
2018-09-04 08:52:16 +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' ) );
|
2016-11-22 16:59:15 +00:00
|
|
|
// Update edit link
|
|
|
|
$( '#ca-edit a, #ca-ve-edit a' ).each( function () {
|
|
|
|
var uri = new mw.Uri( $( this ).attr( 'href' ) );
|
|
|
|
uri.query.oldid = diff;
|
|
|
|
$( this ).attr( 'href', uri.toString() );
|
|
|
|
} );
|
2017-07-18 15:52:10 +00:00
|
|
|
// Update wgRevisionId (T161257), and wgDiffOldId/NewId
|
2018-09-04 08:52:16 +00:00
|
|
|
mw.config.set( {
|
|
|
|
wgRevisionId: diff,
|
|
|
|
wgDiffOldId: oldid,
|
|
|
|
wgDiffNewId: diff
|
|
|
|
} );
|
2016-10-24 08:53:08 +00:00
|
|
|
|
|
|
|
$( '.mw-revslider-revisions-container' ).scrollLeft( scrollLeft );
|
2016-06-03 09:52:49 +00:00
|
|
|
|
2017-02-02 17:24:51 +00:00
|
|
|
self.addHandlersToCoreLinks( sliderView );
|
|
|
|
|
2018-09-04 08:52:16 +00:00
|
|
|
mw.hook( 'wikipage.content' ).fire( $contentText );
|
2017-04-12 09:57:56 +00:00
|
|
|
mw.hook( 'wikipage.diff' ).fire( $( 'body' ).find( 'table.diff' ) );
|
2016-09-06 10:57:45 +00:00
|
|
|
|
2016-08-15 18:15:30 +00:00
|
|
|
}, function ( xhr ) {
|
2016-07-02 20:17:40 +00:00
|
|
|
$( 'table.diff[data-mw="interface"]' ).removeClass( 'mw-revslider-diff-loading' );
|
2016-08-15 18:15:30 +00:00
|
|
|
if ( xhr.statusText !== 'abort' ) {
|
2016-05-12 08:42:43 +00:00
|
|
|
this.tryCount++;
|
|
|
|
mw.track( 'counter.MediaWiki.RevisionSlider.error.refresh' );
|
2016-08-15 18:15:30 +00:00
|
|
|
if ( retryAttempt <= retryLimit ) {
|
2017-05-08 15:45:07 +00:00
|
|
|
self.refresh( diff, oldid, sliderView, retryAttempt + 1 );
|
2016-05-12 08:42:43 +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?
|
2016-05-10 18:53:31 +00:00
|
|
|
}
|
|
|
|
} );
|
2016-05-11 10:01:44 +00:00
|
|
|
},
|
|
|
|
|
2016-07-31 19:13:00 +00:00
|
|
|
/**
|
|
|
|
* Replaces the current state in the history stack
|
|
|
|
*
|
2017-05-08 15:45:07 +00:00
|
|
|
* @param {number} diff
|
|
|
|
* @param {number} oldid
|
2016-07-31 19:13:00 +00:00
|
|
|
* @param {SliderView} sliderView
|
|
|
|
*/
|
2017-05-08 15:45:07 +00:00
|
|
|
replaceState: function ( diff, oldid, sliderView ) {
|
2017-02-22 13:00:13 +00:00
|
|
|
// IE9 does not have history.replaceState()
|
2016-07-31 19:13:00 +00:00
|
|
|
if ( typeof history.replaceState === 'function' ) {
|
|
|
|
history.replaceState(
|
2017-05-08 15:45:07 +00:00
|
|
|
this.getStateObject( diff, oldid, sliderView ),
|
2016-07-31 19:13:00 +00:00
|
|
|
$( document ).find( 'title' ).text(),
|
2017-05-08 15:45:07 +00:00
|
|
|
this.getStateUrl( diff, oldid )
|
2016-07-31 19:13:00 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-06-17 13:06:12 +00:00
|
|
|
/**
|
|
|
|
* Pushes the current state onto the history stack
|
|
|
|
*
|
2017-05-08 15:45:07 +00:00
|
|
|
* @param {number} diff
|
|
|
|
* @param {number} oldid
|
2016-06-17 13:06:12 +00:00
|
|
|
* @param {SliderView} sliderView
|
|
|
|
*/
|
2017-05-08 15:45:07 +00:00
|
|
|
pushState: function ( diff, oldid, sliderView ) {
|
2017-02-22 13:00:13 +00:00
|
|
|
// IE9 does not have history.pushState()
|
2016-06-01 17:18:48 +00:00
|
|
|
if ( typeof history.pushState === 'function' ) {
|
|
|
|
history.pushState(
|
2017-05-08 15:45:07 +00:00
|
|
|
this.getStateObject( diff, oldid, sliderView ),
|
2016-06-01 17:18:48 +00:00
|
|
|
$( document ).find( 'title' ).text(),
|
2017-05-08 15:45:07 +00:00
|
|
|
this.getStateUrl( diff, oldid )
|
2016-06-01 17:18:48 +00:00
|
|
|
);
|
|
|
|
}
|
2016-05-11 10:01:44 +00:00
|
|
|
},
|
|
|
|
|
2016-07-31 19:13:00 +00:00
|
|
|
/**
|
|
|
|
* Gets a state object to be used with history.replaceState and history.pushState
|
|
|
|
*
|
2017-05-08 15:45:07 +00:00
|
|
|
* @param {number} diff
|
|
|
|
* @param {number} oldid
|
2016-07-31 19:13:00 +00:00
|
|
|
* @param {SliderView} sliderView
|
2016-09-01 11:17:47 +00:00
|
|
|
* @return {Object}
|
2016-07-31 19:13:00 +00:00
|
|
|
*/
|
2017-05-08 15:45:07 +00:00
|
|
|
getStateObject: function ( diff, oldid, sliderView ) {
|
2016-07-31 19:13:00 +00:00
|
|
|
return {
|
2017-05-08 15:45:07 +00:00
|
|
|
diff: diff,
|
|
|
|
oldid: oldid,
|
2016-07-31 19:13:00 +00:00
|
|
|
pointerOlderPos: sliderView.pointerOlder.getPosition(),
|
|
|
|
pointerNewerPos: sliderView.pointerNewer.getPosition(),
|
2017-03-27 19:04:31 +00:00
|
|
|
sliderPos: sliderView.slider.getOldestVisibleRevisionIndex()
|
2016-07-31 19:13:00 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a URL to be used with history.replaceState and history.pushState
|
|
|
|
*
|
2017-05-08 15:45:07 +00:00
|
|
|
* @param {number} diff
|
|
|
|
* @param {number} oldid
|
2016-09-01 11:17:47 +00:00
|
|
|
* @return {string}
|
2016-07-31 19:13:00 +00:00
|
|
|
*/
|
2017-05-08 15:45:07 +00:00
|
|
|
getStateUrl: function ( diff, oldid ) {
|
|
|
|
var url = mw.util.wikiScript( 'index' ) + '?diff=' + diff + '&oldid=' + oldid,
|
2016-08-01 11:33:36 +00:00
|
|
|
params = this.getExtraDiffPageParams();
|
|
|
|
if ( Object.keys( params ).length > 0 ) {
|
|
|
|
Object.keys( params ).forEach( function ( key ) {
|
|
|
|
url += '&' + key + '=' + params[ key ];
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an object containing all possible parameters that should be included in diff URLs
|
|
|
|
* when selected revisions change, e.g. uselang
|
|
|
|
*
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
|
|
|
getExtraDiffPageParams: function () {
|
|
|
|
var params = {},
|
|
|
|
paramArray = location.search.substr( 1 ).split( '&' ).filter( function ( elem ) {
|
|
|
|
return elem.indexOf( '=' ) > 0 && elem.match( /^(diff|oldid)=/ ) === null;
|
|
|
|
} );
|
|
|
|
paramArray.forEach( function ( elem ) {
|
|
|
|
var pair = elem.split( '=', 2 );
|
|
|
|
params[ pair[ 0 ] ] = pair[ 1 ];
|
|
|
|
} );
|
|
|
|
return params;
|
2016-07-31 19:13:00 +00:00
|
|
|
},
|
|
|
|
|
2016-06-17 13:06:12 +00:00
|
|
|
/**
|
|
|
|
* @param {SliderView} sliderView
|
|
|
|
*/
|
2016-05-11 10:01:44 +00:00
|
|
|
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 );
|
2017-07-12 15:03:25 +00:00
|
|
|
sliderView.updatePointersAndDiffView(
|
|
|
|
event.state.pointerNewerPos,
|
|
|
|
event.state.pointerOlderPos,
|
|
|
|
false
|
2016-05-19 16:39:58 +00:00
|
|
|
);
|
2016-05-11 10:01:44 +00:00
|
|
|
} );
|
2017-02-02 17:24:51 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {SliderView} sliderView
|
|
|
|
*/
|
|
|
|
addHandlersToCoreLinks: function ( sliderView ) {
|
2019-02-10 16:54:28 +00:00
|
|
|
$( '#differences-nextlink' ).on( 'click', function () {
|
2017-02-02 17:24:51 +00:00
|
|
|
sliderView.showNextDiff();
|
|
|
|
return false;
|
|
|
|
} );
|
2019-02-10 16:54:28 +00:00
|
|
|
$( '#differences-prevlink' ).on( 'click', function () {
|
2017-02-02 17:24:51 +00:00
|
|
|
sliderView.showPrevDiff();
|
|
|
|
return false;
|
|
|
|
} );
|
2016-05-10 18:53:31 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
mw.libs.revisionSlider = mw.libs.revisionSlider || {};
|
|
|
|
mw.libs.revisionSlider.DiffPage = DiffPage;
|
2018-11-16 16:33:52 +00:00
|
|
|
}() );
|