Merge "ForeignStructuredUpload: Depend on mediawiki.widgets.DateInputWidget"
[mediawiki.git] / resources / src / mediawiki.action / mediawiki.action.history.js
blob077d5e3a09199e73e9014897310683bed13650de
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          */
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                                         $historySubmitter.hasClass( 'mw-history-editchangetags-button' ) ) {
90                                 $copyRadios.remove();
91                                 $copyAction.val( $historySubmitter.attr( 'name' ) );
92                                 $copyForm.find( ':submit' ).remove();
93                         }
95                         // IE7 doesn't do submission from an off-DOM clone, so insert hidden into document first
96                         // Also remove potentially conflicting id attributes that we don't need anyway
97                         $copyForm
98                                 .css( 'display', 'none' )
99                                 .find( '[id]' )
100                                         .removeAttr( 'id' )
101                                 .end()
102                                 .insertAfter( $historyCompareForm )
103                                 .submit();
105                         e.preventDefault();
106                         return false; // Because the submit is special, return false as well.
107                 }
109                 // Continue natural browser handling other wise
110                 return true;
111         } );
112 } );