-
Notifications
You must be signed in to change notification settings - Fork 0
/
feedOwn.js
170 lines (146 loc) · 5.07 KB
/
feedOwn.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
var googlePlusFeed = new GoogleFeed(settings.id);
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();
}