Skip to content

Instantly share code, notes, and snippets.

@jyokyoku
Created August 1, 2011 23:32
Show Gist options
  • Save jyokyoku/1119248 to your computer and use it in GitHub Desktop.
Save jyokyoku/1119248 to your computer and use it in GitHub Desktop.
jQuery TwetterTrackback Custom
(function($) {
$.fn.twitterTrackback = function(options){
$.twitterTrackback.init(this, options);
$.twitterTrackback.update(1);
return this;
};
$.twitterTrackback = {
$element: null,
options: {
url: window.location.href,
wrapperTag: 'ul',
removeHash: true,
renderCallback: renderHtml,
pagerCallback: pagerHtml,
pagerPos: 'both',
perpage: 10,
noTrackback: '<p>Twitterからのトラックバックはありません。</p>',
exclude: []
},
init: function(element, options) {
this.$element = $(element);
this.options = $.extend(this.options, options);
if (this.options.removeHash) {
this.options.url = this.options.url.replace(/#.+/, '');
}
},
update: function(page) {
$self = this;
$.ajax({
type: 'GET',
url: 'http://otter.topsy.com/trackbacks.js',
cache: false,
dataType: 'jsonp',
data: {
perpage: this.options.perpage,
page: page || 1,
order: 'date',
url: this.options.url
},
success: function(trackbackData) {
var tweetHtml = '';
if(trackbackData.response.list.length == 0) {
tweetHtml += $self.options.noTrackback;
} else {
jQuery.each(trackbackData.response.list, function(n) {
for (var i = 0; i < $self.options.exclude.length; i++) {
if ($self.options.exclude[i] != this.author.nick) {
tweetHtml += $self.options.renderCallback.call(this, n);
}
}
});
if (!tweetHtml) {
tweetHtml = $self.options.noTrackback;
} else if ($self.options.wrapperTag) {
tweetHtml = '<' + $self.options.wrapperTag + '>' + tweetHtml + '</' + $self.options.wrapperTag + '>';
}
}
if (trackbackData.response.total > $self.options.perpage && $self.options.pagerCallback) {
var pagerHtml = $self.options.pagerCallback.call($self, page, $self.options.perpage, trackbackData.response.total);
if ($self.options.pagerPos.toLowerCase() == 'both') {
tweetHtml = pagerHtml + tweetHtml + pagerHtml;
} else if ($self.options.pagerPos.toLowerCase() == 'top') {
tweetHtml = pagetHtml + tweetHtml;
} else {
tweetHtml += pagerHtml;
}
}
$self.$element.html(tweetHtml);
},
complete: function() {}
});
}
}
function renderHtml(n) {
return (
'<li class="tweet' + n + '">' +
'<a href="' + this.author.url + '"><img src="' + this.author.photo_url + '" alt="' + this.author.name + '" width="48" /></a>' +
'<ul class="section">' +
'<li class="nick"><a href="' + this.author.url + '">' + this.author.nick + '</a></li>' +
'<li class="content">' + this.content.replace(/((http:|https:)\/\/[\x21-\x7e]+)/gi, "<a href='$1'>$1</a>") + '</li>' +
'<li class="date"><a href="' + this.permalink_url + '">' + this.date_alpha + '</a></li>' +
'</ul>' +
'</li>'
);
}
function pagerHtml(current, perpage, total) {
var totalPages = total / perpage;
var links = '';
for (var i = 1; i < totalPages; i++) {
if (current != i) {
links += '<span><a href="javascript:void();" onclick="jQuery.twitterTrackback.update(' + i + '); return false;">' + i + '</a></span>';
} else {
links += '<span class="current">' + i + '</span>'
}
}
return (
'<div class="tweetPager">' + links + '</div>'
);
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment