-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathui5_openweathermap_app.html
396 lines (323 loc) · 10.3 KB
/
ui5_openweathermap_app.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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
<!DOCTYPE html>
<html>
<head>
<meta http-equiv='X-UA-Compatible' content='IE=edge' charset='utf-8' />
<title>Weather Forecast Using OpenWeatherMap API</title>
<!-- Load UI5, select gold reflection theme and the "commons" and "table" control libraries -->
<script id='sap-ui-bootstrap' type='text/javascript'
src='resources/sap-ui-core.js' data-sap-ui-theme='sap_platinum'
data-sap-ui-libs='sap.ui.commons,sap.ui.table'></script>
<script>
//Create a matrix layout with 2 columns
var oMatrixHead = new sap.ui.commons.layout.MatrixLayout({
layoutFixed : true,
width : '300px',
columns : 2
});
oMatrixHead.setWidths('100px', '200px');
//Create a header
var oCell = new sap.ui.commons.layout.MatrixLayoutCell({
colSpan : 3
});
oCell.addContent(new sap.ui.commons.TextView({
text : 'SAPUI5 App based on Open Weather Map JSON Search API',
design : sap.ui.commons.TextViewDesign.H1
}));
oMatrixHead.createRow(oCell);
//Create Matrix layout
var oLabel = new sap.ui.commons.Label({
text : 'City'
});
var oInputQuery = new sap.ui.commons.TextField({
value : '',
width : '100%',
required : true
});
oLabel.setLabelFor(oInputQuery);
oMatrixHead.createRow(oLabel, oInputQuery);
//Create a standard divider
var oCell = new sap.ui.commons.layout.MatrixLayoutCell({
colSpan : 2
});
oCell.addContent(new sap.ui.commons.HorizontalDivider());
oMatrixHead.createRow(oCell);
// Attach the layout to the page
oMatrixHead.placeAt("content");
// create matrix layout
var oMatrix = new sap.ui.commons.layout.MatrixLayout({
id : 'matrix',
layoutFixed : false,
columns : 3,
});
// create a JSONModel, fill in the data and bind the Table to this model
var oModel = new sap.ui.model.json.JSONModel();
// create button
var oButtonSearch = new sap.ui.commons.Button(
{
text : "Search",
tooltip : "Search",
press : function() {
var qString = oInputQuery.getValue();
//Validation for Query String
if (qString == '') {
alert('City cannot be blank');
return false;
}
// service url for Open Weather Map JSON API
var url = 'http://api.openweathermap.org/data/2.5/weather?q='
+ qString
+ '&mode=json&units=metric&callback=getJSON';
//Ajax Call with Callback function and JSONP data type
$
.ajax({
url : url,
jsonpCallback : 'getJSON',
contentType : "application/json",
dataType : 'jsonp',
success : function(data, textStatus, jqXHR) {
oModel.setData(data);
sap.ui.getCore().setModel(oModel);
oMatrix.setVisible(true);
//Lable and TextField for City
oLabelCity = new sap.ui.commons.Label({
id : 'L-City',
text : 'City'
});
oTFCity = new sap.ui.commons.TextField({
id : 'TF-City',
tooltip : 'City',
editable : false,
value : '{/name}',
width : '200px'
});
oLabelCity.setLabelFor(oTFCity);
oMatrix.createRow(oLabelCity, oTFCity);
//Lable and TextField for Country
oLabelCountry = new sap.ui.commons.Label({
id : 'L-Elev',
text : 'Country'
});
oTFCountry = new sap.ui.commons.TextField({
id : 'TF-Elev',
tooltip : 'Country',
editable : false,
value : '{/sys/country}',
width : '200px'
});
oLabelCountry.setLabelFor(oTFCountry);
oMatrix.createRow(oLabelCountry, oTFCountry);
//Lable and TextField for Latitute
oLabelLat = new sap.ui.commons.Label({
id : 'L-Lat',
text : 'Latitude'
});
oTFLat = new sap.ui.commons.TextField({
id : 'TF-Lat',
tooltip : 'Latitude',
editable : false,
value : '{/coord/lat}',
width : '200px'
});
oLabelLat.setLabelFor(oTFLat);
oMatrix.createRow(oLabelLat, oTFLat);
//Lable and TextField for longitude
oLabelLon = new sap.ui.commons.Label({
id : 'L-Lon',
text : 'Longitude'
});
oTFLon = new sap.ui.commons.TextField({
id : 'TF-Lon',
tooltip : 'Longitude',
editable : false,
value : '{/coord/lon}',
width : '200px'
});
oLabelLon.setLabelFor(oTFLon);
oMatrix.createRow(oLabelLon, oTFLon);
//Create a standard divider
var oCell = new sap.ui.commons.layout.MatrixLayoutCell(
{
colSpan : 3
});
oCell
.addContent(new sap.ui.commons.HorizontalDivider());
oMatrix.createRow(oCell);
var imageIcon = oModel
.getProperty("/weather/0/icon");
var imageIconsrc = 'http://openweathermap.org/img/w/'
+ imageIcon + '.png';
//Weather image
var oImageWeather = new sap.ui.commons.Image(
{
src : imageIconsrc,
alt : '{/weather/0/icon}'
});
//Lable and TextField for weather
oLabelWeather = new sap.ui.commons.Label({
id : 'L-Weather',
text : 'Weather'
});
oTFWeather = new sap.ui.commons.TextField({
id : 'TF-Weather',
tooltip : 'Weather',
editable : false,
value : '{/weather/0/description}',
width : '200px'
});
oLabelWeather.setLabelFor(oTFWeather);
oMatrix.createRow(oLabelWeather,
oTFWeather, oImageWeather);
//Create a standard divider
var oCell = new sap.ui.commons.layout.MatrixLayoutCell(
{
colSpan : 3
});
oCell
.addContent(new sap.ui.commons.HorizontalDivider());
oMatrix.createRow(oCell);
//Lable and TextField for temp_c
oLabelTemp = new sap.ui.commons.Label({
id : 'L-Temp',
text : 'Temperature'
});
var tempstring = oModel
.getProperty("/main/temp");
//Append Degree Celsius unit symbol to Temperature reading
var tempinC = tempstring + "\u2103";
oTFTemp = new sap.ui.commons.TextField({
id : 'TF-Temp',
tooltip : 'Temperature',
editable : false,
value : tempinC,
width : '220px'
});
oLabelTemp.setLabelFor(oTFTemp);
oMatrix.createRow(oLabelTemp, oTFTemp);
var date = oModel.getProperty("/dt");
var dt = new Date(date * 1000);
var hr = dt.getHours();
if (hr < 10)
hr = '0' + hr;
var mn = dt.getMinutes();
if (mn < 10)
mn = '0' + mn;
var mon = dt.getMonth() + 1;
if (mon < 10)
mon = '0' + mon;
var day = dt.getDate();
if (day < 10)
day = '0' + day;
var year = dt.getFullYear();
var obs_time = year + '.' + mon + '.' + day
+ ' ' + hr + ':' + mn;
//Lable and TextField for Obervation Time
oLabelObsTime = new sap.ui.commons.Label({
id : 'L-ObsTime',
text : 'Observation Time'
});
oTFObsTime = new sap.ui.commons.TextField({
id : 'TF-ObsTime',
tooltip : 'Observation Time',
editable : false,
value : obs_time,
width : '220px'
});
oLabelObsTime.setLabelFor(oTFObsTime);
oMatrix
.createRow(oLabelObsTime,
oTFObsTime);
var sunrise = oModel
.getProperty("/sys/sunrise");
var dt_sr = new Date(sunrise * 1000);
var hr_sr = dt_sr.getHours();
if (hr_sr < 10)
hr_sr = '0' + hr_sr;
var mn_sr = dt_sr.getMinutes();
if (mn_sr < 10)
mn_sr = '0' + mn;
var sunrise_time = (hr_sr + ':' + mn_sr + ' AM');
//Lable and TextField for Sunrise
oLabelLclTime = new sap.ui.commons.Label({
id : 'L-LclTime',
text : 'Sunrise'
});
oTFLclTime = new sap.ui.commons.TextField({
id : 'TF-LclTime',
tooltip : 'Sunrise',
editable : false,
value : sunrise_time,
width : '220px'
});
oLabelLclTime.setLabelFor(oTFLclTime);
oMatrix
.createRow(oLabelLclTime,
oTFLclTime);
var sunset = oModel
.getProperty("/sys/sunset");
var dt = new Date(sunset * 1000);
var hr = dt.getHours();
if (hr < 10)
hr = '0' + hr;
var mn = dt.getMinutes();
if (mn < 10)
mn = '0' + mn;
var sunset_time = (hr + ':' + mn + ' PM');
//Lable and TextField for Sunset
oLabelSunsetTime = new sap.ui.commons.Label(
{
id : 'L-SunsetTime',
text : 'Sunset'
});
oTFSunsetTime = new sap.ui.commons.TextField(
{
id : 'TF-SunsetTime',
tooltip : 'Sunset',
editable : false,
value : sunset_time,
width : '220px'
});
oLabelSunsetTime.setLabelFor(oTFSunsetTime);
oMatrix.createRow(oLabelSunsetTime,
oTFSunsetTime);
//Lable and TextField for relative humidity
oLabelRelHum = new sap.ui.commons.Label({
id : 'L-RelHum',
text : 'Humidity'
});
oTFRelHum = new sap.ui.commons.TextField({
id : 'TF-RelHum',
tooltip : 'Humidity',
editable : false,
value : '{/main/humidity}',
width : '220px'
});
oLabelRelHum.setLabelFor(oTFRelHum);
oMatrix.createRow(oLabelRelHum, oTFRelHum);
//Lable and TextField for Wind
oLabelWind = new sap.ui.commons.Label({
id : 'L-Wind',
text : 'Wind'
});
oTFWind = new sap.ui.commons.TextField({
id : 'TF-Wind',
tooltip : 'Wind',
editable : false,
value : '{/wind/speed}',
width : '220px'
});
oLabelWind.setLabelFor(oTFWind);
oMatrix.createRow(oLabelWind, oTFWind);
//attach it to some element in the page
oMatrix.placeAt('content');
}
});
}
});
// attach it to some element in the page
oButtonSearch.placeAt("content");
</script>
</head>
<body class='sapUiBody'>
<div id='content'></div>
</body>
</html>