home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress2 / wp-includes / js / comment-reply.js < prev    next >
Encoding:
JavaScript  |  2017-08-24  |  3.4 KB  |  128 lines

  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.  
  30.         if ( ! comm || ! respond || ! cancel || ! parent || ! commentForm ) {
  31.             return;
  32.         }
  33.  
  34.         t.respondId = respondId;
  35.         postId = postId || false;
  36.  
  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 );
  42.         }
  43.  
  44.         comm.parentNode.insertBefore( respond, comm.nextSibling );
  45.         if ( post && postId ) {
  46.             post.value = postId;
  47.         }
  48.         parent.value = parentId;
  49.         cancel.style.display = '';
  50.  
  51.         /**
  52.          * @summary Puts back the comment, hides the cancel button and removes the onclick event.
  53.          *
  54.          * @returns {boolean} Always returns false.
  55.          */
  56.         cancel.onclick = function() {
  57.             var t       = addComment,
  58.                 temp    = t.I( 'wp-temp-form-div' ),
  59.                 respond = t.I( t.respondId );
  60.  
  61.             if ( ! temp || ! respond ) {
  62.                 return;
  63.             }
  64.  
  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.         };
  72.  
  73.         /*
  74.          * Sets initial focus to the first form focusable element.
  75.          * Uses try/catch just to avoid errors in IE 7- which return visibility
  76.          * 'inherit' when the visibility value is inherited from an ancestor.
  77.          */
  78.         try {
  79.             for ( var i = 0; i < commentForm.elements.length; i++ ) {
  80.                 element = commentForm.elements[i];
  81.                 cssHidden = false;
  82.  
  83.                 // Modern browsers.
  84.                 if ( 'getComputedStyle' in window ) {
  85.                     style = window.getComputedStyle( element );
  86.                 // IE 8.
  87.                 } else if ( document.documentElement.currentStyle ) {
  88.                     style = element.currentStyle;
  89.                 }
  90.  
  91.                 /*
  92.                  * For display none, do the same thing jQuery does. For visibility,
  93.                  * check the element computed style since browsers are already doing
  94.                  * the job for us. In fact, the visibility computed style is the actual
  95.                  * computed value and already takes into account the element ancestors.
  96.                  */
  97.                 if ( ( element.offsetWidth <= 0 && element.offsetHeight <= 0 ) || style.visibility === 'hidden' ) {
  98.                     cssHidden = true;
  99.                 }
  100.  
  101.                 // Skip form elements that are hidden or disabled.
  102.                 if ( 'hidden' === element.type || element.disabled || cssHidden ) {
  103.                     continue;
  104.                 }
  105.  
  106.                 element.focus();
  107.                 // Stop after the first focusable element.
  108.                 break;
  109.             }
  110.  
  111.         } catch( er ) {}
  112.  
  113.         return false;
  114.     },
  115.  
  116.     /**
  117.      * @summary Returns the object corresponding to the given ID.
  118.      *
  119.      * @since 2.7.0
  120.      *
  121.      * @param {string} id The ID.
  122.      * @returns {Element} The element belonging to the ID.
  123.      */
  124.     I: function( id ) {
  125.         return document.getElementById( id );
  126.     }
  127. };
  128.