Merge "Put the check for empty comment outside of the section anchor section of EditP...
[lhc/web/wiklou.git] / resources / mediawiki.special / mediawiki.special.preferences.js
1 /**
2 * JavaScript for Special:Preferences
3 */
4 jQuery( document ).ready( function ( $ ) {
5 var $preftoc, $preferences, $fieldsets, $legends,
6 hash,
7 $tzSelect, $tzTextbox, $localtimeHolder, servertime;
8
9 $( '#prefsubmit' ).attr( 'id', 'prefcontrol' );
10
11 $preftoc = $('<ul id="preftoc"></ul>');
12 $preferences = $( '#preferences' )
13 .addClass( 'jsprefs' )
14 .before( $preftoc );
15 $fieldsets = $preferences.children( 'fieldset' )
16 .hide()
17 .addClass( 'prefsection' );
18 $legends = $fieldsets
19 .children( 'legend' )
20 .addClass( 'mainLegend' );
21
22 /**
23 * It uses document.getElementById for security reasons (HTML injections in $()).
24 *
25 * @param String name: the name of a tab without the prefix ("mw-prefsection-")
26 * @param String mode: [optional] A hash will be set according to the current
27 * open section. Set mode 'noHash' to surpress this.
28 */
29 function switchPrefTab( name, mode ) {
30 var $tab, scrollTop;
31 // Handle hash manually to prevent jumping,
32 // therefore save and restore scrollTop to prevent jumping.
33 scrollTop = $( window ).scrollTop();
34 if ( mode !== 'noHash' ) {
35 window.location.hash = '#mw-prefsection-' + name;
36 }
37 $( window ).scrollTop( scrollTop );
38
39 $preftoc.find( 'li' ).removeClass( 'selected' );
40 $tab = $( document.getElementById( 'preftab-' + name ) );
41 if ( $tab.length ) {
42 $tab.parent().addClass( 'selected' );
43 $preferences.children( 'fieldset' ).hide();
44 $( document.getElementById( 'mw-prefsection-' + name ) ).show();
45 }
46 }
47
48 // Populate the prefToc
49 $legends.each( function ( i, legend ) {
50 var $legend = $(legend),
51 ident, $li, $a;
52 if ( i === 0 ) {
53 $legend.parent().show();
54 }
55 ident = $legend.parent().attr( 'id' );
56
57 $li = $( '<li>' )
58 .addClass( i === 0 ? 'selected' : '' );
59 $a = $( '<a>' )
60 .attr( {
61 id: ident.replace( 'mw-prefsection', 'preftab' ),
62 href: '#' + ident
63 } )
64 .text( $legend.text() );
65 $li.append( $a );
66 $preftoc.append( $li );
67 } );
68
69 // If we've reloaded the page or followed an open-in-new-window,
70 // make the selected tab visible.
71 hash = window.location.hash;
72 if ( hash.match( /^#mw-prefsection-[\w\-]+/ ) ) {
73 switchPrefTab( hash.replace( '#mw-prefsection-' , '' ) );
74 }
75
76 // In browsers that support the onhashchange event we will not bind click
77 // handlers and instead let the browser do the default behavior (clicking the
78 // <a href="#.."> will naturally set the hash, handled by onhashchange.
79 // But other things that change the hash will also be catched (e.g. using
80 // the Back and Forward browser navigation).
81 // Note the special check for IE "compatibility" mode.
82 if ( 'onhashchange' in window &&
83 ( document.documentMode === undefined || document.documentMode >= 8 )
84 ) {
85 $(window).on( 'hashchange' , function () {
86 var hash = window.location.hash;
87 if ( hash.match( /^#mw-prefsection-[\w\-]+/ ) ) {
88 switchPrefTab( hash.replace( '#mw-prefsection-', '' ) );
89 } else if ( hash === '' ) {
90 switchPrefTab( 'personal', 'noHash' );
91 }
92 });
93 // In older browsers we'll bind a click handler as fallback.
94 // We must not have onhashchange *and* the click handlers, other wise
95 // the click handler calls switchPrefTab() which sets the hash value,
96 // which triggers onhashcange and calls switchPrefTab() again.
97 } else {
98 $preftoc.on( 'click', 'li a', function ( e ) {
99 switchPrefTab( $( this ).attr( 'href' ).replace( '#mw-prefsection-', '' ) );
100 e.preventDefault();
101 });
102 }
103
104 /**
105 * Timezone functions.
106 * Guesses Timezone from browser and updates fields onchange
107 */
108
109 $tzSelect = $( '#mw-input-wptimecorrection' );
110 $tzTextbox = $( '#mw-input-wptimecorrection-other' );
111 $localtimeHolder = $( '#wpLocalTime' );
112 servertime = parseInt( $( 'input[name="wpServerTime"]' ).val(), 10 );
113
114 function minutesToHours( min ) {
115 var tzHour = Math.floor( Math.abs( min ) / 60 ),
116 tzMin = Math.abs( min ) % 60,
117 tzString = ( ( min >= 0 ) ? '' : '-' ) + ( ( tzHour < 10 ) ? '0' : '' ) + tzHour +
118 ':' + ( ( tzMin < 10 ) ? '0' : '' ) + tzMin;
119 return tzString;
120 }
121
122 function hoursToMinutes( hour ) {
123 var minutes,
124 arr = hour.split( ':' );
125
126 arr[0] = parseInt( arr[0], 10 );
127
128 if ( arr.length === 1 ) {
129 // Specification is of the form [-]XX
130 minutes = arr[0] * 60;
131 } else {
132 // Specification is of the form [-]XX:XX
133 minutes = Math.abs( arr[0] ) * 60 + parseInt( arr[1], 10 );
134 if ( arr[0] < 0 ) {
135 minutes *= -1;
136 }
137 }
138 // Gracefully handle non-numbers.
139 if ( isNaN( minutes ) ) {
140 return 0;
141 } else {
142 return minutes;
143 }
144 }
145
146 function updateTimezoneSelection () {
147 var minuteDiff, localTime,
148 type = $tzSelect.val();
149
150 if ( type === 'guess' ) {
151 // Get browser timezone & fill it in
152 minuteDiff = -( new Date().getTimezoneOffset() );
153 $tzTextbox.val( minutesToHours( minuteDiff ) );
154 $tzSelect.val( 'other' );
155 $tzTextbox.prop( 'disabled', false );
156 } else if ( type === 'other' ) {
157 // Grab data from the textbox, parse it.
158 minuteDiff = hoursToMinutes( $tzTextbox.val() );
159 } else {
160 // Grab data from the $tzSelect value
161 minuteDiff = parseInt( type.split( '|' )[1], 10 ) || 0;
162 $tzTextbox.val( minutesToHours( minuteDiff ) );
163 }
164
165 // Determine local time from server time and minutes difference, for display.
166 localTime = servertime + minuteDiff;
167
168 // Bring time within the [0,1440) range.
169 while ( localTime < 0 ) {
170 localTime += 1440;
171 }
172 while ( localTime >= 1440 ) {
173 localTime -= 1440;
174 }
175 $localtimeHolder.text( minutesToHours( localTime ) );
176 }
177
178 if ( $tzSelect.length && $tzTextbox.length ) {
179 $tzSelect.change( updateTimezoneSelection );
180 $tzTextbox.blur( updateTimezoneSelection );
181 updateTimezoneSelection();
182 }
183
184 // Preserve the tab after saving the preferences
185 // Not using cookies, because their deletion results are inconsistent.
186 // Not using jStorage due to its enormous size (for this feature)
187 if ( window.sessionStorage ) {
188 if ( sessionStorage.getItem( 'mediawikiPreferencesTab' ) !== null ) {
189 switchPrefTab( sessionStorage.getItem( 'mediawikiPreferencesTab' ), 'noHash' );
190 }
191 // Deleting the key, the tab states should be reset until we press Save
192 sessionStorage.removeItem( 'mediawikiPreferencesTab' );
193
194 $( '#mw-prefs-form' ).submit( function () {
195 var storageData = $( $preftoc ).find( 'li.selected a' ).attr( 'id' ).replace( 'preftab-', '' );
196 sessionStorage.setItem( 'mediawikiPreferencesTab', storageData );
197 } );
198 }
199 } );