[ 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              t.cancelForm( this );
  66          };
  67  
  68          t.scrollToForm();
  69  
  70          /* Prevent click from going through */
  71          return false;
  72      },
  73  
  74      /**
  75       * Cancel the reply form.
  76       *
  77       * @since 2.6.6
  78       * @returns {void}
  79       */
  80      cancelForm: function () {
  81          var r       = addReply,
  82              temp    = r.getElement( 'bbp-temp-form-div' ),
  83              cancel  = r.getElement( 'bbp-cancel-reply-to-link' ),
  84              respond = r.getElement( r.respondId );
  85  
  86          r.removeEditor();
  87  
  88          /* Allow click to go through */
  89          if ( ! temp || ! respond ) {
  90              return;
  91          }
  92  
  93          r.getElement( 'bbp_reply_to' ).value = '0';
  94  
  95          temp.parentNode.insertBefore( respond, temp );
  96          temp.parentNode.removeChild( temp );
  97  
  98          cancel.style.display = 'none';
  99          cancel.onclick       = null;
 100  
 101          r.addEditor();
 102          r.scrollToForm();
 103  
 104          /* Prevent click from going through */
 105          return false;
 106      },
 107  
 108      /**
 109       * Scrolls to the top of the page.
 110       *
 111       * @since 2.6.2
 112       * @return {void}
 113       */
 114      scrollToForm: function() {
 115  
 116          /* Get initial variables to start computing boundaries */
 117          var t           = this,
 118              form        = t.getElement( 'new-post' ),
 119              elemRect    = form.getBoundingClientRect(),
 120              position    = (window.pageYOffset || document.scrollTop)  - (document.clientTop || 0),
 121              destination = ( position + elemRect.top ),
 122              negative    = ( destination < position ), // jshint ignore:line
 123              adminbar    = t.getElement( 'wpadminbar'),
 124              offset      = 0;
 125  
 126          /* Offset by the adminbar */
 127          if ( adminbar && ( typeof ( adminbar ) !== 'undefined' ) ) {
 128              offset = adminbar.scrollHeight;
 129          }
 130  
 131          /* Compute the difference, depending on direction */
 132          /* jshint ignore:start */
 133          distance = ( true === negative )
 134              ? ( position - destination )
 135              : ( destination - position );
 136  
 137          /* Do some math to compute the animation steps */
 138          var vast       = ( distance > 800 ),
 139              speed_step = vast ? 30 : 20,
 140              speed      = Math.min( 12, Math.round( distance / speed_step ) ),
 141              step       = Math.round( distance / speed_step ),
 142              steps      = [],
 143              timer      = 0;
 144  
 145          /* Scroll up */
 146          if ( true === negative ) {
 147              while ( position > destination ) {
 148                  position -= step;
 149  
 150                  if ( position < destination ) {
 151                      position = destination;
 152                  }
 153  
 154                  steps.push( position - offset );
 155  
 156                  setTimeout( function() {
 157                      window.scrollTo( 0, steps.shift() );
 158                  }, timer * speed );
 159  
 160                  timer++;
 161              }
 162  
 163          /* Scroll down */
 164          } else {
 165              while ( position < destination ) {
 166                  position += step;
 167  
 168                  if ( position > destination ) {
 169                      position = destination;
 170                  }
 171  
 172                  steps.push( position - offset );
 173  
 174                  setTimeout( function() {
 175                      window.scrollTo( 0, steps.shift() );
 176                  }, timer * speed );
 177  
 178                  timer++;
 179              }
 180          }
 181          /* jshint ignore:end */
 182      },
 183  
 184      /**
 185       * Get an element by ID
 186       *
 187       * @since 2.6.2
 188       * @param {string} e
 189       * @returns {HTMLElement} Element
 190       */
 191      getElement: function (e) {
 192          return document.getElementById(e);
 193      },
 194  
 195      /**
 196       * Remove the Editor
 197       *
 198       * @since 2.6.2
 199       * @returns {void}
 200       */
 201      removeEditor: function () {
 202  
 203          /* Bail to avoid error */
 204          if ( typeof ( tinyMCE ) === 'undefined' ) {
 205              return;
 206          }
 207  
 208          var tmce = tinyMCE.get( 'bbp_reply_content' );
 209  
 210          if ( tmce && ! tmce.isHidden() ) {
 211              this.mode = 'tmce';
 212              tmce.remove();
 213  
 214          } else {
 215              this.mode = 'html';
 216          }
 217      },
 218  
 219      /**
 220       * Add the Editor
 221       *
 222       * @since 2.6.2
 223       * @returns {void}
 224       */
 225      addEditor: function () {
 226  
 227          /* Bail to avoid error */
 228          if ( typeof ( tinyMCE ) === 'undefined' ) {
 229              return;
 230          }
 231  
 232          if ( 'tmce' === this.mode ) {
 233              switchEditors.go( 'bbp_reply_content', 'tmce' );
 234  
 235          } else if ( 'html' === this.mode ) {
 236              switchEditors.go( 'bbp_reply_content', 'html' );
 237          }
 238      }
 239  };


Generated: Tue Apr 23 01:01:01 2024 Cross-referenced by PHPXref 0.7.1