Guest User

Untitled

a guest
Apr 20th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. /* --
  2.  
  3. DiggDugg Class
  4. Written by: Matt Vickers
  5. Website: EnvexLabs.com
  6. Date: November 28, 2009
  7.  
  8. 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.
  9.  
  10. -- */
  11.  
  12. var diggDugg = new Class({
  13. Implements: [Log, Events],
  14. options: {},
  15. initialize: function(){
  16.  
  17. //set the various buttons so that we can use them in the class
  18. this.digg_it_buttons = $$('.digg_it_button');
  19. this.bury_it_buttons = $$('.bury_it_button');
  20. this.share_it_buttons = $$('.share_it_button');
  21. this.share_containers = $$('.share_container');
  22.  
  23. //setup all the buttons
  24. //so that we can attach some events to them
  25. this.setupDiggButtons();
  26. this.setupBuryButtons();
  27. this.setupShareButtons();
  28.  
  29. //setup the page
  30. //are any of the articles buried
  31. //hide the shared box
  32. this.isBurried();
  33. this.hideShares();
  34.  
  35. },
  36. //We use this function to get the ID of the article
  37. getID: function(el){
  38.  
  39. //split the ID
  40. //ex. article:15
  41. //id = 15
  42. var s = el.id.split(':');
  43. var id = s[1];
  44.  
  45. //return the id
  46. return id;
  47.  
  48. },
  49. setupDiggButtons: function(){
  50.  
  51. //reset this to self so we can reuse it inside out addEvent function
  52. var self = this;
  53.  
  54. //loop through each digg_it button on the page
  55. this.digg_it_buttons.each(function(el){
  56.  
  57. //grab the id
  58. var id = self.getID(el);
  59.  
  60. //add the event to each of the buttons
  61. el.addEvent('click', function(e){
  62. e.stop();
  63. //execute diggIt on click
  64. self.diggIt(id);
  65. });
  66.  
  67. });
  68.  
  69. },
  70. setupBuryButtons: function(){
  71.  
  72. //reset this to self so we can reuse it inside out addEvent function
  73. var self = this;
  74.  
  75. //loop through each digg_it button on the page
  76. this.bury_it_buttons.each(function(el){
  77.  
  78. //grab the id
  79. var s = el.id.split(':');
  80. var id = s[1];
  81.  
  82. //add the event to each of the buttons
  83. el.addEvent('click', function(e){
  84. e.stop();
  85. self.buryIt(id);
  86. });
  87.  
  88. });
  89.  
  90. },
  91. setupShareButtons: function(){
  92.  
  93. //reset this to self so we can reuse it inside out addEvent function
  94. var self = this;
  95.  
  96. //loop through each digg_it button on the page
  97. this.share_it_buttons.each(function(el){
  98.  
  99. //grab the id
  100. var s = el.id.split(':');
  101. var id = s[1];
  102.  
  103. //add the event to each of the buttons
  104. el.addEvent('mouseover', function(e){
  105. e.stop();
  106. self.showShareIt(id);
  107. });
  108.  
  109. });
  110.  
  111. },
  112. isBurried: function(){
  113.  
  114. //fade out any article that was already buried
  115. $$('.buried').each(function(e){
  116.  
  117. //fade it out
  118. e.set('opacity','.5');
  119.  
  120. //set all the text
  121. e.getElement('a.bury_it_button').set('text','Buried!');
  122.  
  123. //this is a real dirty way of doing this
  124. //but until i find a different way, it works :/
  125. var d = new Element('div',{'class':'disable_overlay'});
  126. d.inject(e);
  127.  
  128. });
  129.  
  130. },
  131. hideShares: function(){
  132.  
  133. var self = this;
  134.  
  135. this.share_containers.each(function(e){
  136.  
  137. //grab the id
  138. var s = e.id.split(':');
  139. var id = s[1];
  140.  
  141. //hide the container
  142. e.set('opacity',0);
  143.  
  144. //add an event so it gets hidden after the user
  145. //mouse's out
  146. e.addEvent('mouseleave',function(e){
  147. e.stop();
  148. self.hideShareIt(id);
  149. });
  150.  
  151. });
  152.  
  153. },
  154. diggIt: function(id){
  155.  
  156. var count_container = $('digg_count:'+id);
  157. var digg_it_button = $('digg_it:'+id);
  158.  
  159. //get the current count of the digg
  160. var count = count_container.get('text');
  161.  
  162. //make the json request to change the count in the database
  163. new Request.JSON({
  164. url: 'inc/php/json.php',
  165. onSuccess: function(response){
  166. if(response.result = 'success'){
  167.  
  168. //update the new count
  169. count_container.set('text',response.new_count);
  170.  
  171. digg_it_button.set('text','Dugg!');
  172. digg_it_button.removeEvents('click');
  173. digg_it_button.setProperty('href','#');
  174. }
  175. }
  176. }).post({'state':'diggIt','count':count,'article_id':id});
  177.  
  178. },
  179. buryIt: function(id){
  180.  
  181. var bury_link = $('bury_it:'+id);
  182.  
  183. //make the json request to change the count in the database
  184. new Request.JSON({
  185. url: 'inc/php/json.php',
  186. onSuccess: function(response){
  187.  
  188. if(response.result = 'success'){
  189.  
  190. //change the text of the link to buried and disable it from
  191. //being clicked again
  192. bury_link.set('text','Buried!');
  193. bury_link.removeEvents('click');
  194. bury_link.setProperty('href','#');
  195.  
  196. //fade out the article
  197. $('article:'+id).tween('opacity','.5');
  198.  
  199. }
  200.  
  201. }
  202. }).post({'state':'buryIt','article_id':id});
  203.  
  204. },
  205. showShareIt: function(id){
  206.  
  207. var share_container = $('share_container:'+id);
  208.  
  209. share_container.tween('opacity',1);
  210.  
  211. },
  212. hideShareIt: function(id){
  213.  
  214. var share_container = $('share_container:'+id);
  215.  
  216. share_container.tween('opacity',0);
  217.  
  218. }
  219. });
Add Comment
Please, Sign In to add comment