Advertisement
framp

Buddypress show/hide activity comments - per Pollycoke

May 26th, 2011
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. jQuery(document).ready(function () {
  2.     function setCookie(name, value) {
  3.         var exp = new Date();
  4.         exp.setTime(exp.getTime() + (1000 * 60 * 60 * 24 * 30));
  5.         document.cookie = name + "=" + escape(value) + "; path=/" + ((exp == null) ? "" : "; expires=" + exp.toGMTString());
  6.     }
  7.  
  8.     function getCookie(name) {
  9.         var dc = document.cookie;
  10.         var cname = name + "=";
  11.         if (dc.length > 0) {
  12.             begin = dc.indexOf(cname);
  13.             if (begin != -1) {
  14.                 begin += cname.length;
  15.                 end = dc.indexOf(";", begin);
  16.                 if (end == -1) end = dc.length;
  17.                 return unescape(dc.substring(begin, end));
  18.             }
  19.         }
  20.         return null;
  21.     }
  22.  
  23.     //testo per il tasto mostra commenti
  24.     var showText = '►';
  25.  
  26.     //testo per il tasto nascondi commenti
  27.     var hideText = '▼';
  28.  
  29.     //può contenere tag html (eg: <img src="/img/showcommentsicon.png">)
  30.     var showIcon = '<img src="http://pollycoke.org/images/no-commenti.png">';
  31.  
  32.     //può contenere tag html (eg: <img src="/img/hidecommentsicon.png">)
  33.     var hideIcon = '<img src="http://pollycoke.org/images/commenti.png">';
  34.  
  35.     //nome del cookie che contiene i dati
  36.     var cookie = 'hideComments';
  37.  
  38.     //una volta che avrai inserito il codice html del tasto (con id="showHideComments")
  39.     //potrai rimuovere la riga "button.appendTo..." e questa riga diventerà:
  40.     //var button = jQuery('#showHideComments');
  41.     var button = jQuery('<a href="#" id="showHideComments"></a>');
  42.  
  43.     //il tasto viene aggiunto a fianco a INFO e RSS
  44.     button.prependTo(jQuery('#content>div>ul:eq(1)'));
  45.  
  46.     //selettore dei commenti
  47.     var comments = jQuery('.activity-comments');
  48.     if (comments.length<2) return;
  49.  
  50.     //una volta che avrai inserito il codice html del tasto sotto ogni attività
  51.     //(con  class="showHideCommentsActivityButton")
  52.     //potrai rimuovere la riga "var activityButton.. e la riga "activityButton.appendTo.."
  53.     var activityButton = jQuery('<a href="#" class="showHideCommentsActivityButton"></a>');
  54.  
  55.     //il tasto viene aggiunto sotto ogni attività a destra dei bottoni attuali
  56.     activityButton.prependTo(jQuery('.has-comments>.activity-content>.activity-meta'));
  57.  
  58.     //selettore dei bottoni
  59.     var activityButtons = jQuery('.showHideCommentsActivityButton');
  60.  
  61.     if (getCookie(cookie) == 1) {
  62.         comments.hide();
  63.         button.html(showIcon);
  64.         setCookie(cookie, 1);
  65.         activityButtons.html(showText);
  66.     } else {
  67.         button.html(hideIcon);
  68.         setCookie(cookie, 0);
  69.         activityButtons.html(hideText);
  70.     }
  71.  
  72.     button.click(function () {
  73.         if (getCookie(cookie) == 1) {
  74.             setCookie(cookie, 0);
  75.             comments.show();
  76.             button.html(hideIcon);
  77.             activityButtons.html(hideText);
  78.         } else {
  79.             setCookie(cookie, 1);
  80.             comments.hide();
  81.             button.html(showIcon);
  82.             activityButtons.html(showText);
  83.         }
  84.         return false;
  85.     });
  86.  
  87.     activityButtons.click(function () {
  88.         var theActivityButton = jQuery(this);
  89.         if (theActivityButton.html() == showText) {
  90.             theActivityButton.parent().parent().next().show();
  91.             theActivityButton.html(hideText);
  92.         } else {
  93.             theActivityButton.parent().parent().next().hide();
  94.             theActivityButton.html(showText);
  95.         }
  96.         return false;
  97.     });
  98.  
  99.     jQuery('.acomment-reply').click(function () {
  100.         var theReplyButton = jQuery(this);
  101.         theReplyButton.parent().parent().next().show();
  102.         theReplyButton.prev('.showHideCommentsActivityButton').html(showText);
  103.     });
  104. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement