Skip to content

Instantly share code, notes, and snippets.

@envex
Created November 29, 2009 18:28
Show Gist options
  • Save envex/245002 to your computer and use it in GitHub Desktop.
Save envex/245002 to your computer and use it in GitHub Desktop.
DiggDugg Class
/* --
DiggDugg Class
Written by: Matt Vickers
Website: EnvexLabs.com
Date: November 28, 2009
DIGG, DIGG IT, DUGG, DIGG THIS, Digg graphics, logos, designs, page headers, button icons, scripts, and other service names are the trademarks of Digg Inc.
-- */
var diggDugg = new Class({
Implements: [Log, Events],
options: {},
initialize: function(){
//set the various buttons so that we can use them in the class
this.digg_it_buttons = $$('.digg_it_button');
this.bury_it_buttons = $$('.bury_it_button');
this.share_it_buttons = $$('.share_it_button');
this.share_containers = $$('.share_container');
//setup all the buttons
//so that we can attach some events to them
this.setupDiggButtons();
this.setupBuryButtons();
this.setupShareButtons();
//setup the page
//are any of the articles buried
//hide the shared box
this.isBurried();
this.hideShares();
},
//We use this function to get the ID of the article
getID: function(el){
//split the ID
//ex. article:15
//id = 15
var s = el.id.split(':');
var id = s[1];
//return the id
return id;
},
setupDiggButtons: function(){
//reset this to self so we can reuse it inside out addEvent function
var self = this;
//loop through each digg_it button on the page
this.digg_it_buttons.each(function(el){
//grab the id
var id = self.getID(el);
//add the event to each of the buttons
el.addEvent('click', function(e){
e.stop();
//execute diggIt on click
self.diggIt(id);
});
});
},
setupBuryButtons: function(){
//reset this to self so we can reuse it inside out addEvent function
var self = this;
//loop through each digg_it button on the page
this.bury_it_buttons.each(function(el){
//grab the id
var s = el.id.split(':');
var id = s[1];
//add the event to each of the buttons
el.addEvent('click', function(e){
e.stop();
self.buryIt(id);
});
});
},
setupShareButtons: function(){
//reset this to self so we can reuse it inside out addEvent function
var self = this;
//loop through each digg_it button on the page
this.share_it_buttons.each(function(el){
//grab the id
var s = el.id.split(':');
var id = s[1];
//add the event to each of the buttons
el.addEvent('mouseover', function(e){
e.stop();
self.showShareIt(id);
});
});
},
isBurried: function(){
//fade out any article that was already buried
$$('.buried').each(function(e){
//fade it out
e.set('opacity','.5');
//set all the text
e.getElement('a.bury_it_button').set('text','Buried!');
//this is a real dirty way of doing this
//but until i find a different way, it works :/
var d = new Element('div',{'class':'disable_overlay'});
d.inject(e);
});
},
hideShares: function(){
var self = this;
this.share_containers.each(function(e){
//grab the id
var s = e.id.split(':');
var id = s[1];
//hide the container
e.set('opacity',0);
//add an event so it gets hidden after the user
//mouse's out
e.addEvent('mouseleave',function(e){
e.stop();
self.hideShareIt(id);
});
});
},
diggIt: function(id){
var count_container = $('digg_count:'+id);
var digg_it_button = $('digg_it:'+id);
//get the current count of the digg
var count = count_container.get('text');
//make the json request to change the count in the database
new Request.JSON({
url: 'inc/php/json.php',
onSuccess: function(response){
if(response.result = 'success'){
//update the new count
count_container.set('text',response.new_count);
digg_it_button.set('text','Dugg!');
digg_it_button.removeEvents('click');
digg_it_button.setProperty('href','#');
}
}
}).post({'state':'diggIt','count':count,'article_id':id});
},
buryIt: function(id){
var bury_link = $('bury_it:'+id);
//make the json request to change the count in the database
new Request.JSON({
url: 'inc/php/json.php',
onSuccess: function(response){
if(response.result = 'success'){
//change the text of the link to buried and disable it from
//being clicked again
bury_link.set('text','Buried!');
bury_link.removeEvents('click');
bury_link.setProperty('href','#');
//fade out the article
$('article:'+id).tween('opacity','.5');
}
}
}).post({'state':'buryIt','article_id':id});
},
showShareIt: function(id){
var share_container = $('share_container:'+id);
share_container.tween('opacity',1);
},
hideShareIt: function(id){
var share_container = $('share_container:'+id);
share_container.tween('opacity',0);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment