98a97cd0a0f7b383e918ec6419e9b887ab9cf397
[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 = true;
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 = true;
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.css( 'visibility', 'visible' );
41 } else {
42 // We're above the selected radios
43 $inputs.eq(1).css( 'visibility', 'visible' );
44 $inputs.eq(0).css( 'visibility', 'hidden' );
45 }
46 }
47 });
48 return true;
49 };
50
51 var fixCompare = function () {
52 var $diffList = $( '#pagehistory' ),
53 $histForm = $( '#mw-history-compare' ),
54 $buttons = $histForm.find( 'input.historysubmit' );
55
56 // There's only one rev, nothing to do here
57 if ( !$buttons.length ) {
58 return false;
59 }
60
61 var buttonText = $buttons.remove().first().val(),
62 $compareLink = $( '<a></a>', {
63 'class': 'compare-link',
64 'text': buttonText
65 }).button();
66 $histForm.prepend( $compareLink );
67 if ( $buttons.length == 2 ) {
68 $histForm.append( $compareLink.clone() );
69 }
70
71 function updateCompare() {
72 var $radio = $histForm.find( 'input[type=radio]:checked' );
73 var genLink = mw.config.get( 'wgScript' )
74 + '?title=' + mw.util.wikiUrlencode( mw.config.get( 'wgPageName' ) )
75 + '&diff=' + $radio.eq(0).val()
76 + '&oldid=' + $radio.eq(1).val();
77 $( '.compare-link' ).each( function() {
78 $(this).attr('href', genLink);
79 });
80 }
81 updateCompare();
82 $diffList.change( updateCompare );
83 };
84
85 $( '#pagehistory li input[name="diff"], #pagehistory li input[name="oldid"]' ).click( updateDiffRadios );
86 fixCompare();
87 updateDiffRadios();
88 });