-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscript.js
More file actions
295 lines (274 loc) · 9.56 KB
/
script.js
File metadata and controls
295 lines (274 loc) · 9.56 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
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
// Note: some RSS feeds can't be loaded in the browser due to CORS security.
// To get around this, you can use a proxy.
CORS_PROXY = 'https://cody-cors.herokuapp.com/'; //"https://cors-anywhere.herokuapp.com/";
parser = new RSSParser({
defaultRSS: 2.0,
customFields: {
item: [
['media:content', 'media:content'],
['media:group', 'media:content', {keepArray: false}],
['media:thumbnail', 'media:content'],
['content:encoded', 'content:encoded'],
['title_detail', 'title_detail']
]
},
header: {'pragma': 'no-cache',
'cache-control': 'no-cache'}
});
/* // Example source format expected by main function
sources = [
['NY Times','http://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml'],
['NPR','https://www.npr.org/rss/rss.php?id=1019'],
['PBS','https://www.pbs.org/newshour/feeds/rss/headlines'],
['BBC','http://feeds.bbci.co.uk/news/rss.xml'],
['NY Times','http://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml'],
['NPR','https://www.npr.org/rss/rss.php?id=1019'],
['PBS','https://www.pbs.org/newshour/feeds/rss/headlines'],
['BBC','http://feeds.bbci.co.uk/news/rss.xml']
];
*/
// Function for extracting image information from parsed feed json. All sources
// seem to include image information differently. This function aims to handle
// all cases with a single operation, but still some sources require their own
// special handlers.
function addImage(item, article){
var img = document.createElement('img');
var onErrorScript = null;
// Necessary to reduce object to string, then perform a search for an image
// URL because all sources seem to include images differently. This is a
// compromise between createing special code for each source and
var regex = /(https?:\/\/((?!\").)*\"{0}\.(?:png|jpg|gif))/ig;
var src = JSON.stringify(item).match(regex);
//console.log(src);
if(src != null){
src = src[0];
// By defaulft, NYTimes images are very small and square.
// This will make them look more like images provided by other sources.
if(src.includes('nyt.com/images/') && src.includes('-moth.jpg')){
src = src.replace('-moth.jpg', '-largeHorizontalJumbo.jpg');
onErrorScript = function(){
if(this.src.includes('-largeHorizontalJumbo.jpg')){
console.log('facebook');
this.setAttribute('data', 'moth');
this.src = this.src.replace('-largeHorizontalJumbo.jpg', '-facebookLarge.jpg');
}
if(this.src.includes('-facebookLarge.jpg') && this.getAttribute('data') == 'moth'){
console.log('moth');
this.setAttribute('data', 'remove');
this.src = this.src.replace('-facebookLarge.jpg', '-moth.jpg');
}
};
// threeByTwoLargeAt2X-v2.jpg
//static01.nyt.com/images/2019/03/28/us/AFFIRMATIVE-photos-slide-0CY5/AFFIRMATIVE-photos-slide-0CY5-threeByTwoLargeAt2X-v2.jpg?quality=75&auto=webp&disable=upscale&width=1620
}
// NPR: For articles that do not include an image, NPR inserts a 1x1px image
// for tracking purposes. I'm fine with this, I want NPR to know I'm using
// their RSS feed so that they keep supporting it, but I don't want it to
// cause display problems like it is so I'm setting them to
// display: none here.
if(src == 'https://media.npr.org/include/images/tracking/npr-rss-pixel.png'){
img.setAttribute('style', 'display:none');
}
img.setAttribute('src', src);
img.onerror = onErrorScript;
article.appendChild(img);
}
}
function fetchSource(column, source){
// CORS_PROXY +
var d = new Date();
var feed = parser.parseURL(CORS_PROXY + source, function(err, feed) {
console.log(feed);
for(var i=0; i<10; i++){
// Create Article
var article = document.createElement('a');
article.setAttribute('href', feed.items[i].link);
// Append Title
var headline = document.createElement('h1');
headline.innerHTML = unescape(feed.items[i].title);
article.appendChild(headline);
// Append Image
addImage(feed.items[i], article);
// Append Snippet
if(feed.items[i].contentSnippet){
var snippet = document.createElement('p');
snippet.innerHTML = unescape(feed.items[i].contentSnippet.slice(0,280));
article.appendChild(snippet);
}
// Append Date
article.classList.add('article');
column.appendChild(article);
}
});
return column
}
if(window.location.hash) {
var page = decodeURIComponent(window.location.hash.replace('#',''));
console.log(page);
} else {
var page = 'Headlines';
}
document.getElementById('title').innerHTML = '<h1>News: '+ page +'</h1>';
// Main function: for parsing feeds and populating #content with thier contents
var pageSources = sources[page];
var k = Object.keys(pageSources);
//var sources = sources[k[]]
var content = document.getElementById('content');
for(j=0; j<k.length; j++){
// make column
console.log('makeCol');
var column = document.createElement('div');
column.classList.add('column');
var div = document.createElement('div');
var org = document.createTextNode(k[j]);
div.appendChild(org);
div.classList.add('org');
column.appendChild(div);
for (m=0; m<pageSources[k[j]].length; m++){
// append articles
console.log(pageSources[k[j]][m]);
content.appendChild(fetchSource(column, pageSources[k[j]][m]));
}
}
// Create menu
window.addEventListener('hashchange', function(){window.location.reload()});
function makeMenu(sources){
var menu = document.getElementById('menu');
var topics = Object.keys(sources);
//console.log(sources);
for(var i=0; i<topics.length; i++){
var topic = document.createElement('a');
topic.innerText = topics[i];
topic.href = '/#'+encodeURIComponent(topics[i]);
//topic.onclick = function(){window.location = self.href;};
menu.appendChild(topic);
}
}
makeMenu(sources);
// Sets date at top of page
function setDate(){
var d = new Date();
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var dateNode = document.getElementById('date');
var date = document.createTextNode(days[d.getDay()] + ', ' + months[d.getMonth()] +' '+ d.getDate().toString() +', '+ d.getFullYear().toString());
dateNode.appendChild(date);
}
setDate();
// Display: block; / Display: none; toggle
function toggle(id){
var elem = document.querySelector(id);
var state = window.getComputedStyle(elem).display;
if(state != 'none'){
elem.style.display = 'none';
}
else{
elem.style.display = 'block';
}
};
window.addEventListener('resize', function(){
// Display menu appropriately when screen width changes no matter current
// menu display state.
var menu = document.getElementById('menu');
if(window.innerWidth > 1000 && menu.style.display != 'block'){
menu.style.display = 'block';
}
if(window.innerWidth <= 1000 && menu.style.display == 'block'){
menu.style.display = 'none';
}
// Set up breakpoints. cols is number of columns to display in addition to reference or 0th column
width = window.innerWidth;
if(width > 1000){
ref=0;
cols = columns.length; // Desktop
}
if(750 < width && width <= 1000){
cols = 1; // Tablet
}
if(width <= 750){
cols = 0; // Phone
}
for(var k=0; k<columns.length; k++){
if(ref<=k && k<=(ref+cols)){
columns[k].style.display = 'block';
}
else{
columns[k].style.display = 'none';
}
}
console.log('resize');
});
// Get a reference to an element.
content = document.querySelector('#content');
// Create an instance of Hammer with the reference.
hammer = new Hammer(content);
cols = 0;
columns = document.querySelectorAll('.column');
debounce = true; // as in allow pan to occur
// Left-most column to display
ref = 0;
width = window.innerWidth;
// Subscribe to a quick start event: press, tap, or doubletap.
// For a full list of quick start events, read the documentation.
hammer.on('panmove panstart', function(e) {
// Set up breakpoints. cols is number of columns to display in addition to reference or 0th column
if(width > 1000){
ref = 0;
cols = columns.length; // Desktop
}
if(750 < width && width <= 1000){
cols = 1; // Tablet
}
if(width <= 750){
cols = 0; // Phone
}
if(Math.abs(e.deltaX/e.deltaY) > 10 && e.distance/width > .1 && debounce){
debounce = false; // debounce // as in do not allow pan to occur
// pan left
if(e.deltaX > 0){
if(ref > 0){
ref = ref - 1;
}
}
// pan right
if(e.deltaX < 0){
if((ref+cols)<columns.length-1){
ref = ref + 1;
}
}
for(var k=0; k<columns.length; k++){
if(ref<=k && k<=(ref+cols)){
columns[k].style.display = 'block';
}
else{
columns[k].style.display = 'none';
}
}
content.scrollTo(scrollX,0); // reset scroll position to top of page on pan
setTimeout(function(){
debounce = true; // turn off debounce // allow pan to occur
},500);
}
console.log(e.deltaX/e.deltaY, e.distance/width, ref, cols, debounce);
});
// Set up breakpoints. cols is number of columns to display in addition to reference or 0th column
width = window.innerWidth;
if(width > 1000){
ref=0;
cols = columns.length; // Desktop
}
if(750 < width && width <= 1000){
cols = 1; // Tablet
}
if(width <= 750){
cols = 0; // Phone
}
for(var k=0; k<columns.length; k++){
if(ref<=k && k<=(ref+cols)){
columns[k].style.display = 'block';
}
else{
columns[k].style.display = 'none';
}
}
console.log('load');