Skip to content

Instantly share code, notes, and snippets.

@mwawrusch
Created May 18, 2011 22:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mwawrusch/979717 to your computer and use it in GitHub Desktop.
Save mwawrusch/979717 to your computer and use it in GitHub Desktop.
Coffeescript implementation of a twitter jquery plugin
# Quick jquery plugin to display recent tweets
# (C) 2011 Martin Wawrusch (@martin_sunset , http://martinatsunset.com, https://github.com/mwawrusch)
#
# The juicy parts are from:
#
# http://www.simonwhatley.co.uk/ (Simon Whatley)
# https://github.com/jimeh (Jim Myhrberg)
#
# I just assembled them in coffeescript.
#
# Sample HTML in HAML:
#
# .recent_tweets_container
# %h2
# .twitter_icon
# On Twitter
# %p.recent_tweets
#
# $('.recent_tweets_container').recentTweets
# userName: "martin_sunset"
#
# Thats it.
$ = jQuery
$.fn.extend
recentTweets: (options) ->
self = $.fn.recentTweets
opts = $.extend {}, self.default_options, options
@each (i,el) ->
self.init el, opts
self.log el if opts.log
$.extend $.fn.recentTweets,
default_options:
userName: "martin_sunset"
numTweets: 10
loaderText: "Loading tweets..."
tweetContentArea: ".recent_tweets"
animationSpeed: "slow"
cycleInterval: 10000 # must be at least twice animationSpeed and safety
log: false
init: (el,opts) ->
$content = $(el).find(opts.tweetContentArea)
$content.text opts.loaderText
url = "http://twitter.com/status/user_timeline/#{opts.userName}.json?count=#{opts.numTweets}&callback=?"
$.getJSON url, (data) ->
tweets = []
$.each data, (i, post) ->
txt = post.text
txt = txt.replace /[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&~\?\/.=]+/g, (url) ->
url.link url
txt = txt.replace /[@]+[A-Za-z0-9-_]+/g, (u) ->
u.link "http://twitter.com/#{u.replace('@','')}"
txt = txt.replace /[#]+[A-Za-z0-9-_]+/g, (t) ->
t.link "http://search.twitter.com/search?q=#{t.replace('#','%23')}"
tweets.push "<span>#{txt}</span>"
if tweets.length is 0
$content.text "No tweets available"
else
counter = 0
$text = $(tweets[counter]).hide()
$content.html $text
$text.fadeIn(opts.animationSpeed)
setInterval (->
#counter++
#counter = 0 if counter >= tweets.length
#replaced by the following thx to sxross
counter = ++counter % tweets.length
$content.children().fadeOut opts.animationSpeed,->
$content.empty()
$text = $(tweets[counter]).hide()
$content.html $text
$text.fadeIn(opts.animationSpeed)
),opts.cycleInterval
log: (msg) ->
console?.log? msg # Courtesy of satyr
@satyr
Copy link

satyr commented May 23, 2011

console?.log? msg

@mwawrusch
Copy link
Author

mwawrusch commented May 23, 2011 via email

@mwawrusch
Copy link
Author

awesome!, thx satyr

@sxross
Copy link

sxross commented May 23, 2011

In your setInterval, how about counter = ++counter % tweets.length ?

@mwawrusch
Copy link
Author

sweet, thx sxross

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment