[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

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


Generated: Wed Nov 20 01:01:38 2019 Cross-referenced by PHPXref 0.7.1