2016-05-10 18:53:31 +00:00
|
|
|
( function ( mw, $ ) {
|
2016-09-06 10:57:45 +00:00
|
|
|
/* global wikEd */
|
|
|
|
|
2016-06-17 13:06:12 +00:00
|
|
|
/**
|
|
|
|
* Module handling diff page reloading and the RevisionSlider browser history
|
|
|
|
*
|
|
|
|
* @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
|
|
|
};
|
|
|
|
|
|
|
|
$.extend( DiffPage.prototype, {
|
2016-06-17 13:06:12 +00:00
|
|
|
/**
|
|
|
|
* Refreshes the diff view with two given revision IDs
|
|
|
|
*
|
|
|
|
* @param {number} revId1
|
|
|
|
* @param {number} revId2
|
2016-08-15 18:15:30 +00:00
|
|
|
* @param {number} [retryAttempt=0]
|
2016-06-17 13:06:12 +00:00
|
|
|
*/
|
2016-08-15 18:15:30 +00:00
|
|
|
refresh: function ( revId1, revId2, retryAttempt ) {
|
|
|
|
var self = this,
|
|
|
|
retryLimit = 2,
|
2016-11-22 16:59:15 +00:00
|
|
|
diff = Math.max( revId1, revId2 ),
|
|
|
|
oldid = Math.min( revId1, revId2 ),
|
2016-08-15 18:15:30 +00:00
|
|
|
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
|
|
|
|
this.lastRequest.then( function ( data ) {
|
|
|
|
var $data,
|
|
|
|
$container = $( '.mw-revslider-container' ),
|
|
|
|
$contentText = $( '#mw-content-text' ),
|
2016-08-18 13:00:55 +00:00
|
|
|
$sidePanel = $( '#mw-panel' ),
|
2016-08-25 12:01:42 +00:00
|
|
|
$catLinks = $( '#catlinks' ),
|
2016-11-16 00:48:33 +00:00
|
|
|
$printFooter = $( '.printfooter' ),
|
2016-08-15 18:15:30 +00:00
|
|
|
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 = $( data );
|
|
|
|
$data.find( '.mw-revslider-container' ).replaceWith( $container );
|
2016-10-24 08:53:08 +00:00
|
|
|
|
|
|
|
// Replace the elements on the page with the newly loaded elements
|
2016-08-25 12:01:42 +00:00
|
|
|
$catLinks.replaceWith( $data.find( '#catlinks' ) );
|
2016-08-18 13:00:55 +00:00
|
|
|
$sidePanel.replaceWith( $data.find( '#mw-panel' ) );
|
2016-08-25 13:18:14 +00:00
|
|
|
$printFooter.replaceWith( $data.find( '.printfooter' ) );
|
2016-10-24 08:53:08 +00:00
|
|
|
$contentText.replaceWith( $data.find( '#mw-content-text' ) );
|
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() );
|
|
|
|
} );
|
2016-10-24 08:53:08 +00:00
|
|
|
|
|
|
|
$( '.mw-revslider-revisions-container' ).scrollLeft( scrollLeft );
|
2016-06-03 09:52:49 +00:00
|
|
|
|
2016-08-15 18:15:30 +00:00
|
|
|
mw.hook( 'wikipage.content' ).fire( $contentText );
|
2016-09-13 07:53:27 +00:00
|
|
|
mw.hook( 'wikipage.diff' ).fire( $contentText.find( 'table.diff' ) );
|
2016-09-06 10:57:45 +00:00
|
|
|
|
|
|
|
// In order to correctly interact with third-party code (extensions and gadgets)
|
|
|
|
// Revision slider should trigger some general (core) hook that other parties listen too
|
|
|
|
// Following wikEdDiff.js-specific code is deprecated and will be removed in the future.
|
|
|
|
// WikEdDiff should be updated to use a hook.
|
|
|
|
if ( self.wikEdDiffDetected() ) {
|
2016-11-16 09:00:27 +00:00
|
|
|
mw.log( 'You are running WikEdDiff & your copy of the code may need to be updated to work with the RevisionSlider.' );
|
|
|
|
mw.log( 'Please see: https://phabricator.wikimedia.org/T143199#2631963' );
|
|
|
|
mw.log( 'If WikEdDiff is still working while using the RevisonSlider then the code change has already been done.' );
|
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 ) {
|
|
|
|
self.refresh( revId1, revId2, 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-09-06 10:57:45 +00:00
|
|
|
wikEdDiffDetected: function () {
|
|
|
|
return typeof wikEd !== 'undefined' && $( 'meta[name=wikEdDiffSetupFlag]' ).length !== 0;
|
|
|
|
},
|
|
|
|
|
2016-07-31 19:13:00 +00:00
|
|
|
/**
|
|
|
|
* Replaces the current state in the history stack
|
|
|
|
*
|
|
|
|
* @param {number} revId1
|
|
|
|
* @param {number} revId2
|
|
|
|
* @param {SliderView} sliderView
|
|
|
|
*/
|
|
|
|
replaceState: function ( revId1, revId2, sliderView ) {
|
|
|
|
// IE8 and IE9 do not have history.pushState()
|
|
|
|
if ( typeof history.replaceState === 'function' ) {
|
|
|
|
history.replaceState(
|
|
|
|
this.getStateObject( revId1, revId2, sliderView ),
|
|
|
|
$( document ).find( 'title' ).text(),
|
|
|
|
this.getStateUrl( revId1, revId2 )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-06-17 13:06:12 +00:00
|
|
|
/**
|
|
|
|
* Pushes the current state onto the history stack
|
|
|
|
*
|
|
|
|
* @param {number} revId1
|
|
|
|
* @param {number} revId2
|
|
|
|
* @param {SliderView} sliderView
|
|
|
|
*/
|
2016-05-11 10:01:44 +00:00
|
|
|
pushState: function ( revId1, revId2, sliderView ) {
|
2016-06-01 17:18:48 +00:00
|
|
|
// IE8 and IE9 do not have history.pushState()
|
|
|
|
if ( typeof history.pushState === 'function' ) {
|
|
|
|
history.pushState(
|
2016-07-31 19:13:00 +00:00
|
|
|
this.getStateObject( revId1, revId2, sliderView ),
|
2016-06-01 17:18:48 +00:00
|
|
|
$( document ).find( 'title' ).text(),
|
2016-07-31 19:13:00 +00:00
|
|
|
this.getStateUrl( revId1, revId2 )
|
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
|
|
|
|
*
|
|
|
|
* @param {number} revId1
|
|
|
|
* @param {number} revId2
|
|
|
|
* @param {SliderView} sliderView
|
2016-09-01 11:17:47 +00:00
|
|
|
* @return {Object}
|
2016-07-31 19:13:00 +00:00
|
|
|
*/
|
|
|
|
getStateObject: function ( revId1, revId2, sliderView ) {
|
|
|
|
return {
|
|
|
|
revid1: revId1,
|
|
|
|
revid2: revId2,
|
|
|
|
pointerOlderPos: sliderView.pointerOlder.getPosition(),
|
|
|
|
pointerNewerPos: sliderView.pointerNewer.getPosition(),
|
|
|
|
sliderPos: sliderView.slider.getFirstVisibleRevisionIndex()
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a URL to be used with history.replaceState and history.pushState
|
|
|
|
*
|
|
|
|
* @param {number} revId1
|
|
|
|
* @param {number} revId2
|
2016-09-01 11:17:47 +00:00
|
|
|
* @return {string}
|
2016-07-31 19:13:00 +00:00
|
|
|
*/
|
|
|
|
getStateUrl: function ( revId1, revId2 ) {
|
2016-08-01 11:33:36 +00:00
|
|
|
var url = mw.util.wikiScript( 'index' ) + '?diff=' + Math.max( revId1, revId2 ) + '&oldid=' + Math.min( revId1, revId2 ),
|
|
|
|
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 ) {
|
|
|
|
var self = this;
|
|
|
|
window.addEventListener( 'popstate', function ( event ) {
|
|
|
|
if ( event.state === null ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mw.track( 'counter.MediaWiki.RevisionSlider.event.historyChange' );
|
2016-05-21 00:10:08 +00:00
|
|
|
sliderView.pointerOlder.setPosition( event.state.pointerOlderPos );
|
|
|
|
sliderView.pointerNewer.setPosition( event.state.pointerNewerPos );
|
2016-05-11 10:01:44 +00:00
|
|
|
sliderView.slider.setFirstVisibleRevisionIndex( event.state.sliderPos );
|
|
|
|
sliderView.slide( 0 );
|
2016-05-17 12:29:08 +00:00
|
|
|
sliderView.resetPointerStylesBasedOnPosition();
|
2016-05-19 16:39:58 +00:00
|
|
|
sliderView.resetRevisionStylesBasedOnPointerPosition(
|
2016-07-01 10:13:53 +00:00
|
|
|
sliderView.$element.find( 'div.mw-revslider-revisions' )
|
2016-05-19 16:39:58 +00:00
|
|
|
);
|
2016-10-10 14:15:47 +00:00
|
|
|
sliderView.updatePointerPositionAttributes();
|
2016-05-11 10:01:44 +00:00
|
|
|
self.refresh( event.state.revid1, event.state.revid2 );
|
|
|
|
} );
|
2016-05-10 18:53:31 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
mw.libs.revisionSlider = mw.libs.revisionSlider || {};
|
|
|
|
mw.libs.revisionSlider.DiffPage = DiffPage;
|
|
|
|
}( mediaWiki, jQuery ) );
|