[ Index ]

PHP Cross Reference of BBPress

title

Body

[close]

/src/templates/default/js/ -> reply.js (source)

   1  /* globals tinyMCE */
   2  addReply = {
   3  
   4      /**
   5       * Move the reply form when "Reply" is clicked.
   6       *
   7       * @since 2.6.2
   8       * @param {string} replyId
   9       * @param {string} parentId
  10       * @param {string} respondId
  11       * @param {string} postId
  12       * @returns {undefined|Boolean}
  13       */
  14      moveForm: function ( replyId, parentId, respondId, postId ) {
  15  
  16          /* Get initial elements */
  17          var t       = this,
  18              reply   = t.getElement( replyId ),
  19              respond = t.getElement( respondId ),
  20              cancel  = t.getElement( 'bbp-cancel-reply-to-link' ),
  21              parent  = t.getElement( 'bbp_reply_to' ),
  22              post    = t.getElement( 'bbp_topic_id' );
  23  
  24          /* Remove the editor, if its already been moved */
  25          t.removeEditor();
  26  
  27          /* Bail to avoid errors */
  28          if ( ! reply || ! respond || ! cancel || ! parent ) {
  29              return;
  30          }
  31  
  32          t.respondId = respondId;
  33          postId      = postId || false;
  34  
  35          /* Setup a temporary div for relocating back when clicking cancel */
  36          if ( ! t.getElement( 'bbp-temp-form-div' ) ) {
  37              var div = document.createElement( 'div' );
  38  
  39              div.id            = 'bbp-temp-form-div';
  40              div.style.display = 'none';
  41  
  42              respond.parentNode.appendChild( div );
  43          }
  44  
  45          /* Relocate the element */
  46          reply.parentNode.appendChild( respond );
  47  
  48          if ( post && postId ) {
  49              post.value = postId;
  50          }
  51  
  52          parent.value         = parentId;
  53          cancel.style.display = '';
  54  
  55          /* Add the editor where it now belongs */
  56          t.addEditor();
  57  
  58          /**
  59           * When canceling a Reply.
  60           *
  61           * @since 2.6.2
  62           * @returns {void}
  63           */
  64          cancel.onclick = function () {
  65              var r       = addReply,
  66                  temp    = r.getElement( 'bbp-temp-form-div' ),
  67                  respond = r.getElement( r.respondId );
  68  
  69              r.removeEditor();
  70  
  71              if ( ! temp || ! respond ) {
  72                  return;
  73              }
  74  
  75              r.getElement( 'bbp_reply_to' ).value = '0';
  76  
  77              temp.parentNode.insertBefore( respond, temp );
  78              temp.parentNode.removeChild( temp );
  79  
  80              this.style.display = 'none';
  81              this.onclick       = null;
  82  
  83              r.addEditor();
  84          };
  85  
  86          t.scrollToForm();
  87  
  88          return false;
  89      },
  90  
  91      /**
  92       * Scrolls to the top of the page.
  93       *
  94       * @since 2.6.2
  95       * @return {void}
  96       */
  97      scrollToForm: function() {
  98  
  99          /* Get initial variables to start computing boundaries */
 100          var t           = this,
 101              form        = t.getElement( 'new-post' ),
 102              elemRect    = form.getBoundingClientRect(),
 103              position    = (window.pageYOffset || document.scrollTop)  - (document.clientTop || 0),
 104              destination = ( position + elemRect.top ),
 105              negative    = ( destination < position ), // jshint ignore:line
 106              adminbar    = t.getElement( 'wpadminbar'),
 107              offset      = 0;
 108  
 109          /* Offset by the adminbar */
 110          if ( typeof ( adminbar ) !== 'undefined' ) {
 111              offset = adminbar.scrollHeight;
 112          }
 113  
 114          /* Compute the difference, depending on direction */
 115          /* jshint ignore:start */
 116          distance = ( true === negative )
 117              ? ( position - destination )
 118              : ( destination - position );
 119  
 120          /* Do some math to compute the animation steps */
 121          var vast       = ( distance > 800 ),
 122              speed_step = vast ? 30 : 20,
 123              speed      = Math.min( 12, Math.round( distance / speed_step ) ),
 124              step       = Math.round( distance / speed_step ),
 125              steps      = [],
 126              timer      = 0;
 127  
 128          /* Scroll up */
 129          if ( true === negative ) {
 130              while ( position > destination ) {
 131                  position -= step;
 132  
 133                  if ( position < destination ) {
 134                      position = destination;
 135                  }
 136  
 137                  steps.push( position - offset );
 138  
 139                  setTimeout( function() {
 140                      window.scrollTo( 0, steps.shift() );
 141                  }, timer * speed );
 142  
 143                  timer++;
 144              }
 145  
 146          /* Scroll down */
 147          } else {
 148              while ( position < destination ) {
 149                  position += step;
 150  
 151                  if ( position > destination ) {
 152                      position = destination;
 153                  }
 154  
 155                  steps.push( position - offset );
 156  
 157                  setTimeout( function() {
 158                      window.scrollTo( 0, steps.shift() );
 159                  }, timer * speed );
 160  
 161                  timer++;
 162              }
 163          }
 164          /* jshint ignore:end */
 165      },
 166  
 167      /**
 168       * Get an element by ID
 169       *
 170       * @since 2.6.2
 171       * @param {string} e
 172       * @returns {HTMLElement} Element
 173       */
 174      getElement: function (e) {
 175          return document.getElementById(e);
 176      },
 177  
 178      /**
 179       * Remove the Editor
 180       *
 181       * @since 2.6.2
 182       * @returns {void}
 183       */
 184      removeEditor: function () {
 185  
 186          /* Bail to avoid error */
 187          if ( typeof ( tinyMCE ) === 'undefined' ) {
 188              return;
 189          }
 190  
 191          var tmce = tinyMCE.get( 'bbp_reply_content' );
 192  
 193          if ( tmce && ! tmce.isHidden() ) {
 194              this.mode = 'tmce';
 195              tmce.remove();
 196  
 197          } else {
 198              this.mode = 'html';
 199          }
 200      },
 201  
 202      /**
 203       * Add the Editor
 204       *
 205       * @since 2.6.2
 206       * @returns {void}
 207       */
 208      addEditor: function () {
 209  
 210          /* Bail to avoid error */
 211          if ( typeof ( tinyMCE ) === 'undefined' ) {
 212              return;
 213          }
 214  
 215          if ( 'tmce' === this.mode ) {
 216              switchEditors.go( 'bbp_reply_content', 'tmce' );
 217  
 218          } else if ( 'html' === this.mode ) {
 219              switchEditors.go( 'bbp_reply_content', 'html' );
 220          }
 221      }
 222  };


Generated: Wed Aug 12 01:01:22 2020 Cross-referenced by PHPXref 0.7.1