resources/js/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 |
function format_date(d){
return $.timeago(Date.parse(d));
}
function feed_entry(entry, element){
var published_at = format_date(entry.publishedDate);
var it = $("<li></li>").addClass('feed-item');
switch(element)
{
case "#twitter":
var content = entry.title
.replace(/^h3rald:/, '')
.replace(/((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)/, '<a href="$1">$1</a>')
.replace(/ @([a-zA-Z1-9_]*)/, ' <a href="http://www.twitter.com/$1">@$1</a>')
.replace(/ #([a-zA-Z1-9_]*)/, ' <a href="http://www.twitter.com/search?q=%23$1">#$1</a>')
dt = $("<span></span>").addClass('feed-item-date').html(published_at+":");
tx = $("<span>» </span>").addClass('feed-item-text').html(content);
it.append(dt);
it.append(tx)
break;
case "#delicious":
var content = "<a href='"+entry.link+"'>"+entry.title+"</a>";
content += "<br />tags: ";
var categories = Array();
for (i=0; i<entry.categories.length; i++)
{
categories[i] = "<a href='http://delicious.com/h3rald/"+entry.categories[i]+"'>"+entry.categories[i]+"</a> ";
}
content += categories.join(', ').replace(/ $/, '');
dt = $("<span></span>").addClass('feed-item-date').html(published_at+":");
tx = $("<span>» </span>").addClass('feed-item-text').html(content);
it.append(dt);
it.append(tx)
break;
}
return it;
};
function display_feed(feed, element){
if (!feed){
$('<p>An error occurred while retrieving this feed.</p>').appendTo(element);
return false;
}
var feed_list = $("<ul></ul>");
var entries = feed.entries;
for(var i=0; i<entries.length; i++){
var entry = entries[i];
feed_list.append(feed_entry(entry, element)).fadeIn(1000);
}
feed_list.appendTo(element)
};
var delicious_feed = function(feed){
display_feed(feed, "#delicious")
};
var twitter_feed = function(feed){
display_feed(feed, "#twitter")
};
// http://api.backtype.com/user/h3rald/comments.json?key=47bf0031e3a18a598b85&html=1
function backtype_comments(max)
{
$.getJSON("/data/comments.json",
function(data){
var comment_list = $("<ul></ul>");
$.each(data.comments, function(i, comment){
c = $("<li></li>").addClass('feed-item');
dt = $("<span></span>").addClass('feed-item-date').html(format_date(comment.comment.date)+":");
tx = $("<span>» </span>").addClass('feed-item-text').append($('<a></a>').attr('href', comment.comment.url).html(comment.post.title));
c.append(dt);
c.append(tx);
c.appendTo(comment_list);
if ( i == max ) {
comment_list.appendTo("#backtype").fadeIn(1000);
return false;
}
});
});
}
|