| 8 | | var addComment = { |
| | 8 | var addComment; |
| | 9 | addComment = ( function( window ) { |
| | 10 | // Avoid scope lookups on commonly used variables. |
| | 11 | var document = window.document; |
| | 12 | |
| | 13 | // Settings. |
| | 14 | var config = { |
| | 15 | commentReplyClass : 'comment-reply-link', |
| | 16 | cancelReplyId : 'cancel-comment-reply-link', |
| | 17 | commentFormId : 'commentform', |
| | 18 | temporaryFormId : 'wp-temp-form-div', |
| | 19 | parentIdFieldId : 'comment_parent', |
| | 20 | postIdFieldId : 'comment_post_ID' |
| | 21 | }; |
| | 22 | |
| | 23 | // Check browser cuts the mustard. |
| | 24 | var cutsTheMustard = 'querySelector' in document && 'addEventListener' in window; |
| | 25 | |
| | 26 | /* |
| | 27 | * Check browser supports dataset. |
| | 28 | * !! sets the variable to true if the property exists. |
| | 29 | */ |
| | 30 | var supportsDataset = !! document.body.dataset; |
| | 31 | |
| | 32 | // For holding the cancel element. |
| | 33 | var cancelElement; |
| | 34 | |
| | 35 | // For holding the comment form element. |
| | 36 | var commentFormElement; |
| | 37 | |
| | 38 | // The respond element. |
| | 39 | var respondElement; |
| | 40 | |
| | 41 | // Initialise the events. |
| | 42 | init(); |
| | 43 | |
| 20 | | moveForm: function( commId, parentId, respondId, postId ) { |
| 21 | | var div, element, style, cssHidden, |
| 22 | | t = this, |
| 23 | | comm = t.I( commId ), |
| 24 | | respond = t.I( respondId ), |
| 25 | | cancel = t.I( 'cancel-comment-reply-link' ), |
| 26 | | parent = t.I( 'comment_parent' ), |
| 27 | | post = t.I( 'comment_post_ID' ), |
| 28 | | commentForm = respond.getElementsByTagName( 'form' )[0]; |
| 29 | | |
| 30 | | if ( ! comm || ! respond || ! cancel || ! parent || ! commentForm ) { |
| | 55 | function init( context ) { |
| | 56 | if ( true !== cutsTheMustard ) { |
| | 57 | return; |
| | 58 | } |
| | 59 | |
| | 60 | // Get required elements. |
| | 61 | cancelElement = getElementById( config.cancelReplyId ); |
| | 62 | commentFormElement = getElementById( config.commentFormId ); |
| | 63 | |
| | 64 | // No cancel element, no replies. |
| | 65 | if ( ! cancelElement ) { |
| 34 | | t.respondId = respondId; |
| 35 | | postId = postId || false; |
| | 69 | cancelElement.addEventListener( 'touchstart', cancelEvent ); |
| | 70 | cancelElement.addEventListener( 'click', cancelEvent ); |
| | 71 | |
| | 72 | var links = replyLinks( context ); |
| | 73 | var element; |
| | 74 | |
| | 75 | for ( var i = 0, l = links.length; i < l; i++ ) { |
| | 76 | element = links[i]; |
| | 77 | |
| | 78 | element.addEventListener( 'touchstart', clickEvent ); |
| | 79 | element.addEventListener( 'click', clickEvent ); |
| | 80 | } |
| | 81 | } |
| | 82 | |
| | 83 | /** |
| | 84 | * Return all links classed .comment-reply-link. |
| | 85 | * |
| | 86 | * @since 5.0.0 |
| | 87 | * |
| | 88 | * @param {HTMLElement} context The parent DOM element to search for links. |
| | 89 | * |
| | 90 | * @return {HTMLCollection|NodeList|Array} |
| | 91 | */ |
| | 92 | function replyLinks( context ) { |
| | 93 | var selectorClass = config.commentReplyClass; |
| | 94 | var allReplyLinks; |
| | 95 | |
| | 96 | // childNodes is a handy check to ensure the context is a HTMLElement. |
| | 97 | if ( ! context || ! context.childNodes ) { |
| | 98 | context = document; |
| | 99 | } |
| 37 | | if ( ! t.I( 'wp-temp-form-div' ) ) { |
| 38 | | div = document.createElement( 'div' ); |
| 39 | | div.id = 'wp-temp-form-div'; |
| 40 | | div.style.display = 'none'; |
| 41 | | respond.parentNode.insertBefore( div, respond ); |
| | 101 | if ( document.getElementsByClassName ) { |
| | 102 | // Fastest. |
| | 103 | allReplyLinks = context.getElementsByClassName( selectorClass ); |
| | 104 | } |
| | 105 | else { |
| | 106 | // Fast. |
| | 107 | allReplyLinks = context.querySelectorAll( '.' + selectorClass ); |
| 51 | | /** |
| 52 | | * @summary Puts back the comment, hides the cancel button and removes the onclick event. |
| 53 | | * |
| 54 | | * @returns {boolean} Always returns false. |
| | 130 | getElementById( config.parentIdFieldId ).value = '0'; |
| | 131 | |
| | 132 | // Move the respond form back in place of the temporary element. |
| | 133 | temporaryElement.parentNode.replaceChild( respondElement ,temporaryElement ); |
| | 134 | cancelLink.style.display = 'none'; |
| | 135 | event.preventDefault(); |
| | 136 | } |
| | 137 | |
| | 138 | /** |
| | 139 | * Click event handler. |
| | 140 | * |
| | 141 | * @since 5.0.0 |
| | 142 | * |
| | 143 | * @param {Event} event The calling event. |
| | 144 | */ |
| | 145 | function clickEvent( event ) { |
| | 146 | var replyLink = this, |
| | 147 | commId = getDataAttribute( replyLink, 'belowelement'), |
| | 148 | parentId = getDataAttribute( replyLink, 'commentid' ), |
| | 149 | respondId = getDataAttribute( replyLink, 'respondelement'), |
| | 150 | postId = getDataAttribute( replyLink, 'postid'), |
| | 151 | follow; |
| | 152 | |
| | 153 | /* |
| | 154 | * Third party comments systems can hook into this function via the global scope, |
| | 155 | * therefore the click event needs to reference the global scope. |
| 61 | | if ( ! temp || ! respond ) { |
| 62 | | return; |
| 63 | | } |
| | 163 | /** |
| | 164 | * Backward compatible getter of data-* attribute. |
| | 165 | * |
| | 166 | * Uses element.dataset if it exists, otherwise uses getAttribute. |
| | 167 | * |
| | 168 | * @since 5.0.0 |
| | 169 | * |
| | 170 | * @param {HTMLElement} Element DOM element with the attribute. |
| | 171 | * @param {String} Attribute the attribute to get. |
| | 172 | * |
| | 173 | * @return {String} |
| | 174 | */ |
| | 175 | function getDataAttribute( element, attribute ) { |
| | 176 | if ( supportsDataset ) { |
| | 177 | return element.dataset[attribute]; |
| | 178 | } |
| | 179 | else { |
| | 180 | return element.getAttribute( 'data-' + attribute ); |
| | 181 | } |
| | 182 | } |
| 65 | | t.I( 'comment_parent' ).value = '0'; |
| 66 | | temp.parentNode.insertBefore( respond, temp ); |
| 67 | | temp.parentNode.removeChild( temp ); |
| 68 | | this.style.display = 'none'; |
| 69 | | this.onclick = null; |
| 70 | | return false; |
| 71 | | }; |
| | 184 | /** |
| | 185 | * Get element by ID. |
| | 186 | * |
| | 187 | * Local alias for document.getElementById. |
| | 188 | * |
| | 189 | * @since 5.0.0 |
| | 190 | * |
| | 191 | * @param {HTMLElement} The requested element. |
| | 192 | */ |
| | 193 | function getElementById( elementId ) { |
| | 194 | return document.getElementById( elementId ); |
| | 195 | } |
| | 196 | |
| | 197 | /** |
| | 198 | * Moves the reply form from its current position to the reply location. |
| | 199 | * |
| | 200 | * @since 2.7.0 |
| | 201 | * |
| | 202 | * @param {String} addBelowId HTML ID of element the form follows. |
| | 203 | * @param {String} commentId Database ID of comment being replied to. |
| | 204 | * @param {String} respondId HTML ID of 'respond' element. |
| | 205 | * @param {String} postId Database ID of the post. |
| | 206 | */ |
| | 207 | function moveForm( addBelowId, commentId, respondId, postId ) { |
| | 208 | // Get elements based on their IDs. |
| | 209 | var addBelowElement = getElementById( addBelowId ); |
| | 210 | respondElement = getElementById( respondId ); |
| | 211 | |
| | 212 | // Get the hidden fields. |
| | 213 | var parentIdField = getElementById( config.parentIdFieldId ); |
| | 214 | var postIdField = getElementById( config.postIdFieldId ); |
| | 215 | var element, cssHidden, style; |
| | 216 | |
| | 217 | if ( ! addBelowElement || ! respondElement || ! parentIdField ) { |
| | 218 | // Missing key elements, fail. |
| | 219 | return; |
| | 220 | } |
| | 221 | |
| | 222 | addPlaceHolder( respondElement ); |
| | 223 | |
| | 224 | // Set the value of the post. |
| | 225 | if ( postId && postIdField ) { |
| | 226 | postIdField.value = postId; |
| | 227 | } |
| | 228 | |
| | 229 | parentIdField.value = commentId; |
| | 230 | |
| | 231 | cancelElement.style.display = ''; |
| | 232 | addBelowElement.parentNode.insertBefore( respondElement, addBelowElement.nextSibling ); |
| 124 | | I: function( id ) { |
| 125 | | return document.getElementById( id ); |
| | 298 | function addPlaceHolder( respondElement ) { |
| | 299 | var temporaryFormId = config.temporaryFormId; |
| | 300 | var temporaryElement = getElementById( temporaryFormId ); |
| | 301 | |
| | 302 | if ( temporaryElement ) { |
| | 303 | // The element already exists, no need to recreate. |
| | 304 | return; |
| | 305 | } |
| | 306 | |
| | 307 | temporaryElement = document.createElement( 'div' ); |
| | 308 | temporaryElement.id = temporaryFormId; |
| | 309 | temporaryElement.style.display = 'none'; |
| | 310 | respondElement.parentNode.insertBefore( temporaryElement, respondElement ); |