Advertisement
soyuka

FLuxBB Plugin Like

May 5th, 2012
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Ajax add-on to the Fluxbb Like plugin
  3.     Copyright (C) 2012 soyuka
  4.  
  5.     This program is free software: you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation, either version 3 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. */
  18.  
  19. (function($){
  20.   $.fn.wsLiker = function(id, pseudo, likeString, unlikeString, likeThisPostString) {
  21.  
  22.     $(this).live('click', function(event) {
  23.  
  24.             //Pas de lien
  25.             event.preventDefault();
  26.            
  27.             //Url à envoyer
  28.             var url = $(this).attr("href");
  29.        
  30.             //Ajax envoi
  31.             $.get(url, function(data) {
  32.                 //Data(+data+)
  33.             });
  34.            
  35.             //J'aime ou J'aime pas ?
  36.             var value = $(this).text();
  37.            
  38.             var like = 0;
  39.            
  40.             //On change le texte, l'url et on récupère la valeur du j'aime
  41.             if(value == "J'aime") {
  42.                 $(this).text(unlikeString);
  43.                 $(this).attr('href', url + '&dislike=1')
  44.                 like = 0;
  45.             } else {
  46.                 $(this).text(likeString);
  47.                 $(this).attr('href', url.slice(0, - 10) ); //fait à l'arrache je l'avoue
  48.                 like = 1;
  49.             }
  50.            
  51.            
  52.             var pseudoHtml = '<span title="'+pseudo+'">, <a href="profile.php?id='+id+'">'+pseudo+'</a></span>';
  53.  
  54.            
  55.             //Si on aime, on regarde si le block d'aimer existe, on le créé, on ajoute le pseudo
  56.             if(like == 0) {
  57.  
  58.                 //La div "Like"
  59.                 var liker = $(this).parents(".blockpost").next();
  60.            
  61.                 if(liker.hasClass('liker')) {
  62.  
  63.                     //Bloc existe, on ajoute le pseudo...
  64.                     var parent = liker.find(".postmsg p");
  65.                                        
  66.                     //Je clone pour l'apparence
  67.                     tpl = $(pseudoHtml).clone();
  68.                    
  69.                     //On cache
  70.                     tpl.hide();
  71.                    
  72.                     tpl.appendTo(parent).fadeIn().css('display', 'inline');
  73.                    
  74.                 } else {
  75.                     //Template etc.
  76.                    
  77. var template = '<div class="blockpost  rowodd liker">'+
  78.     '<div class="box">'+
  79.         '<div class="inbox">'+
  80.             '<div class="postbody">'+
  81.                 '<div class="postleft">'+
  82.                     '<dl>'+
  83.                         '<dd class="usertitle"><strong>'+likeThisPostString+'</strong></dd>'+
  84.                     '</dl>'+
  85.                 '</div>'+
  86.                 '<div class="postright">'+
  87.                     '<div class="postmsg">'+
  88.                         '<p><span title="'+pseudo+'"><a href="profile.php?id='+id+'">'+pseudo+'</a></span></p>'+
  89.                     '</div>'+
  90.                 '</div>'+
  91.             '</div>'+
  92.         '</div>'+
  93.     '</div>'+
  94. '</div>';
  95.                     //Bloc n'existe pas faut le créer...
  96.                     var parent = $(this).parents(".blockpost");
  97.                    
  98.                     //Création du clone pour les effets
  99.                     tpl = $(template).clone();
  100.                    
  101.                     //On le cache
  102.                     tpl.hide();
  103.                    
  104.                     //On l'insère
  105.                     tpl.insertAfter(parent).delay(100).fadeIn();
  106.                 }
  107.             //Sinon on trouve le pseudo et on le jarte 
  108.             } else {
  109.                 //La div "like" qui existe forcément
  110.                 var liker = $(this).parents(".blockpost").next('.liker');
  111.                
  112.                 //Le lien du pseudo à chercher
  113.                 //var linkPseudo = $('a[href="profile.php?id='+id+'"]');
  114.                                
  115.                 var linkPseudo = $("span[title="+pseudo+"]");
  116.  
  117.                 //On enlève le pseudo
  118.                 liker.find(linkPseudo).fadeOut('fast', function () {
  119.                     $(this).remove();
  120.                 });
  121.  
  122.                 /*
  123.                 Compter le nb de a si 0 => liker disparait
  124.                 */
  125.                 var nb = liker.find('a').length;
  126.  
  127.                 if(nb == 0 || nb == 1)
  128.                     liker.fadeOut('fast').fadeOut('fast', function () {
  129.                         $(this).empty().remove();
  130.                     });
  131.                    
  132.                 //Apparement .empty().remove() = gain de 2% en vitesse
  133.             }
  134.         });
  135.   };
  136. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement