Created
May 18, 2011 22:18
-
-
Save mwawrusch/979717 to your computer and use it in GitHub Desktop.
Coffeescript implementation of a twitter jquery plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
Author
mwawrusch
commented
May 23, 2011
via email
OMFG, that's awesome. I love coffeescript.
Thx
…On Sun, May 22, 2011 at 9:31 PM, satyr < ***@***.***>wrote:
`console?.log? msg`
##
Reply to this email directly or view it on GitHub:
https://gist.github.com/979717
awesome!, thx satyr
In your setInterval, how about counter = ++counter % tweets.length
?
sweet, thx sxross
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment