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