{{user.name}}{{user.screen_name}}
{{{text}}}
// Tweets
// A plugin to process a JSON feed of tweets into a HTML container
// Created by Donovan Hutchinson
// Requires:
// jQuery
// Handlebars (for templating)
// Full instructions: http://github.com/donovanh/tweets-plugin/
// Based on: jQuery Plugin Boilerplate by Stefan Gabos
;(function($) {
$.tweets = function(options) {
var defaults = {
searchPhrase: 'hop.ie',
templateID: 'tweet-template-default',
destinationID: 'tweet-container',
tweetSource: 'Your source URL, eg. tweets.example.com',
maxTweets: 15
}
var plugin = this;
var updateTimer = null;
plugin.settings = {}
var init = function() {
plugin.settings = options || defaults;
// Check if the destination exists
if ($('#'+plugin.settings.destinationID).length == 0) {
console.log('Tweets plugin error: Please supply a destination element for the tweets.');
return;
}
// Check if there's a source to get the tweets (essential)
if (plugin.settings.tweetSource.length == 0) {
console.log('Tweets plugin error: This plugin requires a server component specified. Please check github.com/donovanh/tweets/ for details.');
return;
}
plugin.settings.searchPhrase = cleanSearchPhrase(plugin.settings.searchPhrase);
// Get the tweet template
if ($('#'+plugin.settings.templateID).length == 0) {
// Use the default template
plugin.settings.templateHTML = tweet_template_default;
} else {
plugin.settings.templateHTML = $('#'+plugin.settings.templateID).html();
}
if (plugin.settings.searchPhrase.length > 0) {
$.get(plugin.settings.tweetSource+'/search/'+plugin.settings.searchPhrase, function(data) {
var outputHTML = '';
console.log(data.statuses);
if (data.statuses !== undefined && data.statuses.length > 0) {
$.each(data.statuses, function(index, tweet) {
tweet.text = replaceURLWithHTMLLinks(tweet.text);
tweet.relative_timestamp = time_ago(tweet.created_at);
var template = Handlebars.compile(plugin.settings.templateHTML);
outputHTML += template(tweet);
});
}
$("#"+plugin.settings.destinationID).html(outputHTML);
});
// Since the success was successful, add a listener for a stream of further tweets
listenForMoreTweets(plugin.settings);
} else {
console.log('Tweets plugin error: Please supply a searchPhrase.');
return;
}
// Set timeout to update the timestamps
updateTimer = window.setInterval(function() { updateDatestamps(); }, 10000);
}
// Default handlebars template
var tweet_template_default = ' {{user.name}}{{user.screen_name}} {{{text}}}