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


Generated: Sat Dec 7 01:01:37 2019 Cross-referenced by PHPXref 0.7.1