summaryrefslogtreecommitdiff
path: root/skins/common/prefs.js
blob: 1eb8e5bfbb5e3406206a4ceca5eee5fee11d1ded (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// 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 );