Skip to content
Browse files

Language option, callback and event

  • Loading branch information...
1 parent cbf181e commit 6ea6fe9c16370394a6ddac38004780b262c4faa9 @elektronaut elektronaut committed Nov 21, 2009
Showing with 88 additions and 20 deletions.
  1. +26 −0 README.rdoc
  2. +61 −19 jquery.livetwitter.js
  3. +1 −1 jquery.livetwitter.min.js
View
26 README.rdoc
@@ -36,13 +36,39 @@ Stopping, starting and refreshing manually::
$("#twitterSearch").each(function(){ this.twitter.start(); });
$("#twitterSearch").each(function(){ this.twitter.refresh(); });
+
+== Search options
+
Restricting results by geocode is possible:
$("#twitterSearch").liveTwitter('bacon', {geocode: '59.919151,10.749950,50km'});
+..or by language:
+
+ $("#twitterSearch").liveTwitter('crossaint', {lang: 'fr'});
+
+== Callbacks and events
+
+If you want to apply behavior when new tweets are loaded, you can pass a callback function:
+
+ $('#twitterSearch').liveTwitter('bacon', {}, function(container, newCount){
+ alert(newCount + ' new tweets loaded!');
+ });
+
+..or if events are more your style:
+
+ $('#twitterSearch').liveTwitter('bacon').bind('tweets', function(){
+ alert('New tweets!');
+ });
+
== Changelog
+=== 1.4.0
+
+* Callback and event trigger
+* Support for language selection
+
=== 1.3.3
* Now supports geocoding
View
80 jquery.livetwitter.js
@@ -1,5 +1,5 @@
/*
- * jQuery LiveTwitter 1.3.3
+ * jQuery LiveTwitter 1.4.0
* - Live updating Twitter plugin for jQuery
*
* Copyright (c) 2009 Inge Jørgensen (elektronaut.no)
@@ -19,35 +19,47 @@
return this.pushStack(this.get().reverse(), arguments);
};
}
- $.fn.liveTwitter = function(query, options){
+ $.fn.liveTwitter = function(query, options, callback){
+ var domNode = this;
$(this).each(function(){
var settings = {};
- if(this.twitter){
+
+ // Handle changing of options
+ if(this.twitter) {
settings = jQuery.extend(this.twitter.settings, options);
this.twitter.settings = settings;
if(query) {
this.twitter.query = query;
}
- this.twitter.limit = settings.limit;
- this.twitter.mode = settings.mode;
+ this.twitter.limit = settings.limit;
+ this.twitter.mode = settings.mode;
if(this.twitter.interval){
this.twitter.refresh();
}
+ if(callback){
+ this.twitter.callback = callback;
+ }
+
+ // ..or create a new twitter object
} else {
+ // Extend settings with the defaults
settings = jQuery.extend({
mode: 'search', // Mode, valid options are: 'search', 'user_timeline'
rate: 15000, // Refresh rate in ms
limit: 10, // Limit number of results
refresh: true
}, options);
+
+ // Default setting for showAuthor if not provided
if(typeof settings.showAuthor == "undefined"){
- if(settings.mode == 'search'){
- settings.showAuthor = true;
- } else {
- settings.showAuthor = false;
- }
+ settings.showAuthor = (settings.mode == 'search') ? true : false;
}
- window.twitter_callback = function(){return true;};
+
+ // Set up a dummy function for the Twitter API callback
+ if(!window.twitter_callback){
+ window.twitter_callback = function(){return true;};
+ }
+
this.twitter = {
settings: settings,
query: query,
@@ -56,6 +68,9 @@
interval: false,
container: this,
lastTimeStamp: 0,
+ callback: callback,
+
+ // Convert the time stamp to a more human readable format
relativeTime: function(timeString){
var parsedDate = Date.parse(timeString);
var delta = (Date.parse(Date()) - parsedDate) / 1000;
@@ -77,25 +92,42 @@
}
return r;
},
+
+ // Update the timestamps in realtime
refreshTime: function() {
var twitter = this;
$(twitter.container).find('span.time').each(function(){
$(this).html(twitter.relativeTime(this.timeStamp));
});
},
- refresh: function(initialize){
+
+ // Handle reloading
+ refresh: function(initialize){
var twitter = this;
if(this.settings.refresh || initialize) {
- var encodedQuery = encodeURIComponent(this.query);
var url = '';
+ var params = {};
if(twitter.mode == 'search'){
+ params.q = this.query;
+
if(this.settings.geocode){
- url = "http://search.twitter.com/search.json?q="+encodedQuery+"&geocode="+encodeURIComponent(this.settings.geocode)+"&callback=?";
- } else {
- url = "http://search.twitter.com/search.json?q="+encodedQuery+"&callback=?";
+ params.geocode = this.settings.geocode;
+ }
+ if(this.settings.lang){
+ params.lang = this.settings.lang;
}
+
+ // Convert params to string
+ var paramsString = [];
+ for(var param in params){
+ if(params.hasOwnProperty(param)){
+ paramsString[paramsString.length] = param + '=' + encodeURIComponent(params[param]);
+ }
+ }
+ paramsString = paramsString.join("&");
+ url = "http://search.twitter.com/search.json?"+paramsString+"&callback=?";
} else if(twitter.mode == 'user_timeline') {
- url = "http://twitter.com/statuses/user_timeline/"+encodedQuery+".json?count="+twitter.limit+"&callback=?";
+ url = "http://twitter.com/statuses/user_timeline/"+encodeURIComponent(this.query)+".json?count="+twitter.limit+"&callback=?";
}
$.getJSON(url, function(json) {
var results = null;
@@ -104,6 +136,7 @@
} else {
results = json;
}
+ var newTweets = 0;
$(results).reverse().each(function(){
var screen_name = '';
var profile_image_url = '';
@@ -118,6 +151,7 @@
var linkified_text = this.text.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) { return m.link(m); });
linkified_text = linkified_text.replace(/@[A-Za-z0-9_]+/, function(u){return u.link('http://twitter.com/'+u.replace(/^@/,''));});
if(Date.parse(this.created_at) > twitter.lastTimeStamp) {
+ newTweets += 1;
var tweetHTML = '<div class="tweet tweet-'+this.id+'">';
if(twitter.settings.showAuthor) {
tweetHTML +=
@@ -143,8 +177,16 @@
twitter.lastTimeStamp = Date.parse(this.created_at);
}
});
- // Limit number of entries
- $(twitter.container).find('div.tweet:gt('+(twitter.limit-1)+')').remove();
+ if(newTweets > 0) {
+ // Limit number of entries
+ $(twitter.container).find('div.tweet:gt('+(twitter.limit-1)+')').remove();
+ // Run callback
+ if(twitter.callback){
+ twitter.callback(domNode, newTweets);
+ }
+ // Trigger event
+ $(domNode).trigger('tweets');
+ }
});
}
},
View
2 jquery.livetwitter.min.js
@@ -1 +1 @@
-(function(a){if(!a.fn.reverse){a.fn.reverse=function(){return this.pushStack(this.get().reverse(),arguments)}}a.fn.liveTwitter=function(c,b){a(this).each(function(){var d={};if(this.twitter){d=jQuery.extend(this.twitter.settings,b);this.twitter.settings=d;if(c){this.twitter.query=c}this.twitter.limit=d.limit;this.twitter.mode=d.mode;if(this.twitter.interval){this.twitter.refresh()}}else{d=jQuery.extend({mode:"search",rate:15000,limit:10,refresh:true},b);if(typeof d.showAuthor=="undefined"){if(d.mode=="search"){d.showAuthor=true}else{d.showAuthor=false}}window.twitter_callback=function(){return true};this.twitter={settings:d,query:c,limit:d.limit,mode:d.mode,interval:false,container:this,lastTimeStamp:0,relativeTime:function(g){var f=Date.parse(g);var i=(Date.parse(Date())-f)/1000;var h="";if(i<60){h=i+" seconds ago"}else{if(i<120){h="a minute ago"}else{if(i<(45*60)){h=(parseInt(i/60,10)).toString()+" minutes ago"}else{if(i<(90*60)){h="an hour ago"}else{if(i<(24*60*60)){h=""+(parseInt(i/3600,10)).toString()+" hours ago"}else{if(i<(48*60*60)){h="a day ago"}else{h=(parseInt(i/86400,10)).toString()+" days ago"}}}}}}return h},refreshTime:function(){var f=this;a(f.container).find("span.time").each(function(){a(this).html(f.relativeTime(this.timeStamp))})},refresh:function(f){var i=this;if(this.settings.refresh||f){var h=encodeURIComponent(this.query);var g="";if(i.mode=="search"){if(this.settings.geocode){g="http://search.twitter.com/search.json?q="+h+"&geocode="+encodeURIComponent(this.settings.geocode)+"&callback=?"}else{g="http://search.twitter.com/search.json?q="+h+"&callback=?"}}else{if(i.mode=="user_timeline"){g="http://twitter.com/statuses/user_timeline/"+h+".json?count="+i.limit+"&callback=?"}}a.getJSON(g,function(k){var j=null;if(i.mode=="search"){j=k.results}else{j=k}a(j).reverse().each(function(){var p="";var n="";if(i.mode=="search"){p=this.from_user;n=this.profile_image_url}else{p=this.user.screen_name;n=this.user.profile_image_url}var l=this.user;var o=this.text.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/,function(r){return r.link(r)});o=o.replace(/@[A-Za-z0-9_]+/,function(r){return r.link("http://twitter.com/"+r.replace(/^@/,""))});if(Date.parse(this.created_at)>i.lastTimeStamp){var q='<div class="tweet tweet-'+this.id+'">';if(i.settings.showAuthor){q+='<img width="24" height="24" src="'+n+'" /><p class="text"><span class="username"><a href="http://twitter.com/'+p+'">'+p+"</a>:</span> "}else{q+='<p class="text"> '}q+=o+' <span class="time">'+i.relativeTime(this.created_at)+"</span></p></div>";a(i.container).prepend(q);var m=this.created_at;a(i.container).find("span.time:first").each(function(){this.timeStamp=m});if(!f){a(i.container).find(".tweet-"+this.id).hide().fadeIn()}i.lastTimeStamp=Date.parse(this.created_at)}});a(i.container).find("div.tweet:gt("+(i.limit-1)+")").remove()})}},start:function(){var f=this;if(!this.interval){this.interval=setInterval(function(){f.refresh()},f.settings.rate);this.refresh(true)}},stop:function(){if(this.interval){clearInterval(this.interval);this.interval=false}}};var e=this.twitter;this.timeInterval=setInterval(function(){e.refreshTime()},5000);this.twitter.start()}});return this}})(jQuery);
+(function(a){if(!a.fn.reverse){a.fn.reverse=function(){return this.pushStack(this.get().reverse(),arguments)}}a.fn.liveTwitter=function(d,b,e){var c=this;a(this).each(function(){var f={};if(this.twitter){f=jQuery.extend(this.twitter.settings,b);this.twitter.settings=f;if(d){this.twitter.query=d}this.twitter.limit=f.limit;this.twitter.mode=f.mode;if(this.twitter.interval){this.twitter.refresh()}if(e){this.twitter.callback=e}}else{f=jQuery.extend({mode:"search",rate:15000,limit:10,refresh:true},b);if(typeof f.showAuthor=="undefined"){f.showAuthor=(f.mode=="search")?true:false}if(!window.twitter_callback){window.twitter_callback=function(){return true}}this.twitter={settings:f,query:d,limit:f.limit,mode:f.mode,interval:false,container:this,lastTimeStamp:0,callback:e,relativeTime:function(i){var h=Date.parse(i);var k=(Date.parse(Date())-h)/1000;var j="";if(k<60){j=k+" seconds ago"}else{if(k<120){j="a minute ago"}else{if(k<(45*60)){j=(parseInt(k/60,10)).toString()+" minutes ago"}else{if(k<(90*60)){j="an hour ago"}else{if(k<(24*60*60)){j=""+(parseInt(k/3600,10)).toString()+" hours ago"}else{if(k<(48*60*60)){j="a day ago"}else{j=(parseInt(k/86400,10)).toString()+" days ago"}}}}}}return j},refreshTime:function(){var h=this;a(h.container).find("span.time").each(function(){a(this).html(h.relativeTime(this.timeStamp))})},refresh:function(h){var j=this;if(this.settings.refresh||h){var i="";var m={};if(j.mode=="search"){m.q=this.query;if(this.settings.geocode){m.geocode=this.settings.geocode}if(this.settings.lang){m.lang=this.settings.lang}var k=[];for(var l in m){if(m.hasOwnProperty(l)){k[k.length]=l+"="+encodeURIComponent(m[l])}}k=k.join("&");i="http://search.twitter.com/search.json?"+k+"&callback=?"}else{if(j.mode=="user_timeline"){i="http://twitter.com/statuses/user_timeline/"+encodeURIComponent(this.query)+".json?count="+j.limit+"&callback=?"}}a.getJSON(i,function(o){var n=null;if(j.mode=="search"){n=o.results}else{n=o}var p=0;a(n).reverse().each(function(){var u="";var s="";if(j.mode=="search"){u=this.from_user;s=this.profile_image_url}else{u=this.user.screen_name;s=this.user.profile_image_url}var q=this.user;var t=this.text.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/,function(w){return w.link(w)});t=t.replace(/@[A-Za-z0-9_]+/,function(w){return w.link("http://twitter.com/"+w.replace(/^@/,""))});if(Date.parse(this.created_at)>j.lastTimeStamp){p+=1;var v='<div class="tweet tweet-'+this.id+'">';if(j.settings.showAuthor){v+='<img width="24" height="24" src="'+s+'" /><p class="text"><span class="username"><a href="http://twitter.com/'+u+'">'+u+"</a>:</span> "}else{v+='<p class="text"> '}v+=t+' <span class="time">'+j.relativeTime(this.created_at)+"</span></p></div>";a(j.container).prepend(v);var r=this.created_at;a(j.container).find("span.time:first").each(function(){this.timeStamp=r});if(!h){a(j.container).find(".tweet-"+this.id).hide().fadeIn()}j.lastTimeStamp=Date.parse(this.created_at)}});if(p>0){a(j.container).find("div.tweet:gt("+(j.limit-1)+")").remove();if(j.callback){j.callback(c,p)}a(c).trigger("tweets")}})}},start:function(){var h=this;if(!this.interval){this.interval=setInterval(function(){h.refresh()},h.settings.rate);this.refresh(true)}},stop:function(){if(this.interval){clearInterval(this.interval);this.interval=false}}};var g=this.twitter;this.timeInterval=setInterval(function(){g.refreshTime()},5000);this.twitter.start()}});return this}})(jQuery);

0 comments on commit 6ea6fe9

Please sign in to comment.
Something went wrong with that request. Please try again.