diff options
author | Arnaud Bos <arnaud.bos@aeon-consulting.fr> | 2012-10-20 02:52:02 +0200 |
---|---|---|
committer | Arnaud Bos <arnaud.bos@aeon-consulting.fr> | 2012-10-20 02:52:02 +0200 |
commit | 3ffd14fa4da0ca71e1f97a070973e4994bae8f61 (patch) | |
tree | dd7fea1a54417fc5df25eb559870c6ad06019ef1 /syte/static/js/libs/twitter.js | |
parent | 01a8792a5116a305368d9d98fa366923086e0633 (diff) | |
download | pelican-themes-3ffd14fa4da0ca71e1f97a070973e4994bae8f61.tar.gz |
add client side social integration, use webassets
Diffstat (limited to 'syte/static/js/libs/twitter.js')
-rw-r--r-- | syte/static/js/libs/twitter.js | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/syte/static/js/libs/twitter.js b/syte/static/js/libs/twitter.js new file mode 100644 index 0000000..2b61c3d --- /dev/null +++ b/syte/static/js/libs/twitter.js @@ -0,0 +1,112 @@ +/* +UI functions dedicated to the Twitter modal panel +*/ + +var twitter_api_user = 'https://twitter.com/users/'; +var twitter_api_timeline = 'https://api.twitter.com/1/statuses/user_timeline.json?screen_name='; +var twitter_api_json = '.json'; + +var spinner = (new Spinner(spin_opts)).spin(); +var template = null; +var url = null; +var twitter_data = {}; + +$('a[id^="Twitter-link"]').click(function (e) +{ + var url = prepare_link(e, this); + adjustSelection("Twitter-link"); + remove_modal(); + showTwitter(url, this); +}); + +function showTwitter(e, t) { + url = t.href; + var twitter_profile = $("#twitter-profile"); + if (twitter_profile.length > 0) { + twitter_profile.modal('show'); + } + else { + $("#Twitter-link").append(spinner.el); + + $.get('/theme/templates/twitter-view.html', function(data) { + // Request succeeded, data contains HTML template, we can load data + template = Handlebars.compile(data); + var user_url = twitter_api_user+twitter_username+twitter_api_json; + + try { + $.ajax({ + url: user_url, + dataType: "jsonp", + jsonpCallback: "readTwitterData", + error: function(s, statusCode, errorThrown) { + window.location.href = url; + spinner.stop(); + } + }); + } + catch (err) { + window.location.href = url; + spinner.stop(); + } + }) + .error(function() { + window.location.href = url; + spinner.stop(); + }); + } +} + +function readTwitterData(user) { + try { + user.statuses_count = numberWithCommas(user.statuses_count); + user.friends_count = numberWithCommas(user.friends_count); + user.followers_count = numberWithCommas(user.followers_count); + user.description = twitterLinkify(user.description); + twitter_data['user'] = user + + var tweets_url = twitter_api_timeline+twitter_username; + $.ajax({ + url: tweets_url, + dataType: "jsonp", + jsonpCallback: "readTweets", + error: function(s, statusCode, errorThrown) { + window.location.href = url; + spinner.stop(); + } + }); + } + catch (err) { + window.location.href = url; + spinner.stop(); + } +} + +function readTweets(tweets) { + try { + for(var index = 0 ; index < tweets.length ; index++) { + var tweet = tweets[index]; + tweet.formated_date = moment(tweet.created_at).fromNow(); + tweet.text = twitterLinkify(tweet.text); + } + twitter_data['tweets'] = tweets + + var html = template(twitter_data); + $('body').append(html); + $("#twitter-profile").modal(); + spinner.stop(); + } + catch (err) { + window.location.href = url; + spinner.stop(); + } +} + +function twitterLinkify(e) { + return e = e.replace(/(https?:\/\/\S+)/gi, function (e) { + return '<a href="' + e + '">' + e + "</a>" + }), e = e.replace(/(^|) @(\w+)/gi, function (e) { + return '<a href="http://twitter.com/' + e + '">' + e + "</a>" + }), e = e.replace(/(^|) #(\w+)/gi, function (e) { + return '<a href="http://search.twitter.com/search?q=' + e.replace(/#/, "%23") + '">' + e + "</a>" + }), e +} |