[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-templates/bp-legacy/js/ -> buddypress.js (source)

   1  /* jshint undef: false, unused:false */
   2  /* @version 1.7.0 */
   3  /* @version 10.0.0 */
   4  // AJAX Functions
   5  var jq = jQuery;
   6  
   7  // Global variable to prevent multiple AJAX requests
   8  var bp_ajax_request = null;
   9  
  10  // Global variables to temporarily store newest activities
  11  var newest_activities = '';
  12  var activity_last_recorded  = 0;
  13  
  14  var directoryPreferences = {};
  15  
  16  jq( function() {
  17      var activity_oldestpage = 1;
  18  
  19      /**** Page Load Actions *******************************************************/
  20  
  21      /* Activity filter and scope set */
  22      bp_init_activity();
  23  
  24      var objects  = [ 'members', 'groups', 'blogs', 'group_members' ],
  25          $whats_new = jq('#whats-new');
  26  
  27      /* Object filter and scope set. */
  28      bp_init_objects( objects );
  29  
  30      /* @mention Compose Scrolling */
  31      if ( $whats_new.length && bp_get_querystring('r') ) {
  32          var $member_nicename = $whats_new.val();
  33  
  34          jq('#whats-new-options').slideDown();
  35  
  36          $whats_new.animate({
  37              height:'3.8em'
  38          });
  39  
  40          jq.scrollTo( $whats_new, 500, {
  41              offset:-125,
  42              easing:'swing'
  43          } );
  44  
  45          $whats_new.val('').trigger( 'focus' ).val( $member_nicename );
  46      } else {
  47          jq('#whats-new-options').hide();
  48      }
  49  
  50      /**** Activity Posting ********************************************************/
  51  
  52      /* Textarea focus */
  53      $whats_new.on( 'focus', function() {
  54          jq( '#whats-new-options' ).slideDown();
  55  
  56          jq( this ).animate({
  57              height:'3.8em'
  58          });
  59  
  60          jq('#aw-whats-new-submit').prop('disabled', false);
  61  
  62          jq( this ).parent().addClass( 'active' );
  63          jq( '#whats-new-content' ).addClass( 'active' );
  64  
  65          var $whats_new_form = jq('form#whats-new-form'),
  66              $activity_all = jq( '#activity-all' );
  67  
  68          if ( $whats_new_form.hasClass('submitted') ) {
  69              $whats_new_form.removeClass('submitted');
  70          }
  71  
  72          // Return to the 'All Members' tab and 'Everything' filter,
  73          // to avoid inconsistencies with the heartbeat integration
  74          if ( $activity_all.length  ) {
  75              if ( ! $activity_all.hasClass( 'selected' ) ) {
  76                  // reset to everything
  77                  jq( '#activity-filter-select select' ).val( '-1' );
  78                  $activity_all.children( 'a' ).trigger( 'click' );
  79              } else if ( '-1' !== jq( '#activity-filter-select select' ).val() ) {
  80                  jq( '#activity-filter-select select' ).val( '-1' );
  81                  jq( '#activity-filter-select select' ).trigger( 'change' );
  82              }
  83          }
  84      } );
  85  
  86      /* For the "What's New" form, do the following on focusout. */
  87      jq( '#whats-new-form' ).on( 'focusout', function( e ) {
  88          var elem = jq( this );
  89  
  90          // Let child hover actions passthrough.
  91          // This allows click events to go through without focusout.
  92          setTimeout( function () {
  93              if ( ! elem.find(':hover').length ) {
  94                  // Do not slide up if textarea has content.
  95                  if ( '' !== $whats_new.val() ) {
  96                      return;
  97                  }
  98  
  99                  $whats_new.animate({
 100                      height:'2.2em'
 101                  });
 102  
 103                  jq( '#whats-new-options' ).slideUp();
 104  
 105                  jq('#aw-whats-new-submit').prop( 'disabled', true );
 106  
 107                  jq( '#whats-new-content' ).removeClass( 'active' );
 108                  $whats_new.parent().removeClass( 'active' );
 109              }
 110          }, 0 );
 111      } );
 112  
 113      /* New posts */
 114      jq('#aw-whats-new-submit').on( 'click', function() {
 115          var last_date_recorded = 0,
 116              button = jq(this),
 117              form   = button.closest('form#whats-new-form'),
 118              inputs = {}, post_data;
 119  
 120          // Get all inputs and organize them into an object {name: value}
 121          jq.each( form.serializeArray(), function( key, input ) {
 122              // Only include public extra data
 123              if ( '_' !== input.name.substr( 0, 1 ) && 'whats-new' !== input.name.substr( 0, 9 ) ) {
 124                  if ( ! inputs[ input.name ] ) {
 125                      inputs[ input.name ] = input.value;
 126                  } else {
 127                      // Checkboxes/dropdown list can have multiple selected value
 128                      if ( ! Array.isArray( inputs[ input.name ] ) ) {
 129                          inputs[ input.name ] = new Array( inputs[ input.name ], input.value );
 130                      } else {
 131                          inputs[ input.name ].push( input.value );
 132                      }
 133                  }
 134              }
 135          } );
 136  
 137          form.find( '*' ).each( function( i, elem ) {
 138              if ( elem.nodeName.toLowerCase() === 'textarea' || elem.nodeName.toLowerCase() ==='input' ) {
 139                  jq( elem ).prop( 'disabled', true );
 140              }
 141          } );
 142  
 143          /* Remove any errors */
 144          jq('div.error').remove();
 145          button.addClass('loading');
 146          button.prop('disabled', true);
 147          form.addClass('submitted');
 148  
 149          /* Default POST values */
 150          object = '';
 151          item_id = jq('#whats-new-post-in').val();
 152          content = jq('#whats-new').val();
 153          firstrow = jq( '#buddypress ul.activity-list li' ).first();
 154          activity_row = firstrow;
 155          timestamp = null;
 156  
 157          // Checks if at least one activity exists
 158          if ( firstrow.length ) {
 159  
 160              if ( activity_row.hasClass( 'load-newest' ) ) {
 161                  activity_row = firstrow.next();
 162              }
 163  
 164              timestamp = activity_row.prop( 'class' ).match( /date-recorded-([0-9]+)/ );
 165          }
 166  
 167          if ( timestamp ) {
 168              last_date_recorded = timestamp[1];
 169          }
 170  
 171          /* Set object for non-profile posts */
 172          if ( item_id > 0 ) {
 173              object = jq('#whats-new-post-object').val();
 174          }
 175  
 176          post_data = jq.extend( {
 177              action: 'post_update',
 178              'cookie': bp_get_cookies(),
 179              '_wpnonce_post_update': jq('#_wpnonce_post_update').val(),
 180              'content': content,
 181              'object': object,
 182              'item_id': item_id,
 183              'since': last_date_recorded,
 184              '_bp_as_nonce': jq('#_bp_as_nonce').val() || ''
 185          }, inputs );
 186  
 187          jq.post( ajaxurl, post_data, function( response ) {
 188              form.find( '*' ).each( function( i, elem ) {
 189                  if ( elem.nodeName.toLowerCase() === 'textarea' || elem.nodeName.toLowerCase() ==='input' ) {
 190                      jq( elem ).prop( 'disabled', false );
 191                  }
 192              } );
 193  
 194              /* Check for errors and append if found. */
 195              if ( response[0] + response[1] === '-1' ) {
 196                  form.prepend( response.substr( 2, response.length ) );
 197                  jq( '#' + form.attr('id') + ' div.error').hide().fadeIn( 200 );
 198              } else {
 199                  if ( 0 === jq('ul.activity-list').length ) {
 200                      jq('div.error').slideUp(100).remove();
 201                      jq('#message').slideUp(100).remove();
 202                      jq('div.activity').append( '<ul id="activity-stream" class="activity-list item-list">' );
 203                  }
 204  
 205                  if ( firstrow.hasClass( 'load-newest' ) ) {
 206                      firstrow.remove();
 207                  }
 208  
 209                  jq('#activity-stream').prepend(response);
 210  
 211                  if ( ! last_date_recorded ) {
 212                      jq('#activity-stream li:first').addClass('new-update just-posted');
 213                  }
 214  
 215                  if ( 0 !== jq('#latest-update').length ) {
 216                      var l   = jq('#activity-stream li.new-update .activity-content .activity-inner p').html(),
 217                          v     = jq('#activity-stream li.new-update .activity-content .activity-header p a.view').attr('href'),
 218                          ltext = jq('#activity-stream li.new-update .activity-content .activity-inner p').text(),
 219                          u     = '';
 220  
 221                      if ( ltext !== '' ) {
 222                          u = l + ' ';
 223                      }
 224  
 225                      u += '<a href="' + v + '" rel="nofollow">' + BP_DTheme.view + '</a>';
 226  
 227                      jq('#latest-update').slideUp(300,function(){
 228                          jq('#latest-update').html( u );
 229                          jq('#latest-update').slideDown(300);
 230                      });
 231                  }
 232  
 233                  jq('li.new-update').hide().slideDown( 300 );
 234                  jq('li.new-update').removeClass( 'new-update' );
 235                  jq('#whats-new').val('');
 236                  form.get(0).reset();
 237  
 238                  // reset vars to get newest activities
 239                  newest_activities = '';
 240                  activity_last_recorded  = 0;
 241              }
 242  
 243              jq('#whats-new-options').slideUp();
 244              jq('#whats-new-form textarea').animate({
 245                  height:'2.2em'
 246              });
 247              jq('#aw-whats-new-submit').prop('disabled', true).removeClass('loading');
 248              jq( '#whats-new-content' ).removeClass( 'active' );
 249          });
 250  
 251          return false;
 252      });
 253  
 254      /* List tabs event delegation */
 255      jq('div.activity-type-tabs').on( 'click', function(event) {
 256          var target = jq(event.target).parent(),
 257              scope, filter;
 258  
 259          if ( event.target.nodeName === 'STRONG' || event.target.nodeName === 'SPAN' ) {
 260              target = target.parent();
 261          } else if ( event.target.nodeName !== 'A' ) {
 262              return false;
 263          }
 264  
 265          /* Activity Stream Tabs */
 266          scope  = target.attr('id').substr( 9, target.attr('id').length );
 267          filter = jq('#activity-filter-select select').val();
 268  
 269          if ( scope === 'mentions' ) {
 270              jq( '#' + target.attr('id') + ' a strong' ).remove();
 271          }
 272  
 273          bp_activity_request(scope, filter);
 274  
 275          return false;
 276      });
 277  
 278      /* Activity filter select */
 279      jq( '#activity-filter-select select' ).on( 'change', function() {
 280          var selected_tab = jq( 'div.activity-type-tabs li.selected' ),
 281              filter = jq(this).val(),
 282              scope;
 283  
 284          if ( !selected_tab.length ) {
 285              scope = null;
 286          } else {
 287              scope = selected_tab.attr('id').substr( 9, selected_tab.attr('id').length );
 288          }
 289  
 290          bp_activity_request(scope, filter);
 291  
 292          return false;
 293      } );
 294  
 295      /* Stream event delegation */
 296      jq('div.activity').on( 'click', function(event) {
 297          var target = jq(event.target),
 298              type, parent, parent_id,
 299              li, id, link_href, nonce, timestamp,
 300              oldest_page, just_posted;
 301  
 302          /* Favoriting activity stream items */
 303          if ( target.hasClass('fav') || target.hasClass('unfav') ) {
 304              /* Bail if a request is in progress */
 305              if ( target.hasClass( 'loading' ) ) {
 306                  return false;
 307              }
 308  
 309              type      = target.hasClass('fav') ? 'fav' : 'unfav';
 310              parent    = target.closest('.activity-item');
 311              parent_id = parent.attr('id').substr( 9, parent.attr('id').length );
 312              nonce     = bp_get_query_var( '_wpnonce', target.attr( 'href' ) );
 313  
 314              target.addClass('loading');
 315  
 316              jq.post( ajaxurl, {
 317                  action: 'activity_mark_' + type,
 318                  'cookie': bp_get_cookies(),
 319                  'id': parent_id,
 320                  nonce: nonce
 321              },
 322              function(response) {
 323                  target.removeClass('loading');
 324  
 325                  target.fadeOut( 200, function() {
 326                      jq(this).html(response);
 327                      jq(this).attr('title', 'fav' === type ? BP_DTheme.remove_fav : BP_DTheme.mark_as_fav);
 328                      jq(this).fadeIn(200);
 329                  });
 330  
 331                  if ( 'fav' === type ) {
 332                      if ( !jq('.item-list-tabs #activity-favs-personal-li').length ) {
 333                          if ( !jq('.item-list-tabs #activity-favorites').length ) {
 334                              jq('.item-list-tabs ul #activity-mentions').before( '<li id="activity-favorites"><a href="#">' + BP_DTheme.my_favs + ' <span>0</span></a></li>');
 335                          }
 336  
 337                          jq('.item-list-tabs ul #activity-favorites span').html( Number( jq('.item-list-tabs ul #activity-favorites span').html() ) + 1 );
 338                      }
 339  
 340                      target.removeClass('fav');
 341                      target.addClass('unfav');
 342  
 343                  } else {
 344                      target.removeClass('unfav');
 345                      target.addClass('fav');
 346  
 347                      jq('.item-list-tabs ul #activity-favorites span').html( Number( jq('.item-list-tabs ul #activity-favorites span').html() ) - 1 );
 348  
 349                      if ( !Number( jq('.item-list-tabs ul #activity-favorites span').html() ) ) {
 350                          if ( jq('.item-list-tabs ul #activity-favorites').hasClass('selected') ) {
 351                              bp_activity_request( null, null );
 352                          }
 353  
 354                          jq('.item-list-tabs ul #activity-favorites').remove();
 355                      }
 356                  }
 357  
 358                  if ( 'activity-favorites' === jq( '.item-list-tabs li.selected').attr('id') ) {
 359                      target.closest( '.activity-item' ).slideUp( 100 );
 360                  }
 361              });
 362  
 363              return false;
 364          }
 365  
 366          /* Delete activity stream items */
 367          if ( target.hasClass('delete-activity') ) {
 368              li        = target.parents('div.activity ul li');
 369              id        = li.attr('id').substr( 9, li.attr('id').length );
 370              link_href = target.attr('href');
 371              nonce     = link_href.split('_wpnonce=');
 372              timestamp = li.prop( 'class' ).match( /date-recorded-([0-9]+)/ );
 373              nonce     = nonce[1];
 374  
 375              target.addClass('loading');
 376  
 377              jq.post( ajaxurl, {
 378                  action: 'delete_activity',
 379                  'cookie': bp_get_cookies(),
 380                  'id': id,
 381                  '_wpnonce': nonce
 382              },
 383              function(response) {
 384  
 385                  if ( response[0] + response[1] === '-1' ) {
 386                      li.prepend( response.substr( 2, response.length ) );
 387                      li.children('#message').hide().fadeIn(300);
 388                  } else {
 389                      li.slideUp(300);
 390  
 391                      // reset vars to get newest activities
 392                      if ( timestamp && activity_last_recorded === timestamp[1] ) {
 393                          newest_activities = '';
 394                          activity_last_recorded  = 0;
 395                      }
 396                  }
 397              });
 398  
 399              return false;
 400          }
 401  
 402          // Spam activity stream items
 403          if ( target.hasClass( 'spam-activity' ) ) {
 404              li        = target.parents( 'div.activity ul li' );
 405              timestamp = li.prop( 'class' ).match( /date-recorded-([0-9]+)/ );
 406              target.addClass( 'loading' );
 407  
 408              jq.post( ajaxurl, {
 409                  action: 'bp_spam_activity',
 410                  'cookie': encodeURIComponent( document.cookie ),
 411                  'id': li.attr( 'id' ).substr( 9, li.attr( 'id' ).length ),
 412                  '_wpnonce': target.attr( 'href' ).split( '_wpnonce=' )[1]
 413              },
 414  
 415              function(response) {
 416                  if ( response[0] + response[1] === '-1' ) {
 417                      li.prepend( response.substr( 2, response.length ) );
 418                      li.children( '#message' ).hide().fadeIn(300);
 419                  } else {
 420                      li.slideUp( 300 );
 421                      // reset vars to get newest activities
 422                      if ( timestamp && activity_last_recorded === timestamp[1] ) {
 423                          newest_activities = '';
 424                          activity_last_recorded  = 0;
 425                      }
 426                  }
 427              });
 428  
 429              return false;
 430          }
 431  
 432          /* Load more updates at the end of the page */
 433          if ( target.parent().hasClass('load-more') ) {
 434              if ( bp_ajax_request ) {
 435                  bp_ajax_request.abort();
 436              }
 437  
 438              jq('#buddypress li.load-more').addClass('loading');
 439  
 440              oldest_page = activity_oldestpage + 1;
 441              just_posted = [];
 442  
 443              jq('.activity-list li.just-posted').each( function(){
 444                  just_posted.push( jq(this).attr('id').replace( 'activity-','' ) );
 445              });
 446  
 447              load_more_args = {
 448                  action: 'activity_get_older_updates',
 449                  'cookie': bp_get_cookies(),
 450                  'page': oldest_page,
 451                  'exclude_just_posted': just_posted.join(',')
 452              };
 453  
 454              load_more_search = bp_get_querystring('s');
 455  
 456              if ( load_more_search ) {
 457                  load_more_args.search_terms = load_more_search;
 458              }
 459  
 460              bp_ajax_request = jq.post( ajaxurl, load_more_args,
 461              function(response)
 462              {
 463                  jq('#buddypress li.load-more').removeClass('loading');
 464                  activity_oldestpage = oldest_page;
 465                  jq('#buddypress ul.activity-list').append(response.contents);
 466  
 467                  target.parent().hide();
 468              }, 'json' );
 469  
 470              return false;
 471          }
 472  
 473          /* Load newest updates at the top of the list */
 474          if ( target.parent().hasClass('load-newest') ) {
 475  
 476              event.preventDefault();
 477  
 478              target.parent().hide();
 479  
 480              /**
 481               * If a plugin is updating the recorded_date of an activity
 482               * it will be loaded as a new one. We need to look in the
 483               * stream and eventually remove similar ids to avoid "double".
 484               */
 485              activity_html = jq.parseHTML( newest_activities );
 486  
 487              jq.each( activity_html, function( i, el ){
 488                  if( 'LI' === el.nodeName && jq(el).hasClass( 'just-posted' ) ) {
 489                      if( jq( '#' + jq(el).attr( 'id' ) ).length ) {
 490                          jq( '#' + jq(el).attr( 'id' ) ).remove();
 491                      }
 492                  }
 493              } );
 494  
 495              // Now the stream is cleaned, prepend newest
 496              jq( '#buddypress ul.activity-list' ).prepend( newest_activities );
 497  
 498              // reset the newest activities now they're displayed
 499              newest_activities = '';
 500          }
 501      });
 502  
 503      // Activity "Read More" links
 504      jq('div.activity').on('click', '.activity-read-more a', function(event) {
 505          var target = jq(event.target),
 506              link_id = target.parent().attr('id').split('-'),
 507              a_id    = link_id[3],
 508              type    = link_id[0], /* activity or acomment */
 509              inner_class, a_inner;
 510  
 511          inner_class = type === 'acomment' ? 'acomment-content' : 'activity-inner';
 512          a_inner = jq('#' + type + '-' + a_id + ' .' + inner_class + ':first' );
 513          jq(target).addClass('loading');
 514  
 515          jq.post( ajaxurl, {
 516              action: 'get_single_activity_content',
 517              'activity_id': a_id
 518          },
 519          function(response) {
 520              jq(a_inner).slideUp(300).html(response).slideDown(300);
 521          });
 522  
 523          return false;
 524      });
 525  
 526      /**** Activity Comments *******************************************************/
 527  
 528      /* Hide all activity comment forms */
 529      jq('form.ac-form').hide();
 530  
 531      /* Hide excess comments */
 532      if ( jq('.activity-comments').length ) {
 533          bp_legacy_theme_hide_comments();
 534      }
 535  
 536      /* Activity list event delegation */
 537      jq('div.activity').on( 'click', function(event) {
 538          var target = jq(event.target),
 539              id, ids, a_id, c_id, form,
 540              form_parent, form_id,
 541              tmp_id, comment_id, comment,
 542              ajaxdata,
 543              ak_nonce,
 544              show_all_a, new_count,
 545              link_href, comment_li, nonce;
 546  
 547          /* Comment / comment reply links */
 548          if ( target.hasClass('acomment-reply') || target.parent().hasClass('acomment-reply') ) {
 549              if ( target.parent().hasClass('acomment-reply') ) {
 550                  target = target.parent();
 551              }
 552  
 553              id  = target.attr('id');
 554              ids = id.split('-');
 555  
 556              a_id = ids[2];
 557              c_id = target.attr('href').substr( 10, target.attr('href').length );
 558              form = jq( '#ac-form-' + a_id );
 559  
 560              if ( ! form.length ) {
 561                  var viewDiscussionLink = target.closest( 'li.activity' ).find( '.activity-meta a.view' ).prop( 'href' );
 562  
 563                  if ( viewDiscussionLink ) {
 564                      window.location.href = viewDiscussionLink;
 565                  }
 566  
 567                  return false;
 568              }
 569  
 570              form.css( 'display', 'none' );
 571              form.removeClass('root');
 572              jq('.ac-form').hide();
 573  
 574              /* Hide any error messages */
 575              form.children('div').each( function() {
 576                  if ( jq(this).hasClass( 'error' ) ) {
 577                      jq(this).hide();
 578                  }
 579              });
 580  
 581              if ( ids[1] !== 'comment' ) {
 582                  jq('#acomment-' + c_id).append( form );
 583              } else {
 584                  jq('#activity-' + a_id + ' .activity-comments').append( form );
 585              }
 586  
 587              if ( form.parent().hasClass( 'activity-comments' ) ) {
 588                  form.addClass('root');
 589              }
 590  
 591              form.slideDown( 200 );
 592              jq.scrollTo( form, 500, {
 593                  offset:-100,
 594                  easing:'swing'
 595              } );
 596              jq('#ac-form-' + ids[2] + ' textarea').trigger( 'focus' );
 597  
 598              return false;
 599          }
 600  
 601          /* Activity comment posting */
 602          if ( target.attr('name') === 'ac_form_submit' ) {
 603              form = target.parents( 'form' );
 604              form_parent = form.parent();
 605              form_id = form.attr('id').split('-');
 606  
 607              if ( !form_parent.hasClass('activity-comments') ) {
 608                  tmp_id = form_parent.attr('id').split('-');
 609                  comment_id = tmp_id[1];
 610              } else {
 611                  comment_id = form_id[2];
 612              }
 613  
 614              content = jq( '#' + form.attr('id') + ' textarea' );
 615  
 616              /* Hide any error messages */
 617              jq( '#' + form.attr('id') + ' div.error').hide();
 618              target.addClass('loading').prop('disabled', true);
 619              content.addClass('loading').prop('disabled', true);
 620  
 621              ajaxdata = {
 622                  action: 'new_activity_comment',
 623                  'cookie': bp_get_cookies(),
 624                  '_wpnonce_new_activity_comment': jq('#_wpnonce_new_activity_comment' + '_' + form_id[2] ).val(),
 625                  'comment_id': comment_id,
 626                  'form_id': form_id[2],
 627                  'content': content.val()
 628              };
 629  
 630              // Akismet
 631              ak_nonce = jq('#_bp_as_nonce_' + comment_id).val();
 632              if ( ak_nonce ) {
 633                  ajaxdata['_bp_as_nonce_' + comment_id] = ak_nonce;
 634              }
 635  
 636              jq.post( ajaxurl, ajaxdata, function(response) {
 637                  target.removeClass('loading');
 638                  content.removeClass('loading');
 639  
 640                  /* Check for errors and append if found. */
 641                  if ( response[0] + response[1] === '-1' ) {
 642                      form.append( jq( response.substr( 2, response.length ) ).hide().fadeIn( 200 ) );
 643                  } else {
 644                      var activity_comments = form.parent();
 645                      form.fadeOut( 200, function() {
 646                          if ( 0 === activity_comments.children('ul').length ) {
 647                              if ( activity_comments.hasClass('activity-comments') ) {
 648                                  activity_comments.prepend('<ul></ul>');
 649                              } else {
 650                                  activity_comments.append('<ul></ul>');
 651                              }
 652                          }
 653  
 654                          /* Preceding whitespace breaks output with jQuery 1.9.0 */
 655                          var the_comment = jq.trim( response );
 656  
 657                          activity_comments.children('ul').append( jq( the_comment ).hide().fadeIn( 200 ) );
 658                          form.children('textarea').val('');
 659                          activity_comments.parent().addClass('has-comments');
 660                      } );
 661                      jq( '#' + form.attr('id') + ' textarea').val('');
 662  
 663                      /* Increase the "Reply (X)" button count */
 664                      new_count = Number( jq('#activity-' + form_id[2] + ' a.acomment-reply span').html() ) + 1;
 665                      jq('#activity-' + form_id[2] + ' a.acomment-reply span').html( new_count );
 666  
 667                      // Increment the 'Show all x comments' string, if present
 668                      show_all_a = activity_comments.parents('.activity-comments').find('.show-all a');
 669                      if ( show_all_a ) {
 670                          show_all_a.html( BP_DTheme.show_x_comments.replace( '%d', new_count ) );
 671                      }
 672                  }
 673  
 674                  jq(target).prop('disabled', false);
 675                  jq(content).prop('disabled', false);
 676              });
 677  
 678              return false;
 679          }
 680  
 681          /* Deleting an activity comment */
 682          if ( target.hasClass('acomment-delete') ) {
 683              link_href = target.attr('href');
 684              comment_li = target.closest( 'li' );
 685  
 686              form = comment_li.find( 'form.ac-form' );
 687  
 688              nonce = link_href.split('_wpnonce=');
 689              nonce = nonce[1];
 690  
 691              comment_id = link_href.split('cid=');
 692              comment_id = comment_id[1].split('&');
 693              comment_id = comment_id[0];
 694  
 695              target.addClass('loading');
 696  
 697              /* Remove any error messages */
 698              jq('.activity-comments ul .error').remove();
 699  
 700              /* Reset the form position */
 701              if ( form && form.length ) {
 702                  comment_li.closest( '.activity-comments' ).append( form );
 703              }
 704  
 705              jq.post( ajaxurl, {
 706                  action: 'delete_activity_comment',
 707                  'cookie': bp_get_cookies(),
 708                  '_wpnonce': nonce,
 709                  'id': comment_id
 710              },
 711              function(response) {
 712                  /* Check for errors and append if found. */
 713                  if ( response[0] + response[1] === '-1' ) {
 714                      comment_li.prepend( jq( response.substr( 2, response.length ) ).hide().fadeIn( 200 ) );
 715                  } else {
 716                      var children  = jq( '#' + comment_li.attr('id') + ' ul' ).children('li'),
 717                          child_count = 0,
 718                          count_span, new_count, show_all_a;
 719  
 720                      jq(children).each( function() {
 721                          if ( !jq(this).is(':hidden') ) {
 722                              child_count++;
 723                          }
 724                      });
 725                      comment_li.fadeOut(200, function() {
 726                          comment_li.remove();
 727                      });
 728  
 729                      /* Decrease the "Reply (X)" button count */
 730                      count_span = jq('#' + comment_li.parents('#activity-stream > li').attr('id') + ' a.acomment-reply span');
 731                      new_count = count_span.html() - ( 1 + child_count );
 732                      count_span.html(new_count);
 733  
 734                      // Change the 'Show all x comments' text
 735                      show_all_a = comment_li.parents('.activity-comments').find('.show-all a');
 736                      if ( show_all_a ) {
 737                          show_all_a.html( BP_DTheme.show_x_comments.replace( '%d', new_count ) );
 738                      }
 739  
 740                      /* If that was the last comment for the item, remove the has-comments class to clean up the styling */
 741                      if ( 0 === new_count ) {
 742                          jq(comment_li.parents('#activity-stream > li')).removeClass('has-comments');
 743                      }
 744                  }
 745              });
 746  
 747              return false;
 748          }
 749  
 750          // Spam an activity stream comment
 751          if ( target.hasClass( 'spam-activity-comment' ) ) {
 752              link_href  = target.attr( 'href' );
 753              comment_li = target.parent().parent();
 754  
 755              target.addClass('loading');
 756  
 757              // Remove any error messages
 758              jq( '.activity-comments ul div.error' ).remove();
 759  
 760              // Reset the form position
 761              comment_li.parents( '.activity-comments' ).append( comment_li.parents( '.activity-comments' ).children( 'form' ) );
 762  
 763              jq.post( ajaxurl, {
 764                  action: 'bp_spam_activity_comment',
 765                  'cookie': encodeURIComponent( document.cookie ),
 766                  '_wpnonce': link_href.split( '_wpnonce=' )[1],
 767                  'id': link_href.split( 'cid=' )[1].split( '&' )[0]
 768              },
 769  
 770              function ( response ) {
 771                  // Check for errors and append if found.
 772                  if ( response[0] + response[1] === '-1' ) {
 773                      comment_li.prepend( jq( response.substr( 2, response.length ) ).hide().fadeIn( 200 ) );
 774  
 775                  } else {
 776                      var children  = jq( '#' + comment_li.attr( 'id' ) + ' ul' ).children( 'li' ),
 777                          child_count = 0,
 778                          parent_li;
 779  
 780                      jq(children).each( function() {
 781                          if ( !jq( this ).is( ':hidden' ) ) {
 782                              child_count++;
 783                          }
 784                      });
 785                      comment_li.fadeOut( 200 );
 786  
 787                      // Decrease the "Reply (X)" button count
 788                      parent_li = comment_li.parents( '#activity-stream > li' );
 789                      jq( '#' + parent_li.attr( 'id' ) + ' a.acomment-reply span' ).html( jq( '#' + parent_li.attr( 'id' ) + ' a.acomment-reply span' ).html() - ( 1 + child_count ) );
 790                  }
 791              });
 792  
 793              return false;
 794          }
 795  
 796          /* Showing hidden comments - pause for half a second */
 797          if ( target.parent().hasClass('show-all') ) {
 798              target.parent().addClass('loading');
 799  
 800              setTimeout( function() {
 801                  target.parent().parent().children('li').fadeIn(200, function() {
 802                      target.parent().remove();
 803                  });
 804              }, 600 );
 805  
 806              return false;
 807          }
 808  
 809          // Canceling an activity comment
 810          if ( target.hasClass( 'ac-reply-cancel' ) ) {
 811              jq(target).closest('.ac-form').slideUp( 200 );
 812              return false;
 813          }
 814      });
 815  
 816      /* Escape Key Press for cancelling comment forms */
 817      jq(document).on( 'keydown', function(e) {
 818          e = e || window.event;
 819          if (e.target) {
 820              element = e.target;
 821          } else if (e.srcElement) {
 822              element = e.srcElement;
 823          }
 824  
 825          if( element.nodeType === 3) {
 826              element = element.parentNode;
 827          }
 828  
 829          if( e.ctrlKey === true || e.altKey === true || e.metaKey === true ) {
 830              return;
 831          }
 832  
 833          var keyCode = (e.keyCode) ? e.keyCode : e.which;
 834  
 835          if ( keyCode === 27 ) {
 836              if (element.tagName === 'TEXTAREA') {
 837                  if ( jq(element).hasClass('ac-input') ) {
 838                      jq(element).parent().parent().parent().slideUp( 200 );
 839                  }
 840              }
 841          }
 842      });
 843  
 844      /**** Directory Search ****************************************************/
 845  
 846      /* The search form on all directory pages */
 847      jq( '.dir-search, .groups-members-search' ).on( 'click', function(event) {
 848          if ( jq(this).hasClass('no-ajax') ) {
 849              return;
 850          }
 851  
 852          var target = jq(event.target),
 853              css_id, object, template, search_terms;
 854  
 855          if ( target.attr('type') === 'submit' ) {
 856              css_id = jq('.item-list-tabs li.selected').attr('id').split( '-' );
 857              object = css_id[0];
 858              template = null;
 859              search_terms = target.parent().find( '#' + object + '_search' ).val();
 860  
 861              // The Group Members page specifies its own template
 862              if ( event.currentTarget.className === 'groups-members-search' ) {
 863                  object = 'group_members';
 864                  template = 'groups/single/members';
 865              }
 866  
 867              var scope  = bp_get_directory_preference( object, 'scope' );
 868              var filter = bp_get_directory_preference( object, 'filter' );
 869              var extras = bp_get_directory_preference( object, 'extras' );
 870  
 871              bp_filter_request( object, filter, scope, 'div.' + object, search_terms, 1, extras, null, template );
 872  
 873              return false;
 874          }
 875      });
 876  
 877      /**** Tabs and Filters ****************************************************/
 878  
 879      /* When a navigation tab is clicked - e.g. | All Groups | My Groups | */
 880      jq('div.item-list-tabs').on( 'click', function(event) {
 881          // If on a directory page with a type filter, add no-ajax class.
 882          if ( jq( 'body' ).hasClass( 'type' ) && jq( 'body' ).hasClass( 'directory' ) ) {
 883              jq(this).addClass( 'no-ajax' );
 884          }
 885  
 886          if ( jq(this).hasClass('no-ajax')  || jq( event.target ).hasClass('no-ajax') )  {
 887              return;
 888          }
 889  
 890          var targetElem = ( event.target.nodeName === 'SPAN' ) ? event.target.parentNode : event.target,
 891              target       = jq( targetElem ).parent(),
 892              css_id, object, scope, filter, search_terms;
 893  
 894          if ( 'LI' === target[0].nodeName && !target.hasClass( 'last' ) ) {
 895              css_id = target.attr('id').split( '-' );
 896              object = css_id[0];
 897  
 898              if ( 'activity' === object ) {
 899                  return false;
 900              }
 901  
 902              scope = css_id[1];
 903              filter = jq('#' + object + '-order-select select').val();
 904              search_terms = jq('#' + object + '_search').val();
 905  
 906              var extras = bp_get_directory_preference( object, 'extras' );
 907  
 908              bp_filter_request( object, filter, scope, 'div.' + object, search_terms, 1, extras );
 909  
 910              return false;
 911          }
 912      });
 913  
 914      /* When the filter select box is changed re-query */
 915      jq( 'li.filter select' ).on( 'change', function() {
 916          var el,
 917              css_id, object, scope, filter, search_terms, template,
 918              $gm_search;
 919  
 920          if ( jq('.item-list-tabs li.selected').length ) {
 921              el = jq('.item-list-tabs li.selected');
 922          } else {
 923              el = jq(this);
 924          }
 925  
 926          css_id = el.attr('id').split('-');
 927          object = css_id[0];
 928          scope = css_id[1];
 929          filter = jq(this).val();
 930          search_terms = false;
 931          template = null;
 932  
 933          if ( jq('.dir-search input').length ) {
 934              search_terms = jq('.dir-search input').val();
 935          }
 936  
 937          // The Group Members page has a different selector for its
 938          // search terms box
 939          $gm_search = jq( '.groups-members-search input' );
 940          if ( $gm_search.length ) {
 941              search_terms = $gm_search.val();
 942              object = 'members';
 943              scope = 'groups';
 944          }
 945  
 946          // On the Groups Members page, we specify a template
 947          if ( 'members' === object && 'groups' === scope ) {
 948              object = 'group_members';
 949              template = 'groups/single/members';
 950          }
 951  
 952          if ( 'friends' === object ) {
 953              object = 'members';
 954          }
 955  
 956          var extras = bp_get_directory_preference( object, 'extras' );
 957  
 958          bp_filter_request( object, filter, scope, 'div.' + object, search_terms, 1, extras, null, template );
 959  
 960          return false;
 961      } );
 962  
 963      /* All pagination links run through this function */
 964      jq('#buddypress').on( 'click', function(event) {
 965          var target = jq(event.target),
 966              el,
 967              css_id, object, search_terms, pagination_id, template,
 968              page_number,
 969              $gm_search,
 970              caller;
 971  
 972          if ( target.hasClass('button') ) {
 973              return true;
 974          }
 975  
 976          if ( target.parent().parent().hasClass('pagination') && !target.parent().parent().hasClass('no-ajax') ) {
 977              if ( target.hasClass('dots') || target.hasClass('current') ) {
 978                  return false;
 979              }
 980  
 981              if ( jq('.item-list-tabs li.selected').length ) {
 982                  el = jq('.item-list-tabs li.selected');
 983              } else {
 984                  el = jq('li.filter select');
 985              }
 986  
 987              css_id = el.attr('id').split( '-' );
 988              object = css_id[0];
 989              search_terms = false;
 990              pagination_id = jq(target).closest('.pagination-links').attr('id');
 991              template = null;
 992  
 993              // Search terms
 994              if ( jq('div.dir-search input').length ) {
 995                  search_terms =  jq('.dir-search input');
 996  
 997                  if ( ! search_terms.val() && bp_get_querystring( search_terms.attr( 'name' ) ) ) {
 998                      search_terms = jq('.dir-search input').prop('placeholder');
 999                  } else {
1000                      search_terms = search_terms.val();
1001                  }
1002              }
1003  
1004              // Page number
1005              if ( jq(target).hasClass('next') || jq(target).hasClass('prev') ) {
1006                  page_number = jq('.pagination span.current').html();
1007              } else {
1008                  page_number = jq(target).html();
1009              }
1010  
1011              // Remove any non-numeric characters from page number text (commas, etc.)
1012              page_number = Number( page_number.replace(/\D/g,'') );
1013  
1014              if ( jq(target).hasClass('next') ) {
1015                  page_number++;
1016              } else if ( jq(target).hasClass('prev') ) {
1017                  page_number--;
1018              }
1019  
1020              // The Group Members page has a different selector for
1021              // its search terms box
1022              $gm_search = jq( '.groups-members-search input' );
1023              if ( $gm_search.length ) {
1024                  search_terms = $gm_search.val();
1025                  object = 'members';
1026              }
1027  
1028              // On the Groups Members page, we specify a template
1029              if ( 'members' === object && 'groups' === css_id[1] ) {
1030                  object = 'group_members';
1031                  template = 'groups/single/members';
1032              }
1033  
1034              // On the Admin > Requests page, we need to reset the object,
1035              // since "admin" isn't specific enough
1036              if ( 'admin' === object && jq( 'body' ).hasClass( 'membership-requests' ) ) {
1037                  object = 'requests';
1038              }
1039  
1040              if ( pagination_id.indexOf( 'pag-bottom' ) !== -1 ) {
1041                  caller = 'pag-bottom';
1042              } else {
1043                  caller = null;
1044              }
1045  
1046              var scope  = bp_get_directory_preference( object, 'scope' );
1047              var filter = bp_get_directory_preference( object, 'filter' );
1048              var extras = bp_get_directory_preference( object, 'extras' );
1049  
1050              bp_filter_request( object, filter, scope, 'div.' + object, search_terms, page_number, extras, caller, template );
1051  
1052              return false;
1053          }
1054  
1055      });
1056  
1057      /** Invite Friends Interface ****************************************/
1058  
1059      /* Select a user from the list of friends and add them to the invite list */
1060      jq('#send-invite-form').on( 'click', '#invite-list input', function() {
1061          // invites-loop template contains a div with the .invite class
1062          // We use the existence of this div to check for old- vs new-
1063          // style templates.
1064          var invites_new_template = jq( '#send-invite-form > .invite' ).length,
1065              friend_id, friend_action;
1066  
1067          jq('.ajax-loader').toggle();
1068  
1069          // Dim the form until the response arrives
1070          if ( invites_new_template ) {
1071              jq( this ).parents( 'ul' ).find( 'input' ).prop( 'disabled', true );
1072          }
1073  
1074          friend_id = jq(this).val();
1075  
1076          if ( jq(this).prop('checked') === true ) {
1077              friend_action = 'invite';
1078          } else {
1079              friend_action = 'uninvite';
1080          }
1081  
1082          if ( ! invites_new_template ) {
1083              jq( '.item-list-tabs li.selected' ).addClass( 'loading' );
1084          }
1085  
1086          jq.post( ajaxurl, {
1087              action: 'groups_invite_user',
1088              'friend_action': friend_action,
1089              'cookie': bp_get_cookies(),
1090              '_wpnonce': jq('#_wpnonce_invite_uninvite_user').val(),
1091              'friend_id': friend_id,
1092              'group_id': jq('#group_id').val()
1093          },
1094          function(response)
1095          {
1096              if ( jq('#message') ) {
1097                  jq('#message').hide();
1098              }
1099  
1100              if ( invites_new_template ) {
1101                  // With new-style templates, we refresh the
1102                  // entire list
1103                  bp_filter_request( 'invite', 'bp-invite-filter', 'bp-invite-scope', 'div.invite', false, 1, '', '', '' );
1104              } else {
1105                  // Old-style templates manipulate only the
1106                  // single invitation element
1107                  jq('.ajax-loader').toggle();
1108  
1109                  if ( friend_action === 'invite' ) {
1110                      jq('#friend-list').append(response);
1111                  } else if ( friend_action === 'uninvite' ) {
1112                      jq('#friend-list li#uid-' + friend_id).remove();
1113                  }
1114  
1115                  jq('.item-list-tabs li.selected').removeClass('loading');
1116              }
1117          });
1118      });
1119  
1120      /* Remove a user from the list of users to invite to a group */
1121      jq('#send-invite-form').on('click', 'a.remove', function() {
1122          // invites-loop template contains a div with the .invite class
1123          // We use the existence of this div to check for old- vs new-
1124          // style templates.
1125          var invites_new_template = jq('#send-invite-form > .invite').length,
1126              friend_id = jq(this).attr('id');
1127  
1128          jq('.ajax-loader').toggle();
1129  
1130          friend_id = friend_id.split('-');
1131          friend_id = friend_id[1];
1132  
1133          jq.post( ajaxurl, {
1134              action: 'groups_invite_user',
1135              'friend_action': 'uninvite',
1136              'cookie': bp_get_cookies(),
1137              '_wpnonce': jq('#_wpnonce_invite_uninvite_user').val(),
1138              'friend_id': friend_id,
1139              'group_id': jq('#group_id').val()
1140          },
1141          function()
1142          {
1143              if ( invites_new_template ) {
1144                  // With new-style templates, we refresh the
1145                  // entire list
1146                  bp_filter_request( 'invite', 'bp-invite-filter', 'bp-invite-scope', 'div.invite', false, 1, '', '', '' );
1147              } else {
1148                  // Old-style templates manipulate only the
1149                  // single invitation element
1150                  jq('.ajax-loader').toggle();
1151                  jq('#friend-list #uid-' + friend_id).remove();
1152                  jq('#invite-list #f-' + friend_id).prop('checked', false);
1153              }
1154          });
1155  
1156          return false;
1157      });
1158  
1159      /** Profile Visibility Settings *********************************/
1160      jq( '.visibility-toggle-link' ).on( 'click', function( event ) {
1161          event.preventDefault();
1162  
1163          jq( this ).attr( 'aria-expanded', 'true' ).parent().hide().addClass( 'field-visibility-settings-hide' )
1164              .siblings( '.field-visibility-settings' ).show().addClass( 'field-visibility-settings-open' );
1165      } );
1166  
1167      jq( '.field-visibility-settings-close' ).on( 'click', function( event ) {
1168          event.preventDefault();
1169  
1170          jq( '.visibility-toggle-link' ).attr( 'aria-expanded', 'false' );
1171  
1172          var settings_div = jq( this ).parent(),
1173              vis_setting_text = settings_div.find( 'input:checked' ).parent().text();
1174  
1175          settings_div.hide().removeClass( 'field-visibility-settings-open' )
1176              .siblings( '.field-visibility-settings-toggle' )
1177                  .find( '.current-visibility-level' ).text( vis_setting_text ).end()
1178              .show().removeClass( 'field-visibility-settings-hide' );
1179      } );
1180  
1181      jq( '#profile-edit-form input:not(:submit), #profile-edit-form textarea, #profile-edit-form select, #signup_form input:not(:submit), #signup_form textarea, #signup_form select' ).on( 'change', function() {
1182          var shouldconfirm = true;
1183  
1184          jq('#profile-edit-form input:submit, #signup_form input:submit').on( 'click', function() {
1185              shouldconfirm = false;
1186          });
1187  
1188          window.onbeforeunload = function(e) {
1189              if ( shouldconfirm ) {
1190                  return BP_DTheme.unsaved_changes;
1191              }
1192          };
1193      } );
1194  
1195      /** Friendship Requests **************************************/
1196  
1197      /* Accept and Reject friendship request buttons */
1198      jq('#friend-list a.accept, #friend-list a.reject').on( 'click', function() {
1199          var button   = jq(this),
1200              li         = jq(this).parents('#friend-list li'),
1201              action_div = jq(this).parents('li div.action'),
1202              id         = li.attr('id').substr( 11, li.attr('id').length ),
1203              link_href  = button.attr('href'),
1204              nonce      = link_href.split('_wpnonce=')[1],
1205              action;
1206  
1207          if ( jq(this).hasClass('accepted') || jq(this).hasClass('rejected') ) {
1208              return false;
1209          }
1210  
1211          if ( jq(this).hasClass('accept') ) {
1212              action = 'accept_friendship';
1213              action_div.children('a.reject').css( 'visibility', 'hidden' );
1214          } else {
1215              action = 'reject_friendship';
1216              action_div.children('a.accept').css( 'visibility', 'hidden' );
1217          }
1218  
1219          button.addClass('loading');
1220  
1221          jq.post( ajaxurl, {
1222              action: action,
1223              'cookie': bp_get_cookies(),
1224              'id': id,
1225              '_wpnonce': nonce
1226          },
1227          function(response) {
1228              button.removeClass('loading');
1229  
1230              if ( response[0] + response[1] === '-1' ) {
1231                  li.prepend( response.substr( 2, response.length ) );
1232                  li.children('#message').hide().fadeIn(200);
1233              } else {
1234                  button.fadeOut( 100, function() {
1235                      if ( jq(this).hasClass('accept') ) {
1236                          action_div.children('a.reject').hide();
1237                          jq(this).html( BP_DTheme.accepted ).contents().unwrap();
1238                      } else {
1239                          action_div.children('a.accept').hide();
1240                          jq(this).html( BP_DTheme.rejected ).contents().unwrap();
1241                      }
1242                  });
1243              }
1244          });
1245  
1246          return false;
1247      });
1248  
1249      /* Add / Remove friendship buttons */
1250      jq( '#members-dir-list, #members-group-list, #item-header' ).on('click', '.friendship-button a', function() {
1251          jq(this).parent().addClass('loading');
1252          var fid   = jq(this).attr('id'),
1253              nonce   = jq(this).attr('href'),
1254              thelink = jq(this);
1255  
1256          fid = fid.split('-');
1257          fid = fid[1];
1258  
1259          nonce = nonce.split('?_wpnonce=');
1260          nonce = nonce[1].split('&');
1261          nonce = nonce[0];
1262  
1263          jq.post( ajaxurl, {
1264              action: 'addremove_friend',
1265              'cookie': bp_get_cookies(),
1266              'fid': fid,
1267              '_wpnonce': nonce
1268          },
1269          function(response)
1270          {
1271              var action  = thelink.attr('rel');
1272                  parentdiv = thelink.parent();
1273  
1274              if ( action === 'add' ) {
1275                  jq(parentdiv).fadeOut(200,
1276                      function() {
1277                          parentdiv.removeClass('add_friend');
1278                          parentdiv.removeClass('loading');
1279                          parentdiv.addClass('pending_friend');
1280                          parentdiv.fadeIn(200).html(response);
1281                      }
1282                      );
1283  
1284              } else if ( action === 'remove' ) {
1285                  jq(parentdiv).fadeOut(200,
1286                      function() {
1287                          parentdiv.removeClass('remove_friend');
1288                          parentdiv.removeClass('loading');
1289                          parentdiv.addClass('add');
1290                          parentdiv.fadeIn(200).html(response);
1291                      }
1292                      );
1293              }
1294          });
1295          return false;
1296      } );
1297  
1298      /** Group Join / Leave Buttons **************************************/
1299  
1300      // Confirmation when clicking Leave Group in group headers
1301      jq('#buddypress').on('click', '.group-button .leave-group', function() {
1302          if ( false === confirm( BP_DTheme.leave_group_confirm ) ) {
1303              return false;
1304          }
1305      });
1306  
1307      jq('#groups-dir-list').on('click', '.group-button a', function() {
1308          var gid   = jq(this).parent().attr('id'),
1309              nonce   = jq(this).attr('href'),
1310              thelink = jq(this);
1311  
1312          gid = gid.split('-');
1313          gid = gid[1];
1314  
1315          nonce = nonce.split('?_wpnonce=');
1316          nonce = nonce[1].split('&');
1317          nonce = nonce[0];
1318  
1319          // Leave Group confirmation within directories - must intercept
1320          // AJAX request
1321          if ( thelink.hasClass( 'leave-group' ) && false === confirm( BP_DTheme.leave_group_confirm ) ) {
1322              return false;
1323          }
1324  
1325          jq.post( ajaxurl, {
1326              action: 'joinleave_group',
1327              'cookie': bp_get_cookies(),
1328              'gid': gid,
1329              '_wpnonce': nonce
1330          },
1331          function(response)
1332          {
1333              var parentdiv = thelink.parent();
1334  
1335              // user groups page
1336              if ( ! jq('body.directory').length ) {
1337                  window.location.reload();
1338  
1339              // groups directory
1340              } else {
1341                  jq(parentdiv).fadeOut(200,
1342                      function() {
1343                          parentdiv.fadeIn(200).html(response);
1344  
1345                          var mygroups = jq('#groups-personal span'),
1346                              add        = 1;
1347  
1348                          if( thelink.hasClass( 'leave-group' ) ) {
1349                              // hidden groups slide up
1350                              if ( parentdiv.hasClass( 'hidden' ) ) {
1351                                  parentdiv.closest('li').slideUp( 200 );
1352                              }
1353  
1354                              add = 0;
1355                          } else if ( thelink.hasClass( 'request-membership' ) ) {
1356                              add = false;
1357                          }
1358  
1359                          // change the "My Groups" value
1360                          if ( mygroups.length && add !== false ) {
1361                              if ( add ) {
1362                                  mygroups.text( ( mygroups.text() >> 0 ) + 1 );
1363                              } else {
1364                                  mygroups.text( ( mygroups.text() >> 0 ) - 1 );
1365                              }
1366                          }
1367  
1368                      }
1369                  );
1370              }
1371          });
1372          return false;
1373      } );
1374  
1375      // Fix hidden group visibility with themes using the .hidden CSS rule.
1376      jq('#groups-list li.hidden').each(function() {
1377          if ( jq(this).css('display') === 'none' ) {
1378              jq(this).css('cssText', 'display: list-item !important');
1379          }
1380      });
1381  
1382      /** Button disabling ************************************************/
1383  
1384      jq('#buddypress').on( 'click', '.pending', function() {
1385          return false;
1386      });
1387  
1388      /** Registration ***********************************************/
1389  
1390      if ( jq('body').hasClass('register') ) {
1391          var blog_checked = jq('#signup_with_blog');
1392  
1393          // hide "Blog Details" block if not checked by default
1394          if ( ! blog_checked.prop('checked') ) {
1395              jq('#blog-details').toggle();
1396          }
1397  
1398          // toggle "Blog Details" block whenever checkbox is checked
1399          blog_checked.on( 'change', function() {
1400              jq('#blog-details').toggle();
1401          });
1402      }
1403  
1404      /** Private Messaging ******************************************/
1405  
1406      /** Message search */
1407      jq('.message-search').on( 'click', function(event) {
1408          if ( jq(this).hasClass('no-ajax') ) {
1409              return;
1410          }
1411  
1412          var target = jq(event.target),
1413              object;
1414  
1415          if ( target.attr('type') === 'submit' || target.attr('type') === 'button' ) {
1416              object = 'messages';
1417  
1418              var scope  = bp_get_directory_preference( object, 'scope' );
1419              var filter = bp_get_directory_preference( object, 'filter' );
1420              var extras = bp_get_directory_preference( object, 'extras' );
1421  
1422              bp_filter_request(
1423                  object,
1424                  filter,
1425                  scope,
1426                  'div.' + object, jq('#messages_search').val(),
1427                  1,
1428                  extras
1429              );
1430  
1431              return false;
1432          }
1433      });
1434  
1435      /* AJAX send reply functionality */
1436      jq( '#send_reply_button' ).on( 'click',
1437          function() {
1438              var order = jq('#messages_order').val() || 'ASC',
1439                  offset  = jq('#message-recipients').offset(),
1440                  button  = jq('#send_reply_button');
1441  
1442              jq(button).addClass('loading').prop( 'disabled', true );
1443  
1444              jq.post( ajaxurl, {
1445                  action: 'messages_send_reply',
1446                  'cookie': bp_get_cookies(),
1447                  '_wpnonce': jq('#send_message_nonce').val(),
1448  
1449                  'content': jq('#message_content').val(),
1450                  'send_to': jq('#send_to').val(),
1451                  'subject': jq('#subject').val(),
1452                  'thread_id': jq('#thread_id').val()
1453              },
1454              function(response)
1455              {
1456                  if ( response[0] + response[1] === '-1' ) {
1457                      jq('#send-reply').prepend( response.substr( 2, response.length ) );
1458                  } else {
1459                      jq('#send-reply #message').remove();
1460                      jq('#message_content').val('');
1461  
1462                      if ( 'ASC' === order ) {
1463                          jq('#send-reply').before( response );
1464                      } else {
1465                          jq('#message-recipients').after( response );
1466                          jq(window).scrollTop(offset.top);
1467                      }
1468  
1469                      jq('.new-message').hide().slideDown( 200, function() {
1470                          jq('.new-message').removeClass('new-message');
1471                      });
1472                  }
1473                  jq(button).removeClass('loading').prop( 'disabled', false );
1474              });
1475  
1476              return false;
1477          }
1478      );
1479  
1480      /* Selecting unread and read messages in inbox */
1481      jq( 'body.messages #item-body div.messages' ).on( 'change', '#message-type-select', function() {
1482          var selection   = this.value,
1483              checkboxes    = jq( 'td input[type="checkbox"]' ),
1484              checked_value = 'checked';
1485  
1486          checkboxes.each( function(i) {
1487              checkboxes[i].checked = '';
1488          });
1489  
1490          switch ( selection ) {
1491              case 'unread':
1492                  checkboxes = jq('tr.unread td input[type="checkbox"]');
1493                  break;
1494              case 'read':
1495                  checkboxes = jq('tr.read td input[type="checkbox"]');
1496                  break;
1497              case '':
1498                  checked_value = '';
1499                  break;
1500          }
1501  
1502          checkboxes.each( function(i) {
1503              checkboxes[i].checked = checked_value;
1504          });
1505      });
1506  
1507      /* Selecting/Deselecting all messages */
1508      jq( '#select-all-messages' ).on( 'click', function() {
1509          if ( this.checked ) {
1510              jq( '.message-check' ).each( function() {
1511                  this.checked = true;
1512              } );
1513          } else {
1514              jq( '.message-check' ).each( function() {
1515                  this.checked = false;
1516              } );
1517          }
1518      } );
1519  
1520      /* Make sure a 'Bulk Action' is selected before submitting the messages bulk action form */
1521      jq('#messages-bulk-manage').attr('disabled', 'disabled');
1522  
1523      /* Remove the disabled attribute from the messages form submit button when bulk action has a value */
1524      jq('#messages-select').on('change', function(){
1525          jq('#messages-bulk-manage').attr('disabled', jq(this).val().length <= 0);
1526      });
1527  
1528      /* Star action function */
1529      starAction = function() {
1530          var link = jq(this);
1531  
1532          jq.post( ajaxurl, {
1533              action: 'messages_star',
1534              'message_id': link.data('message-id'),
1535              'star_status': link.data('star-status'),
1536              'nonce': link.data('star-nonce'),
1537              'bulk': link.data('star-bulk')
1538          },
1539          function(response) {
1540              if ( 1 === parseInt( response, 10 ) ) {
1541                  if ( 'unstar' === link.data('star-status') ) {
1542                      link.data('star-status', 'star');
1543                      link.removeClass('message-action-unstar').addClass('message-action-star');
1544                      link.find('.bp-screen-reader-text').text( BP_PM_Star.strings.text_star );
1545  
1546                      if ( 1 === BP_PM_Star.is_single_thread ) {
1547                          link.attr('data-bp-tooltip', BP_PM_Star.strings.title_star );
1548                      } else {
1549                          link.attr('data-bp-tooltip', BP_PM_Star.strings.title_star_thread );
1550                      }
1551  
1552                  } else {
1553                      link.data('star-status', 'unstar');
1554                      link.removeClass('message-action-star').addClass('message-action-unstar');
1555                      link.find('.bp-screen-reader-text').text(BP_PM_Star.strings.text_unstar);
1556  
1557                      if ( 1 === BP_PM_Star.is_single_thread ) {
1558                          link.attr('data-bp-tooltip', BP_PM_Star.strings.title_unstar );
1559                      } else {
1560                          link.attr('data-bp-tooltip', BP_PM_Star.strings.title_unstar_thread );
1561                      }
1562                  }
1563              }
1564          });
1565          return false;
1566      };
1567  
1568      /* Star actions */
1569      jq('#message-threads').on('click', 'td.thread-star a', starAction );
1570      jq('#message-thread').on('click', '.message-star-actions a', starAction );
1571  
1572      /* Star bulk manage - Show only the valid action based on the starred item. */
1573      jq('#message-threads td.bulk-select-check :checkbox').on('change', function() {
1574          var box = jq(this),
1575              star = box.closest('tr').find('.thread-star a');
1576  
1577          if ( box.prop('checked') ) {
1578              if( 'unstar' === star.data('star-status') ) {
1579                  BP_PM_Star.star_counter++;
1580              } else {
1581                  BP_PM_Star.unstar_counter++;
1582              }
1583          } else {
1584              if( 'unstar' === star.data('star-status') ) {
1585                  BP_PM_Star.star_counter--;
1586              } else {
1587                  BP_PM_Star.unstar_counter--;
1588              }
1589          }
1590  
1591          if ( BP_PM_Star.star_counter > 0 && parseInt( BP_PM_Star.unstar_counter, 10 ) === 0 ) {
1592              jq('option[value="star"]').hide();
1593          } else {
1594              jq('option[value="star"]').show();
1595          }
1596  
1597          if ( BP_PM_Star.unstar_counter > 0 && parseInt( BP_PM_Star.star_counter, 10 ) === 0 ) {
1598              jq('option[value="unstar"]').hide();
1599          } else {
1600              jq('option[value="unstar"]').show();
1601          }
1602      });
1603  
1604      /** Notifications **********************************************/
1605  
1606      /* Selecting/Deselecting all notifications */
1607      jq( '#select-all-notifications' ).on( 'click', function() {
1608          if ( this.checked ) {
1609              jq( '.notification-check' ).each( function() {
1610                  this.checked = true;
1611              } );
1612          } else {
1613              jq( '.notification-check' ).each( function() {
1614                  this.checked = false;
1615              } );
1616          }
1617      } );
1618  
1619      /* Make sure a 'Bulk Action' is selected before submitting the form */
1620      jq('#notification-bulk-manage').attr('disabled', 'disabled');
1621  
1622      /* Remove the disabled attribute from the form submit button when bulk action has a value */
1623      jq('#notification-select').on('change', function(){
1624          jq('#notification-bulk-manage').attr('disabled', jq(this).val().length <= 0);
1625      });
1626  
1627      /** Members Invitations *****************************************/
1628  
1629      /* Selecting/Deselecting all invitations */
1630      jq( '#select-all-invitations' ).on( 'click', function() {
1631          if ( this.checked ) {
1632              jq( '.invitation-check' ).each( function() {
1633                  this.checked = true;
1634              } );
1635          } else {
1636              jq( '.invitation-check' ).each( function() {
1637                  this.checked = false;
1638              } );
1639          }
1640      } );
1641  
1642      /* Make sure a 'Bulk Action' is selected before submitting the form */
1643      jq('#invitation-bulk-manage').attr('disabled', 'disabled');
1644  
1645      /* Remove the disabled attribute from the form submit button when bulk action has a value */
1646      jq('#invitation-select').on('change', function(){
1647          jq('#invitation-bulk-manage').attr('disabled', jq(this).val().length <= 0);
1648      });
1649  
1650      /* Close site wide notices in the sidebar */
1651      jq('#close-notice').on( 'click', function() {
1652          jq(this).addClass('loading');
1653          jq('#sidebar div.error').remove();
1654  
1655          jq.post( ajaxurl, {
1656              action: 'messages_close_notice',
1657              'notice_id': jq('.notice').attr('rel').substr( 2, jq('.notice').attr('rel').length ),
1658              nonce: jq( '#close-notice-nonce' ).val()
1659          },
1660          function(response) {
1661              jq('#close-notice').removeClass('loading');
1662  
1663              if ( response[0] + response[1] === '-1' ) {
1664                  jq('.notice').prepend( response.substr( 2, response.length ) );
1665                  jq( '#sidebar div.error').hide().fadeIn( 200 );
1666              } else {
1667                  jq('.notice').slideUp( 100 );
1668              }
1669          });
1670          return false;
1671      });
1672  
1673      /* Toolbar & wp_list_pages JavaScript IE6 hover class */
1674      jq('#wp-admin-bar ul.main-nav li, #nav li').on( 'mouseover', function() {
1675          jq(this).addClass('sfhover');
1676      });
1677  
1678      jq('#wp-admin-bar ul.main-nav li, #nav li').on( 'mouseout', function() {
1679          jq(this).removeClass('sfhover');
1680      });
1681  
1682      /* Clear BP cookies on logout */
1683      jq('#wp-admin-bar-logout, a.logout').on( 'click', function() {
1684          jq.removeCookie('bp-activity-scope', {
1685              path: '/',
1686              secure: ( 'https:' === window.location.protocol )
1687          });
1688          jq.removeCookie('bp-activity-filter', {
1689              path: '/',
1690              secure: ( 'https:' === window.location.protocol )
1691          });
1692          jq.removeCookie('bp-activity-oldestpage', {
1693              path: '/',
1694              secure: ( 'https:' === window.location.protocol )
1695          });
1696  
1697          var objects = [ 'members', 'groups', 'blogs', 'forums' ];
1698          jq(objects).each( function(i) {
1699              jq.removeCookie('bp-' + objects[i] + '-scope', {
1700                  path: '/',
1701                  secure: ( 'https:' === window.location.protocol )
1702              } );
1703              jq.removeCookie('bp-' + objects[i] + '-filter', {
1704                  path: '/',
1705                  secure: ( 'https:' === window.location.protocol )
1706              } );
1707              jq.removeCookie('bp-' + objects[i] + '-extras', {
1708                  path: '/',
1709                  secure: ( 'https:' === window.location.protocol )
1710              } );
1711          });
1712      });
1713  
1714      /* if js is enabled then replace the no-js class by a js one */
1715      if( jq('body').hasClass('no-js') ) {
1716          jq('body').attr('class', jq('body').attr('class').replace( /no-js/,'js' ) );
1717      }
1718  
1719      /** Activity HeartBeat ************************************************/
1720  
1721      // Set the interval and the namespace event
1722      if ( typeof wp !== 'undefined' && typeof wp.heartbeat !== 'undefined' && typeof BP_DTheme.pulse !== 'undefined' ) {
1723  
1724          wp.heartbeat.interval( Number( BP_DTheme.pulse ) );
1725  
1726          jq.fn.extend({
1727              'heartbeat-send': function() {
1728                  return this.bind( 'heartbeat-send.buddypress' );
1729              }
1730          });
1731      }
1732  
1733      // Set the last id to request after
1734      var first_item_recorded = 0;
1735      jq( document ).on( 'heartbeat-send.buddypress', function( e, data ) {
1736  
1737          first_item_recorded = 0;
1738  
1739          // First row is default latest activity id
1740          if ( jq( '#buddypress ul.activity-list li' ).first().prop( 'id' ) ) {
1741              // getting the timestamp
1742              timestamp = jq( '#buddypress ul.activity-list li' ).first().prop( 'class' ).match( /date-recorded-([0-9]+)/ );
1743  
1744              if ( timestamp ) {
1745                  first_item_recorded = timestamp[1];
1746              }
1747          }
1748  
1749          if ( 0 === activity_last_recorded || Number( first_item_recorded ) > activity_last_recorded ) {
1750              activity_last_recorded = Number( first_item_recorded );
1751          }
1752  
1753          data.bp_activity_last_recorded = activity_last_recorded;
1754  
1755          last_recorded_search = bp_get_querystring('s');
1756  
1757          if ( last_recorded_search ) {
1758              data.bp_activity_last_recorded_search_terms = last_recorded_search;
1759          }
1760      });
1761  
1762      // Increment newest_activities and activity_last_recorded if data has been returned
1763      jq( document ).on( 'heartbeat-tick', function( e, data ) {
1764  
1765          // Only proceed if we have newest activities
1766          if ( ! data.bp_activity_newest_activities ) {
1767              return;
1768          }
1769  
1770          newest_activities = data.bp_activity_newest_activities.activities + newest_activities;
1771          activity_last_recorded  = Number( data.bp_activity_newest_activities.last_recorded );
1772  
1773          if ( jq( '#buddypress ul.activity-list li' ).first().hasClass( 'load-newest' ) ) {
1774              return;
1775          }
1776  
1777          jq( '#buddypress ul.activity-list' ).prepend( '<li class="load-newest"><a href="#newest">' + BP_DTheme.newest + '</a></li>' );
1778      });
1779  });
1780  
1781  /**
1782   * Gets the user's current preference for a directory option.
1783   */
1784  function bp_get_directory_preference( directoryType, pref ) {
1785      var defaultPrefs = {
1786          filter: '',
1787          scope: '',
1788          extras: ''
1789      };
1790  
1791      if ( ! directoryPreferences.hasOwnProperty( directoryType ) ) {
1792          var newPreferences = {};
1793          for ( var prefName in defaultPrefs ) {
1794              if ( defaultPrefs.hasOwnProperty( prefName ) ) {
1795                  newPreferences[ prefName ] = defaultPrefs[ prefName ];
1796              }
1797          }
1798          directoryPreferences[ directoryType ] = newPreferences;
1799      }
1800  
1801      if ( BP_DTheme.store_filter_settings ) {
1802          directoryPreferences[ directoryType ][ pref ] = jq.cookie( 'bp-' + directoryType + '-' + pref );
1803      }
1804  
1805      return directoryPreferences[ directoryType ][ pref ];
1806  }
1807  
1808  /**
1809   * Sets the user's current preference for a directory option.
1810   */
1811  function bp_set_directory_preference( directoryType, pref, value ) {
1812      var defaultPrefs = {
1813          filter: '',
1814          scope: '',
1815          extras: ''
1816      };
1817  
1818      if ( ! directoryPreferences.hasOwnProperty( directoryType ) ) {
1819          var newPreferences = {};
1820          for ( var prefName in defaultPrefs ) {
1821              if ( defaultPrefs.hasOwnProperty( prefName ) ) {
1822                  newPreferences[ prefName ] = defaultPrefs[ prefName ];
1823              }
1824          }
1825          directoryPreferences[ directoryType ] = newPreferences;
1826      }
1827  
1828      if ( BP_DTheme.store_filter_settings ) {
1829          jq.cookie( 'bp-' + directoryType + '-' + pref, value, {
1830              path: '/',
1831              secure: ( 'https:' === window.location.protocol )
1832          } );
1833      }
1834  
1835      directoryPreferences[ directoryType ][ pref ] = value;
1836  }
1837  
1838  /* Setup activity scope and filter based on the current cookie settings. */
1839  function bp_init_activity() {
1840      var scope  = bp_get_directory_preference( 'activity', 'scope' );
1841      var filter = bp_get_directory_preference( 'activity', 'filter' );
1842  
1843      if ( undefined !== filter && jq('#activity-filter-select').length ) {
1844          jq('#activity-filter-select select option[value="' + filter + '"]').prop( 'selected', true );
1845      }
1846  
1847      /* Activity Tab Set */
1848      if ( undefined !== scope && jq('.activity-type-tabs').length ) {
1849          jq('.activity-type-tabs li').each( function() {
1850              jq(this).removeClass('selected');
1851          });
1852          jq('#activity-' + scope + ', .item-list-tabs li.current').addClass('selected');
1853      }
1854  }
1855  
1856  /* Setup object scope and filter based on the current cookie settings for the object. */
1857  function bp_init_objects(objects) {
1858      jq(objects).each( function(i) {
1859          var scope  = bp_get_directory_preference( objects[i], 'scope' );
1860          var filter = bp_get_directory_preference( objects[i], 'filter' );
1861  
1862          if ( undefined !== filter && jq('#' + objects[i] + '-order-select select').length ) {
1863              jq('#' + objects[i] + '-order-select select option[value="' + filter + '"]').prop( 'selected', true );
1864          }
1865  
1866          if ( undefined !== scope && jq('div.' + objects[i]).length ) {
1867              jq('.item-list-tabs li').each( function() {
1868                  jq(this).removeClass('selected');
1869              });
1870              jq('#' + objects[i] + '-' + scope + ', #object-nav li.current').addClass('selected');
1871          }
1872      });
1873  }
1874  
1875  /* Filter the current content list (groups/members/blogs/topics) */
1876  function bp_filter_request( object, filter, scope, target, search_terms, page, extras, caller, template ) {
1877      if ( 'activity' === object ) {
1878          return false;
1879      }
1880  
1881      if ( null === scope ) {
1882          scope = 'all';
1883      }
1884  
1885      /* Save the settings we want to remain persistent */
1886      bp_set_directory_preference( object, 'scope', scope );
1887      bp_set_directory_preference( object, 'filter', filter );
1888      bp_set_directory_preference( object, 'extras', extras );
1889  
1890      /* Set the correct selected nav and filter */
1891      jq('.item-list-tabs li').each( function() {
1892          jq(this).removeClass('selected');
1893      });
1894      jq('#' + object + '-' + scope + ', #object-nav li.current').addClass('selected');
1895      jq('.item-list-tabs li.selected').addClass('loading');
1896      jq('.item-list-tabs select option[value="' + filter + '"]').prop( 'selected', true );
1897  
1898      if ( 'friends' === object || 'group_members' === object ) {
1899          object = 'members';
1900      }
1901  
1902      if ( bp_ajax_request ) {
1903          bp_ajax_request.abort();
1904      }
1905  
1906      // Get directory preferences (called "cookie" for legacy reasons).
1907      var cookies = {};
1908      cookies['bp-' + object + '-filter'] = bp_get_directory_preference( object, 'filter' );
1909      cookies['bp-' + object + '-scope']  = bp_get_directory_preference( object, 'scope' );
1910  
1911      var cookie = encodeURIComponent( jq.param( cookies ) );
1912  
1913      bp_ajax_request = jq.post( ajaxurl, {
1914          action: object + '_filter',
1915          'cookie': cookie,
1916          'object': object,
1917          'filter': filter,
1918          'search_terms': search_terms,
1919          'scope': scope,
1920          'page': page,
1921          'extras': extras,
1922          'template': template
1923      },
1924      function(response)
1925      {
1926          /* animate to top if called from bottom pagination */
1927          if ( caller === 'pag-bottom' && jq('#subnav').length ) {
1928              var top = jq('#subnav').parent();
1929              jq('html,body').animate({scrollTop: top.offset().top}, 'slow', function() {
1930                  jq(target).fadeOut( 100, function() {
1931                      jq(this).html(response);
1932                      jq(this).fadeIn(100);
1933                  });
1934              });
1935  
1936          } else {
1937              jq(target).fadeOut( 100, function() {
1938                  jq(this).html(response);
1939                  jq(this).fadeIn(100);
1940              });
1941          }
1942  
1943          jq('.item-list-tabs li.selected').removeClass('loading');
1944      });
1945  }
1946  
1947  /* Activity Loop Requesting */
1948  function bp_activity_request(scope, filter) {
1949      /* Save the type and filter */
1950      bp_set_directory_preference( 'activity', 'scope', scope );
1951      bp_set_directory_preference( 'activity', 'filter', filter );
1952  
1953      /* Remove selected and loading classes from tabs */
1954      jq('.item-list-tabs li').each( function() {
1955          jq(this).removeClass('selected loading');
1956      });
1957      /* Set the correct selected nav and filter */
1958      jq('#activity-' + scope + ', .item-list-tabs li.current').addClass('selected');
1959      jq('#object-nav.item-list-tabs li.selected, div.activity-type-tabs li.selected').addClass('loading');
1960      jq('#activity-filter-select select option[value="' + filter + '"]').prop( 'selected', true );
1961  
1962      /* Reload the activity stream based on the selection */
1963      jq('.widget_bp_activity_widget h2 span.ajax-loader').show();
1964  
1965      if ( bp_ajax_request ) {
1966          bp_ajax_request.abort();
1967      }
1968  
1969      // Get directory preferences (called "cookie" for legacy reasons).
1970      var cookies = {
1971          'bp-activity-filter': bp_get_directory_preference( 'activity', 'filter' ),
1972          'bp-activity-scope': bp_get_directory_preference( 'activity', 'scope' )
1973      };
1974  
1975      var cookie = encodeURIComponent( jq.param( cookies ) );
1976  
1977      bp_ajax_request = jq.post( ajaxurl, {
1978          action: 'activity_widget_filter',
1979          'cookie': cookie,
1980          '_wpnonce_activity_filter': jq('#_wpnonce_activity_filter').val(),
1981          'scope': scope,
1982          'filter': filter
1983      },
1984      function(response)
1985      {
1986          jq('.widget_bp_activity_widget h2 span.ajax-loader').hide();
1987  
1988          jq('div.activity').fadeOut( 100, function() {
1989              jq(this).html(response.contents);
1990              jq(this).fadeIn(100);
1991  
1992              /* Selectively hide comments */
1993              bp_legacy_theme_hide_comments();
1994          });
1995  
1996          /* Update the feed link */
1997          if ( undefined !== response.feed_url ) {
1998              jq('.directory #subnav li.feed a, .home-page #subnav li.feed a').attr('href', response.feed_url);
1999          }
2000  
2001          jq('.item-list-tabs li.selected').removeClass('loading');
2002  
2003      }, 'json' );
2004  }
2005  
2006  /* Hide long lists of activity comments, only show the latest five root comments. */
2007  function bp_legacy_theme_hide_comments() {
2008      var comments_divs = jq('div.activity-comments'),
2009          parent_li, comment_lis, comment_count;
2010  
2011      if ( !comments_divs.length ) {
2012          return false;
2013      }
2014  
2015      comments_divs.each( function() {
2016          if ( jq(this).children('ul').children('li').length < 5 ) {
2017              return;
2018          }
2019  
2020          comments_div = jq(this);
2021          parent_li = comments_div.parents('#activity-stream > li');
2022          comment_lis = jq(this).children('ul').children('li');
2023          comment_count = ' ';
2024  
2025          if ( jq('#' + parent_li.attr('id') + ' li[id*="acomment-"]').length ) {
2026              comment_count = jq('#' + parent_li.attr('id') + ' li[id*="acomment-"]').length;
2027          }
2028  
2029          comment_lis.each( function(i) {
2030              /* Show the latest 5 root comments */
2031              if ( i < comment_lis.length - 5 ) {
2032                  jq(this).hide();
2033  
2034                  if ( !i ) {
2035                      jq(this).before( '<li class="show-all"><a href="#' + parent_li.attr('id') + '/show-all/">' + BP_DTheme.show_x_comments.replace( '%d', comment_count ) + '</a></li>' );
2036                  }
2037              }
2038          });
2039  
2040      });
2041  }
2042  
2043  /* Helper Functions */
2044  
2045  function checkAll() {
2046      var checkboxes = document.getElementsByTagName('input'),
2047          i;
2048  
2049      for(i=0; i<checkboxes.length; i++) {
2050          if(checkboxes[i].type === 'checkbox') {
2051              if($('check_all').checked === '') {
2052                  checkboxes[i].checked = '';
2053              }
2054              else {
2055                  checkboxes[i].checked = 'checked';
2056              }
2057          }
2058      }
2059  }
2060  
2061  /**
2062   * Deselects any select options or input options for the specified field element.
2063   *
2064   * @param {String} container HTML ID of the field
2065   * @since 1.2.0
2066   */
2067  function clear( container ) {
2068      container = document.getElementById( container );
2069      if ( ! container ) {
2070          return;
2071      }
2072  
2073      var radioButtons = container.getElementsByTagName( 'INPUT' ),
2074          options = container.getElementsByTagName( 'OPTION' ),
2075          i       = 0;
2076  
2077      if ( radioButtons ) {
2078          for ( i = 0; i < radioButtons.length; i++ ) {
2079              radioButtons[i].checked = '';
2080          }
2081      }
2082  
2083      if ( options ) {
2084          for ( i = 0; i < options.length; i++ ) {
2085              options[i].selected = false;
2086          }
2087      }
2088  }
2089  
2090  /* Returns a querystring of BP cookies (cookies beginning with 'bp-') */
2091  function bp_get_cookies() {
2092      var allCookies = document.cookie.split(';'),  // get all cookies and split into an array
2093          bpCookies      = {},
2094          cookiePrefix   = 'bp-',
2095          i, cookie, delimiter, name, value;
2096  
2097      // loop through cookies
2098      for (i = 0; i < allCookies.length; i++) {
2099          cookie    = allCookies[i];
2100          delimiter = cookie.indexOf('=');
2101          name      = jq.trim( unescape( cookie.slice(0, delimiter) ) );
2102          value     = unescape( cookie.slice(delimiter + 1) );
2103  
2104          // if BP cookie, store it
2105          if ( name.indexOf(cookiePrefix) === 0 ) {
2106              bpCookies[name] = value;
2107          }
2108      }
2109  
2110      // returns BP cookies as querystring
2111      return encodeURIComponent( jq.param(bpCookies) );
2112  }
2113  
2114  /**
2115   * Get a querystring parameter from a URL.
2116   *
2117   * @param {String} Query string parameter name.
2118   * @param {String} URL to parse. Defaults to current URL.
2119   */
2120  function bp_get_query_var( param, url ) {
2121      var qs = {};
2122  
2123      // Use current URL if no URL passed.
2124      if ( typeof url === 'undefined' ) {
2125          url = location.search.substr(1).split('&');
2126      } else {
2127          url = url.split('?')[1].split('&');
2128      }
2129  
2130      // Parse querystring into object props.
2131      // http://stackoverflow.com/a/21152762
2132      url.forEach(function(item) {
2133          qs[item.split('=')[0]] = item.split('=')[1] && decodeURIComponent( item.split('=')[1] );
2134      });
2135  
2136      if ( qs.hasOwnProperty( param ) && qs[param] != null ) {
2137          return qs[param];
2138      } else {
2139          return false;
2140      }
2141  }


Generated: Fri Dec 6 01:00:58 2024 Cross-referenced by PHPXref 0.7.1