Implement extension registration from an extension.json file
[mediawiki.git] / resources / src / mediawiki.action / mediawiki.action.history.js
blobac48c596aed51a686d245472b683b9e16551bafa
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 e {jQuery.Event}
13          */
14         function updateDiffRadios() {
15                 var nextState = 'before',
16                         $li,
17                         $inputs,
18                         $oldidRadio,
19                         $diffRadio;
21                 if ( !$lis.length ) {
22                         return true;
23                 }
25                 $lis
26                 .each( function () {
27                         $li = $( this );
28                         $inputs = $li.find( 'input[type="radio"]' );
29                         $oldidRadio = $inputs.filter( '[name="oldid"]' ).eq( 0 );
30                         $diffRadio = $inputs.filter( '[name="diff"]' ).eq( 0 );
32                         $li.removeClass( 'selected between before after' );
34                         if ( !$oldidRadio.length || !$diffRadio.length ) {
35                                 return true;
36                         }
38                         if ( $oldidRadio.prop( 'checked' ) ) {
39                                 $li.addClass( 'selected after' );
40                                 nextState = 'after';
41                         } else if ( $diffRadio.prop( 'checked' ) ) {
42                                 $li.addClass( 'selected ' + nextState );
43                                 nextState = 'between';
44                         } else {
45                                 // This list item has neither checked
46                                 // apply the appropriate class following the previous item.
47                                 $li.addClass( nextState );
48                         }
49                 } );
51                 return true;
52         }
54         $lis.find( 'input[name="diff"], input[name="oldid"]' ).click( updateDiffRadios );
56         // Set initial state
57         updateDiffRadios();
59         // Prettify url output for HistoryAction submissions,
60         // to cover up action=historysubmit construction.
62         // Ideally we'd use e.target instead of $historySubmitter, but e.target points
63         // to the form element for submit actions, so.
64         $historyCompareForm.find( '.historysubmit' ).click( function () {
65                 $historySubmitter = $( this );
66         } );
68         // On submit we clone the form element, remove unneeded fields in the clone
69         // that pollute the query parameter with stuff from the other "use case",
70         // and then submit the clone.
71         // Without the cloning we'd be changing the real form, which is slower, could make
72         // the page look broken for a second in slow browsers and might show the form broken
73         // again when coming back from a "next" page.
74         $historyCompareForm.submit( function ( e ) {
75                 var     $copyForm, $copyRadios, $copyAction;
77                 if ( $historySubmitter ) {
78                         $copyForm = $historyCompareForm.clone();
79                         $copyRadios = $copyForm.find( '#pagehistory > li' ).find( 'input[name="diff"], input[name="oldid"]' );
80                         $copyAction = $copyForm.find( '> [name="action"]' );
82                         // Remove action=historysubmit and ids[..]=..
83                         if ( $historySubmitter.hasClass( 'mw-history-compareselectedversions-button' ) ) {
84                                 $copyAction.remove();
85                                 $copyForm.find( 'input[name^="ids["]:checked' ).prop( 'checked', false );
87                         // Remove diff=&oldid=, change action=historysubmit to revisiondelete, remove revisiondelete
88                         } else if ( $historySubmitter.hasClass( 'mw-history-revisiondelete-button' ) ) {
89                                 $copyRadios.remove();
90                                 $copyAction.val( $historySubmitter.attr( 'name' ) );
91                                 $copyForm.find( ':submit' ).remove();
92                         }
94                         // IE7 doesn't do submission from an off-DOM clone, so insert hidden into document first
95                         // Also remove potentially conflicting id attributes that we don't need anyway
96                         $copyForm
97                                 .css( 'display', 'none' )
98                                 .find( '[id]' )
99                                         .removeAttr( 'id' )
100                                 .end()
101                                 .insertAfter( $historyCompareForm )
102                                 .submit();
104                         e.preventDefault();
105                         return false; // Because the submit is special, return false as well.
106                 }
108                 // Continue natural browser handling other wise
109                 return true;
110         } );
111 } );