-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
261 lines (241 loc) · 10.1 KB
/
script.js
File metadata and controls
261 lines (241 loc) · 10.1 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
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
256
257
258
259
260
261
$(document).on('pagecreate','#homepage',function(){
var weekday = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var minmumtemp = new Array();
var maximumtemp = new Array();
var chartLabels = new Array();
var recentCities = new Array();
var dateRecieved;
var acceptedFormat;
var separate;
var latitude;
var longitude;
var url;
$('#input').val('');
$('#myChart').hide();
load();
//load function populates the predefined cities in the select element
function load()
{
currentLocation();
refreshRecentSearch();
$.getJSON('pre-defined-cities.json')
.done(function(data){
$('#famouscities').empty();
$.each(data.cities,function(index,name){
var citiesTemplate = $('#CitiesTemp').html();
var generateCityHtml = Handlebars.compile(citiesTemplate);
var data = {
"cityValue":name,
"cityNum":index+1,
"cityName":name
};
console.log(generateCityHtml(data));
var html = generateCityHtml(data);
$("#famouscities").append(html);
//$('<option value="'+ name +'"></option>').append((index+1) +'. '+name).appendTo('#famouscities');
});
});
}
//refreshes the recent searches group in select
function refreshRecentSearch(){
if(localStorage.recentSearch){
recentCities = JSON.parse(localStorage.recentSearch);
$('#recentsearches').empty();
$.each(recentCities,function(index,city){
$('<option value="'+ city +'"></option>').append((index+1) +'. '+city).appendTo('#recentsearches');
});
}
}
function currentLocation(){
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(showPosition, showError);
}
}
function showPosition(position)
{
latitude = position.coords.latitude;
longitude = position.coords.longitude;
url = 'http://api.apixu.com/v1/forecast.json?key=2b0d6ec6d55e48efa49225215171208&q=' + latitude + '%2C' + longitude + '&days=7';
currentWeatherData(url);
}
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
$('<li></li>').append('<h1>User denied the request for Geolocation.</h1>').appendTo('#currentWeather');
$('#currentWeather').listview('refresh');
break;
case error.POSITION_UNAVAILABLE:
$('<li></li>').append('<h1>Location information is unavailable.</h1>').appendTo('#currentWeather');
$('#currentWeather').listview('refresh');
break;
case error.TIMEOUT:
$('<li></li>').append('<h1>The request to get user location timed out.</h1>').appendTo('#currentWeather');
$('#currentWeather').listview('refresh');
break;
case error.UNKNOWN_ERROR:
$('<li></li>').append('<h1>An unknown error occurred.</h1>').appendTo('#currentWeather');
$('#currentWeather').listview('refresh');
break;
}
};
//gets the data for the current location or searched or selected location and updates the content
function currentWeatherData(url){
var dataFill = $('#currentWeather');
var todayBrief = $('#todayWeatherBrief');
var todayInfo = $('#todayWeatherInfo');
$(todayBrief).empty();
$(todayInfo).empty();
$('#day').text("");
$(dataFill).empty();
minmumtemp = [];
maximumtemp = [];
chartLabels = [];
$.getJSON(url)
.done(function(currentData){
dateRecieved = currentData.forecast.forecastday["0"].date;
separate = dateRecieved.split('-');
acceptedFormat = new Date((separate[1] + '/' + separate[2] + '/' + separate[0]));
$('#day').append('<h3 align="center">'+weekday[acceptedFormat.getDay()]+'</h3>');
$('<li style="height: 160px;"></li>').append('<h1 align="center">'+ currentData.location.name +'</h1> <p align="center">' + currentData.current.condition.text
+ '</p> <h1 align="center">' + currentData.current.temp_c + '<i class="wi wi-celsius"></i><br/><img src="http:' + currentData.current.condition.icon
+ '" /></h1>').appendTo(todayBrief);
$.each(currentData.forecast.forecastday,function(index,object){
dateRecieved = currentData.forecast.forecastday[index].date;
separate = dateRecieved.split('-');
acceptedFormat = new Date((separate[1] + '/' + separate[2] + '/' + separate[0]));
$('<li></li>').append('<h3 align="center">' + weekday[acceptedFormat.getDay()] + ' | '
+ currentData.forecast.forecastday[index].day.condition.text + ' - [' + currentData.forecast.forecastday[index].day.mintemp_c +
' | '+ currentData.forecast.forecastday[index].day.maxtemp_c + '] <img src="http:'
+ currentData.forecast.forecastday[index].day.condition.icon + '" /> ' + '</h3>').appendTo(dataFill);
minmumtemp.push(currentData.forecast.forecastday[index].day.mintemp_c);
maximumtemp.push(currentData.forecast.forecastday[index].day.maxtemp_c);
chartLabels.push(weekday[acceptedFormat.getDay()]);
});
$('<li style="height: 160px;"></li>').append('<h3 align="center">Today: ' + currentData.current.condition.text + ' currently. It is ' + currentData.current.temp_c
+ '<i class="wi wi-celsius"></i>; the high will be ' + currentData.forecast.forecastday["0"].day.maxtemp_c
+ '<i class="wi wi-celsius"></i>.</h3><h3 style="margin-left: 22%;"><span style="display:inline-block; width: 90px; text-align: right; margin-right: 15px">Sunrise: </span>'+ currentData.forecast.forecastday["0"].astro.sunrise + ' <i class="wi wi-sunrise"></i><br/><span style="display:inline-block; width: 90px; text-align: right; margin-right: 15px"> Sunset: </span>'
+ currentData.forecast.forecastday["0"].astro.sunset + ' <i class="wi wi-sunset"></i><br/><span style="display:inline-block; width: 90px; text-align: right; margin-right: 15px">Humidity: </span>' + currentData.current.humidity + '% <i class="wi wi-humidity"></i><br/><span style="display:inline-block; width: 90px; text-align: right; margin-right: 15px"> Wind: </span>'
+ currentData.current.wind_dir + ' ' + currentData.current.wind_kph + 'Km/hr <i class="wi wi-windy"></i><br/><span style="display:inline-block; width: 90px; text-align: right; margin-right: 15px"> Feels Like: </span>'
+ currentData.current.feelslike_c + '<i class="wi wi-celsius"></i><br/>'+'</h3>').appendTo(todayInfo);
$(todayInfo).listview('refresh');
$(todayBrief).listview('refresh');
$(dataFill).listview('refresh');
chartMaker();
})
.fail(function(){
$('<li></li>').append('<h1>APIXU API is not responding!! Or You could have misspelt city name!!</h1>').appendTo(dataFill);
$(dataFill).listview('refresh');
$('#myChart').hide();
});
};
//send the city name to the api from search box
$(document).on('click','#submit',function(event){
event.preventDefault();
var val = $.trim($('#input').val());
$('#input').val('');
$('#myChart').hide();
if(val.length>0){
recentCities = [];
if(localStorage.recentSearch){
recentCities = JSON.parse(localStorage.recentSearch);
}
recentCities.push(val);
localStorage.recentSearch = JSON.stringify(recentCities);
val = val.replace(' ', '+');
url = "http://api.apixu.com/v1/forecast.json?key=2b0d6ec6d55e48efa49225215171208&q="+ val +"&days=7";
currentWeatherData(url);
refreshRecentSearch();
}
});
//send the city name to the weather api from selection control
$(document).on('click','option',function(event){
event.preventDefault();
console.log("clicked");
$('#myChart').hide();
var val = $(this).attr('value');
url = "http://api.apixu.com/v1/forecast.json?key=2b0d6ec6d55e48efa49225215171208&q="+ val +"&days=7";
recentCities = [];
if(localStorage.recentSearch){
recentCities = JSON.parse(localStorage.recentSearch);
}
recentCities.push(val);
localStorage.recentSearch = JSON.stringify(recentCities);
currentWeatherData(url);
refreshRecentSearch();
});
//creates the chart with the available data for 7 days
function chartMaker(){
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: chartLabels,
datasets: [{
label: 'Minimum temperature',
data: minmumtemp,
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)',
'rgba(204, 230, 255, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)',
'rgba(0, 64, 128, 1)'
],
borderWidth: 1
},
{
label: 'Maximum temperature',
data: maximumtemp,
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)',
'rgba(204, 230, 255, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)',
'rgba(0, 64, 128, 1)'
],
borderWidth: 1
}
]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
$('#myChart').show();
};
//change the page to homepage
$("#goHome").on('click', function(){
$.mobile.changePage("#homepage");
});
//change the page to chart page
$("#goChart").on('click', function(){
$.mobile.changePage("#chart");
});
});