Repository navigation
Expand file tree
/
Copy pathtz_select.js
More file actions
230 lines (197 loc) · 6.51 KB
/
Copy pathtz_select.js
File metadata and controls
230 lines (197 loc) · 6.51 KB
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
/* -*- Mode: js; js-indent-level: 2; indent-tabs-mode: nil -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
'use strict';
function tzSelect(regionSelector, citySelector, onchange, onload) {
var TIMEZONE_FILE = '/shared/resources/tz.json';
var APN_TZ_FILE = '/shared/resources/apn_tz.json';
function loadJSON(href, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', href, true);
xhr.responseType = 'json';
xhr.>
xhr. {
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';
// 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.network.connected) {
mcc = conn.voice.network.mcc;
mnc = conn.voice.network.mnc;
} else if (IccHelper.iccInfo) {
mcc = IccHelper.iccInfo.mcc;
mnc = IccHelper.iccInfo.mnc;
}
}
if (!mcc) {
callback(tzDefault);
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') {
tzDefault = tz;
} else if (tz && (mnc in tz)) {
tzDefault = tz[mnc];
}
}
callback(tzDefault);
});
}
/**
* Activate a timezone selector UI
*/
function newTZSelector(onchangeTZ, currentID, alreadyDefined) {
var gRegion = currentID.replace(/\/.*/, '');
var gCity = currentID.replace(/.*?\//, '');
var gTZ = null;
var loaded = false;
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);
if (loaded) {
setTimezone();
} else {
if (onload) {
onload(getTZInfo());
}
loaded = true;
}
}
function setTimezone() {
onchangeTZ(getTZInfo());
}
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.>
citySelector.>
loadJSON(TIMEZONE_FILE, function loadTZ(response) {
gTZ = response;
fillRegions();
if (!alreadyDefined) { // no timezone defined: `currentID' is a new value
setTimezone();
}
});
}
/**
* 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, alreadyDefined) {
// initialize the timezone selector with the initial TZ setting
newTZSelector(function updateTZ(tz) {
var req = settings.createLock().set({'time.timezone': tz.id});
if (onchange) {
req. updateTZ_callback() {
// Store the user manually selected timezone separately
settings.createLock().set({'time.timezone.user-selected': tz.id});
// Wait until the timezone is actually set
// before calling the callback.
window.addEventListener('moztimechange', function timeChanged() {
window.removeEventListener('moztimechange', timeChanged);
onchange(tz);
});
};
}
}, initialValue, alreadyDefined);
}
var reqTimezone = settings.createLock().get('time.timezone');
reqTimezone. 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. dt_getUserTimezoneSuccess() {
var userSelTimezone = reqUserTZ.result['time.timezone.user-selected'];
if (userSelTimezone) {
initSelector(userSelTimezone, true);
} else {
getDefaultTimezoneID(initSelector);
}
};
};
function setTimezoneDescription(timezoneID) {
regionSelector.value = timezoneID.replace(/\/.*/, '');
citySelector.value = timezoneID.replace(/.*?\//, '');
}
}
/**
* Startup -- make sure webL10n is ready before using tzSelect()
*/
newTZObserver();
}
You can’t perform that action at this time.