2017-08-24 13:58:44 -04:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Handles the addition of the comment form.
|
2017-08-24 13:58:44 -04:00
|
|
|
*
|
|
|
|
* @since 2.7.0
|
2018-06-27 22:30:15 -04:00
|
|
|
* @output wp-includes/js/comment-reply.js
|
2017-08-24 13:58:44 -04:00
|
|
|
*
|
2017-12-15 08:42:46 -05:00
|
|
|
* @namespace addComment
|
|
|
|
*
|
2017-08-24 13:58:44 -04:00
|
|
|
* @type {Object}
|
|
|
|
*/
|
2018-08-19 09:33:24 -04:00
|
|
|
window.addComment = ( function( window ) {
|
2017-12-03 17:37:45 -05:00
|
|
|
// Avoid scope lookups on commonly used variables.
|
|
|
|
var document = window.document;
|
|
|
|
|
|
|
|
// Settings.
|
|
|
|
var config = {
|
|
|
|
commentReplyClass : 'comment-reply-link',
|
|
|
|
cancelReplyId : 'cancel-comment-reply-link',
|
|
|
|
commentFormId : 'commentform',
|
|
|
|
temporaryFormId : 'wp-temp-form-div',
|
|
|
|
parentIdFieldId : 'comment_parent',
|
|
|
|
postIdFieldId : 'comment_post_ID'
|
|
|
|
};
|
|
|
|
|
2019-02-17 23:27:50 -05:00
|
|
|
// Cross browser MutationObserver.
|
|
|
|
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
|
|
|
|
|
2017-12-03 17:37:45 -05:00
|
|
|
// Check browser cuts the mustard.
|
|
|
|
var cutsTheMustard = 'querySelector' in document && 'addEventListener' in window;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check browser supports dataset.
|
|
|
|
* !! sets the variable to true if the property exists.
|
|
|
|
*/
|
2019-03-04 20:52:51 -05:00
|
|
|
var supportsDataset = !! document.documentElement.dataset;
|
2017-12-03 17:37:45 -05:00
|
|
|
|
|
|
|
// For holding the cancel element.
|
|
|
|
var cancelElement;
|
|
|
|
|
|
|
|
// For holding the comment form element.
|
|
|
|
var commentFormElement;
|
|
|
|
|
|
|
|
// The respond element.
|
|
|
|
var respondElement;
|
|
|
|
|
2019-02-17 23:27:50 -05:00
|
|
|
// The mutation observer.
|
|
|
|
var observer;
|
|
|
|
|
2019-03-04 20:52:51 -05:00
|
|
|
if ( cutsTheMustard && document.readyState !== 'loading' ) {
|
|
|
|
ready();
|
|
|
|
} else if ( cutsTheMustard ) {
|
|
|
|
window.addEventListener( 'DOMContentLoaded', ready, false );
|
|
|
|
}
|
2017-12-03 17:37:45 -05:00
|
|
|
|
2019-03-04 20:52:51 -05:00
|
|
|
/**
|
|
|
|
* Sets up object variables after the DOM is ready.
|
|
|
|
*
|
|
|
|
* @since 5.1.1
|
|
|
|
*/
|
|
|
|
function ready() {
|
|
|
|
// Initialise the events.
|
|
|
|
init();
|
|
|
|
|
|
|
|
// Set up a MutationObserver to check for comments loaded late.
|
|
|
|
observeChanges();
|
|
|
|
}
|
2019-02-17 23:27:50 -05:00
|
|
|
|
2017-08-24 13:58:44 -04:00
|
|
|
/**
|
2017-12-03 17:37:45 -05:00
|
|
|
* Add events to links classed .comment-reply-link.
|
2017-08-24 13:58:44 -04:00
|
|
|
*
|
2017-12-03 17:37:45 -05:00
|
|
|
* Searches the context for reply links and adds the JavaScript events
|
|
|
|
* required to move the comment form. To allow for lazy loading of
|
|
|
|
* comments this method is exposed as window.commentReply.init().
|
|
|
|
*
|
2019-01-03 14:29:51 -05:00
|
|
|
* @since 5.1.0
|
2017-08-24 13:58:44 -04:00
|
|
|
*
|
2017-12-15 08:42:46 -05:00
|
|
|
* @memberOf addComment
|
|
|
|
*
|
2017-12-03 17:37:45 -05:00
|
|
|
* @param {HTMLElement} context The parent DOM element to search for links.
|
2017-08-24 13:58:44 -04:00
|
|
|
*/
|
2017-12-03 17:37:45 -05:00
|
|
|
function init( context ) {
|
2019-02-17 23:27:50 -05:00
|
|
|
if ( ! cutsTheMustard ) {
|
2017-12-03 17:37:45 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get required elements.
|
|
|
|
cancelElement = getElementById( config.cancelReplyId );
|
|
|
|
commentFormElement = getElementById( config.commentFormId );
|
|
|
|
|
|
|
|
// No cancel element, no replies.
|
|
|
|
if ( ! cancelElement ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
return;
|
2015-11-09 19:43:26 -05:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2017-12-03 17:37:45 -05:00
|
|
|
cancelElement.addEventListener( 'touchstart', cancelEvent );
|
|
|
|
cancelElement.addEventListener( 'click', cancelEvent );
|
|
|
|
|
2019-08-13 17:26:56 -04:00
|
|
|
// Submit the comment form when the user types CTRL or CMD + 'Enter'.
|
|
|
|
var submitFormHandler = function( e ) {
|
|
|
|
if ( ( e.metaKey || e.ctrlKey ) && e.keyCode === 13 ) {
|
|
|
|
commentFormElement.removeEventListener( 'keydown', submitFormHandler );
|
|
|
|
e.preventDefault();
|
|
|
|
// The submit button ID is 'submit' so we can't call commentFormElement.submit(). Click it instead.
|
|
|
|
commentFormElement.submit.click();
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-13 20:02:55 -04:00
|
|
|
};
|
|
|
|
|
2019-11-11 11:59:02 -05:00
|
|
|
if ( commentFormElement ) {
|
|
|
|
commentFormElement.addEventListener( 'keydown', submitFormHandler );
|
|
|
|
}
|
2019-08-13 17:26:56 -04:00
|
|
|
|
2017-12-03 17:37:45 -05:00
|
|
|
var links = replyLinks( context );
|
|
|
|
var element;
|
|
|
|
|
|
|
|
for ( var i = 0, l = links.length; i < l; i++ ) {
|
|
|
|
element = links[i];
|
|
|
|
|
|
|
|
element.addEventListener( 'touchstart', clickEvent );
|
|
|
|
element.addEventListener( 'click', clickEvent );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all links classed .comment-reply-link.
|
|
|
|
*
|
2019-01-03 14:29:51 -05:00
|
|
|
* @since 5.1.0
|
2017-12-03 17:37:45 -05:00
|
|
|
*
|
|
|
|
* @param {HTMLElement} context The parent DOM element to search for links.
|
|
|
|
*
|
|
|
|
* @return {HTMLCollection|NodeList|Array}
|
|
|
|
*/
|
|
|
|
function replyLinks( context ) {
|
|
|
|
var selectorClass = config.commentReplyClass;
|
|
|
|
var allReplyLinks;
|
|
|
|
|
|
|
|
// childNodes is a handy check to ensure the context is a HTMLElement.
|
|
|
|
if ( ! context || ! context.childNodes ) {
|
|
|
|
context = document;
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2017-12-03 17:37:45 -05:00
|
|
|
if ( document.getElementsByClassName ) {
|
|
|
|
// Fastest.
|
|
|
|
allReplyLinks = context.getElementsByClassName( selectorClass );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Fast.
|
|
|
|
allReplyLinks = context.querySelectorAll( '.' + selectorClass );
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
|
|
|
|
2017-12-03 17:37:45 -05:00
|
|
|
return allReplyLinks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancel event handler.
|
|
|
|
*
|
2019-01-03 14:29:51 -05:00
|
|
|
* @since 5.1.0
|
2017-12-03 17:37:45 -05:00
|
|
|
*
|
|
|
|
* @param {Event} event The calling event.
|
|
|
|
*/
|
|
|
|
function cancelEvent( event ) {
|
|
|
|
var cancelLink = this;
|
|
|
|
var temporaryFormId = config.temporaryFormId;
|
|
|
|
var temporaryElement = getElementById( temporaryFormId );
|
|
|
|
|
|
|
|
if ( ! temporaryElement || ! respondElement ) {
|
|
|
|
// Conditions for cancel link fail.
|
|
|
|
return;
|
2015-11-09 19:43:26 -05:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2017-12-03 17:37:45 -05:00
|
|
|
getElementById( config.parentIdFieldId ).value = '0';
|
|
|
|
|
|
|
|
// Move the respond form back in place of the temporary element.
|
|
|
|
temporaryElement.parentNode.replaceChild( respondElement ,temporaryElement );
|
|
|
|
cancelLink.style.display = 'none';
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Click event handler.
|
|
|
|
*
|
2019-01-03 14:29:51 -05:00
|
|
|
* @since 5.1.0
|
2017-12-03 17:37:45 -05:00
|
|
|
*
|
|
|
|
* @param {Event} event The calling event.
|
|
|
|
*/
|
|
|
|
function clickEvent( event ) {
|
|
|
|
var replyLink = this,
|
|
|
|
commId = getDataAttribute( replyLink, 'belowelement'),
|
|
|
|
parentId = getDataAttribute( replyLink, 'commentid' ),
|
|
|
|
respondId = getDataAttribute( replyLink, 'respondelement'),
|
|
|
|
postId = getDataAttribute( replyLink, 'postid'),
|
|
|
|
follow;
|
|
|
|
|
2019-03-04 20:52:51 -05:00
|
|
|
if ( ! commId || ! parentId || ! respondId || ! postId ) {
|
|
|
|
/*
|
|
|
|
* Theme or plugin defines own link via custom `wp_list_comments()` callback
|
|
|
|
* and calls `moveForm()` either directly or via a custom event hook.
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-03 17:37:45 -05:00
|
|
|
/*
|
|
|
|
* Third party comments systems can hook into this function via the global scope,
|
|
|
|
* therefore the click event needs to reference the global scope.
|
2017-08-24 13:58:44 -04:00
|
|
|
*/
|
2017-12-03 17:37:45 -05:00
|
|
|
follow = window.addComment.moveForm(commId, parentId, respondId, postId);
|
|
|
|
if ( false === follow ) {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2019-02-17 23:27:50 -05:00
|
|
|
/**
|
|
|
|
* Creates a mutation observer to check for newly inserted comments.
|
|
|
|
*
|
|
|
|
* @since 5.1.0
|
|
|
|
*/
|
|
|
|
function observeChanges() {
|
|
|
|
if ( ! MutationObserver ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var observerOptions = {
|
|
|
|
childList: true,
|
2019-07-15 12:46:55 -04:00
|
|
|
subtree: true
|
2019-02-17 23:27:50 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
observer = new MutationObserver( handleChanges );
|
|
|
|
observer.observe( document.body, observerOptions );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles DOM changes, calling init() if any new nodes are added.
|
|
|
|
*
|
|
|
|
* @since 5.1.0
|
|
|
|
*
|
|
|
|
* @param {Array} mutationRecords Array of MutationRecord objects.
|
|
|
|
*/
|
|
|
|
function handleChanges( mutationRecords ) {
|
|
|
|
var i = mutationRecords.length;
|
|
|
|
|
|
|
|
while ( i-- ) {
|
|
|
|
// Call init() once if any record in this set adds nodes.
|
|
|
|
if ( mutationRecords[ i ].addedNodes.length ) {
|
|
|
|
init();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-03 17:37:45 -05:00
|
|
|
/**
|
|
|
|
* Backward compatible getter of data-* attribute.
|
|
|
|
*
|
|
|
|
* Uses element.dataset if it exists, otherwise uses getAttribute.
|
|
|
|
*
|
2019-01-03 14:29:51 -05:00
|
|
|
* @since 5.1.0
|
2017-12-03 17:37:45 -05:00
|
|
|
*
|
|
|
|
* @param {HTMLElement} Element DOM element with the attribute.
|
|
|
|
* @param {String} Attribute the attribute to get.
|
|
|
|
*
|
|
|
|
* @return {String}
|
|
|
|
*/
|
|
|
|
function getDataAttribute( element, attribute ) {
|
|
|
|
if ( supportsDataset ) {
|
|
|
|
return element.dataset[attribute];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return element.getAttribute( 'data-' + attribute );
|
|
|
|
}
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2017-12-03 17:37:45 -05:00
|
|
|
/**
|
|
|
|
* Get element by ID.
|
|
|
|
*
|
|
|
|
* Local alias for document.getElementById.
|
|
|
|
*
|
2019-01-03 14:29:51 -05:00
|
|
|
* @since 5.1.0
|
2017-12-03 17:37:45 -05:00
|
|
|
*
|
|
|
|
* @param {HTMLElement} The requested element.
|
|
|
|
*/
|
|
|
|
function getElementById( elementId ) {
|
|
|
|
return document.getElementById( elementId );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Moves the reply form from its current position to the reply location.
|
|
|
|
*
|
|
|
|
* @since 2.7.0
|
|
|
|
*
|
2017-12-15 08:42:46 -05:00
|
|
|
* @memberOf addComment
|
|
|
|
*
|
2017-12-03 17:37:45 -05:00
|
|
|
* @param {String} addBelowId HTML ID of element the form follows.
|
|
|
|
* @param {String} commentId Database ID of comment being replied to.
|
|
|
|
* @param {String} respondId HTML ID of 'respond' element.
|
|
|
|
* @param {String} postId Database ID of the post.
|
|
|
|
*/
|
|
|
|
function moveForm( addBelowId, commentId, respondId, postId ) {
|
|
|
|
// Get elements based on their IDs.
|
|
|
|
var addBelowElement = getElementById( addBelowId );
|
|
|
|
respondElement = getElementById( respondId );
|
|
|
|
|
|
|
|
// Get the hidden fields.
|
|
|
|
var parentIdField = getElementById( config.parentIdFieldId );
|
|
|
|
var postIdField = getElementById( config.postIdFieldId );
|
|
|
|
var element, cssHidden, style;
|
|
|
|
|
|
|
|
if ( ! addBelowElement || ! respondElement || ! parentIdField ) {
|
|
|
|
// Missing key elements, fail.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
addPlaceHolder( respondElement );
|
|
|
|
|
|
|
|
// Set the value of the post.
|
|
|
|
if ( postId && postIdField ) {
|
|
|
|
postIdField.value = postId;
|
|
|
|
}
|
|
|
|
|
|
|
|
parentIdField.value = commentId;
|
|
|
|
|
|
|
|
cancelElement.style.display = '';
|
|
|
|
addBelowElement.parentNode.insertBefore( respondElement, addBelowElement.nextSibling );
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2015-11-18 14:15:28 -05:00
|
|
|
/*
|
2017-12-03 17:37:45 -05:00
|
|
|
* This is for backward compatibility with third party commenting systems
|
|
|
|
* hooking into the event using older techniques.
|
2015-11-18 14:15:28 -05:00
|
|
|
*/
|
2019-08-13 20:02:55 -04:00
|
|
|
cancelElement.onclick = function() {
|
2017-12-03 17:37:45 -05:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Focus on the first field in the comment form.
|
2015-11-09 19:43:26 -05:00
|
|
|
try {
|
2017-12-03 17:37:45 -05:00
|
|
|
for ( var i = 0; i < commentFormElement.elements.length; i++ ) {
|
|
|
|
element = commentFormElement.elements[i];
|
2015-11-18 14:15:28 -05:00
|
|
|
cssHidden = false;
|
2015-11-09 19:43:26 -05:00
|
|
|
|
2017-12-03 17:37:45 -05:00
|
|
|
// Get elements computed style.
|
2015-11-09 19:43:26 -05:00
|
|
|
if ( 'getComputedStyle' in window ) {
|
2017-12-03 17:37:45 -05:00
|
|
|
// Modern browsers.
|
2015-11-18 14:15:28 -05:00
|
|
|
style = window.getComputedStyle( element );
|
|
|
|
} else if ( document.documentElement.currentStyle ) {
|
2017-12-03 17:37:45 -05:00
|
|
|
// IE 8.
|
2015-11-18 14:15:28 -05:00
|
|
|
style = element.currentStyle;
|
|
|
|
}
|
2015-11-09 19:43:26 -05:00
|
|
|
|
2015-11-18 14:15:28 -05:00
|
|
|
/*
|
|
|
|
* For display none, do the same thing jQuery does. For visibility,
|
|
|
|
* check the element computed style since browsers are already doing
|
|
|
|
* the job for us. In fact, the visibility computed style is the actual
|
|
|
|
* computed value and already takes into account the element ancestors.
|
|
|
|
*/
|
|
|
|
if ( ( element.offsetWidth <= 0 && element.offsetHeight <= 0 ) || style.visibility === 'hidden' ) {
|
|
|
|
cssHidden = true;
|
|
|
|
}
|
2015-11-09 19:43:26 -05:00
|
|
|
|
2015-11-18 14:15:28 -05:00
|
|
|
// Skip form elements that are hidden or disabled.
|
|
|
|
if ( 'hidden' === element.type || element.disabled || cssHidden ) {
|
|
|
|
continue;
|
2015-11-09 19:43:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
element.focus();
|
|
|
|
// Stop after the first focusable element.
|
|
|
|
break;
|
|
|
|
}
|
2017-12-03 17:37:45 -05:00
|
|
|
}
|
|
|
|
catch(e) {
|
2015-11-18 14:15:28 -05:00
|
|
|
|
2017-12-03 17:37:45 -05:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2017-12-03 17:37:45 -05:00
|
|
|
/*
|
|
|
|
* false is returned for backward compatibility with third party commenting systems
|
|
|
|
* hooking into this function.
|
|
|
|
*/
|
2012-08-22 20:04:18 -04:00
|
|
|
return false;
|
2017-12-03 17:37:45 -05:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2017-08-24 13:58:44 -04:00
|
|
|
/**
|
2017-12-03 17:37:45 -05:00
|
|
|
* Add placeholder element.
|
|
|
|
*
|
|
|
|
* Places a place holder element above the #respond element for
|
|
|
|
* the form to be returned to if needs be.
|
2017-08-24 13:58:44 -04:00
|
|
|
*
|
|
|
|
* @since 2.7.0
|
|
|
|
*
|
2017-12-03 17:37:45 -05:00
|
|
|
* @param {HTMLelement} respondElement the #respond element holding comment form.
|
2017-08-24 13:58:44 -04:00
|
|
|
*/
|
2017-12-03 17:37:45 -05:00
|
|
|
function addPlaceHolder( respondElement ) {
|
|
|
|
var temporaryFormId = config.temporaryFormId;
|
|
|
|
var temporaryElement = getElementById( temporaryFormId );
|
|
|
|
|
|
|
|
if ( temporaryElement ) {
|
|
|
|
// The element already exists, no need to recreate.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
temporaryElement = document.createElement( 'div' );
|
|
|
|
temporaryElement.id = temporaryFormId;
|
|
|
|
temporaryElement.style.display = 'none';
|
|
|
|
respondElement.parentNode.insertBefore( temporaryElement, respondElement );
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
2017-12-03 17:37:45 -05:00
|
|
|
|
|
|
|
return {
|
|
|
|
init: init,
|
|
|
|
moveForm: moveForm
|
|
|
|
};
|
|
|
|
})( window );
|