2 * JavaScript for diff inline toggle
4 module.exports = function ( $inlineToggleSwitchLayout ) {
5 const url = new URL( location.href ),
7 $inlineLegendContainer = $( '.mw-diff-inline-legend' ),
8 inlineToggleSwitchLayout = OO.ui.FieldLayout.static.infuse( $inlineToggleSwitchLayout ),
9 inlineToggleSwitch = inlineToggleSwitchLayout.getField();
11 inlineToggleSwitch.on( 'change', ( e ) => {
12 onDiffTypeInlineChange( e, true );
14 inlineToggleSwitch.on( 'disable', onDiffTypeInlineDisabled );
16 const $wikitextDiffContainer = $( 'table.diff[data-mw="interface"]' );
17 const $wikitextDiffHeader = $wikitextDiffContainer.find( 'tr.diff-title' )
18 .add( $wikitextDiffContainer.find( 'td.diff-multi, td.diff-notice' ).parent() );
19 let $wikitextDiffBody = $wikitextDiffContainer.find( 'tr' ).not( $wikitextDiffHeader );
21 let $wikitextDiffBodyInline, $wikitextDiffBodyTable;
22 if ( inlineToggleSwitch.getValue() ) {
23 $wikitextDiffBodyInline = $wikitextDiffBody;
25 $wikitextDiffBodyTable = $wikitextDiffBody;
29 * Handler for inlineToggleSwitch onChange event
31 * @param {boolean} isInline
32 * @param {boolean} saveDiffTypeOption
34 function onDiffTypeInlineChange( isInline, saveDiffTypeOption ) {
35 if ( ( isInline && typeof $wikitextDiffBodyInline === 'undefined' ) ||
36 ( !isInline && typeof $wikitextDiffBodyTable === 'undefined' ) ) {
37 fetchDiff( isInline );
39 toggleDiffTypeVisibility( isInline );
42 if ( saveDiffTypeOption ) {
43 api.saveOption( 'diff-type', isInline ? 'inline' : 'table' )
45 if ( error === 'notloggedin' ) {
46 // Can't save preference, so use query parameter stickiness
47 switchQueryParams( isInline );
54 * Toggle legend and the DOM containers according to the format selected.
56 * @param {boolean} isInline
58 function toggleDiffTypeVisibility( isInline ) {
59 $inlineLegendContainer.toggleClass( 'oo-ui-element-hidden', !isInline );
60 if ( typeof $wikitextDiffBodyInline !== 'undefined' ) {
61 $wikitextDiffBodyInline.toggleClass( 'oo-ui-element-hidden', !isInline );
64 if ( typeof $wikitextDiffBodyTable !== 'undefined' ) {
65 $wikitextDiffBodyTable.toggleClass( 'oo-ui-element-hidden', isInline );
70 * Change the query parameters to maintain the new diff type.
71 * This is used for anonymous users.
73 * @param {boolean} isInline
75 function switchQueryParams( isInline ) {
76 $( '#differences-prevlink, #differences-nextlink' )
80 linkUrl = new URL( this.href );
85 linkUrl.searchParams.set( 'diff-type', 'inline' );
87 linkUrl.searchParams.delete( 'diff-type' );
89 this.href = linkUrl.toString();
94 * Toggle the legend when the toggle switch disabled state changes.
96 * @param {boolean} disabled
98 function onDiffTypeInlineDisabled( disabled ) {
100 $inlineLegendContainer.addClass( 'oo-ui-element-hidden' );
102 $inlineLegendContainer.toggleClass( 'oo-ui-element-hidden', !inlineToggleSwitch.getValue() );
103 // When Inline Switch is enabled, toggle wikitext according to its value.
104 // Do not save user 'diff-type' preference
105 onDiffTypeInlineChange( inlineToggleSwitch.getValue(), false );
110 * Fetch the diff through mw.API in the given format.
112 * @param {boolean} isInline
114 function fetchDiff( isInline ) {
115 const diffType = isInline ? 'inline' : 'table',
116 oldRevId = mw.config.get( 'wgDiffOldId' ),
117 newRevId = mw.config.get( 'wgDiffNewId' );
119 let oldPageName, newPageName;
120 if ( mw.config.get( 'wgCanonicalSpecialPageName' ) !== 'ComparePages' ) {
121 oldPageName = newPageName = mw.config.get( 'wgRelevantPageName' );
123 oldPageName = url.searchParams.get( 'page1' );
124 newPageName = url.searchParams.get( 'page2' );
129 fromtitle: oldPageName,
130 totitle: newPageName,
136 api.get( apiParams ).done( ( diffData ) => {
138 $wikitextDiffBodyInline = $( diffData.compare[ '*' ] );
140 $wikitextDiffBodyTable = $( diffData.compare[ '*' ] );
143 toggleDiffTypeVisibility( inlineToggleSwitch.getValue() );
145 $wikitextDiffBody.last().after( isInline ? $wikitextDiffBodyInline : $wikitextDiffBodyTable );
146 $wikitextDiffBody = $wikitextDiffContainer.find( 'tr' ).not( $wikitextDiffHeader );
148 * Fired when the wikitext DOM is updated so others can react accordingly.
150 * @event ~'wikipage.diff.wikitextDiffBody'
152 * @param {jQuery} $wikitextDiffBody
154 mw.hook( 'wikipage.diff.wikitextBodyUpdate' ).fire( $wikitextDiffBody );
159 * Fired when the diff type switch is present so others can decide
160 * how to manipulate the DOM.
162 * @event ~'wikipage.diff.diffTypeSwitch'
164 * @param {OO.ui.ToggleSwitchWidget} inlineToggleSwitch
166 mw.hook( 'wikipage.diff.diffTypeSwitch' ).fire( inlineToggleSwitch );