/
feeds.js
280 lines (241 loc) · 18.5 KB
/
feeds.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
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
/*********************************************\
jquery.kyco.googleplusfeed
v1.0.3
Brought to you by http://www.kyco.co.za
Copyright 2013 Cornelius Weidmann
Distributed under the GPL
\*********************************************/
(function($) {
var methods = {
init: function(options) {
var defaults = {
feedPosts: 3, // Feed posts to show on load
postsIncrement: 3, // Number of feed posts to show on "Show more" button click
maxPosts: 20, // Max number of posts to pull before "Show more" will go to Google+, cannot exceed 20 because of Google API in use
profileImageSize: 50, // Max size is 250
lang: 'en' // Default language, can also be set to 'de'
};
var settings = $.extend({}, defaults, options);
return this.each(function() {
var selector = $(this);
// Localization
settings.lang = settings.lang === 'en' ? {
langCode: 'en',
loading: 'Loading...',
showMore: 'Show more',
viewMore: 'View more posts on Google+',
shared: 'Shared publicly - ',
viewPost: 'View post',
errorEmpty: 'Nothing to show. Empty feed. ',
retryEmpty: 'Refresh',
errorGeneral: 'Unable to retrieve feed contents. ',
retryGeneral: 'Retry'
} : {
langCode: 'de',
loading: 'Wird geladen...',
showMore: 'Mehr anzeigen',
viewMore: 'Weitere Beiträge ansehen bei Google+',
shared: 'Öffentlich geteilt - ',
viewPost: 'Beitrag ansehen',
errorEmpty: 'Nichts zu sehen. Keine Beiträge. ',
retryEmpty: 'Wiederholen',
errorGeneral: 'Es ist ein Fehler beim abrufen der Beiträge erfolgt. ',
retryGeneral: 'Wiederholen'
};
// Create feed DOM elements.
var container = $('<div id="feed_' + selector.attr('class') + '" class="kyco_googleplusfeed ' + settings.lang.langCode + '"></div>');
var loader = $('<div class="feed_loader">' + settings.lang.loading + '</div>');
var wrapper = $('<div class="feed_wrapper"></div>');
var header = $('<div class="feed_header"></div>');
var screenName = $('<h3 class="feed_screen_name"><a href="#" target="_blank"></a></h3>');
var profileImage = $('<a href="#" class="feed_profile_image" target="_blank"></a>');
var content = $('<div class="feed_content"></div>');
var showMoreButton = $('<span class="feed_sow_more">' + settings.lang.showMore + '</span>');
var errorMessage = $('<div class="error"></div>');
var retryButton = $('<span class="retry"></span>')
header.append(profileImage, screenName);
wrapper.append(header, content, showMoreButton);
container.append(loader, wrapper);
selector.append(container);
// Main functionality
var googlePlusFeed = new GoogleFeed(settings.id);
var gFeed = new GFeed();
function GFeed(){
$.get("https://www.googleapis.com/plus/v1/people/me/activities/public", function(data){
console.log(data);
});
}
function initFeed() {
var feedEntries = googlePlusFeed.entries;
var totalPosts = feedEntries.length;
var currentPosts = settings.feedPosts;
var postsIncrement = settings.postsIncrement;
var postsLimit = 0;
var showMore = false;
var i = 0;
var j = currentPosts;
var str = '';
// Check to see if there are enough user posts to show on first load
currentPosts = totalPosts < currentPosts ? totalPosts : currentPosts;
if (totalPosts > 0) {
// Posts exist for the given Google+ ID
for (; i < currentPosts; i++) {
str += stringBuilder(i);
}
screenName.children('a').attr('href', googlePlusFeed.url).text(googlePlusFeed.screenName);
profileImage.attr('href', googlePlusFeed.url);
profileImage.append('<img src="' + googlePlusFeed.image + '">');
content.html(str); // Update feed
content.animate({scrollTop: 0}, 1); // Force scroll to top of content
wrapper.fadeIn(300); // Show the content
} else {
// No posts exist for the given Google+ ID
wrapper.children().remove();
errorMessage.text(settings.lang.errorEmpty);
retryButton.text(settings.lang.retryEmpty);
errorMessage.append(retryButton);
wrapper.append(errorMessage);
wrapper.fadeIn(300); // Show the content
// Refresh button functionality
wrapper.find('.retry').click(function() {
wrapper.children().remove();
wrapper.append(loader);
loader.show();
googlePlusFeed.getFeed(); // Try again
});
}
// Show more button functionality
showMoreButton.click(function() {
if (postsLimit <= totalPosts) {
postsLimit += showMore ? postsIncrement : currentPosts + postsIncrement;
postsLimit = postsLimit > totalPosts ? totalPosts : postsLimit;
for (; j < postsLimit; j++) {
str += stringBuilder(j);
if (j === (totalPosts - 1)) {
showMoreButton.unbind('click').addClass('link');
showMoreButton.text(settings.lang.viewMore).click(function() {
window.open(googlePlusFeed.url);
});
}
}
content.animate({scrollTop: content[0].scrollHeight}, 500);
showMore = true;
}
content.html(str); // Update feed
});
function stringBuilder(e) {
// Generates the HTML for each post
var newStr = '';
newStr += '<div class="feed_post post_' + (e + 1) + '">';
newStr += '<span>' + settings.lang.shared + formatTime(e, settings.lang.langCode) + '</span>';
newStr += '<p>' + feedEntries[e].contentSnippet + '</p>';
newStr += '<a href="' + feedEntries[e].link + '" target="_blank">' + settings.lang.viewPost + '</a>';
newStr += '</div>';
return newStr;
}
function formatTime(e, lang) {
// Generates language friendly time strings
var formattedTime = '';
var abbreviatedMonths = [
'Jan', 'Feb', 'Mar',
'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep',
'Oct', 'Nov', 'Dec'
];
var monthsToNum = [
'01', '02', '03',
'04', '05', '06',
'07',' 08', '09',
'10', '11', '12'
];
if (lang === 'en') {
formattedTime = feedEntries[e].publishedDate.substr(0, 16);
} else {
var timeArray = $.trim(feedEntries[e].publishedDate.substr(0, 16).split(',')[1]).split(' ');
formattedTime = timeArray[0] + '.' + monthsToNum[abbreviatedMonths.indexOf(timeArray[1])] + '.' + timeArray[2];
}
return formattedTime;
}
}
function GoogleFeed(id) {
var self = this;
self.id = id;
self.image = 'https://plus.google.com/s2/photos/profile/' + self.id + '?sz=' + settings.profileImageSize;
self.url = 'https://plus.google.com/' + self.id;
self.init = function() {
initFeed();
};
self.getFeed = function() {
$.ajax({
// Retrieve RSS feed using a handy Google API and http://plusfeed.frosas.net
url: 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=' + settings.maxPosts + '&callback=?&q=' + encodeURIComponent('http://plusfeed.frosas.net/' + self.id),
dataType: 'json',
success: function(response) {
try {
// Find and trim data to fit our needs
self.raw = response; // Used for debugging
var title = response.responseData.feed.title;
self.screenName = title.substr(0, (title.indexOf('@') - 1));
self.entries = response.responseData.feed.entries;
self.entries.forEach(function(entry) {
// Overwrite the default contentSnippet that gets returned with a cleaner version
entry.contentSnippet = $(entry.content).first().html();
});
// Preload profile image and only show content thereafter
$('<img src="' + self.image + '">').load(function() {
loader.fadeOut(300, function() {
loader.remove();
self.init();
});
});
} catch (error) {
loader.fadeOut(300, function() {
wrapper.children().remove();
errorMessage.text(settings.lang.errorGeneral);
retryButton.text(settings.lang.retryGeneral);
errorMessage.append(retryButton);
wrapper.append(errorMessage);
wrapper.fadeIn(300); // Show the content
// Retry button functionality
wrapper.find('.retry').click(function() {
wrapper.children().remove();
wrapper.append(loader);
loader.show();
self.getFeed(); // Try again
});
});
}
}
});
};
self.getFeed();
}
});
}
};
$.fn.kycoGooglePlusFeed = function(method) {
// Check if browser supports Array.forEach() method, if it doesn't mimic that functionality,
// implementation from here: http://stackoverflow.com/questions/2790001/fixing-javascript-array-functions-in-internet-explorer-indexof-foreach-etc
if (!('forEach' in Array.prototype)) {
Array.prototype.forEach = function(action, that /*opt*/) {
for (var i = 0, n = this.length; i < n; i++) {
if (i in this) {
action.call(that, this[i], i, this);
}
}
};
}
if (typeof method === 'string') {
method = {
id: method
};
}
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.kycoGooglePlusFeed');
}
};
})(jQuery);