-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadFeed.js
132 lines (103 loc) · 4.76 KB
/
loadFeed.js
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
/* LOAD A FEED AND CONVERT TO HTML */
function loadFeeds(feedData) {
/* exit quietly if no feed data / url is supplied */
if ((!feedData && feedData.feedUrl)) return false;
var feedUrl = feedData.feedUrl;
var defaultImageUrl = feedData.defaultImageUrl;
var defaultImageWidth = (feedData.defaultImageWidth) ? feedData.defaultImageWidth : 153;
var defaultImageHeight = (feedData.defaultImageHeight) ? feedData.defaultImageHeight : 102;
var defaultFeedWidth = (feedData.defaultFeedWidth) ? feedData.defaultFeedWidth : 406;
var defaultTextColumnWidth = (feedData.defaultTextColumnWidth) ? feedData.defaultTextColumnWidth : 233;
var feedContainerID = (feedData.feedContainerID) ? feedData.feedContainerID : '';
var defaultLinkToSource = (feedData.defaultLinkToSource) ? feedData.defaultLinkToSource : '';
var defaultSourceLinkText = (feedData.defaultSourceLinkText) ? feedData.defaultSourceLinkText : '';
var feed = new google.feeds.Feed(feedUrl);
feed.setNumEntries(10);
feed.load(function(result) {
if (!result.error) {
/* read feed source / name */
if (result.feed){
channelLink = (result.feed.link) ? result.feed.link : '';
channelDescription = (result.feed.description) ? result.feed.description : '';
channelTitle = (result.feed.title) ? result.feed.title : '';
linkToMore = (defaultLinkToSource) ? defaultLinkToSource : channelLink;
moreLinkText = (defaultSourceLinkText) ? defaultSourceLinkText : 'Read more from ' + channelTitle + ' - ' + channelDescription;
}
/* create a container for the feed, use passed in id or append to body */
var container = document.getElementById(feedContainerID);
if (!container) {
container = document.createElement("div");
document.body.appendChild(container);
}
container.style.width = defaultFeedWidth + "px";
/* loop through items */
for (var i = 0; i < result.feed.entries.length; i++) {
/* get item data */
var entry = result.feed.entries[i];
/* create container for item */
var itemBlockEl = document.createElement("div");
var oddEvenClass = (i % 2) ? "table_even" : "table_odd";
itemBlockEl.setAttribute("class", "body_article "+oddEvenClass);
/* create container for news item text content */
itemBlockText = document.createElement("div");
itemBlockText.setAttribute("class", "body_itemtextarea");
itemBlockText.style.width = defaultTextColumnWidth + "px";
/* Set up title and content elements */
var titleEl = document.createElement("a");
var bodyEl = document.createElement("p");
var dateEl = document.createElement("span");
/* Set up title element */
titleEl.setAttribute("href", entry.link);
titleEl.setAttribute("title", entry.title);
titleEl.setAttribute("class", "body_itemarticle_title");
titleEl.appendChild(document.createTextNode(entry.title));
itemBlockText.appendChild(titleEl);
/* Set up date element */
dateEl.setAttribute("class", "body_smalltext item_date");
dateEl.appendChild(document.createTextNode(formatDate(new Date(entry.publishedDate))));
itemBlockText.appendChild(dateEl);
/* Set up content element */
bodyEl.setAttribute("class", "body_featurebox2_text");
bodyEl.innerHTML = entry.content;
/* set up image */
var imgEls = bodyEl.getElementsByTagName("IMG");
if (imgEls.length == 0 && defaultImageUrl != '') {
/* feed contains no image but a default image has been set */
imgEls = [];
imgEls[0] = document.createElement("img");
imgEls[0].setAttribute("src", defaultImageUrl);
}
if (imgEls.length > 0) {
imgEls[0].setAttribute("width", defaultImageWidth);
imgEls[0].setAttribute("height", defaultImageHeight);
imgEls[0].setAttribute("alt", entry.title);
itemBlockEl.appendChild(imgEls[0]);
}
/* set up news text content */
itemBlockEl.appendChild(itemBlockText);
itemBlockText.appendChild(bodyEl);
itemBlockEl.innerHTML += "<br class='clear' />";
/* add item to feed container */
container.appendChild(itemBlockEl);
}
/* 'more' link */
if (linkToMore != ''){
moreEl = document.createElement("p");
moreEl.setAttribute("class", "link_to_more");
moreLinkEl = document.createElement("a");
moreLinkEl.setAttribute("href", linkToMore);
moreLinkEl.appendChild(document.createTextNode(moreLinkText));
moreEl.appendChild(moreLinkEl);
container.appendChild(moreEl);
}
}
});
}
/**
* Format a date as DD MMMM YYYY
*/
formatDate = function(date) {
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var hours = date.getHours() > 12 ? date.getHours() - 12 : date.getHours();
return date.getDate() + ' ' + months[date.getMonth()] + ' ' + date.getFullYear();
}