/
feeds.js
348 lines (296 loc) · 11.4 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/**
* @title WET-BOEW Feeds
* @overview Aggregates and displays entries from one or more Web feeds.
* @license wet-boew.github.io/wet-boew/License-en.html / wet-boew.github.io/wet-boew/Licence-fr.html
* @author @pjackson28
*/
(function( $, window, wb, undef ) {
"use strict";
/*
* Variable and function definitions.
* These are global to the plugin - meaning that they will be initialized once per page,
* not once per instance of plugin on the page. So, this is a good place to define
* variables that are common to all instances of the plugin on a page.
*/
var pluginName = "wb-feeds",
selector = "." + pluginName,
feedLinkSelector = "li > a",
initedClass = pluginName + "-inited",
initEvent = "wb-init" + selector,
$document = wb.doc,
patt = /\\u([\d\w]{4})/g,
/**
* Helper function that returns the string representaion of a unicode character
* @method decode
* @param {regex} match unicode pattern
* @param {string} code string where unicode is needed to be converted
* @return {string} unicode string character
*/
decode = function( match, code ) {
return String.fromCharCode( parseInt( code, 16 ) );
},
/**
* Helper wrapper function that performs unicode decodes on a string
* @method fromCharCode
* @param {string} s string to sanitize with escaped unicode characters
* @return {string} sanitized string
*/
fromCharCode = function(s) {
return s.replace( patt, decode );
},
/**
* Process Feed/JSON Entries
* @method processEntries
* @param {data} JSON formatted data to process
* @return {string} of HTML output
*/
processEntries = function( data ) {
var items = data,
entries = [],
icon = this.fIcon,
$content = this._content,
toProcess = $content.data( "toProcess" ),
i, len;
len = items.length;
for ( i = 0; i !== len; i += 1 ) {
items[ i ].fIcon = icon ;
if ( items[ i ].publishedDate === undef && items[ i ].published !== undef ) {
items[ i ].publishedDate = items[ i ].published;
}
entries.push( items[ i ] );
}
// lets merge with latest entries
entries = $.merge( entries, $content.data( "entries" ) );
if ( toProcess === 1 ) {
parseEntries( entries, $content.data( "feedLimit" ), $content, this.feedType );
return 0;
}
toProcess -= 1 ;
$content.data({
"toProcess": toProcess,
"entries": entries
});
return toProcess;
},
/**
* @object Templates
* @properties {function}
* @param {object} requires a entry object of various ATOM based properties
* @returns {string} modified string with appropiate markup/format for a entry object
*/
Templates = {
/**
* [facebook template]
* @param {entry object} data
* @return {string} HTML string of formatted using Media Object (twitter bootstrap)
*/
facebook: function( data ) {
// Facebook feeds does not really do titles in ATOM RSS. It simply truncates content at 150 characters. We are using a JS based sentence
// detection algorithm to better split content and titles
var content = fromCharCode( data.content ),
title = content.replace( /(<([^>]+)>)/ig, "" ).match( /\(?[^\.\?\!]+[\.!\?]\)?/g );
// Sanitize the HTML from Facebook - extra 'br' tags
content = content.replace( /(<br>\n?)+/gi, "<br>" );
return "<li class='media'><a class='pull-left' href=''><img src='" + data.fIcon + "' alt='" + data.author +
"' height='64px' width='64px' class='media-object'/></a><div class='media-body'>" +
"<h4 class='media-heading'><a href='" + data.link + "'><span class='wb-inv'>" + title[0] + " - </span>" + data.author + "</a> " +
( data.publishedDate !== "" ? " <small class='feeds-date text-right'>[" +
wb.date.toDateISO( data.publishedDate, true ) + "]</small>" : "" ) +
"</h4><p>" + content + "</p></div></li>";
},
/**
* [fickr template]
* @param {entry object} data
* @return {string} HTML string for creating a photowall effect
*/
flickr: function( data ) {
var seed = "id" + wb.guid(),
title = data.title,
media = data.media.m,
thumbnail = media.replace( "_m.", "_s." ),
image = media.replace("_m", ""),
description = data.description.replace( /^\s*<p>(.*?)<\/p>\s*<p>(.*?)<\/p>/i, "");
// due to CORS we cannot default to simple ajax pulls of the image. We have to inline the content box
return "<li class='col-md-4 col-sm-6'><a class='wb-lbx' href='#" + seed + "' title='" + title + "'><img src='" + thumbnail + "' alt='" + title + "' class='img-responsive'/></a>" +
"<section id='" + seed + "' class='mfp-hide modal-dialog modal-content overlay-def'>" +
"<header class='modal-header'><h2 class='modal-title'>" + title + "</h2></header>" +
"<div class='modal-body'><img src='" + image + "' class='thumbnail center-block' alt='" + title + "' />" +
description + "</div></section>" +
"</li>";
},
/**
* [Youtube template]
* @param {entry object} data
* @return {string} HTML string for creating a photowall effect
*/
youtube: function( data ) {
var seed = "id" + wb.guid(),
mediaGroup = data.media$group,
title = mediaGroup.media$title.$t,
thumbnail = mediaGroup.media$thumbnail[1].url,
description = mediaGroup.media$description.$t,
videoid = mediaGroup.yt$videoid.$t;
// due to CORS we cannot default to simple ajax pulls of the image. We have to inline the content box
return "<li class='col-md-4 col-sm-6' ><a class='wb-lbx' href='#" + seed + "' title='" + title + "'><img src='" + thumbnail + "' alt='" + title + "' class='img-responsive' /></a>" +
"<section id='" + seed + "' class='mfp-hide modal-dialog modal-content overlay-def'>" +
"<header class='modal-header'><h2 class='modal-title'>" + title + "</h2></header>" +
"<div class='modal-body'>" +
"<figure class='wb-mltmd'><video title='" + title + "'>" +
"<source type='video/youtube' src='http://www.youtube.com/watch?v=" + videoid + "' />" +
"</video><figcaption><p>" + description + "</p>" +
"</figcaption></figure>" +
"</div></section>" +
"</li>";
},
/**
* [generic template]
* @param {entry object} data
* @return {string} HTML string of formatted using a simple list / anchor view
*/
generic: function( data ) {
return "<li><a href='" + data.link + "'>" + data.title + "</a>" +
( data.publishedDate !== "" ? " <span class='feeds-date'>[" +
wb.date.toDateISO( data.publishedDate, true ) + "]</span>" : "" ) + "</li>";
}
},
/**
* Init runs once per plugin element on the page. There may be multiple elements.
* It will run more than once per plugin if you don't remove the selector from the timer.
* @method init
* @param {jQuery Event} event Event that triggered this handler
*/
init = function( event ) {
var elm = event.target,
fetch, url, $content, limit, feeds, fType, last, i, callback, fElem, fIcon;
// Filter out any events triggered by descendants
// and only initialize the element once
if ( event.currentTarget === elm &&
elm.className.indexOf( initedClass ) === -1 ) {
wb.remove( selector );
elm.className += " " + initedClass;
$content = $( elm ).find( ".feeds-cont" );
limit = getLimit( elm );
feeds = $content.find( feedLinkSelector );
last = feeds.length - 1;
i = last;
// Lets bind some varialbes to the node to ensure safe ajax thread counting
$content.data( "toProcess", feeds.length )
.data( "feedLimit", limit )
.data( "entries", []);
while ( i >= 0 ) {
fElem = feeds.eq( i );
fIcon = fElem.find( "> img" );
fetch = {
dataType: "jsonp",
timeout: 3000
};
if ( fElem.attr( "data-ajax" ) ) {
if ( fElem.attr( "href" ).indexOf( "flickr" ) > -1 ) {
fType = "flickr";
callback = "jsoncallback";
$content.data( "postProcess", [ ".wb-lbx" ] );
} else {
fType = "youtube";
$content.data( "postProcess", [ ".wb-lbx", ".wb-mltmd" ] );
}
// We need a Gallery so lets add another plugin
// #TODO: Lightbox review for more abstraction we should not have to add a wb.add() for overlaying
fetch.url = fElem.attr( "data-ajax");
fetch.jsonp = callback;
} else {
url = jsonRequest( fElem.attr( "href" ), limit );
fetch.url = url;
// lets bind the template to the Entries
if ( url.indexOf( "facebook.com" ) > -1 ) {
fType = "facebook";
} else {
fType = "generic";
}
}
fetch.jsonp = callback;
fetch.context = {
fIcon: ( fIcon.length > 0 ) ? fIcon.attr( "src" ) : "",
feedType: fType,
_content: $content
};
$document.trigger({
type: "ajax-fetch.wb",
element: fElem,
fetch: fetch
});
i -= 1;
}
//$.extend( {}, results );
}
},
/**
* Returns a class-based set limit on plugin instances
* @method getLimit
* @param {DOM object} elm The element to search for a class of the form limit-5
* @return {number} 0 if none found, which means the plugin default
*/
getLimit = function( elm ) {
var count = elm.className.match( /\blimit-\d+/ );
if ( !count ) {
return 0;
}
return Number( count[ 0 ].replace( /limit-/i, "" ) );
},
/**
* Builds the URL for the JSON request
* @method jsonRequest
* http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q=https%3A%2F%2Fwww.facebook.com%2Ffeeds%2Fpage.php%3Fid%3D318424514044%26format%3Drss20&num=20
* @param {url} url URL of the feed.
* @param {integer} limit Limit on the number of results for the JSON request to return.
* @return {url} The URL for the JSON request
*/
jsonRequest = function( url, limit ) {
var requestURL = wb.pageUrlParts.protocol + "//ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q=" + encodeURIComponent( decodeURIComponent( url ) );
// API returns a maximum of 4 entries by default so only override if more entries should be returned
if ( limit > 4 ) {
requestURL += "&num=" + limit;
}
return requestURL;
},
/**
* Parses the results from a JSON request and appends to an element
* @method parseEntries
* @param {object} entries Results from a JSON request.
* @param {integer} limit Limit on the number of results to append to the element.
* @param {jQuery DOM element} $elm Element to which the elements will be appended.
* @return {url} The URL for the JSON request
*/
parseEntries = function( entries, limit, $elm, feedtype ) {
var cap = ( limit > 0 && limit < entries.length ? limit : entries.length ),
result = "",
postProcess = $elm.data( "postProcess" ),
compare = wb.date.compare,
i, sorted, sortedEntry;
sorted = entries.sort( function( a, b ) {
return compare( b.publishedDate, a.publishedDate );
});
for ( i = 0; i !== cap; i += 1 ) {
sortedEntry = sorted[ i ];
result += Templates[ feedtype ]( sortedEntry );
}
$elm.empty().append( result );
if ( postProcess ) {
for ( i = postProcess.length - 1; i >= 0; i -= 1 ) {
wb.add( postProcess[i] );
}
}
return true;
};
$document.on( "ajax-fetched.wb", selector + " " + feedLinkSelector, function( event, context ) {
var response = event.fetch.response,
data;
// Filter out any events triggered by descendants
if ( event.currentTarget === event.target ) {
data = ( response.responseData ) ? response.responseData.feed.entries : response.items || response.feed.entry,
processEntries.apply( context, [ data ] );
}
});
$document.on( "timerpoke.wb " + initEvent, selector, init );
// Add the timer poke to initialize the plugin
wb.add( selector );
})( jQuery, window, wb );