| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
module.exports = function ( $inlineToggleSwitchLayout ) { |
| 5 |
const url = new URL( location.href ), |
| 6 |
api = new mw.Api(), |
| 7 |
$inlineLegendContainer = $( '.mw-diff-inline-legend' ), |
| 8 |
inlineToggleSwitchLayout = OO.ui.FieldLayout.static.infuse( $inlineToggleSwitchLayout ), |
| 9 |
inlineToggleSwitch = inlineToggleSwitchLayout.getField(); |
| 10 |
|
| 11 |
inlineToggleSwitch.on( 'change', ( e ) => { |
| 12 |
onDiffTypeInlineChange( e, true ); |
| 13 |
} ); |
| 14 |
inlineToggleSwitch.on( 'disable', onDiffTypeInlineDisabled ); |
| 15 |
|
| 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 ); |
| 20 |
|
| 21 |
let $wikitextDiffBodyInline, $wikitextDiffBodyTable; |
| 22 |
if ( inlineToggleSwitch.getValue() ) { |
| 23 |
$wikitextDiffBodyInline = $wikitextDiffBody; |
| 24 |
} else { |
| 25 |
$wikitextDiffBodyTable = $wikitextDiffBody; |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
function onDiffTypeInlineChange( isInline, saveDiffTypeOption ) { |
| 35 |
if ( ( isInline && typeof $wikitextDiffBodyInline === 'undefined' ) || |
| 36 |
( !isInline && typeof $wikitextDiffBodyTable === 'undefined' ) ) { |
| 37 |
fetchDiff( isInline ); |
| 38 |
} else { |
| 39 |
toggleDiffTypeVisibility( isInline ); |
| 40 |
} |
| 41 |
|
| 42 |
if ( saveDiffTypeOption ) { |
| 43 |
api.saveOption( 'diff-type', isInline ? 'inline' : 'table' ) |
| 44 |
.fail( ( error ) => { |
| 45 |
if ( error === 'notloggedin' ) { |
| 46 |
|
| 47 |
switchQueryParams( isInline ); |
| 48 |
} |
| 49 |
} ); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 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 ); |
| 62 |
} |
| 63 |
|
| 64 |
if ( typeof $wikitextDiffBodyTable !== 'undefined' ) { |
| 65 |
$wikitextDiffBodyTable.toggleClass( 'oo-ui-element-hidden', isInline ); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
function switchQueryParams( isInline ) { |
| 76 |
$( '#differences-prevlink, #differences-nextlink' ) |
| 77 |
.each( function () { |
| 78 |
let linkUrl; |
| 79 |
try { |
| 80 |
linkUrl = new URL( this.href ); |
| 81 |
} catch ( e ) { |
| 82 |
return; |
| 83 |
} |
| 84 |
if ( isInline ) { |
| 85 |
linkUrl.searchParams.set( 'diff-type', 'inline' ); |
| 86 |
} else { |
| 87 |
linkUrl.searchParams.delete( 'diff-type' ); |
| 88 |
} |
| 89 |
this.href = linkUrl.toString(); |
| 90 |
} ); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
function onDiffTypeInlineDisabled( disabled ) { |
| 99 |
if ( disabled ) { |
| 100 |
$inlineLegendContainer.addClass( 'oo-ui-element-hidden' ); |
| 101 |
} else { |
| 102 |
$inlineLegendContainer.toggleClass( 'oo-ui-element-hidden', !inlineToggleSwitch.getValue() ); |
| 103 |
|
| 104 |
|
| 105 |
onDiffTypeInlineChange( inlineToggleSwitch.getValue(), false ); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
function fetchDiff( isInline ) { |
| 115 |
const diffType = isInline ? 'inline' : 'table', |
| 116 |
oldRevId = mw.config.get( 'wgDiffOldId' ), |
| 117 |
newRevId = mw.config.get( 'wgDiffNewId' ); |
| 118 |
|
| 119 |
let oldPageName, newPageName; |
| 120 |
if ( mw.config.get( 'wgCanonicalSpecialPageName' ) !== 'ComparePages' ) { |
| 121 |
oldPageName = newPageName = mw.config.get( 'wgRelevantPageName' ); |
| 122 |
} else { |
| 123 |
oldPageName = url.searchParams.get( 'page1' ); |
| 124 |
newPageName = url.searchParams.get( 'page2' ); |
| 125 |
} |
| 126 |
|
| 127 |
const apiParams = { |
| 128 |
action: 'compare', |
| 129 |
fromtitle: oldPageName, |
| 130 |
totitle: newPageName, |
| 131 |
fromrev: oldRevId, |
| 132 |
torev: newRevId, |
| 133 |
difftype: diffType |
| 134 |
}; |
| 135 |
|
| 136 |
api.get( apiParams ).done( ( diffData ) => { |
| 137 |
if ( isInline ) { |
| 138 |
$wikitextDiffBodyInline = $( diffData.compare[ '*' ] ); |
| 139 |
} else { |
| 140 |
$wikitextDiffBodyTable = $( diffData.compare[ '*' ] ); |
| 141 |
} |
| 142 |
|
| 143 |
toggleDiffTypeVisibility( inlineToggleSwitch.getValue() ); |
| 144 |
|
| 145 |
$wikitextDiffBody.last().after( isInline ? $wikitextDiffBodyInline : $wikitextDiffBodyTable ); |
| 146 |
$wikitextDiffBody = $wikitextDiffContainer.find( 'tr' ).not( $wikitextDiffHeader ); |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
mw.hook( 'wikipage.diff.wikitextBodyUpdate' ).fire( $wikitextDiffBody ); |
| 155 |
} ); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
mw.hook( 'wikipage.diff.diffTypeSwitch' ).fire( inlineToggleSwitch ); |
| 167 |
}; |