Skip to content

Instantly share code, notes, and snippets.

@jhgaylor
Forked from anonymous/gist:3173300
Created July 25, 2012 02:11
Show Gist options
  • Save jhgaylor/3173955 to your computer and use it in GitHub Desktop.
Save jhgaylor/3173955 to your computer and use it in GitHub Desktop.
//To turn on the mapping stuff uncomment lines marked //--//
(function($) {
$.widget("ui.tweeter", {
options: {
url_prefix:"/",
default_value: "buffalo wild wings",
color: "#fff",
width: "50%",
speed_up_size: 25,
time_on_tweet: 12000,
time_on_tweet_fast: 9000,
tweets_on_page: 5,
backgroundColor: "#ccc",
font_size:".8em",
geo: false,
result_size: 10,
current_page: -1,
time_on_page:15000,
cycle_started:false,
display_data: new Array(),
gmap:null,
map_zoom: 5,
page_size: 5,
tweets_between_promotions: 3,
tweets_since_promotion:0,
start_time:"",
start_time_local:0,
last_fill_time: 0,
},
_create: function() {
this.tweets = []
this.promos = []
this.dataset = []
this.tweets_since_promotion = 0
var now = new Date();
var now_utc = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
this.options.start_time_local = Math.round(new Date().getTime()/1000)
this.options.last_fill_time = this.options.start_time
this.queue_timer = 0
this.display_data = []
if(document.domain == "uneeklabs.com" || document.domain == "www.uneeklabs.com"){
this.options.url_prefix ='/messagebin/'
}
var self = this,
o = self.options, //reference to the widget's options
el = self.element, //reference to the widget container
tweet_holder = $('<div id="tweet_holder"></div>').appendTo(el);
self.start_feed();
},
destroy: function() {
this.element.next().remove();
},
_setOption: function(option, value) {
$.Widget.prototype._setOption.apply( this, arguments );
},
//the function that gets called on creation
start_feed: function () {
self = this;
self.fill_queue();
},
//gets data from the bin database
fill_queue: function () {
var self = this;
//update search term
filter = self.options.default_value
update_search_term()
//var d = new Date();
//d.setDate(d.getDate() - 10 )
//self.last_queue_fill = d;
epoch = self.options.last_fill_time
console.log(epoch)
var now = new Date();
epoch = 1341514116
var url = 'http://'+location.host+self.options.url_prefix+'bin/get/'+encodeURIComponent(filter)+'/'+encodeURIComponent(epoch)+'/';
$.get(url, function(data) {
data = $.parseJSON(data)
if(data.tweets.length > 0){
self.options.last_fill_time = data.current_time - 5
}
self.tweets = data.tweets
self.get_promos()
})
},
get_promos: function () {
self = this
var url = 'http://'+location.host+self.options.url_prefix+'displaysetting/get_active_promotions/'
$.getJSON(url, function(data){
self.promos = data
self.merge_sets()
})
},
merge_sets: function () {
for(var i = 0; i < self.tweets.length; i++){
if(this.tweets_since_promotion == this.options.tweets_between_promotions){
if(this.promos.length > 0){
current_promo = this.promos.shift()
this.dataset.push(current_promo)
this.promos.push(current_promo)
this.tweets_since_promotion = 0
if(this.promos.length == 0){
this.get_promos()
}
}
}
this.dataset.push(this.tweets.shift())
this.tweets_since_promotion += 1
}
//console.log(this.dataset)
this.process_queue()
},
process_queue: function () {
self = this;
if(self.dataset.length < 5){
clearTimeout(self.queue_timer)
clearTimeout(self.fill_timer)
self.fill_timer = setTimeout(function () {
self.fill_queue()
},5000)
}
if(self.dataset.length==0)
{
//this is where we will handle what to do if there is no data to display
console.log("we have a big problem here. there is no data")
}
else{
//change the speed of the widget if we need to.
self.update_widget_speed()
self.queue_timer = setTimeout(function () {
var obj = $("#tweet-space").data("tweeter")
obj.display_data.push(obj.dataset.shift());
var size = obj.options.tweets_on_page;
var len = obj.display_data.length;
if(len > size)
{
$("#tweet-space").data("tweeter").display_data = obj.display_data.slice(len-size);
}
console.log(self.dataset.length)
self.display_queue();
self.process_queue();
}, self.options.time_on_tweet);
}
},
update_widget_speed: function () {
self = this
if(self.dataset.length > self.options.speed_up_size)
{
self.options.time_on_tweet = self.options.time_on_tweet_fast;
}
return true
},
display_queue: function () {
data = self.display_data
$("#tweet_holder").empty();
var html_string = "";
var emphasis_string = "";
//console.log("display_queue")
//console.log(data)
for(var i=0; i<data.length;i++)
{
if(i+1==data.length)
{
emphasis_string = "emphasis";
}
else
{
emphasis_string = "";
}
//data[i].profile_image_url
var str = data[i].body;
str=str.replace(/\\'/g,'\'');
str=str.replace(/\\"/g,'"');
str=str.replace(/\\0/g,'\0');
str=str.replace(/\\\\/g,'\\');
big_image_url = 'https://api.twitter.com/1/users/profile_image/'+data[i].handle+'?size=bigger';
html_string += '<div class="tweet '+emphasis_string+' " id=""><div class="profile-pic"><img src="'+big_image_url+'"></div><div class="message"><p><span class="handle">@'+data[i].handle+'</span>'+str+'</p></div></div>';
}
$("#tweet_holder").append(html_string);
},
});
})(jQuery);
function update_search_term() {
//return true
//get_active_hastag
var url = 'http://'+location.host+self.options.url_prefix+'bin/get_active_hashtag/';
$.getJSON(url, function (data) {
//console.log(data)
$('#tweet-space').data('tweeter').options.default_value = data['current_hashtag']
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment