author | ymh <ymh.work@gmail.com> |
Tue, 15 Dec 2020 13:49:49 +0100 | |
changeset 16 | a86126ab1dd4 |
parent 9 | 177826044cd9 |
child 18 | be944660c56a |
permissions | -rw-r--r-- |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1 |
/** |
9 | 2 |
* Handles the addition of the comment form. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4 |
* @since 2.7.0 |
9 | 5 |
* @output wp-includes/js/comment-reply.js |
6 |
* |
|
7 |
* @namespace addComment |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
8 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
9 |
* @type {Object} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
10 |
*/ |
9 | 11 |
window.addComment = ( function( window ) { |
12 |
// Avoid scope lookups on commonly used variables. |
|
13 |
var document = window.document; |
|
14 |
||
15 |
// Settings. |
|
16 |
var config = { |
|
16 | 17 |
commentReplyClass : 'comment-reply-link', |
18 |
commentReplyTitleId : 'reply-title', |
|
19 |
cancelReplyId : 'cancel-comment-reply-link', |
|
20 |
commentFormId : 'commentform', |
|
21 |
temporaryFormId : 'wp-temp-form-div', |
|
22 |
parentIdFieldId : 'comment_parent', |
|
23 |
postIdFieldId : 'comment_post_ID' |
|
9 | 24 |
}; |
25 |
||
26 |
// Cross browser MutationObserver. |
|
27 |
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver; |
|
28 |
||
29 |
// Check browser cuts the mustard. |
|
30 |
var cutsTheMustard = 'querySelector' in document && 'addEventListener' in window; |
|
31 |
||
32 |
/* |
|
33 |
* Check browser supports dataset. |
|
34 |
* !! sets the variable to true if the property exists. |
|
35 |
*/ |
|
36 |
var supportsDataset = !! document.documentElement.dataset; |
|
37 |
||
38 |
// For holding the cancel element. |
|
39 |
var cancelElement; |
|
40 |
||
41 |
// For holding the comment form element. |
|
42 |
var commentFormElement; |
|
43 |
||
44 |
// The respond element. |
|
45 |
var respondElement; |
|
46 |
||
47 |
// The mutation observer. |
|
48 |
var observer; |
|
49 |
||
50 |
if ( cutsTheMustard && document.readyState !== 'loading' ) { |
|
51 |
ready(); |
|
52 |
} else if ( cutsTheMustard ) { |
|
53 |
window.addEventListener( 'DOMContentLoaded', ready, false ); |
|
54 |
} |
|
55 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
56 |
/** |
9 | 57 |
* Sets up object variables after the DOM is ready. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
58 |
* |
9 | 59 |
* @since 5.1.1 |
60 |
*/ |
|
61 |
function ready() { |
|
62 |
// Initialise the events. |
|
63 |
init(); |
|
64 |
||
65 |
// Set up a MutationObserver to check for comments loaded late. |
|
66 |
observeChanges(); |
|
67 |
} |
|
68 |
||
69 |
/** |
|
70 |
* Add events to links classed .comment-reply-link. |
|
71 |
* |
|
72 |
* Searches the context for reply links and adds the JavaScript events |
|
73 |
* required to move the comment form. To allow for lazy loading of |
|
74 |
* comments this method is exposed as window.commentReply.init(). |
|
75 |
* |
|
76 |
* @since 5.1.0 |
|
77 |
* |
|
78 |
* @memberOf addComment |
|
79 |
* |
|
80 |
* @param {HTMLElement} context The parent DOM element to search for links. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
81 |
*/ |
9 | 82 |
function init( context ) { |
83 |
if ( ! cutsTheMustard ) { |
|
84 |
return; |
|
85 |
} |
|
86 |
||
87 |
// Get required elements. |
|
88 |
cancelElement = getElementById( config.cancelReplyId ); |
|
89 |
commentFormElement = getElementById( config.commentFormId ); |
|
90 |
||
91 |
// No cancel element, no replies. |
|
92 |
if ( ! cancelElement ) { |
|
93 |
return; |
|
94 |
} |
|
95 |
||
96 |
cancelElement.addEventListener( 'touchstart', cancelEvent ); |
|
97 |
cancelElement.addEventListener( 'click', cancelEvent ); |
|
98 |
||
16 | 99 |
// Submit the comment form when the user types [Ctrl] or [Cmd] + [Enter]. |
100 |
var submitFormHandler = function( e ) { |
|
101 |
if ( ( e.metaKey || e.ctrlKey ) && e.keyCode === 13 ) { |
|
102 |
commentFormElement.removeEventListener( 'keydown', submitFormHandler ); |
|
103 |
e.preventDefault(); |
|
104 |
// The submit button ID is 'submit' so we can't call commentFormElement.submit(). Click it instead. |
|
105 |
commentFormElement.submit.click(); |
|
106 |
return false; |
|
107 |
} |
|
108 |
}; |
|
109 |
||
110 |
if ( commentFormElement ) { |
|
111 |
commentFormElement.addEventListener( 'keydown', submitFormHandler ); |
|
112 |
} |
|
113 |
||
9 | 114 |
var links = replyLinks( context ); |
115 |
var element; |
|
116 |
||
117 |
for ( var i = 0, l = links.length; i < l; i++ ) { |
|
118 |
element = links[i]; |
|
119 |
||
120 |
element.addEventListener( 'touchstart', clickEvent ); |
|
121 |
element.addEventListener( 'click', clickEvent ); |
|
122 |
} |
|
123 |
} |
|
0 | 124 |
|
9 | 125 |
/** |
126 |
* Return all links classed .comment-reply-link. |
|
127 |
* |
|
128 |
* @since 5.1.0 |
|
129 |
* |
|
130 |
* @param {HTMLElement} context The parent DOM element to search for links. |
|
131 |
* |
|
132 |
* @return {HTMLCollection|NodeList|Array} |
|
133 |
*/ |
|
134 |
function replyLinks( context ) { |
|
135 |
var selectorClass = config.commentReplyClass; |
|
136 |
var allReplyLinks; |
|
137 |
||
138 |
// childNodes is a handy check to ensure the context is a HTMLElement. |
|
139 |
if ( ! context || ! context.childNodes ) { |
|
140 |
context = document; |
|
141 |
} |
|
142 |
||
143 |
if ( document.getElementsByClassName ) { |
|
144 |
// Fastest. |
|
145 |
allReplyLinks = context.getElementsByClassName( selectorClass ); |
|
146 |
} |
|
147 |
else { |
|
148 |
// Fast. |
|
149 |
allReplyLinks = context.querySelectorAll( '.' + selectorClass ); |
|
150 |
} |
|
151 |
||
152 |
return allReplyLinks; |
|
153 |
} |
|
154 |
||
155 |
/** |
|
156 |
* Cancel event handler. |
|
157 |
* |
|
158 |
* @since 5.1.0 |
|
159 |
* |
|
160 |
* @param {Event} event The calling event. |
|
161 |
*/ |
|
162 |
function cancelEvent( event ) { |
|
163 |
var cancelLink = this; |
|
164 |
var temporaryFormId = config.temporaryFormId; |
|
165 |
var temporaryElement = getElementById( temporaryFormId ); |
|
166 |
||
167 |
if ( ! temporaryElement || ! respondElement ) { |
|
168 |
// Conditions for cancel link fail. |
|
0 | 169 |
return; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
170 |
} |
0 | 171 |
|
9 | 172 |
getElementById( config.parentIdFieldId ).value = '0'; |
173 |
||
174 |
// Move the respond form back in place of the temporary element. |
|
16 | 175 |
var headingText = temporaryElement.textContent; |
176 |
temporaryElement.parentNode.replaceChild( respondElement, temporaryElement ); |
|
9 | 177 |
cancelLink.style.display = 'none'; |
16 | 178 |
|
179 |
var replyHeadingElement = getElementById( config.commentReplyTitleId ); |
|
180 |
var replyHeadingTextNode = replyHeadingElement && replyHeadingElement.firstChild; |
|
181 |
||
182 |
if ( replyHeadingTextNode && replyHeadingTextNode.nodeType === Node.TEXT_NODE && headingText ) { |
|
183 |
replyHeadingTextNode.textContent = headingText; |
|
184 |
} |
|
185 |
||
9 | 186 |
event.preventDefault(); |
187 |
} |
|
188 |
||
189 |
/** |
|
190 |
* Click event handler. |
|
191 |
* |
|
192 |
* @since 5.1.0 |
|
193 |
* |
|
194 |
* @param {Event} event The calling event. |
|
195 |
*/ |
|
196 |
function clickEvent( event ) { |
|
16 | 197 |
var replyNode = getElementById( config.commentReplyTitleId ); |
198 |
var defaultReplyHeading = replyNode && replyNode.firstChild.textContent; |
|
9 | 199 |
var replyLink = this, |
200 |
commId = getDataAttribute( replyLink, 'belowelement'), |
|
201 |
parentId = getDataAttribute( replyLink, 'commentid' ), |
|
16 | 202 |
respondId = getDataAttribute( replyLink, 'respondelement' ), |
203 |
postId = getDataAttribute( replyLink, 'postid' ), |
|
204 |
replyTo = getDataAttribute( replyLink, 'replyto' ) || defaultReplyHeading, |
|
9 | 205 |
follow; |
0 | 206 |
|
9 | 207 |
if ( ! commId || ! parentId || ! respondId || ! postId ) { |
208 |
/* |
|
209 |
* Theme or plugin defines own link via custom `wp_list_comments()` callback |
|
210 |
* and calls `moveForm()` either directly or via a custom event hook. |
|
211 |
*/ |
|
212 |
return; |
|
213 |
} |
|
214 |
||
215 |
/* |
|
216 |
* Third party comments systems can hook into this function via the global scope, |
|
217 |
* therefore the click event needs to reference the global scope. |
|
218 |
*/ |
|
16 | 219 |
follow = window.addComment.moveForm( commId, parentId, respondId, postId, replyTo ); |
9 | 220 |
if ( false === follow ) { |
221 |
event.preventDefault(); |
|
222 |
} |
|
223 |
} |
|
224 |
||
225 |
/** |
|
226 |
* Creates a mutation observer to check for newly inserted comments. |
|
227 |
* |
|
228 |
* @since 5.1.0 |
|
229 |
*/ |
|
230 |
function observeChanges() { |
|
231 |
if ( ! MutationObserver ) { |
|
232 |
return; |
|
0 | 233 |
} |
234 |
||
9 | 235 |
var observerOptions = { |
236 |
childList: true, |
|
16 | 237 |
subtree: true |
9 | 238 |
}; |
239 |
||
240 |
observer = new MutationObserver( handleChanges ); |
|
241 |
observer.observe( document.body, observerOptions ); |
|
242 |
} |
|
0 | 243 |
|
9 | 244 |
/** |
245 |
* Handles DOM changes, calling init() if any new nodes are added. |
|
246 |
* |
|
247 |
* @since 5.1.0 |
|
248 |
* |
|
249 |
* @param {Array} mutationRecords Array of MutationRecord objects. |
|
250 |
*/ |
|
251 |
function handleChanges( mutationRecords ) { |
|
252 |
var i = mutationRecords.length; |
|
0 | 253 |
|
9 | 254 |
while ( i-- ) { |
255 |
// Call init() once if any record in this set adds nodes. |
|
256 |
if ( mutationRecords[ i ].addedNodes.length ) { |
|
257 |
init(); |
|
0 | 258 |
return; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
259 |
} |
9 | 260 |
} |
261 |
} |
|
0 | 262 |
|
9 | 263 |
/** |
264 |
* Backward compatible getter of data-* attribute. |
|
265 |
* |
|
266 |
* Uses element.dataset if it exists, otherwise uses getAttribute. |
|
267 |
* |
|
268 |
* @since 5.1.0 |
|
269 |
* |
|
270 |
* @param {HTMLElement} Element DOM element with the attribute. |
|
16 | 271 |
* @param {string} Attribute the attribute to get. |
9 | 272 |
* |
16 | 273 |
* @return {string} |
9 | 274 |
*/ |
275 |
function getDataAttribute( element, attribute ) { |
|
276 |
if ( supportsDataset ) { |
|
277 |
return element.dataset[attribute]; |
|
278 |
} |
|
279 |
else { |
|
280 |
return element.getAttribute( 'data-' + attribute ); |
|
281 |
} |
|
282 |
} |
|
283 |
||
284 |
/** |
|
285 |
* Get element by ID. |
|
286 |
* |
|
287 |
* Local alias for document.getElementById. |
|
288 |
* |
|
289 |
* @since 5.1.0 |
|
290 |
* |
|
291 |
* @param {HTMLElement} The requested element. |
|
292 |
*/ |
|
293 |
function getElementById( elementId ) { |
|
294 |
return document.getElementById( elementId ); |
|
295 |
} |
|
296 |
||
297 |
/** |
|
298 |
* Moves the reply form from its current position to the reply location. |
|
299 |
* |
|
300 |
* @since 2.7.0 |
|
301 |
* |
|
302 |
* @memberOf addComment |
|
303 |
* |
|
16 | 304 |
* @param {string} addBelowId HTML ID of element the form follows. |
305 |
* @param {string} commentId Database ID of comment being replied to. |
|
306 |
* @param {string} respondId HTML ID of 'respond' element. |
|
307 |
* @param {string} postId Database ID of the post. |
|
308 |
* @param {string} replyTo Form heading content. |
|
9 | 309 |
*/ |
16 | 310 |
function moveForm( addBelowId, commentId, respondId, postId, replyTo ) { |
9 | 311 |
// Get elements based on their IDs. |
312 |
var addBelowElement = getElementById( addBelowId ); |
|
313 |
respondElement = getElementById( respondId ); |
|
314 |
||
315 |
// Get the hidden fields. |
|
316 |
var parentIdField = getElementById( config.parentIdFieldId ); |
|
317 |
var postIdField = getElementById( config.postIdFieldId ); |
|
318 |
var element, cssHidden, style; |
|
319 |
||
16 | 320 |
var replyHeading = getElementById( config.commentReplyTitleId ); |
321 |
var replyHeadingTextNode = replyHeading && replyHeading.firstChild; |
|
322 |
||
9 | 323 |
if ( ! addBelowElement || ! respondElement || ! parentIdField ) { |
324 |
// Missing key elements, fail. |
|
325 |
return; |
|
326 |
} |
|
327 |
||
16 | 328 |
if ( 'undefined' === typeof replyTo ) { |
329 |
replyTo = replyHeadingTextNode && replyHeadingTextNode.textContent; |
|
330 |
} |
|
331 |
||
9 | 332 |
addPlaceHolder( respondElement ); |
333 |
||
334 |
// Set the value of the post. |
|
335 |
if ( postId && postIdField ) { |
|
336 |
postIdField.value = postId; |
|
337 |
} |
|
338 |
||
339 |
parentIdField.value = commentId; |
|
340 |
||
341 |
cancelElement.style.display = ''; |
|
342 |
addBelowElement.parentNode.insertBefore( respondElement, addBelowElement.nextSibling ); |
|
343 |
||
16 | 344 |
if ( replyHeadingTextNode && replyHeadingTextNode.nodeType === Node.TEXT_NODE ) { |
345 |
replyHeadingTextNode.textContent = replyTo; |
|
346 |
} |
|
347 |
||
9 | 348 |
/* |
349 |
* This is for backward compatibility with third party commenting systems |
|
350 |
* hooking into the event using older techniques. |
|
351 |
*/ |
|
16 | 352 |
cancelElement.onclick = function() { |
0 | 353 |
return false; |
5 | 354 |
}; |
0 | 355 |
|
9 | 356 |
// Focus on the first field in the comment form. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
357 |
try { |
9 | 358 |
for ( var i = 0; i < commentFormElement.elements.length; i++ ) { |
359 |
element = commentFormElement.elements[i]; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
360 |
cssHidden = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
361 |
|
9 | 362 |
// Get elements computed style. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
363 |
if ( 'getComputedStyle' in window ) { |
9 | 364 |
// Modern browsers. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
365 |
style = window.getComputedStyle( element ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
366 |
} else if ( document.documentElement.currentStyle ) { |
9 | 367 |
// IE 8. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
368 |
style = element.currentStyle; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
369 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
370 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
371 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
372 |
* For display none, do the same thing jQuery does. For visibility, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
373 |
* check the element computed style since browsers are already doing |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
374 |
* the job for us. In fact, the visibility computed style is the actual |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
375 |
* computed value and already takes into account the element ancestors. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
376 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
377 |
if ( ( element.offsetWidth <= 0 && element.offsetHeight <= 0 ) || style.visibility === 'hidden' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
378 |
cssHidden = true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
379 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
380 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
381 |
// Skip form elements that are hidden or disabled. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
382 |
if ( 'hidden' === element.type || element.disabled || cssHidden ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
383 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
384 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
385 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
386 |
element.focus(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
387 |
// Stop after the first focusable element. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
388 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
389 |
} |
9 | 390 |
} |
391 |
catch(e) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
392 |
|
9 | 393 |
} |
0 | 394 |
|
9 | 395 |
/* |
396 |
* false is returned for backward compatibility with third party commenting systems |
|
397 |
* hooking into this function. |
|
398 |
*/ |
|
0 | 399 |
return false; |
9 | 400 |
} |
0 | 401 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
402 |
/** |
9 | 403 |
* Add placeholder element. |
404 |
* |
|
405 |
* Places a place holder element above the #respond element for |
|
406 |
* the form to be returned to if needs be. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
407 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
408 |
* @since 2.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
409 |
* |
9 | 410 |
* @param {HTMLelement} respondElement the #respond element holding comment form. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
411 |
*/ |
9 | 412 |
function addPlaceHolder( respondElement ) { |
413 |
var temporaryFormId = config.temporaryFormId; |
|
414 |
var temporaryElement = getElementById( temporaryFormId ); |
|
16 | 415 |
var replyElement = getElementById( config.commentReplyTitleId ); |
416 |
var initialHeadingText = replyElement ? replyElement.firstChild.textContent : ''; |
|
9 | 417 |
|
418 |
if ( temporaryElement ) { |
|
419 |
// The element already exists, no need to recreate. |
|
420 |
return; |
|
421 |
} |
|
422 |
||
423 |
temporaryElement = document.createElement( 'div' ); |
|
424 |
temporaryElement.id = temporaryFormId; |
|
425 |
temporaryElement.style.display = 'none'; |
|
16 | 426 |
temporaryElement.textContent = initialHeadingText; |
9 | 427 |
respondElement.parentNode.insertBefore( temporaryElement, respondElement ); |
0 | 428 |
} |
9 | 429 |
|
430 |
return { |
|
431 |
init: init, |
|
432 |
moveForm: moveForm |
|
433 |
}; |
|
434 |
})( window ); |