Created
June 22, 2012 15:30
-
-
Save anonymous/2973494 to your computer and use it in GitHub Desktop.
Diff pathc for new show_update_bar for the Tweet 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
Index: public/js/libs/seaofclouds/jquery.tweet.js | |
=================================================================== | |
--- public/js/libs/seaofclouds/jquery.tweet.js (revision 19036) | |
+++ public/js/libs/seaofclouds/jquery.tweet.js (working copy) | |
@@ -32,6 +32,7 @@ | |
twitter_api_url: "api.twitter.com", // [string] custom twitter api url, if any (apigee, etc.) | |
twitter_search_url: "search.twitter.com", // [string] custom twitter search url, if any (apigee, etc.) | |
template: "{avatar}{time}{join}{text}", // [string or function] template used to construct each tweet <li> - see code for available vars | |
+ show_update_bar: false, // [boolean] whether a bar to tell users new tweets are available is shown, istead of loading them | |
comparator: function(tweet1, tweet2) { // [function] comparator used to sort tweets (see Array.sort) | |
return tweet2["tweet_time"] - tweet1["tweet_time"]; | |
}, | |
@@ -41,6 +42,8 @@ | |
// You can attach callbacks to the following events using jQuery's standard .bind() mechanism: | |
// "loaded" -- triggered when tweets have been fetched and rendered | |
}, o); | |
+ | |
+ s.force_load = true; | |
// See http://daringfireball.net/2010/07/improved_regex_for_matching_urls | |
var url_regexp = /\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/gi; | |
@@ -107,7 +110,7 @@ | |
} | |
function extract_relative_time(date) { | |
- var toInt = function(val) { return parseInt(val, 10); }; | |
+ var toInt = function(val) {return parseInt(val, 10);}; | |
var relative_to = new Date(); | |
var delta = toInt((relative_to.getTime() - date) / 1000); | |
if (delta < 1) delta = 0; | |
@@ -143,13 +146,16 @@ | |
} | |
} | |
- function build_api_url() { | |
+ function build_api_url(latest_only) { | |
var proto = ('https:' == document.location.protocol ? 'https:' : 'http:'); | |
var count = (s.fetch === null) ? s.count : s.fetch; | |
var common_params = '&include_entities=1&callback=?'; | |
if (s.language !== null) { | |
common_params = '&lang=' + s.language + common_params; | |
} | |
+ if (latest_only && s.last_tweet_id) { | |
+ common_params = '&since_id=' + s.last_tweet_id + common_params; | |
+ } | |
if (s.list) { | |
return proto+"//"+s.twitter_api_url+"/1/"+s.username[0]+"/lists/"+s.list+"/statuses.json?page="+s.page+"&per_page="+count+common_params; | |
} else if (s.favorites) { | |
@@ -220,22 +226,48 @@ | |
var outro = '<p class="tweet_outro">'+s.outro_text+'</p>'; | |
var loading = $('<p class="loading">'+s.loading_text+'</p>'); | |
if (s.loading_text) $(widget).not(":has(.tweet_list)").empty().append(loading); | |
- $.getJSON(build_api_url(), function(data){ | |
- var list = $('<ul class="tweet_list">'); | |
- var tweets = $.map(data.results || data, extract_template_data); | |
- tweets = $.grep(tweets, s.filter).sort(s.comparator).slice(0, s.count); | |
- list.append($.map(tweets, function(o) { return "<li>" + t(s.template, o) + "</li>"; }).join('')). | |
+ var latest_only = (!s.force_load) | |
+ $.getJSON(build_api_url(latest_only), function(data){ | |
+ if (s.force_load || ! s.show_update_bar) { | |
+ // Normal behaviour, load the tweets | |
+ var update_bar = $('<div class="tweet_update_bar">').hide(); | |
+ var list = $('<ul class="tweet_list">').append(update_bar); | |
+ var tweets = $.map(data.results || data, extract_template_data); | |
+ tweets = $.grep(tweets, s.filter).sort(s.comparator).slice(0, s.count); | |
+ list.append($.map(tweets, function(o) {return "<li>" + t(s.template, o) + "</li>";}).join('')). | |
children('li:first').addClass('tweet_first').end(). | |
children('li:odd').addClass('tweet_even').end(). | |
children('li:even').addClass('tweet_odd'); | |
+ | |
+ if (tweets[0]) s.last_tweet_id = tweets[0].tweet_id; | |
+ s.force_load = false; | |
- $(widget).empty().append(list); | |
- if (s.intro_text) list.before(intro); | |
- if (s.outro_text) list.after(outro); | |
+ $(widget).empty().append(list); | |
+ if (s.intro_text) list.before(intro); | |
+ if (s.outro_text) list.after(outro); | |
+ } else { | |
+ // Show the "## new tweets" bar | |
+ | |
+ // Note we do not slice the array as it contains the new ones only | |
+ // and we want them all (well, we only need to know how many) | |
+ var new_tweets = (data.results || data); | |
+ if (new_tweets.length > 0) { | |
+ var text = new_tweets.length + " new Tweet"; | |
+ if (new_tweets.length > 1) text = text + "s"; | |
+ $('.tweet_update_bar') | |
+ .html(text) | |
+ .show() | |
+ .unbind('click') | |
+ .bind('click', function(){ | |
+ s.force_load = true; | |
+ load(widget); | |
+ }); | |
+ } | |
+ } | |
$(widget).trigger("loaded").trigger((tweets.length === 0 ? "empty" : "full")); | |
if (s.refresh_interval) { | |
- window.setTimeout(function() { $(widget).trigger("tweet:load"); }, 1000 * s.refresh_interval); | |
+ window.setTimeout(function() {$(widget).trigger("tweet:load");}, 1000 * s.refresh_interval); | |
} | |
}); | |
} | |
@@ -251,3 +283,5 @@ | |
}); | |
}; | |
})); | |
+ | |
+ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment