[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  /* jshint browser: true */
   2  /* global bp, BP_Nouveau */
   3  /* @version 3.1.0 */
   4  window.bp = window.bp || {};
   5  
   6  ( function( exports, $ ) {
   7  
   8      // Bail if not set
   9      if ( typeof BP_Nouveau === 'undefined' ) {
  10          return;
  11      }
  12  
  13      bp.Nouveau = bp.Nouveau || {};
  14  
  15      /**
  16       * [Activity description]
  17       * @type {Object}
  18       */
  19      bp.Nouveau.Activity = {
  20  
  21          /**
  22           * [start description]
  23           * @return {[type]} [description]
  24           */
  25          start: function() {
  26              this.setupGlobals();
  27  
  28              // Listen to events ("Add hooks!")
  29              this.addListeners();
  30          },
  31  
  32          /**
  33           * [setupGlobals description]
  34           * @return {[type]} [description]
  35           */
  36          setupGlobals: function() {
  37              // Init just posted activities
  38              this.just_posted    = [];
  39  
  40              // Init current page
  41              this.current_page   = 1;
  42  
  43              // Init mentions count
  44              this.mentions_count = Number( $( bp.Nouveau.objectNavParent + ' [data-bp-scope="mentions"]' ).find( 'a span' ).html() ) || 0;
  45  
  46              // HeartBeat Globals
  47              this.heartbeat_data = {
  48                  newest         : '',
  49                  highlights     : {},
  50                  last_recorded  : 0,
  51                  first_recorded : 0,
  52                  document_title : $( document ).prop( 'title' )
  53              };
  54          },
  55  
  56          /**
  57           * [addListeners description]
  58           */
  59          addListeners: function() {
  60              // HeartBeat listeners
  61              $( '#buddypress' ).on( 'bp_heartbeat_send', this.heartbeatSend.bind( this ) );
  62              $( '#buddypress' ).on( 'bp_heartbeat_tick', this.heartbeatTick.bind( this ) );
  63  
  64              // Inject Activities
  65              $( '#buddypress [data-bp-list="activity"]' ).on( 'click', 'li.load-newest, li.load-more', this.injectActivities.bind( this ) );
  66  
  67              // Hightlight new activities & clean up the stream
  68              $( '#buddypress' ).on( 'bp_ajax_request', '[data-bp-list="activity"]', this.scopeLoaded.bind( this ) );
  69  
  70              // Activity comments effect
  71              $( '#buddypress [data-bp-list="activity"]' ).on( 'bp_ajax_append', this.hideComments );
  72              $( '#buddypress [data-bp-list="activity"]' ).on( 'click', '.show-all', this.showComments );
  73  
  74              // Activity actions
  75              $( '#buddypress [data-bp-list="activity"]' ).on( 'click', '.activity-item', bp.Nouveau, this.activityActions );
  76              $( document ).keydown( this.commentFormAction );
  77          },
  78  
  79          /**
  80           * [heartbeatSend description]
  81           * @param  {[type]} event [description]
  82           * @param  {[type]} data  [description]
  83           * @return {[type]}       [description]
  84           */
  85          heartbeatSend: function( event, data ) {
  86              this.heartbeat_data.first_recorded = $( '#buddypress [data-bp-list] [data-bp-activity-id]' ).first().data( 'bp-timestamp' ) || 0;
  87  
  88              if ( 0 === this.heartbeat_data.last_recorded || this.heartbeat_data.first_recorded > this.heartbeat_data.last_recorded ) {
  89                  this.heartbeat_data.last_recorded = this.heartbeat_data.first_recorded;
  90              }
  91  
  92              data.bp_activity_last_recorded = this.heartbeat_data.last_recorded;
  93  
  94              if ( $( '#buddypress .dir-search input[type=search]' ).length ) {
  95                  data.bp_activity_last_recorded_search_terms = $( '#buddypress .dir-search input[type=search]' ).val();
  96              }
  97  
  98              $.extend( data, { bp_heartbeat: bp.Nouveau.getStorage( 'bp-activity' ) } );
  99          },
 100  
 101          /**
 102           * [heartbeatTick description]
 103           * @param  {[type]} event          [description]
 104           * @param  {[type]} data           [description]
 105           * @return {[type]}                [description]
 106           */
 107          heartbeatTick: function( event, data ) {
 108              var newest_activities_count, newest_activities, objects = bp.Nouveau.objects,
 109                  scope = bp.Nouveau.getStorage( 'bp-activity', 'scope' ), self = this;
 110  
 111              // Only proceed if we have newest activities
 112              if ( undefined === data || ! data.bp_activity_newest_activities ) {
 113                  return;
 114              }
 115  
 116              this.heartbeat_data.newest = $.trim( data.bp_activity_newest_activities.activities ) + this.heartbeat_data.newest;
 117              this.heartbeat_data.last_recorded  = Number( data.bp_activity_newest_activities.last_recorded );
 118  
 119              // Parse activities
 120              newest_activities = $( this.heartbeat_data.newest ).filter( '.activity-item' );
 121  
 122              // Count them
 123              newest_activities_count = Number( newest_activities.length );
 124  
 125              /**
 126               * It's not a regular object but we need it!
 127               * so let's add it temporarly..
 128               */
 129              objects.push( 'mentions' );
 130  
 131              /**
 132               * On the All Members tab, we need to know what these activities are about
 133               * in order to update all the other tabs dynamic span
 134               */
 135              if ( 'all' === scope ) {
 136  
 137                  $.each( newest_activities, function( a, activity ) {
 138                      activity = $( activity );
 139  
 140                      $.each( objects, function( o, object ) {
 141                          if ( -1 !== $.inArray( 'bp-my-' + object, activity.get( 0 ).classList ) ) {
 142                              if ( undefined === self.heartbeat_data.highlights[ object ] ) {
 143                                  self.heartbeat_data.highlights[ object ] = [ activity.data( 'bp-activity-id' ) ];
 144                              } else if ( -1 === $.inArray( activity.data( 'bp-activity-id' ), self.heartbeat_data.highlights[ object ] ) ) {
 145                                  self.heartbeat_data.highlights[ object ].push( activity.data( 'bp-activity-id' ) );
 146                              }
 147                          }
 148                      } );
 149                  } );
 150  
 151                  // Remove the specific classes to count highligthts
 152                  var regexp = new RegExp( 'bp-my-(' + objects.join( '|' ) + ')', 'g' );
 153                  this.heartbeat_data.newest = this.heartbeat_data.newest.replace( regexp, '' );
 154  
 155                  /**
 156                   * Deal with the 'All Members' dynamic span from here as HeartBeat is working even when
 157                   * the user is not logged in
 158                   */
 159                   $( bp.Nouveau.objectNavParent + ' [data-bp-scope="all"]' ).find( 'a span' ).html( newest_activities_count );
 160  
 161              // Set all activities to be highlighted for the current scope
 162              } else {
 163                  // Init the array of highlighted activities
 164                  this.heartbeat_data.highlights[ scope ] = [];
 165  
 166                  $.each( newest_activities, function( a, activity ) {
 167                      self.heartbeat_data.highlights[ scope ].push( $( activity ).data( 'bp-activity-id' ) );
 168                  } );
 169              }
 170  
 171              $.each( objects, function( o, object ) {
 172                  if ( undefined !== self.heartbeat_data.highlights[ object ] && self.heartbeat_data.highlights[ object ].length ) {
 173                      var count = 0;
 174  
 175                      if ( 'mentions' === object ) {
 176                          count = self.mentions_count;
 177                      }
 178  
 179                      $( bp.Nouveau.objectNavParent + ' [data-bp-scope="' + object + '"]' ).find( 'a span' ).html( Number( self.heartbeat_data.highlights[ object ].length ) + count );
 180                  }
 181              } );
 182  
 183              /**
 184               * Let's remove the mentions from objects!
 185               */
 186              objects.pop();
 187  
 188              // Add an information about the number of newest activities inside the document's title
 189              $( document ).prop( 'title', '(' + newest_activities_count + ') ' + this.heartbeat_data.document_title );
 190  
 191              // Update the Load Newest li if it already exists.
 192              if ( $( '#buddypress [data-bp-list="activity"] li' ).first().hasClass( 'load-newest' ) ) {
 193                  var newest_link = $( '#buddypress [data-bp-list="activity"] .load-newest a' ).html();
 194                  $( '#buddypress [data-bp-list="activity"] .load-newest a' ).html( newest_link.replace( /([0-9]+)/, newest_activities_count ) );
 195  
 196              // Otherwise add it
 197              } else {
 198                  $( '#buddypress [data-bp-list="activity"] ul.activity-list' ).prepend( '<li class="load-newest"><a href="#newest">' + BP_Nouveau.newest + ' (' + newest_activities_count + ')</a></li>' );
 199              }
 200  
 201              /**
 202               * Finally trigger a pending event containing the activity heartbeat data
 203               */
 204              $( '#buddypress [data-bp-list="activity"]' ).trigger( 'bp_heartbeat_pending', this.heartbeat_data );
 205          },
 206  
 207          /**
 208           * [injectQuery description]
 209           * @param  {[type]} event [description]
 210           * @return {[type]}       [description]
 211           */
 212          injectActivities: function( event ) {
 213              var store = bp.Nouveau.getStorage( 'bp-activity' ),
 214                  scope = store.scope || null, filter = store.filter || null;
 215  
 216              // Load newest activities
 217              if ( $( event.currentTarget ).hasClass( 'load-newest' ) ) {
 218                  // Stop event propagation
 219                  event.preventDefault();
 220  
 221                  $( event.currentTarget ).remove();
 222  
 223                  /**
 224                   * If a plugin is updating the recorded_date of an activity
 225                   * it will be loaded as a new one. We need to look in the
 226                   * stream and eventually remove similar ids to avoid "double".
 227                   */
 228                  var activities = $.parseHTML( this.heartbeat_data.newest );
 229  
 230                  $.each( activities, function( a, activity ){
 231                      if( 'LI' === activity.nodeName && $( activity ).hasClass( 'just-posted' ) ) {
 232                          if( $( '#' + $( activity ).prop( 'id' ) ).length ) {
 233                              $( '#' + $( activity ).prop( 'id' ) ).remove();
 234                          }
 235                      }
 236                  } );
 237  
 238                  // Now the stream is cleaned, prepend newest
 239                  $( event.delegateTarget ).find( '.activity-list' ).prepend( this.heartbeat_data.newest ).trigger( 'bp_heartbeat_prepend', this.heartbeat_data );
 240  
 241                  // Reset the newest activities now they're displayed
 242                  this.heartbeat_data.newest = '';
 243  
 244                  // Reset the All members tab dynamic span id it's the current one
 245                  if ( 'all' === scope ) {
 246                      $( bp.Nouveau.objectNavParent + ' [data-bp-scope="all"]' ).find( 'a span' ).html( '' );
 247                  }
 248  
 249                  // Specific to mentions
 250                  if ( 'mentions' === scope ) {
 251                      // Now mentions are displayed, remove the user_metas
 252                      bp.Nouveau.ajax( { action: 'activity_clear_new_mentions' }, 'activity' );
 253                      this.mentions_count = 0;
 254                  }
 255  
 256                  // Activities are now displayed, clear the newest count for the scope
 257                  $( bp.Nouveau.objectNavParent + ' [data-bp-scope="' + scope + '"]' ).find( 'a span' ).html( '' );
 258  
 259                  // Activities are now displayed, clear the highlighted activities for the scope
 260                  if ( undefined !== this.heartbeat_data.highlights[ scope ] ) {
 261                      this.heartbeat_data.highlights[ scope ] = [];
 262                  }
 263  
 264                  // Remove highlighted for the current scope
 265                  setTimeout( function () {
 266                      $( event.delegateTarget ).find( '[data-bp-activity-id]' ).removeClass( 'newest_' + scope + '_activity' );
 267                  }, 3000 );
 268  
 269                  // Reset the document title
 270                  $( document ).prop( 'title', this.heartbeat_data.document_title );
 271  
 272              // Load more activities
 273              } else if ( $( event.currentTarget ).hasClass( 'load-more' ) ) {
 274                  var next_page = ( Number( this.current_page ) * 1 ) + 1, self = this, search_terms = '';
 275  
 276                  // Stop event propagation
 277                  event.preventDefault();
 278  
 279                  $( event.currentTarget ).find( 'a' ).first().addClass( 'loading' );
 280  
 281                  // reset the just posted
 282                  this.just_posted = [];
 283  
 284                  // Now set it
 285                  $( event.delegateTarget ).children( '.just-posted' ).each( function() {
 286                      self.just_posted.push( $( this ).data( 'bp-activity-id' ) );
 287                  } );
 288  
 289                  if ( $( '#buddypress .dir-search input[type=search]' ).length ) {
 290                      search_terms = $( '#buddypress .dir-search input[type=search]' ).val();
 291                  }
 292  
 293                  bp.Nouveau.objectRequest( {
 294                      object              : 'activity',
 295                      scope               : scope,
 296                      filter              : filter,
 297                      search_terms        : search_terms,
 298                      page                : next_page,
 299                      method              : 'append',
 300                      exclude_just_posted : this.just_posted.join( ',' ),
 301                      target              : '#buddypress [data-bp-list] ul.bp-list'
 302                  } ).done( function( response ) {
 303                      if ( true === response.success ) {
 304                          $( event.currentTarget ).remove();
 305  
 306                          // Update the current page
 307                          self.current_page = next_page;
 308                      }
 309                  } );
 310              }
 311          },
 312  
 313          /**
 314           * [truncateComments description]
 315           * @param  {[type]} event [description]
 316           * @return {[type]}       [description]
 317           */
 318          hideComments: function( event ) {
 319              var comments = $( event.target ).find( '.activity-comments' ),
 320                  activity_item, comment_items, comment_count, comment_parents;
 321  
 322              if ( ! comments.length ) {
 323                  return;
 324              }
 325  
 326              comments.each( function( c, comment ) {
 327                  comment_parents = $( comment ).children( 'ul' );
 328                  comment_items   = $( comment_parents ).find( 'li' );
 329  
 330  
 331                  if ( ! comment_items.length ) {
 332                      return;
 333                  }
 334  
 335                  // Get the activity id
 336                  activity_item = $( comment ).closest( '.activity-item' );
 337  
 338                  // Get the comment count
 339                  comment_count = $( '#acomment-comment-' + activity_item.data( 'bp-activity-id' ) + ' span.comment-count' ).html() || ' ';
 340  
 341                  // Keep latest 5 comments
 342                  comment_items.each( function( i, item ) {
 343                      if ( i < comment_items.length - 5 ) {
 344                          $( item ).addClass('bp-hidden').hide();
 345  
 346                          // Prepend a link to display all
 347                          if ( ! i ) {
 348                              var activity_id = activity_item.data( 'bpActivityId' );
 349                              if ( 'undefined' !== typeof activity_id ) {
 350                                  activity_id = parseInt( activity_id, 10 );
 351                                  $( item ).before( '<li class="show-all"><button class="text-button" type="button" data-bp-show-comments-id="#activity-' + activity_id + '/show-all/"><span class="icon dashicons dashicons-visibility" aria-hidden="true"></span> ' + BP_Nouveau.show_x_comments.replace( '%d', comment_count ) + '</button></li>' );
 352                              }
 353                          }
 354                      }
 355                  } );
 356  
 357                  // If all parents are hidden, reveal at least one. It seems very risky to manipulate the DOM to keep exactly 5 comments!
 358                  if ( $( comment_parents ).children( '.bp-hidden' ).length === $( comment_parents ).children( 'li' ).length - 1 && $( comment_parents ).find( 'li.show-all' ).length ) {
 359                      $( comment_parents ).children( 'li' ).removeClass( 'bp-hidden' ).toggle();
 360                  }
 361              } );
 362          },
 363  
 364          /**
 365           * [showComments description]
 366           * @param  {[type]} event [description]
 367           * @return {[type]}       [description]
 368           */
 369          showComments: function( event ) {
 370              // Stop event propagation
 371              event.preventDefault();
 372  
 373              $( event.target ).addClass( 'loading' );
 374  
 375              setTimeout( function() {
 376                  $( event.target ).closest( 'ul' ).find( 'li' ).removeClass('bp-hidden').fadeIn( 300, function() {
 377                      $( event.target ).parent( 'li' ).remove();
 378                  } );
 379              }, 600 );
 380          },
 381  
 382          /**
 383           * [scopeLoaded description]
 384           * @param  {[type]} event [description]
 385           * @param  {[type]} data  [description]
 386           * @return {[type]}       [description]
 387           */
 388          scopeLoaded: function ( event, data ) {
 389              // Make sure to only keep 5 root comments
 390              this.hideComments( event );
 391  
 392              // Reset the pagination for the scope.
 393              this.current_page = 1;
 394  
 395              // Mentions are specific
 396              if ( 'mentions' === data.scope && undefined !== data.response.new_mentions ) {
 397                  $.each( data.response.new_mentions, function( i, id ) {
 398                      $( '#buddypress #activity-stream' ).find( '[data-bp-activity-id="' + id + '"]' ).addClass( 'newest_mentions_activity' );
 399                  } );
 400  
 401                  // Reset mentions count
 402                  this.mentions_count = 0;
 403              } else if ( undefined !== this.heartbeat_data.highlights[data.scope] && this.heartbeat_data.highlights[data.scope].length ) {
 404                  $.each( this.heartbeat_data.highlights[data.scope], function( i, id ) {
 405                      if ( $( '#buddypress #activity-stream' ).find( '[data-bp-activity-id="' + id + '"]' ).length ) {
 406                          $( '#buddypress #activity-stream' ).find( '[data-bp-activity-id="' + id + '"]' ).addClass( 'newest_' + data.scope + '_activity' );
 407                      }
 408                  } );
 409              }
 410  
 411              // Reset the newest activities now they're displayed
 412              this.heartbeat_data.newest = '';
 413              $.each( $( bp.Nouveau.objectNavParent + ' [data-bp-scope]' ).find( 'a span' ), function( s, count ) {
 414                  if ( 0 === parseInt( $( count ).html(), 10 ) ) {
 415                      $( count ).html( '' );
 416                  }
 417              } );
 418  
 419              // Activities are now loaded, clear the highlighted activities for the scope
 420              if ( undefined !== this.heartbeat_data.highlights[ data.scope ] ) {
 421                  this.heartbeat_data.highlights[ data.scope ] = [];
 422              }
 423  
 424              // Reset the document title
 425              $( document ).prop( 'title', this.heartbeat_data.document_title );
 426  
 427              setTimeout( function () {
 428                  $( '#buddypress #activity-stream .activity-item' ).removeClass( 'newest_' + data.scope +'_activity' );
 429              }, 3000 );
 430          },
 431  
 432          /**
 433           * [activityActions description]
 434           * @param  {[type]} event [description]
 435           * @return {[type]}       [description]
 436           */
 437          activityActions: function( event ) {
 438              var parent = event.data, target = $( event.target ), activity_item = $( event.currentTarget ),
 439                  activity_id = activity_item.data( 'bp-activity-id' ), stream = $( event.delegateTarget ),
 440                  item_id, form;
 441  
 442              // In case the target is set to a span inside the link.
 443              if ( $( target ).is( 'span' ) ) {
 444                  target = $( target ).closest( 'a' );
 445              }
 446  
 447              // Favoriting
 448              if ( target.hasClass( 'fav') || target.hasClass('unfav') ) {
 449                  var type = target.hasClass( 'fav' ) ? 'fav' : 'unfav';
 450  
 451                  // Stop event propagation
 452                  event.preventDefault();
 453  
 454                  target.addClass( 'loading' );
 455  
 456                  parent.ajax( { action: 'activity_mark_' + type, 'id': activity_id }, 'activity' ).done( function( response ) {
 457                      target.removeClass( 'loading' );
 458  
 459                      if ( false === response.success ) {
 460                          return;
 461                      } else {
 462                          target.fadeOut( 200, function() {
 463                              if ( $( this ).find( 'span' ).first().length ) {
 464                                  $( this ).find( 'span' ).first().html( response.data.content );
 465                              } else {
 466                                  $( this ).html( response.data.content );
 467                              }
 468                              $( this ).attr( 'data-bp-tooltip', response.data.content );
 469  
 470                              if ('false' === $(this).attr('aria-pressed') ) {
 471                                  $( this ).attr('aria-pressed', 'true');
 472                              } else {
 473                                  $( this ).attr('aria-pressed', 'false');
 474                              }
 475  
 476                              $( this ).fadeIn( 200 );
 477                          } );
 478                      }
 479  
 480                      if ( 'fav' === type ) {
 481                          if ( undefined !== response.data.directory_tab ) {
 482                              if ( ! $( parent.objectNavParent + ' [data-bp-scope="favorites"]' ).length ) {
 483                                  $( parent.objectNavParent + ' [data-bp-scope="all"]' ).after( response.data.directory_tab );
 484                              }
 485                          }
 486  
 487                          target.removeClass( 'fav' );
 488                          target.addClass( 'unfav' );
 489  
 490                      } else if ( 'unfav' === type ) {
 491                          var favoriteScope = $( '[data-bp-user-scope="favorites"]' ).hasClass( 'selected' ) || $( parent.objectNavParent + ' [data-bp-scope="favorites"]' ).hasClass( 'selected' );
 492  
 493                          // If on user's profile or on the favorites directory tab, remove the entry
 494                          if ( favoriteScope ) {
 495                              activity_item.remove();
 496                          }
 497  
 498                          if ( undefined !== response.data.no_favorite ) {
 499                              // Remove the tab when on activity directory but not on the favorites tabs
 500                              if ( $( parent.objectNavParent + ' [data-bp-scope="all"]' ).length && $( parent.objectNavParent + ' [data-bp-scope="all"]' ).hasClass( 'selected' ) ) {
 501                                  $( parent.objectNavParent + ' [data-bp-scope="favorites"]' ).remove();
 502  
 503                              // In all the other cases, append a message to the empty stream
 504                              } else if ( favoriteScope ) {
 505                                  stream.append( response.data.no_favorite );
 506                              }
 507                          }
 508  
 509                          target.removeClass( 'unfav' );
 510                          target.addClass( 'fav' );
 511                      }
 512                  } );
 513              }
 514  
 515              // Deleting or spamming
 516              if ( target.hasClass( 'delete-activity' ) || target.hasClass( 'acomment-delete' ) || target.hasClass( 'spam-activity' ) || target.hasClass( 'spam-activity-comment' ) ) {
 517                  var activity_comment_li = target.closest( '[data-bp-activity-comment-id]' ),
 518                      activity_comment_id = activity_comment_li.data( 'bp-activity-comment-id' ),
 519                      li_parent, comment_count_span, comment_count, show_all_a, deleted_comments_count = 0;
 520  
 521                  // Stop event propagation
 522                  event.preventDefault();
 523  
 524                  if ( undefined !== BP_Nouveau.confirm && false === window.confirm( BP_Nouveau.confirm ) ) {
 525                      return false;
 526                  }
 527  
 528                  target.addClass( 'loading' );
 529  
 530                  var ajaxData = {
 531                      action      : 'delete_activity',
 532                      'id'        : activity_id,
 533                      '_wpnonce'  : parent.getLinkParams( target.prop( 'href' ), '_wpnonce' ),
 534                      'is_single' : target.closest( '[data-bp-single]' ).length
 535                  };
 536  
 537                  // Only the action changes when spamming an activity or a comment.
 538                  if ( target.hasClass( 'spam-activity' ) || target.hasClass( 'spam-activity-comment' ) ) {
 539                      ajaxData.action = 'bp_spam_activity';
 540                  }
 541  
 542                  // Set defaults parent li to activity container
 543                  li_parent = activity_item;
 544  
 545                  // If it's a comment edit ajaxData.
 546                  if ( activity_comment_id ) {
 547                      delete ajaxData.is_single;
 548  
 549                      // Set comment data.
 550                      ajaxData.id         = activity_comment_id;
 551                      ajaxData.is_comment = true;
 552  
 553                      // Set parent li to activity comment container
 554                      li_parent = activity_comment_li;
 555                  }
 556  
 557                  parent.ajax( ajaxData, 'activity' ).done( function( response ) {
 558                      target.removeClass( 'loading' );
 559  
 560                      if ( false === response.success ) {
 561                          li_parent.prepend( response.data.feedback );
 562                          li_parent.find( '.bp-feedback' ).hide().fadeIn( 300 );
 563                      } else {
 564                          // Specific case of the single activity screen.
 565                          if ( response.data.redirect ) {
 566                              return window.location.href = response.data.redirect;
 567                          }
 568  
 569                          if ( activity_comment_id ) {
 570                              deleted_comments_count = 1;
 571  
 572                              // Move the form if needed
 573                              activity_item.append( activity_comment_li.find( 'form' ) );
 574  
 575                              // Count child comments if there are some
 576                              $.each( activity_comment_li.find( 'li' ), function() {
 577                                  deleted_comments_count += 1;
 578                              } );
 579  
 580                              // Update the button count
 581                              comment_count_span = activity_item.find( '.acomment-reply span.comment-count' );
 582                              comment_count      = Number( comment_count_span.html() - deleted_comments_count );
 583                              comment_count_span.html( comment_count );
 584  
 585                              // Update the show all count
 586                              show_all_a = activity_item.find( 'li.show-all a' );
 587                              if ( show_all_a.length ) {
 588                                  show_all_a.html( BP_Nouveau.show_x_comments.replace( '%d', comment_count ) );
 589                              }
 590  
 591                              // Clean up the parent activity classes.
 592                              if ( 0 === comment_count ) {
 593                                  activity_item.removeClass( 'has-comments' );
 594                              }
 595                          }
 596  
 597                          // Remove the entry
 598                          li_parent.slideUp( 300, function() {
 599                              li_parent.remove();
 600                          } );
 601  
 602                          // reset vars to get newest activities when an activity is deleted
 603                          if ( ! activity_comment_id && activity_item.data( 'bp-timestamp' ) === parent.Activity.heartbeat_data.last_recorded ) {
 604                              parent.Activity.heartbeat_data.newest        = '';
 605                              parent.Activity.heartbeat_data.last_recorded  = 0;
 606                          }
 607                      }
 608                  } );
 609              }
 610  
 611              // Reading more
 612              if ( target.closest( 'span' ).hasClass( 'activity-read-more' ) ) {
 613                  var content = target.closest( 'div' ), readMore = target.closest( 'span' );
 614  
 615                  item_id = null;
 616  
 617                  if ( $( content ).hasClass( 'activity-inner' ) ) {
 618                      item_id = activity_id;
 619                  } else if ( $( content ).hasClass( 'acomment-content' ) ) {
 620                      item_id = target.closest( 'li' ).data( 'bp-activity-comment-id' );
 621                  }
 622  
 623                  if ( ! item_id ) {
 624                      return event;
 625                  }
 626  
 627                  // Stop event propagation
 628                  event.preventDefault();
 629  
 630                  $( readMore ).addClass( 'loading' );
 631  
 632                  parent.ajax( {
 633                      action : 'get_single_activity_content',
 634                      id     : item_id
 635                  }, 'activity' ).done( function( response ) {
 636                      $( readMore ).removeClass( 'loading' );
 637  
 638                      if ( content.parent().find( '.bp-feedback' ).length ) {
 639                          content.parent().find( '.bp-feedback' ).remove();
 640                      }
 641  
 642                      if ( false === response.success ) {
 643                          content.after( response.data.feedback );
 644                          content.parent().find( '.bp-feedback' ).hide().fadeIn( 300 );
 645                      } else {
 646                          $( content ).slideUp( 300 ).html( response.data.contents ).slideDown( 300 );
 647                      }
 648                  } );
 649              }
 650  
 651              // Displaying the comment form
 652              if ( target.hasClass( 'acomment-reply' ) || target.parent().hasClass( 'acomment-reply' ) ) {
 653                  var comment_link = target;
 654  
 655                  form = $( '#ac-form-' + activity_id );
 656                  item_id = activity_id;
 657  
 658                  // Stop event propagation
 659                  event.preventDefault();
 660  
 661                  // If the comment count span inside the link is clicked
 662                  if ( target.parent().hasClass( 'acomment-reply' ) ) {
 663                      comment_link = target.parent();
 664                  }
 665  
 666                  if ( target.closest( 'li' ).data( 'bp-activity-comment-id' ) ) {
 667                      item_id = target.closest( 'li' ).data( 'bp-activity-comment-id' );
 668                  }
 669  
 670                  // ?? hide and display none..
 671                  //form.css( 'display', 'none' );
 672                  form.removeClass( 'root' );
 673                  $('.ac-form').hide();
 674  
 675                  /* Remove any error messages */
 676                  $.each( form.children( 'div' ), function( e, err ) {
 677                      if ( $( err ).hasClass( 'error' ) ) {
 678                          $( err ).remove();
 679                      }
 680                  } );
 681  
 682                  // It's an activity we're commenting
 683                  if ( item_id === activity_id ) {
 684                      $( '[data-bp-activity-id="' + item_id + '"] .activity-comments' ).append( form );
 685                      form.addClass( 'root' );
 686  
 687                  // It's a comment we're replying to
 688                  } else {
 689                      $( '[data-bp-activity-comment-id="' + item_id + '"]' ).append( form );
 690                  }
 691  
 692                  form.slideDown( 200 );
 693  
 694                  // change the aria state from false to true
 695                  target.attr( 'aria-expanded', 'true' );
 696  
 697                  $.scrollTo( form, 500, {
 698                      offset:-100,
 699                      easing:'swing'
 700                  } );
 701  
 702                  $( '#ac-form-' + activity_id + ' textarea' ).focus();
 703              }
 704  
 705              // Removing the form
 706              if ( target.hasClass( 'ac-reply-cancel' ) ) {
 707  
 708                  $( target ).closest( '.ac-form' ).slideUp( 200 );
 709  
 710                  // Change the aria state back to false on comment cancel
 711                  $( '.acomment-reply').attr( 'aria-expanded', 'false' );
 712  
 713                  // Stop event propagation
 714                  event.preventDefault();
 715              }
 716  
 717              // Submitting comments and replies
 718              if ( 'ac_form_submit' === target.prop( 'name' ) ) {
 719                  var comment_content, comment_data;
 720  
 721                  form = target.closest( 'form' );
 722                  item_id = activity_id;
 723  
 724                  // Stop event propagation
 725                  event.preventDefault();
 726  
 727                  if ( target.closest( 'li' ).data( 'bp-activity-comment-id' ) ) {
 728                      item_id    = target.closest( 'li' ).data( 'bp-activity-comment-id' );
 729                  }
 730  
 731                  comment_content = $( form ).find( 'textarea' ).first();
 732  
 733                  target.addClass( 'loading' ).prop( 'disabled', true );
 734                  comment_content.addClass( 'loading' ).prop( 'disabled', true );
 735  
 736                  comment_data = {
 737                      action                        : 'new_activity_comment',
 738                      _wpnonce_new_activity_comment : $( '#_wpnonce_new_activity_comment' ).val(),
 739                      comment_id                    : item_id,
 740                      form_id                       : activity_id,
 741                      content                       : comment_content.val()
 742                  };
 743  
 744                  // Add the Akismet nonce if it exists
 745                  if ( $( '#_bp_as_nonce_' + activity_id ).val() ) {
 746                      comment_data['_bp_as_nonce_' + activity_id] = $( '#_bp_as_nonce_' + activity_id ).val();
 747                  }
 748  
 749                  parent.ajax( comment_data, 'activity' ).done( function( response ) {
 750                      target.removeClass( 'loading' );
 751                      comment_content.removeClass( 'loading' );
 752                      $( '.acomment-reply' ).attr( 'aria-expanded', 'false' );
 753  
 754                      if ( false === response.success ) {
 755                          form.append( $( response.data.feedback ).hide().fadeIn( 200 ) );
 756                      } else {
 757                          var activity_comments = form.parent();
 758                          var the_comment = $.trim( response.data.contents );
 759  
 760                          form.fadeOut( 200, function() {
 761                              if ( 0 === activity_comments.children( 'ul' ).length ) {
 762                                  if ( activity_comments.hasClass( 'activity-comments' ) ) {
 763                                      activity_comments.prepend( '<ul></ul>' );
 764                                  } else {
 765                                      activity_comments.append( '<ul></ul>' );
 766                                  }
 767                              }
 768  
 769                              activity_comments.children( 'ul' ).append( $( the_comment ).hide().fadeIn( 200 ) );
 770                              $( form ).find( 'textarea' ).first().val( '' );
 771  
 772                              activity_comments.parent().addClass( 'has-comments' );
 773                          } );
 774  
 775                          // why, as it's already done a few lines ahead ???
 776                          //jq( '#' + form.attr('id') + ' textarea').val('');
 777  
 778                          // Set the new count
 779                          comment_count = Number( $( activity_item ).find( 'a span.comment-count' ).html() || 0 ) + 1;
 780  
 781                          // Increase the "Reply (X)" button count
 782                          $( activity_item ).find( 'a span.comment-count' ).html( comment_count );
 783  
 784                          // Increment the 'Show all x comments' string, if present
 785                          show_all_a = $( activity_item ).find( '.show-all a' );
 786                          if ( show_all_a ) {
 787                              show_all_a.html( BP_Nouveau.show_x_comments.replace( '%d', comment_count ) );
 788                          }
 789                      }
 790  
 791                      target.prop( 'disabled', false );
 792                      comment_content.prop( 'disabled', false );
 793                  } );
 794              }
 795          },
 796  
 797          /**
 798           * [closeCommentForm description]
 799           * @param  {[type]} event [description]
 800           * @return {[type]}       [description]
 801           */
 802          commentFormAction: function( event ) {
 803              var element, keyCode;
 804  
 805              event = event || window.event;
 806  
 807              if ( event.target ) {
 808                  element = event.target;
 809              } else if ( event.srcElement) {
 810                  element = event.srcElement;
 811              }
 812  
 813              if ( element.nodeType === 3 ) {
 814                  element = element.parentNode;
 815              }
 816  
 817              if ( event.altKey === true || event.metaKey === true ) {
 818                  return event;
 819              }
 820  
 821              // Not in a comment textarea, return
 822              if ( element.tagName !== 'TEXTAREA' || ! $( element ).hasClass( 'ac-input' ) ) {
 823                  return event;
 824              }
 825  
 826              keyCode = ( event.keyCode) ? event.keyCode : event.which;
 827  
 828              if ( 27 === keyCode && false === event.ctrlKey  ) {
 829                  if ( element.tagName === 'TEXTAREA' ) {
 830                      $( element ).closest( 'form' ).slideUp( 200 );
 831                  }
 832              } else if ( event.ctrlKey && 13 === keyCode && $( element ).val() ) {
 833                  $( element ).closest( 'form' ).find( '[type=submit]' ).first().trigger( 'click' );
 834              }
 835          }
 836      };
 837  
 838      // Launch BP Nouveau Activity
 839      bp.Nouveau.Activity.start();
 840  
 841  } )( bp, jQuery );


Generated: Mon Nov 18 01:01:37 2019 Cross-referenced by PHPXref 0.7.1