-
Notifications
You must be signed in to change notification settings - Fork 0
/
autocomplete.html
198 lines (173 loc) · 5 KB
/
autocomplete.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Find the time in any city in the world quickly and easily. Compare times in different cities. Find the weather for any city in the world.">
<title>Current Time by City</title>
</head>
<body>
<style>
html {
box-sizing: border-box;
background: rgba(216, 154, 135,.3);
font-family:'helvetica neue';
font-size: 20px;
font-weight: 200;
}
*, *:before, *:after {
box-sizing: inherit;
}
input {
width: 100%;
padding:20px;
}
.search-form {
max-width:400px;
margin:50px auto;
}
input.search {
margin: 0;
text-align: center;
outline:0;
border: 10px solid #F7F7F7;
width: 120%;
left: -10%;
position: relative;
top: 10px;
z-index: 2;
border-radius: 5px;
font-size: 40px;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.12), inset 0 0 2px rgba(0, 0, 0, 0.19);
}
.suggestions {
margin: 0;
padding: 0;
position: relative;
/*perspective:20px;*/
}
.suggestions li {
background:white;
list-style: none;
border-bottom: 1px solid #D8D8D8;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.14);
margin:0;
padding:20px;
transition:background 0.2s;
display:flex;
justify-content:space-between;
text-transform: capitalize;
}
.suggestions li:nth-child(even) {
transform: perspective(100px) rotateX(3deg) translateY(2px) scale(1.001);
background: linear-gradient(to bottom, #ffffff 0%,#EFEFEF 100%);
}
.suggestions li:nth-child(odd) {
transform: perspective(100px) rotateX(-3deg) translateY(3px);
background: linear-gradient(to top, #ffffff 0%,#EFEFEF 100%);
}
span.population {
font-size: 15px;
}
.hl {
background:#ffc600;
}
a {
color:black;
background:rgba(0,0,0,0.1);
text-decoration: none;
}
</style>
<!-- end styling -->
<form class="search-form">
<input type="text" class="search" placeholder="search cities">
<ul class="suggestions">
<li><p style="margin:auto">👀 👀 👀</p></li>
</ul>
</form>
<script>
function findMatches() {
var s = document.createElement("script");
s.type = "text/javascript";
s.src = "https://autocomplete.wunderground.com/aq?query=" + this.value + "&cb=cityList";
s.innerHTML = null;
s.id = "location_src";
document.getElementById("location_list").innerHTML = "";
document.getElementById("location_list").appendChild(s);
}
let zoneList = [];
const startTime = Date.now(); // save time at page load
let timeHour = '';
function getTimes(data) {
let timeZones = data.zones.map(i => {
let zone = {
timeZone: i.zoneName,
timestampUTC: (i.timestamp * 1000), // convert sec to millisec
};
return zone;
})
zoneList = timeZones;
}
function calcTime(zone) {
// control for time elapsed since timezone timestamps were loaded
const elapsed = Date.now() - startTime;
const currentTime = zone.timestampUTC + elapsed;
const date = new Date(currentTime);
let hour = date.getUTCHours();
timeHour = hour; // set global timeHour var for icon creation
if (hour === 0) { hour = 12 } // control for midnight
hour = hour > 12 ?
(hour-12).toString() : hour.toString()
let minute = date.getUTCMinutes();
minute = minute < 10 ?
`0${minute}` : minute.toString()
return `${hour}:${minute}`
}
function getZoneMatch(place) {
const zoneIndex = zoneList.findIndex((elem, i, arr) => {
return elem.timeZone === place.tz;
})
let timeString = zoneIndex > 0 ? calcTime(zoneList[zoneIndex]) : 'Not Found'
return timeString;
}
// use diff emoji based on rough time of day
function timeIcon() {
if (timeHour > 20) { return 'PM 🌚'}
if (timeHour > 12) { return 'PM 🌞'}
if (timeHour > 6) { return 'AM 🌥'}
return 'AM 🌜'
}
function displayMatches(matches) {
let html = matches.map(place => {
const time = getZoneMatch(place);
const icon = timeIcon();
return `
<li>
<span class="name"><a href="http://www.wunderground.com${place.l}">${place.name}</a></span>
<span class="population">${time} ${icon}</span>
</li>
`
}).join("");
// set li display item when user clears input field
if (document.querySelector('.search').value.length < 1) {
html = "<li><span>Search for Stuff!</span></li>"
}
suggestions.innerHTML = html;
}
const searchInput = document.querySelector('.search');
const suggestions = document.querySelector('.suggestions');
function cityList(data) {
// only return results for locations of type 'city'
let matches = data.RESULTS;
const cityMatches = matches.filter((location) => {
return location.type === "city";
});
displayMatches(cityMatches);
}
searchInput.addEventListener('change', findMatches);
searchInput.addEventListener('keyup', findMatches);
</script>
<div id="location_list"></div>
<script src="http://api.timezonedb.com/v2/list-time-zone?key=PB75OXRDI2QG&format=json&callback=getTimes"></script>
</body>
</html>