This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
/
tz_select.js
256 lines (228 loc) · 7.77 KB
/
tz_select.js
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* globals IccHelper */
/* exported tzSelect */
'use strict';
function tzSelect(regionSelector, citySelector, onchange, onload) {
const TIMEZONE_FILE = '/shared/resources/tz.json';
const APN_TZ_FILE = '/shared/resources/apn_tz.json';
const TIMEZONE_NONE = 'NONE';
const TIMEZONE_NETWORK = 'NETWORK';
const TIMEZONE_SIM = 'SIM';
const TIMEZONE_USER = 'USER';
const TIMEZONE_PREVIOUS_SETTING = 'PREVIOUS_SETTING';
function loadJSON(href, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', href, true);
xhr.responseType = 'json';
xhr.onerror = callback;
xhr.onload = function() {
callback(xhr.response);
};
xhr.onerror = function() {
console.error('Error getting file');
callback(xhr.response);
};
xhr.send();
}
/**
* Guess the current timezone from the MCC/MNC tuple
*/
function getDefaultTimezoneID(callback) {
if (!callback) {
return;
}
// Worst case scenario: default to New York (which is just as good or
// bad as anything else -- we used to default to Pago Pago)
var tzDefault = 'America/New_York';
var source = TIMEZONE_NONE;
// retrieve MCC/MNC: use the current network codes when available,
// default to the SIM codes if necessary.
var mcc, mnc;
// XXX: check bug-926169
// this is used to keep all tests passing while introducing multi-sim APIs
var conn = navigator.mozMobileConnection ||
window.navigator.mozMobileConnections &&
window.navigator.mozMobileConnections[0];
if (conn && IccHelper) {
if (conn.voice && conn.voice.network && conn.voice.connected) {
// we have connection available, so we use it
mcc = conn.voice.network.mcc;
mnc = conn.voice.network.mnc;
source = TIMEZONE_NETWORK;
} else if (IccHelper.iccInfo) {
// we don't have connection available, we rely on the SIM
mcc = IccHelper.iccInfo.mcc;
mnc = IccHelper.iccInfo.mnc;
source = TIMEZONE_SIM;
// if SIM is not available, mcc and mnc are null,
// so we wait for a future event where we have access to the SIM.
if (IccHelper.cardState !== 'ready') {
IccHelper.addEventListener('iccinfochange', function simReady() {
if (IccHelper.iccInfo.mcc) {
IccHelper.removeEventListener('iccinfochange', simReady);
}
getDefaultTimezoneID(callback);
});
}
}
}
if (!mcc) {
callback(tzDefault, TIMEZONE_NONE);
return;
}
// most MCC values only match one country (hence one timezone);
// for the few MCC values that match several countries, rely on MNC.
loadJSON(APN_TZ_FILE, function(response) {
if (response) {
var tz = response[mcc];
if (typeof(tz) === 'string') {
callback(tz, source);
return;
} else if (tz && (mnc in tz)) {
callback(tz[mnc], source);
return;
}
}
callback(tzDefault, TIMEZONE_NONE);
});
}
/**
* Activate a timezone selector UI
*/
function newTZSelector(onchangeTZ, currentID, source) {
// for region, we use whatever is BEFORE the /
var gRegion = currentID.replace(/\/.*/, '');
// for city, we use whatever is AFTER the /
var gCity = currentID.replace(/.*?\//, '');
var gTZ = null;
function fillSelectElement(selector, options) {
selector.innerHTML = '';
options.sort(function(a, b) {
return (a.text > b.text);
});
for (var i = 0; i < options.length; i++) {
var option = document.createElement('option');
option.textContent = options[i].text;
option.selected = options[i].selected;
option.value = options[i].value;
selector.appendChild(option);
}
}
function getSelectedText(selector) {
var options = selector.querySelectorAll('option');
return options[selector.selectedIndex].textContent;
}
function fillRegions() {
var _ = navigator.mozL10n.get;
var options = [];
for (var c in gTZ) {
options.push({
text: _('tzRegion-' + c) || c,
value: c,
selected: (c === gRegion)
});
}
fillSelectElement(regionSelector, options);
fillCities();
}
function fillCities() {
gRegion = regionSelector.value;
var list = gTZ[gRegion];
var options = [];
for (var i = 0; i < list.length; i++) {
options.push({
text: list[i].name || list[i].city.replace(/_/g, ' '),
value: i,
selected: (list[i].city === gCity)
});
}
fillSelectElement(citySelector, options);
setTimezone();
}
function setTimezone() {
onchangeTZ(getTZInfo(), {
'changedByUser': source === TIMEZONE_USER,
'needsConfirmation': source !== TIMEZONE_USER &&
source !== TIMEZONE_NETWORK});
}
function getTZInfo() {
var res = gTZ[gRegion][citySelector.value];
gCity = res.city;
var offset = res.offset.split(',');
return {
id: res.id || gRegion + '/' + res.city,
region: getSelectedText(regionSelector),
city: getSelectedText(citySelector),
cc: res.cc,
utcOffset: offset[0],
dstOffset: offset[1]
};
}
regionSelector.onchange = function() {
source = TIMEZONE_USER;
fillCities();
};
citySelector.onchange = function() {
source = TIMEZONE_USER;
setTimezone();
};
loadJSON(TIMEZONE_FILE, function loadTZ(response) {
gTZ = response;
fillRegions();
});
}
/**
* Monitor time.timezone changes
*/
function newTZObserver() {
var settings = window.navigator.mozSettings;
if (!settings) {
return;
}
settings.addObserver('time.timezone', function(event) {
setTimezoneDescription(event.settingValue);
});
function initSelector(initialValue, source) {
// initialize the timezone selector with the initial TZ setting
newTZSelector(function updateTZ(tz, options) {
var req = settings.createLock().set({'time.timezone': tz.id});
req.onsuccess = function updateTZ_callback() {
// Store the user manually selected timezone separately
if (options.changedByUser) {
settings.createLock().set({'time.timezone.user-selected': tz.id});
// Wait until the timezone is actually set
// before calling the callback.
onchange && window.addEventListener('moztimechange',
function timeChanged() {
window.removeEventListener('moztimechange', timeChanged);
onchange(tz, options.needsConfirmation);
});
} else {
onload && onload(tz, options.needsConfirmation);
}
};
}, initialValue, source);
}
var reqTimezone = settings.createLock().get('time.timezone');
reqTimezone.onsuccess = function dt_getStatusSuccess() {
// load the timezone the user manually selected last time,
// or get the default timezone for the current carrier
var reqUserTZ = settings.createLock().get('time.timezone.user-selected');
reqUserTZ.onsuccess = function dt_getUserTimezoneSuccess() {
var userSelTimezone = reqUserTZ.result['time.timezone.user-selected'];
if (userSelTimezone) {
initSelector(userSelTimezone, TIMEZONE_PREVIOUS_SETTING);
} else {
getDefaultTimezoneID(initSelector);
}
};
};
function setTimezoneDescription(timezoneID) {
regionSelector.value = timezoneID.replace(/\/.*/, '');
citySelector.value = timezoneID.replace(/.*?\//, '');
}
}
/**
* Startup -- make sure webL10n is ready before using tzSelect()
*/
newTZObserver();
}