Skip to content

Instantly share code, notes, and snippets.

@nz
Created April 8, 2009 23:36
Show Gist options
  • Save nz/92136 to your computer and use it in GitHub Desktop.
Save nz/92136 to your computer and use it in GitHub Desktop.
(function($){
$(document).ready(function() {
////////Global Variables//////
$callFinished = false;
$run = false;
window.twitter = [];
$totalAPICalls = 1;
$totalToDisplay = 5;//total tweets to show
$.fn.reverse = [].reverse;
window.url = "";
////////SETTINGS PAGE////////
$('#settings')
.find('#add_term').click(function(){
var term = $('input[name=temp_field]').val().replace(",",""); var exists = false;
$.each( $('.input_term'), function(i){ if( term == $(this).text() ){ exists = true; } } );
if( term != "" && ( exists === false ) ){
$(this).after('<span class="input_term"><a href="#"></a>'+term+'</span>');
var current_terms = $('input[name=terms]').val();
new_terms = ( current_terms == "" ) ? term : current_terms + "," + term;
$('input[name=terms]').val(new_terms.replace(",,",","));
}
return false;
}).end()
.find('.input_term > a').live('click',function(){
var delete_term = $(this).parent().text(); var terms = "";
$.each($('input[name=terms]').val().split(","), function(i){ if(!(this == delete_term)){ terms = (terms == "") ? this : terms + "," + this; } });
$('input[name=terms]').val(terms);
$(this).parent().slideUp(200, function(){$(this).remove();});
return false;
}).end()
.find("input[name=temp_field]").bind("keydown", function(e) {
if (e.keyCode == 13 || e.charCode == 13) { $("#add_term").click(); $(this).val(""); return false; };
}).each(function(){ $(this).val($(this).attr("title")); var $text_value = $(this).val(); if ( $(this).attr("title") == $text_value ) $(this).clearonfocus(); }).end();
});
/////////MODERATE PAGE////////////
$.fetch_tweets = function(url){
$callFinished = false;
$.getJSON(url, function(json) {
$callFinished = true; var big_array = new Array();
$.each($(json),function(){
var tmp_array = [this.tweet.id,this.tweet.profile_image_url,this.tweet.text,this.tweet.from_user, this.tweet.created_at, this.tweet.twitter_id];
big_array.push(tmp_array);
});
big_array.sort();
$.each(big_array, function() {
if($('ul.unbirthed #tw'+this[0]).length == 0 ){
var new_text = this[2].replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g,function(F){return'<a href="'+F+'">'+F+"</a>"}).replace(/\B@([_a-z0-9]+)/ig,function(F){return F.charAt(0)+'<a href="http://www.twitter.com/'+F.substring(1)+'">'+F.substring(1)+"</a>"});
var divstr = '<li id="tw'+this[0]+'"><span class="tweet">'+
'<img width="50" height=50" src="'+this[1]+'" >'+
'<p>'+ new_text +" from <span class='user_name'>" + this[3] + "</span>" +'</p></span><span class="x">x</span></li>';
window.twitter['last_id'] = this[0];//store last id pulled
$('ul.unbirthed').append(divstr);
}
});
if(typeof(window.twitter['last_id']) != "undefined"){/*$('#unbirthed_tweets').find('span.debug').remove().end().append("<span class='debug' style='display:none'>The last id we used was " + window.twitter['last_id'] + "</span>");*/}
else {/*$('#unbirthed_tweets').find('span.debug').remove().end().append("<span class='debug'>Your term did not return any results on that last try...</span>");*/}
});
};
$.concat_collection = function(obj1, obj2) {
var i; var arr = new Array(); var len1 = obj1.length; var len2 = obj2.length; for (i=0; i<len1; i++) {arr.push(obj1[i]);}
for (i=0; i<len2; i++) {arr.push(obj2[i]);}
return arr;
}
//function to show the difference b/w the time that twitter
//returns for the tweet and now... - aka, relative time.
$.relative_time = function(C){
//dateformat: 2009-03-28T21:34:24Z
var B=C.split(" ");
C=B[1]+" "+B[2]+", "+B[4]+" "+B[3];
var A=Date.parse(C);
var D=(arguments.length>1)?arguments[1]:new Date();
var E=parseInt((D.getTime()-A)/1000);
E=E+(D.getTimezoneOffset()*60);
if(E<60){return"less than a minute ago"}else{if(E<120){return"about a minute ago"}else{if(E<(60*60)){return(parseInt(E/60)).toString()+" minutes ago"}else{if(E<(120*60)){return"about an hour ago"}else{if(E<(24*60*60)){return"about "+(parseInt(E/3600)).toString()+" hours ago"}else{if(E<(48*60*60)){return"1 day ago"}else{return(parseInt(E/86400)).toString()+" days ago"}}}}}}
};
$.scrollingTweets = function(){
if($run === true){
$('ul.unbirthed li:first').slideUp(500,function(){
$top_tweet = $(this);
if( $top_tweet.is(".bad") ){
$top_tweet = $top_tweet.next('li:not(".bad")'); $(this).remove();
}
$top_tweet.appendTo('ul.live')/*.find('span.x').remove().end()*/.slideDown();
});
}
setTimeout('jQuery.scrollingTweets()',2900);
if( $('ul.live li').length > 20 ){
$('ul.live li:first').remove();
}
};
$.pullTweets = function(){
if( $('ul.live li').length < 10 && $callFinished === true )
setTimeout('jQuery.pullTweets()',10000);
else
setTimeout('jQuery.pullTweets()',15000);
if($run === true)
$.fetch_tweets(window.url);
};
$.controlButton = function(e, that){
if( $(e.target).is('.start') ){
$run = true;
$.pullTweets();
//after about 8 seconds, we'll start to scroll that sucker up
setTimeout('jQuery.scrollingTweets();',8000);
that.fadeOut(400).fadeIn(400).fadeOut(400).fadeIn(300, function(){
that.removeClass().addClass('pause');
});
}
else if( $(e.target).is('.pause') ){
that.removeClass().addClass('resume');
$run = false;
}
else if( $(e.target).is('.resume') ){
that.removeClass().addClass('pause');
$run = true;
}
return false;
}
$.resetDB = function(that){
that.html("resetting...").fadeOut(400).fadeIn(400).fadeOut(400, function(){ that.html("Clear Database"); }).fadeIn();
//clear the db
$.post($baseurl+"/purge", function(){
//
});
return false;
}
$.rejectTweets = function(e){
if ( $(e.target).is('span.x') ){
$this = $(e.target);
live = ( $this.parents('ul').is('.live') ) ? true : false;
$this_li_id=$this.parent('li').attr('id');
var id_num = $this_li_id.replace('tw','');
$this.parent('li').addClass('bad');
if(live === true)
$.post($baseurl+"/"+id_num+"/reject", function(data){ setTimeout('jQuery("ul.live").find("li#'+$this_li_id+'").slideUp(100, function(){ jQuery(this).remove(); })',300); });
else
$.post($baseurl+"/"+id_num+"/reject", function(data){ setTimeout('jQuery("ul.unbirthed").find("li#'+$this_li_id+'").slideUp(100, function(){ jQuery(this).remove(); })',300); });
return false;
}
}
jQuery.fn.clearonfocus = function() {
return this
.bind('focus', function() {
if ( !this.defaultValue ) this.defaultValue = this.value;
if ( this.defaultValue && this.defaultValue != this.value ) return;
this.value = '';
})
.bind('blur', function() {
if ( this.value.match(/^\s*$/) )
this.value = this.defaultValue;
});
};
})( jQuery.noConflict() );
(function($){
$(document).ready(function() {
//click events for buttons
$('#control').click(function(e){
$.controlButton(e, $(this));
});
$('#reset').click(function(){
$.resetDB($(this));
});
//click events for the big ol' "X"s and use event delegation instead of binding a bagillion click events
$('ul.unbirthed, ul.live').click(function(e){
$.rejectTweets(e);
})
});
})( jQuery.noConflict() );
(function($){
$(document).ready(function() {
$baseurl = "<%= event_tweets_path(@event) %>";
window.url = "<%= pending_event_tweets_path(@event) %>";
});
})( jQuery.noConflict() );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment