-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataService.js
114 lines (100 loc) · 3.4 KB
/
dataService.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
var gmaps = (function() {
var global = {};
/// Utility Function for getting XML Requests
global.getURL = function(url) {
return new Promise(function(resolve, reject){
var xhttp = new XMLHttpRequest();
xhttp.open("GET", url, true);
xhttp.onload = function(){
if(xhttp.status == 200) {
resolve(xhttp.response)
} else{
reject(xhttp.statusText)
}
};
xhttp.onerror = function(){
reject(xhttp.statusText);
};
xhttp.send();
});
}
/// Generate google Autocomplete for Timezone Search Box
var options = {};
function gmapOption() {
try {
var defaultBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(-90, -180),
new google.maps.LatLng(90, 180)
);
options = {
bounds: defaultBounds,
types: ['(regions)']
};
} catch (e) {
console.log(e);
}
}
gmapOption();
// Update Dst and GMT
global.getZone = function(latLng, index) {
var timestamp = Math.floor(Date.now()/1000)
var zoneRequestURL = "https://maps.googleapis.com/maps/api/timezone/json?location="+latLng+"×tamp="+timestamp+"&key=AIzaSyDIepuNMvaVLhkR7ezNCTR_eZuYKuLtG9U"
var timeZone = global.getURL(zoneRequestURL);
return timeZone;
}
global.addAutoCompletes = function(index) {
nthChild = index+1;
var input = document.querySelector('.clock:nth-child('+nthChild+') .local');
/// Init of New Autocomplete attached to the last added clock
var autocomplete = new google.maps.places.Autocomplete(input, options);
/// Attaches an event listener to get and apply the search results.
if(autocomplete) {
autocomplete.addListener('place_changed', function() {
var place = autocomplete.getPlace();
if (typeof place.address_components == 'undefined') {
queryAutocomplete(place.name, index);
} else if (place.geometry) {
passToClockulous(place, index)
} else {
clockulous.message("Whoops, Autocomplete failed. Check your connection?", 'error');
}
// Save Name
});
} else {
// we were not able to init an autocomplete object.
clockulous.message("Whoops, Autocomplete failed. Check your connection?", 'error');
};
return autocomplete;
};
function queryAutocomplete(input, index) {
options.input = input;
var service = new google.maps.places.AutocompleteService();
service.getPlacePredictions(options, autoCallback.bind(index));
};
function autoCallback(predictions, status) {
if (status != google.maps.places.PlacesServiceStatus.OK) {
clockulous.message("Whoops, Autocomplete Service failed. Check your connection?", 'error');
return;
} else if (predictions) {
service = new google.maps.places.PlacesService(document.createElement('div'));
service.getDetails( {reference: predictions[0].reference }, queryLocations.bind(index) );
} else {
clockulous.message("Whoops, Autocomplete Service failed you. Check your connection?", 'error');
}
};
function queryLocations(details, status) {
if (details.geometry) {
passToClockulous(details, index)
} else {
clockulous.message("Whoops, Autocomplete failed. Check your connection?", 'error');
}
};
function passToClockulous(place, index) {
/// Request new Google TimezoneId
var latLng = place.geometry.location.lat() + "," + place.geometry.location.lng();
clockulous.editLatLngGmaps(latLng, index);
clockulous.updateZoneData(index);
clockulous.saveLocal(place.name, index);
}
return global;
})();