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


Generated: Wed Sep 30 01:01:34 2020 Cross-referenced by PHPXref 0.7.1