Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

satyr commented May 23, 2011

console?.log? msg

@mwawrusch

This comment has been minimized.

Copy link
Owner Author

mwawrusch commented May 23, 2011

@mwawrusch

This comment has been minimized.

Copy link
Owner Author

mwawrusch commented May 23, 2011

awesome!, thx satyr

@sxross

This comment has been minimized.

Copy link

sxross commented May 23, 2011

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

@mwawrusch

This comment has been minimized.

Copy link
Owner Author

mwawrusch commented May 23, 2011

sweet, thx sxross

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.