| 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 */ |
47 | | I : function(e) { |
| 91 | /** |
| 92 | * Scrolls to the top of the page. |
| 93 | * |
| 94 | * @since 2.6.2 |
| 95 | * @param {HTMLElement} t The HTML element. |
| 96 | * @return {void} |
| 97 | */ |
| 98 | scrollToForm: function(t) { |
| 99 | |
| 100 | /* Get initial variables to start computing boundaries */ |
| 101 | var 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 ), |
| 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 | distance = ( true === negative ) |
| 116 | ? ( position - destination ) |
| 117 | : ( destination - position ); |
| 118 | |
| 119 | /* Do some math to compute the animation steps */ |
| 120 | var vast = ( distance > 800 ), |
| 121 | speed_step = vast ? 30 : 20, |
| 122 | speed = Math.min( 12, Math.round( distance / speed_step ) ), |
| 123 | step = Math.round( distance / speed_step ), |
| 124 | steps = [], |
| 125 | timer = 0; |
| 126 | |
| 127 | /* Scroll up */ |
| 128 | if ( true === negative ) { |
| 129 | while ( position > destination ) { |
| 130 | position -= step; |
| 131 | |
| 132 | if ( position < destination ) { |
| 133 | position = destination; |
| 134 | } |
| 135 | |
| 136 | steps.push( position - offset ); |
| 137 | |
| 138 | setTimeout( function() { |
| 139 | window.scrollTo( 0, steps.shift() ); |
| 140 | }, timer * speed ); |
| 141 | |
| 142 | timer++; |
| 143 | } |
| 144 | |
| 145 | /* Scroll down */ |
| 146 | } else { |
| 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 | }, |
| 164 | |
| 165 | /** |
| 166 | * Get an element by ID |
| 167 | * |
| 168 | * @since 2.6.2 |
| 169 | * @param {string} e |
| 170 | * @returns {HTMLElement} Element |
| 171 | */ |
| 172 | getElement: function (e) { |
| 188 | |
| 189 | var tmce = tinyMCE.get( 'bbp_reply_content' ); |
| 190 | |
| 191 | if ( tmce && ! tmce.isHidden() ) { |
| 192 | this.mode = 'tmce'; |
| 193 | tmce.remove(); |
| 194 | |
| 195 | } else { |
| 196 | this.mode = 'html'; |
| 197 | } |
| 198 | }, |
| 199 | |
| 200 | /** |
| 201 | * Add the Editor |
| 202 | * |
| 203 | * @since 2.6.2 |
| 204 | * @returns {void} |
| 205 | */ |
| 206 | addEditor: function () { |
| 207 | |
| 208 | /* Bail to avoid error */ |
| 209 | if ( typeof (tinyMCE) === 'undefined' ) { |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | if ( 'tmce' === this.mode ) { |
| 214 | switchEditors.go( 'bbp_reply_content', 'tmce' ); |
| 215 | |
| 216 | } else if ( 'html' === this.mode ) { |
| 217 | switchEditors.go( 'bbp_reply_content', 'html' ); |
| 218 | } |
| 219 | } |