-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_weather.html
145 lines (133 loc) · 5.61 KB
/
index_weather.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
<html>
<head>
<title>Smartbox</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js"></script>
</head>
<body>
<script>
function validateForm() {
var x=document.forms["myForm"]["q"].value;
console.log('query string=' + x);
if (x==null || x=="")
var error = "Empty query, Please enter a query.";
else if(/\d/.test(x)) {
var regUKPostcode = /^([a-zA-Z]){1}([0-9][0-9]|[0-9]|[a-zA-Z][0-9][a-zA-Z]|[a-zA-Z][0-9][0-9]|[a-zA-Z][0-9]){1}([ ])([0-9][a-zA-z][a-zA-z]){1}$/;
var regUSCANZipcode = /(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$)/;
var regLatLong = /^(-?(90|(\d|[1-8]\d)(\.\d{1,6}){0,1}))\,{1}(-?(180|(\d|\d\d|1[0-7]\d)(\.\d{1,6}){0,1}))$/;
var validIP = function(ip) {
var values = ip.split('.');
if(values.length != 4) return false;
for(var v in values)
if(values[v] < 0 || values[v] > 255) return false;
return true;
};
if(validIP(x)) {
updateDom(x);
return false;
} else if(regUSCANZipcode.test(x)) {
updateDom(x);
return false;
} else if(regUKPostcode.test(x)) {
updateDom(x);
return false;
} else if(regLatLong.test(x)) {
updateDom(x);
return false;
} else
error = "Please enter an valid US/UK/Canada Postal Code or IP or (Lat,Long) query.";
} else {
updateDom(x);
return false;
}
document.getElementById('status').innerHTML = error;
return false;
};
function updateDom(q) {
getLocalWeather(q, function(result) {
document.getElementById('weatherData').innerHTML = result;
document.getElementById('status').innerHTML = "";
});
return false;
}
function errorView(error) {
document.getElementById('status').innerHTML = error;
}
function resultView(jsonres) {
var template = '<img src="<%= icon %>" alt="Weather Condition" />\
<div><b><%= location %></b> </div>\
<div style="color:#07CFFC"><b><%= date %></b> </div>\
<div style="color:#1707FC"> <b><%= weatherDesc %></b> </div>\
<div style="color:#FC071B"><b><%= temp %>\u2103</b></div>\
<div>\
<li>Location: <b><%= location %></b></li>\
<li>Date: <b><%= date %></b></li>\
<li>Weather Condition: <b><%= weatherDesc %></b></li>\
<li>Temperature: <b><%= temp %>\u2103</b></li>\
<li>Max Temperature: <b><%= tempMaxC %>\u2103</b></li>\
<li>Min Temperature: <b><%= tempMinC %>\u2103</b></li>\
<li>Cloud Cover: <b><%= cloudcover %></b></li>\
<li>Humidity: <b><%= humidity %></b></li>\
<li>Observation Time: <b><%= observation_time %></b></li>\
<li>Precipitation: <b><%= precipMM %>MM</b></li>\
<li>Pressure: <b><%= pressure %></b></li>\
<li>Visibility: <b><%= visibility %></b></li>\
<li>Wind Direction 16Point: <b><%= winddir16Point %></b></li>\
<li>Wind Direction Degree: <b><%= winddirDegree %></b></li>\
<li>Wind Speed: <b><%= windspeed %>Kmph</b></li>\
</div>';
var vars =
{
location: jsonres.data.request[0].query,
date: jsonres.data.weather[0].date,
temp: jsonres.data.current_condition[0].temp_C,
weatherDesc: jsonres.data.current_condition[0].weatherDesc[0].value,
tempMaxC: jsonres.data.weather[0].tempMaxC,
tempMinC: jsonres.data.weather[0].tempMinC,
cloudcover: jsonres.data.current_condition[0].cloudcover,
humidity: jsonres.data.current_condition[0].humidity,
observation_time: jsonres.data.current_condition[0].observation_time,
precipMM: jsonres.data.current_condition[0].precipMM,
pressure: jsonres.data.current_condition[0].pressure,
visibility: jsonres.data.current_condition[0].visibility,
winddir16Point: jsonres.data.weather[0].winddir16Point,
winddirDegree: jsonres.data.weather[0].winddirDegree,
windspeed: jsonres.data.weather[0].windspeedKmph,
icon: jsonres.data.current_condition[0].weatherIconUrl[0].value,
};
return _.template(template, vars);
}
function getLocalWeather(q, onResult) {
var baseurl = "http://api.worldweatheronline.com/free/v1/weather.ashx";
//JSONP call
//Please note that 'key' here is only for demo purpose. You should replace
//it with your own key which can be obtained from registering on WWO website.
//Also you should put the key on server side on your own domain and get
//it using Ajax call when loading the parent html file, rather than put here
//at client side that everyone can see.
$.getJSON(baseurl + '?callback=?', {
q: q,
num_of_days: '1',
format: 'json',
key: 'xkq544hkar4m69qujdgujn7w'
})
.done(function( jsonres ) {
console.log('getJSON'+JSON.stringify(jsonres));
if(jsonres.data.hasOwnProperty('error'))
errorView(jsonres.data.error[0].msg);
else onResult(resultView(jsonres));
})
.fail(function( jqxhr, textStatus, error ) {
var err = textStatus + ', ' + error;
errorView("Request Failed: " + err);
});
}
</script>
<h1>Smartbox</h1>
<form name="myForm" onsubmit="return validateForm()">
City or town name; IP address; US or Canada Postal Code or US Zipcode; (Latitude,longitude): <input type="text" name="q">
<input type="submit" value="Submit">
</form>
<div style="color:#B20528"><span id="status"></span></div>
<div id="weatherData"></div>
</body></html>