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