From bf99353feb2834b60927250f717bb70c1130efb2 Mon Sep 17 00:00:00 2001 From: Leo Koppelkamm Date: Sat, 16 Apr 2011 14:03:07 +0000 Subject: [PATCH] Move timezone preference functions to mediawiki.special.preferences.js & remove their wikibits dependencies --- includes/specials/SpecialPreferences.php | 1 - resources/Resources.php | 6 - .../mediawiki.special.preferences.js | 82 ++++++++++- skins/common/prefs.js | 138 ------------------ 4 files changed, 81 insertions(+), 146 deletions(-) delete mode 100644 skins/common/prefs.js diff --git a/includes/specials/SpecialPreferences.php b/includes/specials/SpecialPreferences.php index b269afa9ef..edc26bc1a6 100644 --- a/includes/specials/SpecialPreferences.php +++ b/includes/specials/SpecialPreferences.php @@ -52,7 +52,6 @@ class SpecialPreferences extends SpecialPage { return; } - $wgOut->addModules( 'mediawiki.legacy.prefs' ); $wgOut->addModules( 'mediawiki.special.preferences' ); if ( $wgRequest->getCheck( 'success' ) ) { diff --git a/resources/Resources.php b/resources/Resources.php index a2dbb32c36..7ae0d2285f 100644 --- a/resources/Resources.php +++ b/resources/Resources.php @@ -566,12 +566,6 @@ return array( 'dependencies' => array( 'mediawiki.legacy.wikibits', 'jquery.client' ), 'messages' => array( 'search-mwsuggest-enabled', 'search-mwsuggest-disabled' ), ), - 'mediawiki.legacy.prefs' => array( - 'scripts' => 'common/prefs.js', - 'remoteBasePath' => $GLOBALS['wgStylePath'], - 'localBasePath' => "{$GLOBALS['IP']}/skins", - 'dependencies' => array( 'mediawiki.legacy.wikibits', 'mediawiki.htmlform' ), - ), 'mediawiki.legacy.preview' => array( 'scripts' => 'common/preview.js', 'remoteBasePath' => $GLOBALS['wgStylePath'], diff --git a/resources/mediawiki.special/mediawiki.special.preferences.js b/resources/mediawiki.special/mediawiki.special.preferences.js index 7fbd6d5901..ae21e3c2f9 100644 --- a/resources/mediawiki.special/mediawiki.special.preferences.js +++ b/resources/mediawiki.special/mediawiki.special.preferences.js @@ -27,7 +27,7 @@ $( '#preferences' ) .attr( 'href', '#preftab-' + i ) // Use #preftab-N instead of #prefsection-N to avoid jumping on click .click( function() { $(this).parent().parent().find( 'li' ).removeClass( 'selected' ); - $(this).parent().addClass( 'selected' ) + $(this).parent().addClass( 'selected' ); $( '#preferences > fieldset' ).hide(); $( '#prefsection-' + i ).show(); } ) @@ -80,4 +80,84 @@ $( '#mw-input-wpemailaddress' ).one( 'blur', function() { updateMailValidityLabel( $(this).val() ); } ); } ); + + + +/** +* Timezone functions. +* Guesses Timezone from browser and updates fields onchange +*/ + +var $tzSelect = $( '#mw-input-wptimecorrection' ); +var $tzTextbox = $( '#mw-input-wptimecorrection-other' ); + +var $localtimeHolder = $( '#wpLocalTime' ); +var servertime = parseInt( $( 'input[name=wpServerTime]' ).val(), 10 ); +var minuteDiff = 0; + +var minutesToHours = function( min ) { + var tzHour = Math.floor( Math.abs( min ) / 60 ); + var tzMin = Math.abs( min ) % 60; + var tzString = ( ( min >= 0 ) ? '' : '-' ) + ( ( tzHour < 10 ) ? '0' : '' ) + tzHour + + ':' + ( ( tzMin < 10 ) ? '0' : '' ) + tzMin; + return tzString; +}; + +var hoursToMinutes = function( hour ) { + var arr = hour.split( ':' ); + arr[0] = parseInt( arr[0], 10 ); + + if ( arr.length == 1 ) { + // Specification is of the form [-]XX + minutes = arr[0] * 60; + } else { + // Specification is of the form [-]XX:XX + minutes = Math.abs( arr[0] ) * 60 + parseInt( arr[1], 10 ); + if ( arr[0] < 0 ) { + minutes *= -1; + } + } + // Gracefully handle non-numbers. + if ( isNaN( minutes ) ) { + return 0; + } else { + return minutes; + } +}; + +var updateTimezoneSelection = function() { + var type = $tzSelect.val(); + if ( type == 'guess' ) { + // Get browser timezone & fill it in + minuteDiff = -new Date().getTimezoneOffset(); + $tzTextbox.val( minutesToHours( minuteDiff ) ); + $tzSelect.val( 'other' ); + $tzTextbox.get( 0 ).disabled = false; + } else if ( type == 'other' ) { + // Grab data from the textbox, parse it. + minuteDiff = hoursToMinutes( $tzTextbox.val() ); + } else { + // Grab data from the $tzSelect value + minuteDiff = parseInt( type.split( '|' )[1], 10 ) || 0; + $tzTextbox.val( minutesToHours( minuteDiff ) ); + } + + // Determine local time from server time and minutes difference, for display. + var localTime = servertime + minuteDiff; + + // Bring time within the [0,1440) range. + while ( localTime < 0 ) { + localTime += 1440; + } + while ( localTime >= 1440 ) { + localTime -= 1440; + } + $localtimeHolder.text( minutesToHours( localTime ) ); +}; + +if ( $tzSelect.length && $tzTextbox.length ) { + $tzSelect.change( function() { updateTimezoneSelection(); } ); + $tzTextbox.blur( function() { updateTimezoneSelection(); } ); + updateTimezoneSelection(); +} } )( jQuery, mediaWiki ); \ No newline at end of file diff --git a/skins/common/prefs.js b/skins/common/prefs.js deleted file mode 100644 index 1eb8e5bfbb..0000000000 --- a/skins/common/prefs.js +++ /dev/null @@ -1,138 +0,0 @@ -// Timezone stuff -// tz in format [+-]HHMM -window.checkTimezone = function( tz, msg ) { - var localclock = new Date(); - // returns negative offset from GMT in minutes - var tzRaw = localclock.getTimezoneOffset(); - var tzHour = Math.floor( Math.abs( tzRaw ) / 60 ); - var tzMin = Math.abs( tzRaw ) % 60; - var tzString = ( ( tzRaw >= 0 ) ? '-' : '+' ) + ( ( tzHour < 10 ) ? '0' : '' ) + tzHour + ( ( tzMin < 10 ) ? '0' : '' ) + tzMin; - if ( tz != tzString ) { - var junk = msg.split('$1'); - document.write( junk[0] + 'UTC' + tzString + junk[1] ); - } -}; - -window.timezoneSetup = function() { - var tzSelect = document.getElementById( 'mw-input-wptimecorrection' ); - var tzTextbox = document.getElementById( 'mw-input-wptimecorrection-other' ); - - if ( tzSelect && tzTextbox ) { - addHandler( tzSelect, 'change', function( e ) { updateTimezoneSelection( false ); } ); - addHandler( tzTextbox, 'blur', function( e ) { updateTimezoneSelection( true ); } ); - } - - updateTimezoneSelection( false ); -}; - -// in [-]HH:MM format... -// won't yet work with non-even tzs -window.fetchTimezone = function() { - // FIXME: work around Safari bug - var localclock = new Date(); - // returns negative offset from GMT in minutes - var tzRaw = localclock.getTimezoneOffset(); - var tzHour = Math.floor( Math.abs( tzRaw ) / 60 ); - var tzMin = Math.abs( tzRaw ) % 60; - var tzString = ( ( tzRaw >= 0 ) ? '-' : '' ) + ( ( tzHour < 10 ) ? '0' : '' ) + tzHour + - ':' + ( ( tzMin < 10 ) ? '0' : '' ) + tzMin; - return tzString; -}; - -window.guessTimezone = function() { - var textbox = document.getElementById( 'mw-input-wptimecorrection-other' ); - var selector = document.getElementById( 'mw-input-wptimecorrection' ); - - selector.value = 'other'; - textbox.value = fetchTimezone(); - textbox.disabled = false; // The changed handler doesn't trip, obviously. - updateTimezoneSelection( true ); -}; - -window.updateTimezoneSelection = function( force_offset ) { - var selector = document.getElementById( 'mw-input-wptimecorrection' ); - - if ( selector.value == 'guess' ) { - return guessTimezone(); - } - - var textbox = document.getElementById( 'mw-input-wptimecorrection-other' ); - var localtimeHolder = document.getElementById( 'wpLocalTime' ); - var servertime = document.getElementsByName( 'wpServerTime' )[0].value; - var minDiff = 0; - - // Compatibility code. - if ( !selector.value ) { - selector.value = selector.options[selector.selectedIndex].value; - } - - // Handle force_offset - if ( force_offset ) { - selector.value = 'other'; - } - - // Get min_diff - if ( selector.value == 'other' ) { - // Grab data from the textbox, parse it. - var diffArr = textbox.value.split(':'); - if ( diffArr.length == 1 ) { - // Specification is of the form [-]XX - minDiff = parseInt( diffArr[0], 10 ) * 60; - } else { - // Specification is of the form [-]XX:XX - minDiff = Math.abs( parseInt( diffArr[0], 10 ) ) * 60 + parseInt( diffArr[1], 10 ); - if ( parseInt( diffArr[0], 10 ) < 0 ) { - minDiff = -minDiff; - } - } - } else { - // Grab data from the selector value - var diffArr = selector.value.split('|'); - minDiff = parseInt( diffArr[1], 10 ); - } - - // Gracefully handle non-numbers. - if ( isNaN( minDiff ) ) { - minDiff = 0; - } - - // Determine local time from server time and minutes difference, for display. - var localTime = parseInt( servertime, 10 ) + minDiff; - - // Bring time within the [0,1440) range. - while ( localTime < 0 ) { - localTime += 1440; - } - while ( localTime >= 1440 ) { - localTime -= 1440; - } - - // Split to hour and minute - var hour = String( Math.floor( localTime / 60 ) ); - if ( hour.length < 2 ) { - hour = '0' + hour; - } - var min = String(localTime%60); - if ( min.length < 2 ) { - min = '0' + min; - } - changeText( localtimeHolder, hour + ':' + min ); - - // If the user selected from the drop-down, fill the offset field. - if ( selector.value != 'other' ) { - hour = String( Math.abs( Math.floor( minDiff / 60 ) ) ); - if ( hour.length < 2 ) { - hour = '0' + hour; - } - if ( minDiff < 0 ) { - hour = '-' + hour; - } - min = String(minDiff%60); - if ( min.length < 2 ) { - min = '0' + min; - } - textbox.value = hour + ':' + min; - } -}; - -addOnloadHook( timezoneSetup ); -- 2.20.1