Followup to r86056 per CR: Remove unneeded function wrapping
[lhc/web/wiklou.git] / resources / mediawiki.action / mediawiki.action.history.js
1 /*
2 * JavaScript for History action
3 */
4 jQuery( function( $ ) {
5 var $lis = $( 'ul#pagehistory li' );
6 var updateDiffRadios = function() {
7 var diffLi = false, // the li where the diff radio is checked
8 oldLi = false; // the li where the oldid radio is checked
9
10 if ( !$lis.length ) {
11 return true;
12 }
13 $lis.removeClass( 'selected' );
14 $lis.each( function() {
15 var $this = $(this);
16 var $inputs = $this.find( 'input[type=radio]' );
17 if ( $inputs.length !== 2 ) {
18 return true;
19 }
20
21 // this row has a checked radio button
22 if ( $inputs.get(0).checked ) {
23 oldLi = $this;
24 $this.addClass( 'selected' );
25 $inputs.eq(0).css( 'visibility', 'visible' );
26 $inputs.eq(1).css( 'visibility', 'hidden' );
27 } else if ( $inputs.get(1).checked ) {
28 diffLi = $this;
29 $this.addClass( 'selected' );
30 $inputs.eq(0).css( 'visibility', 'hidden' );
31 $inputs.eq(1).css( 'visibility', 'visible' );
32 } else {
33 // no radio is checked in this row
34 if ( diffLi && oldLi ) {
35 // We're below the selected radios
36 $inputs.eq(0).css( 'visibility', 'visible' );
37 $inputs.eq(1).css( 'visibility', 'hidden' );
38 } else if ( diffLi ) {
39 // We're between the selected radios
40 $inputs.eq(0).css( 'visibility', 'visible' );
41 $inputs.eq(1).css( 'visibility', 'visible' );
42 } else {
43 // We're above the selected radios
44 $inputs.eq(1).css( 'visibility', 'visible' );
45 $inputs.eq(0).css( 'visibility', 'hidden' );
46 }
47 }
48 });
49 return true;
50 };
51
52 var fixCompare = function () {
53 var $diffList = $( '#pagehistory' ),
54 $histForm = $( '#mw-history-compare' ),
55 $buttons = $histForm.find( 'input.historysubmit' );
56
57 // There's only one rev, nothing to do here
58 if ( !$buttons.length ) {
59 return false;
60 }
61
62 var buttonText = $buttons.remove().first().val(),
63 $compareLink = $( '<a></a>', {
64 'class': 'compare-link',
65 'text': buttonText
66 }).button();
67 $histForm.prepend( $compareLink );
68 if ( $buttons.length == 2 ) {
69 $histForm.append( $compareLink.clone() );
70 }
71
72 function updateCompare() {
73 var $radio = $histForm.find( 'input[type=radio]:checked' );
74 var genLink = mw.config.get( 'wgScript' )
75 + '?title=' + mw.util.wikiUrlencode( mw.config.get( 'wgPageName' ) )
76 + '&diff=' + $radio.eq(0).val()
77 + '&oldid=' + $radio.eq(1).val();
78 $( '.compare-link' ).each( function() {
79 $(this).attr('href', genLink);
80 });
81 }
82 updateCompare();
83 $diffList.change( updateCompare );
84 };
85
86 $( '#pagehistory li input[name="diff"], #pagehistory li input[name="oldid"]' ).click( updateDiffRadios );
87 fixCompare();
88 updateDiffRadios();
89 });