comment-reply.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * @summary Handles the addition of the comment form.
  3. *
  4. * @since 2.7.0
  5. *
  6. * @type {Object}
  7. */
  8. var addComment = {
  9. /**
  10. * @summary Retrieves the elements corresponding to the given IDs.
  11. *
  12. * @since 2.7.0
  13. *
  14. * @param {string} commId The comment ID.
  15. * @param {string} parentId The parent ID.
  16. * @param {string} respondId The respond ID.
  17. * @param {string} postId The post ID.
  18. * @returns {boolean} Always returns false.
  19. */
  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. if ( ! comm || ! respond || ! cancel || ! parent || ! commentForm ) {
  30. return;
  31. }
  32. t.respondId = respondId;
  33. postId = postId || false;
  34. if ( ! t.I( 'wp-temp-form-div' ) ) {
  35. div = document.createElement( 'div' );
  36. div.id = 'wp-temp-form-div';
  37. div.style.display = 'none';
  38. respond.parentNode.insertBefore( div, respond );
  39. }
  40. comm.parentNode.insertBefore( respond, comm.nextSibling );
  41. if ( post && postId ) {
  42. post.value = postId;
  43. }
  44. parent.value = parentId;
  45. cancel.style.display = '';
  46. /**
  47. * @summary Puts back the comment, hides the cancel button and removes the onclick event.
  48. *
  49. * @returns {boolean} Always returns false.
  50. */
  51. cancel.onclick = function() {
  52. var t = addComment,
  53. temp = t.I( 'wp-temp-form-div' ),
  54. respond = t.I( t.respondId );
  55. if ( ! temp || ! respond ) {
  56. return;
  57. }
  58. t.I( 'comment_parent' ).value = '0';
  59. temp.parentNode.insertBefore( respond, temp );
  60. temp.parentNode.removeChild( temp );
  61. this.style.display = 'none';
  62. this.onclick = null;
  63. return false;
  64. };
  65. /*
  66. * Sets initial focus to the first form focusable element.
  67. * Uses try/catch just to avoid errors in IE 7- which return visibility
  68. * 'inherit' when the visibility value is inherited from an ancestor.
  69. */
  70. try {
  71. for ( var i = 0; i < commentForm.elements.length; i++ ) {
  72. element = commentForm.elements[i];
  73. cssHidden = false;
  74. // Modern browsers.
  75. if ( 'getComputedStyle' in window ) {
  76. style = window.getComputedStyle( element );
  77. // IE 8.
  78. } else if ( document.documentElement.currentStyle ) {
  79. style = element.currentStyle;
  80. }
  81. /*
  82. * For display none, do the same thing jQuery does. For visibility,
  83. * check the element computed style since browsers are already doing
  84. * the job for us. In fact, the visibility computed style is the actual
  85. * computed value and already takes into account the element ancestors.
  86. */
  87. if ( ( element.offsetWidth <= 0 && element.offsetHeight <= 0 ) || style.visibility === 'hidden' ) {
  88. cssHidden = true;
  89. }
  90. // Skip form elements that are hidden or disabled.
  91. if ( 'hidden' === element.type || element.disabled || cssHidden ) {
  92. continue;
  93. }
  94. element.focus();
  95. // Stop after the first focusable element.
  96. break;
  97. }
  98. } catch( er ) {}
  99. return false;
  100. },
  101. /**
  102. * @summary Returns the object corresponding to the given ID.
  103. *
  104. * @since 2.7.0
  105. *
  106. * @param {string} id The ID.
  107. * @returns {Element} The element belonging to the ID.
  108. */
  109. I: function( id ) {
  110. return document.getElementById( id );
  111. }
  112. };