Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / resources / src / mediawiki.action / mediawiki.action.history.js
1 /*!
2 * JavaScript for History action
3 */
4 $( function () {
5 var $historyCompareForm = $( '#mw-history-compare' ),
6 $historySubmitter,
7 $lis = $( '#pagehistory > li' );
8
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;
21
22 if ( !$lis.length ) {
23 return true;
24 }
25
26 $lis.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 );
31
32 $li.removeClass( 'selected between before after' );
33
34 if ( !$oldidRadio.length || !$diffRadio.length ) {
35 return true;
36 }
37
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 } );
50
51 return true;
52 }
53
54 $lis.find( 'input[name="diff"], input[name="oldid"]' ).on( 'click', updateDiffRadios );
55
56 // Set initial state
57 updateDiffRadios();
58
59 // Prettify url output for HistoryAction submissions,
60 // to cover up action=historysubmit construction.
61
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' ).on( 'click', function () {
65 $historySubmitter = $( this );
66 } );
67
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.on( 'submit', function ( e ) {
75 var $copyForm, $copyRadios, $copyAction;
76
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"]' );
81
82 // Remove action=historysubmit and ids[..]=..
83 // eslint-disable-next-line no-jquery/no-class-state
84 if ( $historySubmitter.hasClass( 'mw-history-compareselectedversions-button' ) ) {
85 $copyAction.remove();
86 $copyForm.find( 'input[name^="ids["]:checked' ).prop( 'checked', false );
87
88 // Remove diff=&oldid=, change action=historysubmit to revisiondelete, remove revisiondelete
89 } else if (
90 // eslint-disable-next-line no-jquery/no-class-state
91 $historySubmitter.hasClass( 'mw-history-revisiondelete-button' ) ||
92 // eslint-disable-next-line no-jquery/no-class-state
93 $historySubmitter.hasClass( 'mw-history-editchangetags-button' )
94 ) {
95 $copyRadios.remove();
96 $copyAction.val( $historySubmitter.attr( 'name' ) );
97 // eslint-disable-next-line no-jquery/no-sizzle
98 $copyForm.find( ':submit' ).remove();
99 }
100
101 // Firefox requires the form to be attached, so insert hidden into document first
102 // Also remove potentially conflicting id attributes that we don't need anyway
103 $copyForm
104 .css( 'display', 'none' )
105 .find( '[id]' ).removeAttr( 'id' )
106 .end()
107 .insertAfter( $historyCompareForm )
108 .trigger( 'submit' );
109
110 e.preventDefault();
111 return false; // Because the submit is special, return false as well.
112 }
113
114 // Continue natural browser handling other wise
115 return true;
116 } );
117 } );