[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  /* global wp, BP_Nouveau, JSON */
   2  /* jshint devel: true */
   3  /* jshint browser: true */
   4  /* @since 3.0.0 */
   5  /* @version 10.0.0 */
   6  window.wp = window.wp || {};
   7  window.bp = window.bp || {};
   8  
   9  ( function( bp, $ ) {
  10  
  11      // Bail if not set.
  12      if ( typeof BP_Nouveau === 'undefined' ) {
  13          return;
  14      }
  15  
  16      /**
  17       * [Nouveau description]
  18       * @type {Object}
  19       */
  20      bp.Nouveau = {
  21          /**
  22           * [start description]
  23           * @return {[type]} [description]
  24           */
  25          start: function() {
  26  
  27              // Setup globals.
  28              this.setupGlobals();
  29  
  30              // Adjust Document/Forms properties.
  31              this.prepareDocument();
  32  
  33              // Init the BuddyPress objects.
  34              this.initObjects();
  35  
  36              // Set BuddyPress HeartBeat.
  37              this.setHeartBeat();
  38  
  39              // Listen to events ("Add hooks!").
  40              this.addListeners();
  41          },
  42  
  43          /**
  44           * [setupGlobals description]
  45           * @return {[type]} [description]
  46           */
  47          setupGlobals: function() {
  48              this.ajax_request           = null;
  49  
  50              // Object Globals.
  51              this.objects                = $.map( BP_Nouveau.objects, function( value ) { return value; } );
  52              this.objectNavParent        = BP_Nouveau.object_nav_parent;
  53  
  54              // HeartBeat Global.
  55              this.heartbeat              = wp.heartbeat || {};
  56  
  57              // An object containing each query var.
  58              this.querystring            = this.getLinkParams();
  59          },
  60  
  61          /**
  62           * [prepareDocument description]
  63           * @return {[type]} [description]
  64           */
  65          prepareDocument: function() {
  66  
  67              // Remove the no-js class and add the js one.
  68              if ( $( 'body' ).hasClass( 'no-js' ) ) {
  69                  $('body').removeClass( 'no-js' ).addClass( 'js' );
  70              }
  71  
  72              // Log Warnings into the console instead of the screen.
  73              if ( BP_Nouveau.warnings && 'undefined' !== typeof console && console.warn ) {
  74                  $.each( BP_Nouveau.warnings, function( w, warning ) {
  75                      console.warn( warning );
  76                  } );
  77              }
  78  
  79              // Remove the directory title if there's a widget containing it.
  80              if ( $( '.buddypress_object_nav .widget-title' ).length ) {
  81                  var text = $( '.buddypress_object_nav .widget-title' ).html();
  82  
  83                  $( 'body' ).find( '*:contains("' + text + '")' ).each( function( e, element ) {
  84                      if ( ! $( element ).hasClass( 'widget-title' ) && text === $( element ).html() && ! $( element ).is( 'a' ) ) {
  85                          $( element ).remove();
  86                      }
  87                  } );
  88              }
  89          },
  90  
  91          /** Helpers *******************************************************************/
  92  
  93          /**
  94           * [getStorage description]
  95           * @param  {[type]} type     [description]
  96           * @param  {[type]} property [description]
  97           * @return {[type]}          [description]
  98           */
  99          getStorage: function( type, property ) {
 100              var store = sessionStorage.getItem( type );
 101  
 102              if ( store ) {
 103                  store = JSON.parse( store );
 104              } else {
 105                  store = {};
 106              }
 107  
 108              if ( undefined !== property ) {
 109                  return store[property] || false;
 110              }
 111  
 112              return store;
 113          },
 114  
 115          /**
 116           * [setStorage description]
 117           * @param {[type]} type     [description]
 118           * @param {[type]} property [description]
 119           * @param {[type]} value    [description]
 120           */
 121          setStorage: function( type, property, value ) {
 122              var store = this.getStorage( type );
 123  
 124              if ( undefined === value && undefined !== store[ property ] ) {
 125                  delete store[ property ];
 126              } else {
 127                  // Set property.
 128                  store[ property ] = value;
 129              }
 130  
 131              sessionStorage.setItem( type, JSON.stringify( store ) );
 132  
 133              return sessionStorage.getItem( type ) !== null;
 134          },
 135  
 136          /**
 137           * [getLinkParams description]
 138           * @param  {[type]} url   [description]
 139           * @param  {[type]} param [description]
 140           * @return {[type]}       [description]
 141           */
 142          getLinkParams: function( url, param ) {
 143              var qs;
 144              if ( url ) {
 145                  qs = ( -1 !== url.indexOf( '?' ) ) ? '?' + url.split( '?' )[1] : '';
 146              } else {
 147                  qs = document.location.search;
 148              }
 149  
 150              if ( ! qs ) {
 151                  return null;
 152              }
 153  
 154              var params = qs.replace( /(^\?)/, '' ).split( '&' ).map( function( n ) {
 155                  return n = n.split( '=' ), this[n[0]] = n[1], this;
 156              }.bind( {} ) )[0];
 157  
 158              if ( param ) {
 159                  return params[param];
 160              }
 161  
 162              return params;
 163          },
 164  
 165          /**
 166           * URL Decode a query variable.
 167           *
 168           * @param  {string} qv    The query variable to decode.
 169           * @param  {object} chars The specific characters to use. Optional.
 170           * @return {string}       The URL decoded variable.
 171           */
 172          urlDecode: function( qv, chars ) {
 173              var specialChars = chars || {
 174                  amp: '&',
 175                  lt: '<',
 176                  gt: '>',
 177                  quot: '"',
 178                  '#039': '\''
 179              };
 180  
 181              return decodeURIComponent( qv.replace( /\+/g, ' ' ) ).replace( /&([^;]+);/g, function( v, q ) {
 182                  return specialChars[q] || '';
 183              } );
 184          },
 185  
 186          /**
 187           * [ajax description]
 188           * @param  {[type]} post_data [description]
 189           * @param  {[type]} object    [description]
 190           * @return {[type]}           [description]
 191           */
 192          ajax: function( post_data, object ) {
 193              if ( this.ajax_request ) {
 194                  this.ajax_request.abort();
 195              }
 196  
 197              // Extend posted data with stored data and object nonce.
 198              var postData = $.extend( {}, bp.Nouveau.getStorage( 'bp-' + object ), { nonce: BP_Nouveau.nonces[object] }, post_data );
 199  
 200              if ( undefined !== BP_Nouveau.customizer_settings ) {
 201                  postData.customized = BP_Nouveau.customizer_settings;
 202              }
 203  
 204              this.ajax_request = $.post( BP_Nouveau.ajaxurl, postData, 'json' );
 205  
 206              return this.ajax_request;
 207          },
 208  
 209          inject: function( selector, content, method ) {
 210              if ( ! $( selector ).length || ! content ) {
 211                  return;
 212              }
 213  
 214              /**
 215               * How the content should be injected in the selector
 216               *
 217               * possible methods are
 218               * - reset: the selector will be reset with the content
 219               * - append:  the content will be added after selector's content
 220               * - prepend: the content will be added before selector's content
 221               */
 222              method = method || 'reset';
 223  
 224              if ( 'append' === method ) {
 225                  $( selector ).append( content );
 226              } else if ( 'prepend' === method ) {
 227                  $( selector ).prepend( content );
 228              } else {
 229                  $( selector ).html( content );
 230              }
 231  
 232              if ( 'undefined' !== typeof bp_mentions || 'undefined' !== typeof bp.mentions ) {
 233                  $( '.bp-suggestions' ).bp_mentions( bp.mentions.users );
 234              }
 235          },
 236  
 237          /**
 238           * [objectRequest description]
 239           * @param  {[type]} data [description]
 240           * @return {[type]}      [description]
 241           */
 242          objectRequest: function( data ) {
 243              var postdata = {}, self = this;
 244  
 245              data = $.extend( {
 246                  object       : '',
 247                  scope        : null,
 248                  filter       : null,
 249                  target       : '#buddypress [data-bp-list]',
 250                  search_terms : '',
 251                  page         : 1,
 252                  extras       : null,
 253                  caller       : null,
 254                  template     : null,
 255                  method       : 'reset'
 256              }, data );
 257  
 258              // Do not request if we don't have the object or the target to inject results into.
 259              if ( ! data.object || ! data.target ) {
 260                  return;
 261              }
 262  
 263              // Prepare the search terms for the request.
 264              if ( data.search_terms ) {
 265                  data.search_terms = data.search_terms.replace( /</g, '&lt;' ).replace( />/g, '&gt;' );
 266              }
 267  
 268              // Set session's data.
 269              if ( null !== data.scope ) {
 270                  this.setStorage( 'bp-' + data.object, 'scope', data.scope );
 271              }
 272  
 273              if ( null !== data.filter ) {
 274                  this.setStorage( 'bp-' + data.object, 'filter', data.filter );
 275              }
 276  
 277              if ( null !== data.extras ) {
 278                  this.setStorage( 'bp-' + data.object, 'extras', data.extras );
 279              }
 280  
 281              /* Set the correct selected nav and filter */
 282              $( this.objectNavParent + ' [data-bp-object]' ).each( function() {
 283                  $( this ).removeClass( 'selected loading' );
 284              } );
 285  
 286              $( this.objectNavParent + ' [data-bp-scope="' + data.scope + '"], #object-nav li.current' ).addClass( 'selected loading' );
 287              $( '#buddypress [data-bp-filter="' + data.object + '"] option[value="' + data.filter + '"]' ).prop( 'selected', true );
 288  
 289              if ( 'friends' === data.object || 'friend_requests' === data.object || 'group_members' === data.object ) {
 290                  data.template = data.object;
 291                  data.object   = 'members';
 292              } else if ( 'group_requests' === data.object ) {
 293                  data.object = 'groups';
 294                  data.template = 'group_requests';
 295              } else if ( 'notifications' === data.object ) {
 296                  data.object = 'members';
 297                  data.template = 'member_notifications';
 298              }
 299  
 300              postdata = $.extend( {
 301                  action: data.object + '_filter'
 302              }, data );
 303  
 304              return this.ajax( postdata, data.object ).done( function( response ) {
 305                  if ( false === response.success ) {
 306                      return;
 307                  }
 308  
 309                  var selectedObjectNavParent = $( self.objectNavParent + ' [data-bp-scope="' + data.scope + '"]' );
 310  
 311                  selectedObjectNavParent.removeClass( 'loading' );
 312  
 313                  if ( response.data && response.data.totalItems && response.data.navLabel ) {
 314                      selectedObjectNavParent.find( 'a' ).first().html(
 315                          response.data.navLabel + '&nbsp;'
 316                      ).append( $( '<span></span>' ).addClass( 'count' ).html( response.data.totalItems ) );
 317                  }
 318  
 319                  if ( 'reset' !== data.method ) {
 320                      self.inject( data.target, response.data.contents, data.method );
 321  
 322                      $( data.target ).trigger( 'bp_ajax_' + data.method, $.extend( data, { response: response.data } ) );
 323                  } else {
 324                      /* animate to top if called from bottom pagination */
 325                      if ( data.caller === 'pag-bottom' && $( '#subnav' ).length ) {
 326                          var top = $('#subnav').parent();
 327                          $( 'html,body' ).animate( { scrollTop: top.offset().top }, 'slow', function() {
 328                              $( data.target ).fadeOut( 100, function() {
 329                                  self.inject( this, response.data.contents, data.method );
 330                                  $( this ).fadeIn( 100, 'swing', function(){
 331                                      // Inform other scripts the list of objects has been refreshed.
 332                                      $( data.target ).trigger( 'bp_ajax_request', $.extend( data, { response: response.data } ) );
 333                                  } );
 334                              } );
 335                          } );
 336  
 337                      } else {
 338                          $( data.target ).fadeOut( 100, function() {
 339                              self.inject( this, response.data.contents, data.method );
 340                              $( this ).fadeIn( 100, 'swing', function(){
 341                                  // Inform other scripts the list of objects has been refreshed.
 342                                  $( data.target ).trigger( 'bp_ajax_request', $.extend( data, { response: response.data } ) );
 343                              } );
 344                          } );
 345                      }
 346                  }
 347              } );
 348          },
 349  
 350          /**
 351           * [initObjects description]
 352           * @return {[type]} [description]
 353           */
 354          initObjects: function() {
 355              var self = this, objectData = {}, queryData = {}, scope = 'all', search_terms = '', extras = null, filter = null;
 356  
 357              $.each( this.objects, function( o, object ) {
 358                  objectData = self.getStorage( 'bp-' + object );
 359  
 360                  if ( undefined !== objectData.scope ) {
 361                      scope = objectData.scope;
 362                  }
 363  
 364                  // Notifications always need to start with Newest ones.
 365                  if ( undefined !== objectData.extras && 'notifications' !== object ) {
 366                      extras = objectData.extras;
 367                  }
 368  
 369                  if (  $( '#buddypress [data-bp-filter="' + object + '"]' ).length ) {
 370                      if ( '-1' !== $( '#buddypress [data-bp-filter="' + object + '"]' ).val() && '0' !== $( '#buddypress [data-bp-filter="' + object + '"]' ).val() ) {
 371                          filter = $( '#buddypress [data-bp-filter="' + object + '"]' ).val();
 372                      } else if ( undefined !== objectData.filter ) {
 373                          filter = objectData.filter,
 374                          $( '#buddypress [data-bp-filter="' + object + '"] option[value="' + filter + '"]' ).prop( 'selected', true );
 375                      }
 376                  }
 377  
 378                  if ( $( this.objectNavParent + ' [data-bp-object="' + object + '"]' ).length ) {
 379                      $( this.objectNavParent + ' [data-bp-object="' + object + '"]' ).each( function() {
 380                          $( this ).removeClass( 'selected' );
 381                      } );
 382  
 383                      $( this.objectNavParent + ' [data-bp-scope="' + object + '"], #object-nav li.current' ).addClass( 'selected' );
 384                  }
 385  
 386                  // Check the querystring to eventually include the search terms.
 387                  if ( null !== self.querystring ) {
 388                      if ( undefined !== self.querystring[ object + '_search'] ) {
 389                          search_terms = self.querystring[ object + '_search'];
 390                      } else if ( undefined !== self.querystring.s ) {
 391                          search_terms = self.querystring.s;
 392                      }
 393  
 394                      if ( search_terms ) {
 395                          search_terms = self.urlDecode( search_terms );
 396                          $( '#buddypress [data-bp-search="' + object + '"] input[type=search]' ).val( search_terms );
 397                      }
 398                  }
 399  
 400                  if ( $( '#buddypress [data-bp-list="' + object + '"]' ).length ) {
 401                      queryData =  {
 402                          object       : object,
 403                          scope        : scope,
 404                          filter       : filter,
 405                          search_terms : search_terms,
 406                          extras       : extras
 407                      };
 408  
 409                      // Populate the object list.
 410                      self.objectRequest( queryData );
 411                  }
 412              } );
 413          },
 414  
 415          /**
 416           * [setHeartBeat description]
 417           */
 418          setHeartBeat: function() {
 419              if ( typeof BP_Nouveau.pulse === 'undefined' || ! this.heartbeat ) {
 420                  return;
 421              }
 422  
 423              this.heartbeat.interval( Number( BP_Nouveau.pulse ) );
 424  
 425              // Extend "send" with BuddyPress namespace.
 426              $.fn.extend( {
 427                  'heartbeat-send': function() {
 428                      return this.bind( 'heartbeat-send.buddypress' );
 429                  }
 430              } );
 431  
 432              // Extend "tick" with BuddyPress namespace.
 433              $.fn.extend( {
 434                  'heartbeat-tick': function() {
 435                      return this.bind( 'heartbeat-tick.buddypress' );
 436                  }
 437              } );
 438          },
 439  
 440          /** Event Listeners ***********************************************************/
 441  
 442          /**
 443           * [addListeners description]
 444           */
 445          addListeners: function() {
 446              // Disabled inputs.
 447              $( '[data-bp-disable-input]' ).on( 'change', this.toggleDisabledInput );
 448  
 449              // HeartBeat Send and Receive.
 450              $( document ).on( 'heartbeat-send.buddypress', this.heartbeatSend );
 451              $( document ).on( 'heartbeat-tick.buddypress', this.heartbeatTick );
 452  
 453              // Refreshing.
 454              $( this.objectNavParent + ' .bp-navs' ).on( 'click', 'a', this, this.scopeQuery );
 455  
 456              // Filtering.
 457              $( '#buddypress [data-bp-filter]' ).on( 'change', this, this.filterQuery );
 458  
 459              // Searching.
 460              $( '#buddypress [data-bp-search]' ).on( 'submit', 'form', this, this.searchQuery );
 461              $( '#buddypress [data-bp-search] form' ).on( 'search', 'input[type=search]', this.resetSearch );
 462  
 463              // Buttons.
 464              $( '#buddypress [data-bp-list], #buddypress #item-header' ).on( 'click', '[data-bp-btn-action]', this, this.buttonAction );
 465  
 466              // Close notice.
 467              $( '#buddypress [data-bp-close]' ).on( 'click', this, this.closeNotice );
 468  
 469              // Pagination.
 470              $( '#buddypress [data-bp-list]' ).on( 'click', '[data-bp-pagination] a', this, this.paginateAction );
 471  
 472              // Password updates.
 473              if ( BP_Nouveau.bpPasswordVerify && BP_Nouveau.bpPasswordVerify.requiredPassStrength ) {
 474                  $( '#pass1' ).on( 'input pwupdate', this.checkPassStrength );
 475              }
 476          },
 477  
 478          /** Event Callbacks ***********************************************************/
 479  
 480          /**
 481           * Toggle the availability of Delete My account button.
 482           *
 483           * @param  {Object} event The change event.
 484           */
 485          toggleDisabledInput: function( event ) {
 486              var target = $( event.currentTarget ), disabledControl = '#' + target.data( 'bp-disable-input' ),
 487                  isChecked = target.prop( 'checked' );
 488  
 489              target.removeClass( 'enabled disabled' );
 490  
 491              if ( isChecked ) {
 492                  target.addClass( 'enabled' );
 493                  $( disabledControl ).prop( 'disabled', false );
 494              } else {
 495                  $( disabledControl ).prop( 'disabled', true );
 496                  target.addClass( 'disabled' );
 497              }
 498          },
 499  
 500          /**
 501           * [heartbeatSend description]
 502           * @param  {[type]} event [description]
 503           * @param  {[type]} data  [description]
 504           * @return {[type]}       [description]
 505           */
 506          heartbeatSend: function( event, data ) {
 507              // Add an heartbeat send event to possibly any BuddyPress pages.
 508              $( '#buddypress' ).trigger( 'bp_heartbeat_send', data );
 509          },
 510  
 511          /**
 512           * [heartbeatTick description]
 513           * @param  {[type]} event [description]
 514           * @param  {[type]} data  [description]
 515           * @return {[type]}       [description]
 516           */
 517          heartbeatTick: function( event, data ) {
 518              // Add an heartbeat send event to possibly any BuddyPress pages.
 519              $( '#buddypress' ).trigger( 'bp_heartbeat_tick', data );
 520          },
 521  
 522          /**
 523           * [queryScope description]
 524           * @param  {[type]} event [description]
 525           * @return {[type]}       [description]
 526           */
 527          scopeQuery: function( event ) {
 528              var self = event.data, target = $( event.currentTarget ).parent(),
 529                  scope = 'all', object, filter = null, search_terms = '';
 530  
 531              if ( target.hasClass( 'no-ajax' ) || $( event.currentTarget ).hasClass( 'no-ajax' ) || ! target.attr( 'data-bp-scope' ) ) {
 532                  return event;
 533              }
 534  
 535              scope  = target.data( 'bp-scope' );
 536              object = target.data( 'bp-object' );
 537  
 538              if ( ! scope || ! object ) {
 539                  return event;
 540              }
 541  
 542              // Stop event propagation.
 543              event.preventDefault();
 544  
 545              filter = $( '#buddypress' ).find( '[data-bp-filter="' + object + '"]' ).first().val();
 546  
 547              if ( $( '#buddypress [data-bp-search="' + object + '"] input[type=search]' ).length ) {
 548                  search_terms = $( '#buddypress [data-bp-search="' + object + '"] input[type=search]' ).val();
 549              }
 550  
 551              // Remove the New count on dynamic tabs.
 552              if ( target.hasClass( 'dynamic' ) ) {
 553                  target.find( 'a span' ).html('');
 554              }
 555  
 556              self.objectRequest( {
 557                  object       : object,
 558                  scope        : scope,
 559                  filter       : filter,
 560                  search_terms : search_terms,
 561                  page         : 1
 562              } );
 563          },
 564  
 565          /**
 566           * [filterQuery description]
 567           * @param  {[type]} event [description]
 568           * @return {[type]}       [description]
 569           */
 570          filterQuery: function( event ) {
 571              var self = event.data, object = $( event.target ).data( 'bp-filter' ),
 572                  scope = 'all', filter = $( event.target ).val(),
 573                  search_terms = '', template = null;
 574  
 575              if ( ! object ) {
 576                  return event;
 577              }
 578  
 579              if ( $( self.objectNavParent + ' [data-bp-object].selected' ).length ) {
 580                  scope = $( self.objectNavParent + ' [data-bp-object].selected' ).data( 'bp-scope' );
 581              }
 582  
 583              if ( $( '#buddypress [data-bp-search="' + object + '"] input[type=search]' ).length ) {
 584                  search_terms = $( '#buddypress [data-bp-search="' + object + '"] input[type=search]' ).val();
 585              }
 586  
 587              self.objectRequest( {
 588                  object       : object,
 589                  scope        : scope,
 590                  filter       : filter,
 591                  search_terms : search_terms,
 592                  page         : 1,
 593                  template     : template
 594              } );
 595          },
 596  
 597          /**
 598           * [searchQuery description]
 599           * @param  {[type]} event [description]
 600           * @return {[type]}       [description]
 601           */
 602          searchQuery: function( event ) {
 603              var self = event.data, object, scope = 'all', filter = null, template = null, search_terms = '';
 604  
 605              if ( $( event.delegateTarget ).hasClass( 'no-ajax' ) || undefined === $( event.delegateTarget ).data( 'bp-search' ) ) {
 606                  return event;
 607              }
 608  
 609              // Stop event propagation.
 610              event.preventDefault();
 611  
 612              object       = $( event.delegateTarget ).data( 'bp-search' );
 613              filter       = $( '#buddypress' ).find( '[data-bp-filter="' + object + '"]' ).first().val();
 614              search_terms = $( event.delegateTarget ).find( 'input[type=search]' ).first().val();
 615  
 616              if ( $( self.objectNavParent + ' [data-bp-object]' ).length ) {
 617                  scope = $( self.objectNavParent + ' [data-bp-object="' + object + '"].selected' ).data( 'bp-scope' );
 618              }
 619  
 620              self.objectRequest( {
 621                  object       : object,
 622                  scope        : scope,
 623                  filter       : filter,
 624                  search_terms : search_terms,
 625                  page         : 1,
 626                  template     : template
 627              } );
 628          },
 629  
 630          /**
 631           * [showSearchSubmit description]
 632           * @param  {[type]} event [description]
 633           * @return {[type]}       [description]
 634           */
 635          showSearchSubmit: function( event ) {
 636              $( event.delegateTarget ).find( '[type=submit]' ).addClass( 'bp-show' );
 637              if( $('[type=submit]').hasClass( 'bp-hide' ) ) {
 638                  $( '[type=submit]' ).removeClass( 'bp-hide' );
 639              }
 640          },
 641  
 642          /**
 643           * [resetSearch description]
 644           * @param  {[type]} event [description]
 645           * @return {[type]}       [description]
 646           */
 647          resetSearch: function( event ) {
 648              if ( ! $( event.target ).val() ) {
 649                  $( event.delegateTarget ).submit();
 650              } else {
 651                  $( event.delegateTarget ).find( '[type=submit]' ).show();
 652              }
 653          },
 654  
 655          /**
 656           * [buttonAction description]
 657           * @param  {[type]} event [description]
 658           * @return {[type]}       [description]
 659           */
 660          buttonAction: function( event ) {
 661              var self = event.data, target = $( event.currentTarget ), action = target.data( 'bp-btn-action' ), nonceUrl = target.data( 'bp-nonce' ),
 662                  item = target.closest( '[data-bp-item-id]' ), item_id = item.data( 'bp-item-id' ), item_inner = target.closest('.list-wrap'),
 663                  object = item.data( 'bp-item-component' ), nonce = '';
 664  
 665              // Simply let the event fire if we don't have needed values.
 666              if ( ! action || ! item_id || ! object ) {
 667                  return event;
 668              }
 669  
 670              // Stop event propagation.
 671              event.preventDefault();
 672  
 673              if ( ( undefined !== BP_Nouveau[ action + '_confirm'] && false === window.confirm( BP_Nouveau[ action + '_confirm'] ) ) || target.hasClass( 'pending' ) ) {
 674                  return false;
 675              }
 676  
 677              // If the nonceUrl is not set, use the `href` attribute.
 678              if ( ! nonceUrl ) {
 679                  nonceUrl = target.prop( 'href' );
 680              }
 681  
 682              // Set the nonce.
 683              nonce = self.getLinkParams( nonceUrl, '_wpnonce' );
 684  
 685              // Unfortunately unlike groups
 686              // Friends actions does not match the wpnonce
 687              var friends_actions_map = {
 688                  is_friend         : 'remove_friend',
 689                  not_friends       : 'add_friend',
 690                  pending           : 'withdraw_friendship',
 691                  accept_friendship : 'accept_friendship',
 692                  reject_friendship : 'reject_friendship'
 693              };
 694  
 695              if ( 'members' === object && undefined !== friends_actions_map[ action ] ) {
 696                  action = friends_actions_map[ action ];
 697                  object = 'friends';
 698              }
 699  
 700              // Add a pending class to prevent queries while we're processing the action.
 701              target.addClass( 'pending loading' );
 702  
 703              self.ajax( {
 704                  action   : object + '_' + action,
 705                  item_id  : item_id,
 706                  _wpnonce : nonce
 707              }, object ).done( function( response ) {
 708                  if ( false === response.success ) {
 709                      item_inner.prepend( response.data.feedback );
 710                      target.removeClass( 'pending loading' );
 711                      item.find( '.bp-feedback' ).fadeOut( 6000 );
 712                  } else {
 713                      // Specific cases for groups.
 714                      if ( 'groups' === object ) {
 715  
 716                          // Group's header button.
 717                          if ( undefined !== response.data.is_group && response.data.is_group ) {
 718                              return window.location.reload();
 719                          }
 720                      }
 721  
 722                      // User's groups invitations screen & User's friend screens.
 723                      if ( undefined !== response.data.is_user && response.data.is_user ) {
 724                          target.parent().html( response.data.feedback );
 725                          item.fadeOut( 1500 );
 726                          return;
 727                      }
 728  
 729                      // Update count.
 730                      if ( $( self.objectNavParent + ' [data-bp-scope="personal"]' ).length ) {
 731                          var personal_count = Number( $( self.objectNavParent + ' [data-bp-scope="personal"] span' ).html() ) || 0;
 732  
 733                          if ( -1 !== $.inArray( action, ['leave_group', 'remove_friend'] ) ) {
 734                              personal_count -= 1;
 735                          } else if ( -1 !== $.inArray( action, ['join_group'] ) ) {
 736                              personal_count += 1;
 737                          }
 738  
 739                          if ( personal_count < 0 ) {
 740                              personal_count = 0;
 741                          }
 742  
 743                          $( self.objectNavParent + ' [data-bp-scope="personal"] span' ).html( personal_count );
 744                      }
 745  
 746                      target.parent().replaceWith( response.data.contents );
 747                  }
 748              } );
 749          },
 750  
 751          /**
 752           * [closeNotice description]
 753           * @param  {[type]} event [description]
 754           * @return {[type]}       [description]
 755           */
 756          closeNotice: function( event ) {
 757              var closeBtn = $( event.currentTarget );
 758  
 759              event.preventDefault();
 760  
 761              // Make sure cookies are removed.
 762              if ( 'clear' === closeBtn.data( 'bp-close' ) ) {
 763                  if ( undefined !== $.cookie( 'bp-message' ) ) {
 764                      $.removeCookie( 'bp-message' );
 765                  }
 766  
 767                  if ( undefined !== $.cookie( 'bp-message-type' ) ) {
 768                      $.removeCookie( 'bp-message-type' );
 769                  }
 770              }
 771  
 772              // @todo other cases...
 773              // Dismissing site-wide notices.
 774              if ( closeBtn.closest( '.bp-feedback' ).hasClass( 'bp-sitewide-notice' ) ) {
 775                  bp.Nouveau.ajax( {
 776                      action : 'messages_dismiss_sitewide_notice'
 777                  }, 'messages' );
 778              }
 779  
 780              // Remove the notice.
 781              closeBtn.closest( '.bp-feedback' ).remove();
 782          },
 783  
 784          paginateAction: function( event ) {
 785              var self  = event.data, navLink = $( event.currentTarget ), pagArg,
 786                  scope = null, object, objectData, filter = null, search_terms = null, extras = null;
 787  
 788              pagArg = navLink.closest( '[data-bp-pagination]' ).data( 'bp-pagination' ) || null;
 789  
 790              if ( null === pagArg ) {
 791                  return event;
 792              }
 793  
 794              event.preventDefault();
 795  
 796              object = $( event.delegateTarget ).data( 'bp-list' ) || null;
 797  
 798              // Set the scope & filter.
 799              if ( null !== object ) {
 800                  objectData = self.getStorage( 'bp-' + object );
 801  
 802                  if ( undefined !== objectData.scope ) {
 803                      scope = objectData.scope;
 804                  }
 805  
 806                  if ( undefined !== objectData.filter ) {
 807                      filter = objectData.filter;
 808                  }
 809  
 810                  if ( undefined !== objectData.extras ) {
 811                      extras = objectData.extras;
 812                  }
 813              }
 814  
 815              // Set the search terms.
 816              if ( $( '#buddypress [data-bp-search="' + object + '"] input[type=search]' ).length ) {
 817                  search_terms = $( '#buddypress [data-bp-search="' + object + '"] input[type=search]' ).val();
 818              }
 819  
 820              var queryData = {
 821                  object       : object,
 822                  scope        : scope,
 823                  filter       : filter,
 824                  search_terms : search_terms,
 825                  extras       : extras,
 826                  page         : self.getLinkParams( navLink.prop( 'href' ), pagArg ) || 1
 827              };
 828  
 829              // Request the page.
 830              self.objectRequest( queryData );
 831          },
 832  
 833          checkPassStrength: function( event ) {
 834              var bpPasswordVerify = BP_Nouveau.bpPasswordVerify, strength,
 835                  requiredStrength = parseInt( bpPasswordVerify.requiredPassStrength, 10 ),
 836                  pass1 = $( event.currentTarget ).val(), pass2 = $( '#pass2' ).val(),
 837                  currentForm = $( event.currentTarget ).closest( 'form' );
 838  
 839  
 840              // wp.passwordStrength.userInputBlacklist() has been deprecated in WP 5.5.0.
 841              if ( 'function' === typeof wp.passwordStrength.userInputDisallowedList ) {
 842                  strength = wp.passwordStrength.meter( pass1, wp.passwordStrength.userInputDisallowedList(), pass2 );
 843              } else {
 844                  strength = wp.passwordStrength.meter( pass1, wp.passwordStrength.userInputBlacklist(), pass2 );
 845              }
 846  
 847              if ( requiredStrength && 4 >= requiredStrength ) {
 848                  var passwordWarningContainer = $( currentForm ).find( '#password-warning' );
 849  
 850                  if ( strength < requiredStrength ) {
 851                      if ( ! $( passwordWarningContainer ).length ) {
 852                          $( event.currentTarget ).before(
 853                              $( '<p></p>' ).prop( 'id', 'password-warning' )
 854                                            .addClass( 'description' )
 855                          );
 856                      }
 857  
 858                      $( passwordWarningContainer ).html( bpPasswordVerify.tooWeakPasswordWarning );
 859                  } else if ( $( passwordWarningContainer ).length ) {
 860                      $( passwordWarningContainer ).remove();
 861                  }
 862  
 863                  if ( ! $( currentForm ).find( '#password-strength-score' ).length ) {
 864                      $( currentForm ).prepend(
 865                          $('<input></input>').prop( {
 866                              id: 'password-strength-score',
 867                              type: 'hidden',
 868                              'name': '_password_strength_score'
 869                          } )
 870                      );
 871                  }
 872  
 873                  $( '#password-strength-score' ).val( strength );
 874  
 875                  if ( requiredStrength > strength ) {
 876                      $( '.pw-weak' ).remove();
 877                  }
 878              }
 879          }
 880      };
 881  
 882      // Launch BP Nouveau.
 883      bp.Nouveau.start();
 884  
 885  } )( window.bp, jQuery );


Generated: Thu Aug 11 01:00:53 2022 Cross-referenced by PHPXref 0.7.1