Merge "Special:ListGroupRights: Display the legend at the top"
[lhc/web/wiklou.git] / resources / mediawiki.action / mediawiki.action.history.js
index 98a97cd..04f045a 100644 (file)
-/*
+/**
  * JavaScript for History action
  */
-jQuery( function( $ ) {
-       var $lis = $( 'ul#pagehistory li' );
-       var updateDiffRadios = function() {
+jQuery( function ( $ ) {
+       var     $historyCompareForm = $( '#mw-history-compare' ),
+               $historySubmitter,
+               $lis = $( '#pagehistory > li' );
+
+       /**
+        * @context {Element} input
+        * @param e {jQuery.Event}
+        */
+       function updateDiffRadios() {
                var diffLi = false, // the li where the diff radio is checked
                        oldLi = false; // the li where the oldid radio is checked
 
                if ( !$lis.length ) {
                        return true;
                }
-               $lis.removeClass( 'selected' );
-               $lis.each( function() {
-                       var $this = $(this);
-                       var $inputs = $this.find( 'input[type=radio]' );
-                       if ( $inputs.length !== 2 ) {
+
+               $lis
+               .removeClass( 'selected' )
+               .each( function () {
+                       var     $li = $(this),
+                               $inputs = $li.find( 'input[type="radio"]' ),
+                               $oldidRadio = $inputs.filter( '[name="oldid"]' ).eq(0),
+                               $diffRadio = $inputs.filter( '[name="diff"]' ).eq(0);
+
+                       if ( !$oldidRadio.length || !$diffRadio.length ) {
                                return true;
                        }
 
-                       // this row has a checked radio button
-                       if ( $inputs.get(0).checked ) { 
+                       if ( $oldidRadio.prop( 'checked' ) ) {
                                oldLi = true;
-                               $this.addClass( 'selected' );
-                               $inputs.eq(0).css( 'visibility', 'visible' );
-                               $inputs.eq(1).css( 'visibility', 'hidden' );
-                       } else if ( $inputs.get(1).checked ) {
+                               $li.addClass( 'selected' );
+                               $oldidRadio.css( 'visibility', 'visible' );
+                               $diffRadio.css( 'visibility', 'hidden' );
+
+                       } else if ( $diffRadio.prop( 'checked' ) ) {
                                diffLi = true;
-                               $this.addClass( 'selected' );
-                               $inputs.eq(0).css( 'visibility', 'hidden' );
-                               $inputs.eq(1).css( 'visibility', 'visible' );
-                       } else { 
-                               // no radio is checked in this row
+                               $li.addClass( 'selected' );
+                               $oldidRadio.css( 'visibility', 'hidden' );
+                               $diffRadio.css( 'visibility', 'visible' );
+
+                       // This list item has neither checked
+                       } else {
+                               // We're below the selected radios
                                if ( diffLi && oldLi ) {
-                                       // We're below the selected radios
-                                       $inputs.eq(0).css( 'visibility', 'visible' );
-                                       $inputs.eq(1).css( 'visibility', 'hidden' );
-                               } else if ( diffLi ) {
-                                       // We're between the selected radios
-                                       $inputs.css( 'visibility', 'visible' );
+                                       $oldidRadio.css( 'visibility', 'visible' );
+                                       $diffRadio.css( 'visibility', 'hidden' );
+
+                               // We're between the selected radios
+                               } else if ( diffLi ) {
+                                       $diffRadio.css( 'visibility', 'visible' );
+                                       $oldidRadio.css( 'visibility', 'visible' );
+
+                               // We're above the selected radios
                                } else {
-                                       // We're above the selected radios
-                                       $inputs.eq(1).css( 'visibility', 'visible' );
-                                       $inputs.eq(0).css( 'visibility', 'hidden' );
+                                       $diffRadio.css( 'visibility', 'visible' );
+                                       $oldidRadio.css( 'visibility', 'hidden' );
                                }
                        }
                });
+
                return true;
-       };
-       
-       var fixCompare = function () {
-               var $diffList = $( '#pagehistory' ),
-                $histForm = $( '#mw-history-compare' ),
-                $buttons = $histForm.find( 'input.historysubmit' );
-
-               // There's only one rev, nothing to do here
-               if ( !$buttons.length ) {
-                       return false;
-               }
+       }
 
-               var buttonText = $buttons.remove().first().val(),
-                 $compareLink = $( '<a></a>', {
-                               'class': 'compare-link',
-                               'text': buttonText
-                       }).button();
-               $histForm.prepend( $compareLink );
-               if ( $buttons.length == 2 ) {
-                       $histForm.append( $compareLink.clone() );
-               }
+       $lis.find( 'input[name="diff"], input[name="oldid"]' ).click( updateDiffRadios );
+
+       // Set initial state
+       updateDiffRadios();
 
-               function updateCompare() {
-                       var $radio = $histForm.find( 'input[type=radio]:checked' );
-                       var genLink = mw.config.get( 'wgScript' )
-                               + '?title=' + mw.util.wikiUrlencode( mw.config.get( 'wgPageName' ) )
-                               + '&diff=' + $radio.eq(0).val()
-                               + '&oldid=' + $radio.eq(1).val();
-                       $( '.compare-link' ).each( function() {
-                               $(this).attr('href', genLink);
-                       });
+
+       // Prettify url output for HistoryAction submissions,
+       // to cover up action=historysubmit construction.
+
+       // Ideally we'd use e.target instead of $historySubmitter, but e.target points
+       // to the form element for submit actions, so.
+       $historyCompareForm.find( '.historysubmit' ).click( function () {
+               $historySubmitter = $(this);
+       } );
+
+       // On submit we clone the form element, remove unneeded fields in the clone
+       // that pollute the query parameter with stuff from the other "use case",
+       // and then submit the clone.
+       // Without the cloning we'd be changing the real form, which is slower, could make
+       // the page look broken for a second in slow browsers and might show the form broken
+       // again when coming back from a "next" page.
+       $historyCompareForm.submit( function ( e ) {
+               var     $copyForm, $copyRadios, $copyAction;
+
+               if ( $historySubmitter ) {
+                       $copyForm = $historyCompareForm.clone();
+                       $copyRadios = $copyForm.find( '#pagehistory > li' ).find( 'input[name="diff"], input[name="oldid"]' );
+                       $copyAction = $copyForm.find( '> [name="action"]');
+
+                       // Remove action=historysubmit and ids[..]=..
+                       if ( $historySubmitter.hasClass( 'mw-history-compareselectedversions-button' ) ) {
+                               $copyAction.remove();
+                               $copyForm.find( 'input[name^="ids["]:checked' ).prop( 'checked', false );
+
+                       // Remove diff=&oldid=, change action=historysubmit to revisiondelete, remove revisiondelete
+                       } else if ( $historySubmitter.hasClass( 'mw-history-revisiondelete-button' ) ) {
+                               $copyRadios.remove();
+                               $copyAction.val( $historySubmitter.attr( 'name' ) );
+                               $copyForm.find( ':submit' ).remove();
+                       }
+
+                       // IE7 doesn't do submission from an off-DOM clone, so insert hidden into document first
+                       // Also remove potentially conflicting id attributes that we don't need anyway
+                       $copyForm
+                               .css( 'display', 'none' )
+                               .find('[id]')
+                                       .removeAttr('id')
+                               .end()
+                               .insertAfter( $historyCompareForm )
+                               .submit();
+
+                       e.preventDefault();
+                       return false; // Because the submit is special, return false as well.
                }
-               updateCompare();
-               $diffList.change( updateCompare );
-       };
 
-       $( '#pagehistory li input[name="diff"], #pagehistory li input[name="oldid"]' ).click( updateDiffRadios );
-       fixCompare();
-       updateDiffRadios();
-});
\ No newline at end of file
+               // Continue natural browser handling other wise
+               return true;
+       } );
+} );