Merge "Improve sorting on SpecialWanted*-Pages"
[mediawiki.git] / resources / src / mediawiki.action / mediawiki.action.history.js
blobb3b0af239d4656168df37399118ee94ad2534f79
1 /*!
2  * JavaScript for History action
3  */
4 jQuery( function ( $ ) {
5         var     $historyCompareForm = $( '#mw-history-compare' ),
6                 $historySubmitter,
7                 $lis = $( '#pagehistory > li' );
9         /**
10          * @ignore
11          * @context {Element} input
12          * @param {jQuery.Event} e
13          * @return {boolean} False to cancel the default event
14          */
15         function updateDiffRadios() {
16                 var nextState = 'before',
17                         $li,
18                         $inputs,
19                         $oldidRadio,
20                         $diffRadio;
22                 if ( !$lis.length ) {
23                         return true;
24                 }
26                 $lis
27                 .each( function () {
28                         $li = $( this );
29                         $inputs = $li.find( 'input[type="radio"]' );
30                         $oldidRadio = $inputs.filter( '[name="oldid"]' ).eq( 0 );
31                         $diffRadio = $inputs.filter( '[name="diff"]' ).eq( 0 );
33                         $li.removeClass( 'selected between before after' );
35                         if ( !$oldidRadio.length || !$diffRadio.length ) {
36                                 return true;
37                         }
39                         if ( $oldidRadio.prop( 'checked' ) ) {
40                                 $li.addClass( 'selected after' );
41                                 nextState = 'after';
42                         } else if ( $diffRadio.prop( 'checked' ) ) {
43                                 $li.addClass( 'selected ' + nextState );
44                                 nextState = 'between';
45                         } else {
46                                 // This list item has neither checked
47                                 // apply the appropriate class following the previous item.
48                                 $li.addClass( nextState );
49                         }
50                 } );
52                 return true;
53         }
55         $lis.find( 'input[name="diff"], input[name="oldid"]' ).click( updateDiffRadios );
57         // Set initial state
58         updateDiffRadios();
60         // Prettify url output for HistoryAction submissions,
61         // to cover up action=historysubmit construction.
63         // Ideally we'd use e.target instead of $historySubmitter, but e.target points
64         // to the form element for submit actions, so.
65         $historyCompareForm.find( '.historysubmit' ).click( function () {
66                 $historySubmitter = $( this );
67         } );
69         // On submit we clone the form element, remove unneeded fields in the clone
70         // that pollute the query parameter with stuff from the other "use case",
71         // and then submit the clone.
72         // Without the cloning we'd be changing the real form, which is slower, could make
73         // the page look broken for a second in slow browsers and might show the form broken
74         // again when coming back from a "next" page.
75         $historyCompareForm.submit( function ( e ) {
76                 var     $copyForm, $copyRadios, $copyAction;
78                 if ( $historySubmitter ) {
79                         $copyForm = $historyCompareForm.clone();
80                         $copyRadios = $copyForm.find( '#pagehistory > li' ).find( 'input[name="diff"], input[name="oldid"]' );
81                         $copyAction = $copyForm.find( '> [name="action"]' );
83                         // Remove action=historysubmit and ids[..]=..
84                         if ( $historySubmitter.hasClass( 'mw-history-compareselectedversions-button' ) ) {
85                                 $copyAction.remove();
86                                 $copyForm.find( 'input[name^="ids["]:checked' ).prop( 'checked', false );
88                         // Remove diff=&oldid=, change action=historysubmit to revisiondelete, remove revisiondelete
89                         } else if ( $historySubmitter.hasClass( 'mw-history-revisiondelete-button' ) ||
90                                         $historySubmitter.hasClass( 'mw-history-editchangetags-button' ) ) {
91                                 $copyRadios.remove();
92                                 $copyAction.val( $historySubmitter.attr( 'name' ) );
93                                 $copyForm.find( ':submit' ).remove();
94                         }
96                         // IE7 doesn't do submission from an off-DOM clone, so insert hidden into document first
97                         // Also remove potentially conflicting id attributes that we don't need anyway
98                         $copyForm
99                                 .css( 'display', 'none' )
100                                 .find( '[id]' )
101                                         .removeAttr( 'id' )
102                                 .end()
103                                 .insertAfter( $historyCompareForm )
104                                 .submit();
106                         e.preventDefault();
107                         return false; // Because the submit is special, return false as well.
108                 }
110                 // Continue natural browser handling other wise
111                 return true;
112         } );
113 } );