-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
217 lines (178 loc) · 8.41 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<h1>Super Yahoo pipes</h1>
<style>
#errors-block {
color: red;
}
</style>
<script type="text/javascript">
// on error show error in error block
var OnScriptError = function () {
document.getElementById("errors-block").innerHTML = "Error during script generation. src:"
+ this.src;
};
// "clean" aererors blocl
var CleanError = function () {
document.getElementById("errors-block").innerHTML = "";
};
// questions 1
// 1 - checks - how much - what makes sense?
// 2 - pictures too big - is there a where to get small ones using some magic?
// 3 - iterating elements - how much functional style is better that declarative one?
// 4 - any comments regarding my code?
// answer1
// 1 - add script OnError handler
// 2 - change table to div
// 3 - main loop limit - amount of elements in response
// questions 2 Sep 14
// 1 - css - CDN or can put to dusk close to js?
// 2 - idea with $, $$, _, __ for my lib
// 3 - what is the best name for own lib
// 4 - whwn makes sense to write your own lib?
// 5 - Max - experience with libs?
// answer2
// 1 - templates, innerHTML
// -----------------------------------------------------------------------------------------
// receive news from yahoo server
// -----------------------------------------------------------------------------------------
window.onload = function () {
CleanError();
//Create a SCRIPT element.
var script = document.createElement("script");
//Set the Type.
script.type = "text/javascript";
//Set the source to the URL the JSON Service.
script.src = "http://pipes.yahoo.com/pipes/pipe.run?_id=e9a2e77dffb3205d035c4e311d77bbe6&_render=json&_callback=DisplayNews";
script.onerror = OnScriptError;
//Append the script element to the HEAD section.
document.getElementsByTagName("head")[0].appendChild(script);
};
// -----------------------------------------------------------------------------------------
// Display all received news on the page
// -----------------------------------------------------------------------------------------
function DisplayNews(response) {
var i;
////console.log(response);
for(i = 0; i < response.count; i++) {
CreateMainDivBlock(i);
FillArticle(i, response);
}
}
/*
Example of article block div element
<div id="article-0">
<div id="img-0"><img height="50" id="image-0" src=
"http://www.nasa.gov/sites/default/files/thumbnails/image/18964422099_a371167fa5_o.jpg"
width="50"></div>
<div id="title-0">
Flying Over An Aurora
</div>
<div id="summary-0">
NASA astronaut Scott Kelly (@StationCDRKelly) captured photographs
and video of auroras on June 22, 2015. Kelly wrote, "Yesterday's
aurora was an impressive show from 250 miles up. Good morning from
the International Space Station! ?#?YearInSpace?"
</div>20
<div id="lnk-0">
<a href="http://www.nasa.gov/image-feature/flying-over-an-aurora"
id="article-link-0">-></a>
</div>
</div>
*/
// -----------------------------------------------------------------------------------------
// fill news article from received data
// -----------------------------------------------------------------------------------------
var FillArticle = function (id, response) {
// Yep!!
// if( Object.prototype.toString.call( response.value.items ) === '[object Array]' ) {
// console.log( ' Array!' );
// }
/// http://www.nasa.gov/sites/default/files/thumbnails/image/18964422099_a371167fa5_o.jpg
/// image
document.getElementById("image-"+id).src = response.value.items[id].enclosure.url;
/// Flying Over An Aurora
/// title
document.getElementById("title-"+id).innerHTML = response.value.items[id].title;
/// NASA astronaut Scott Kelly (@StationCDRKelly) captured photographs and video of auroras on June 22, 2015. Kelly wrote, "Yesterday's aurora was an impressive show from 250 miles up. Good morning from the International Space Station! #YearInSpace"
// summary
document.getElementById("summary-"+id).innerHTML = response.value.items[id].description;
/// http://www.nasa.gov/image-feature/flying-over-an-aurora
/// article_link
document.getElementById("article-link-"+id).href = response.value.items[id].link;
}
// -----------------------------------------------------------------------------------------
// "main" div for article block
// -----------------------------------------------------------------------------------------
var CreateMainDivBlock = function(id) {
var div = document.createElement("div");
var parentId = "article-"+id;
var imgId,
aId;
div.id = parentId;
div.onclick = ProcessDivOnClick;
document.getElementById("news").appendChild(div);
imgId = CreateDivBlock(id, parentId, "img");
CreateImgBlock(id, imgId, "image");
CreateDivBlock(id, parentId, "title");
// hide summary at the begining
// TODO: put at separate function later
var summaryId = CreateDivBlock(id, parentId, "summary");
var summary = document.getElementById(summaryId);
summary.style.visibility = 'hidden';
aId = CreateDivBlock(id, parentId, "lnk");
CreateLnkBlock(id, aId, "article-link");
}
var ProcessDivOnClick = function(e) {
var elemId = e.srcElement.id;
// image-1
////console.log(elemId);
var res = elemId.split("-");
var clickedId = res[1];
console.log(clickedId);
///console.log("Clicked!!!");
var visible = document.getElementById("article-link-"+clickedId).style.visibility;
if(visible === 'visible') {
document.getElementById("article-link-"+clickedId).style.visibility = 'hidden';
document.getElementById("summary-"+clickedId).style.visibility = 'hidden';
} else {
document.getElementById("article-link-"+clickedId).style.visibility = 'visible';
document.getElementById("summary-"+clickedId).style.visibility = 'visible';
}
}
// div for an "elementName-<id>" block
var CreateDivBlock = function(id, parentId, elementName) {
var div = document.createElement("div");
var elementId = elementName+"-"+id;
div.id = elementId;
document.getElementById(parentId).appendChild(div);
return elementId;
}
// img block
var CreateImgBlock = function(id, parentId, elementName) {
var img = document.createElement("img");
var elementId = elementName+"-"+id;
img.id = elementId;
img.width = 50;
img.height = 50;
document.getElementById(parentId).appendChild(img);
}
// a href block
var CreateLnkBlock = function(id, parentId, elementName) {
var a = document.createElement("a");
var elementId = elementName+"-"+id;
a.id = elementId;
a.style.visibility = 'hidden';
a.innerHTML = "->";
document.getElementById(parentId).appendChild(a);
}
</script>
</head>
<body>
<div id="errors-block">
</div>
<div id="news">
</div>
</body>
</html>