Skip to content

Instantly share code, notes, and snippets.

@clauswitt
Created February 16, 2011 15:46
Show Gist options
  • Select an option

  • Save clauswitt/829587 to your computer and use it in GitHub Desktop.

Select an option

Save clauswitt/829587 to your computer and use it in GitHub Desktop.
dojo.provide('com.clauswitt.TwitterTickerWidget');
dojo.require("dijit._Widget");
dojo.require("dijit._Templated");
dojo.require("dojo.io.script");
dojo.declare("com.clauswitt.TwitterTickerWidget", [dijit._Widget, dijit._Templated], {
templateString: "<div>${name}Widget</div>",
tweeter: 'clauswitt',
tweetCount: 3,
tweetSearchUrl: 'http://search.twitter.com/search.json',
preamble: function() {
},
postCreate : function() {
this.__fetchTweets();
},
constructor: function() {
},
postMixInProperties: function() {
},
__fetchTweets: function() {
var callBack = dojo.hitch(this, this.__tweetCallBack);
dojo.io.script.get({
callbackParamName: 'callback',
url: this.tweetSearchUrl + '?q=from:' + this.tweeter + '&rpp=' + this.tweetCount,
handleAs: "json",
load: callBack,
error: function(err) {
if (err.dojoType == 'timeout') {
return;
}
if (err.dojoType == 'cancel') {
return;
}
}
});
},
__tweetCallBack: function(data) {
tweets = this.__buildTweetList(data.results);
dojo.empty(this.domNode);
dojo.place(tweets, this.domNode);
},
__buildTweet: function(singleNode) {
var tweet = dojo.create('div', {'class':'singleTweet'});
var tweeterImg = dojo.create('img', {'src': singleNode['profile_image_url'], 'class': 'tweeterImage'});
var tweetText = dojo.create('div', {'innerHTML': this.__autolink(singleNode['text']), 'class': 'tweetText'});
var tweeterName = dojo.create('span', {'innerHTML': singleNode['from_user']});
var tweetetAt = dojo.create('span', {'innerHTML': singleNode['created_at'], 'class': 'tweetTime'});
dojo.place(tweeterImg, tweet);
dojo.place(tweetText, tweet);
dojo.place(tweeterName, tweet);
dojo.place(tweetetAt, tweet);
return tweet;
},
__buildTweetList: function(nodes) {
var tweets = dojo.create('div', {'class': 'tweetList'});
var that = this;
nodes.forEach(function(item) {
dojo.place(that.__buildTweet(item), tweets);
});
return tweets;
},
__autolink: function(s) {
var hlink = /\s(ht|f)tp:\/\/([^ \,\;\:\!\)\(\"\'\<\>\f\n\r\t\v])+/g;
return (s.replace(hlink, function ($0, $1, $2) {
s = $0.substring(1, $0.length);
// remove trailing dots, if any
while (s.length > 0 && s.charAt(s.length - 1) == '.')
s = s.substring(0, s.length - 1);
// add hlink
return " " + s.link(s);
}));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment