wp/wp-includes/js/comment-reply.js
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
       
     1 /**
       
     2  * @summary Handles the addition of the comment form.
       
     3  *
       
     4  * @since 2.7.0
       
     5  *
       
     6  * @type {Object}
       
     7  */
     1 var addComment = {
     8 var addComment = {
     2 	moveForm : function(commId, parentId, respondId, postId) {
     9 	/**
     3 		var t = this, div, comm = t.I(commId), respond = t.I(respondId), cancel = t.I('cancel-comment-reply-link'), parent = t.I('comment_parent'), post = t.I('comment_post_ID');
    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];
     4 
    29 
     5 		if ( ! comm || ! respond || ! cancel || ! parent )
    30 		if ( ! comm || ! respond || ! cancel || ! parent || ! commentForm ) {
     6 			return;
    31 			return;
       
    32 		}
     7 
    33 
     8 		t.respondId = respondId;
    34 		t.respondId = respondId;
     9 		postId = postId || false;
    35 		postId = postId || false;
    10 
    36 
    11 		if ( ! t.I('wp-temp-form-div') ) {
    37 		if ( ! t.I( 'wp-temp-form-div' ) ) {
    12 			div = document.createElement('div');
    38 			div = document.createElement( 'div' );
    13 			div.id = 'wp-temp-form-div';
    39 			div.id = 'wp-temp-form-div';
    14 			div.style.display = 'none';
    40 			div.style.display = 'none';
    15 			respond.parentNode.insertBefore(div, respond);
    41 			respond.parentNode.insertBefore( div, respond );
    16 		}
    42 		}
    17 
    43 
    18 		comm.parentNode.insertBefore(respond, comm.nextSibling);
    44 		comm.parentNode.insertBefore( respond, comm.nextSibling );
    19 		if ( post && postId )
    45 		if ( post && postId ) {
    20 			post.value = postId;
    46 			post.value = postId;
       
    47 		}
    21 		parent.value = parentId;
    48 		parent.value = parentId;
    22 		cancel.style.display = '';
    49 		cancel.style.display = '';
    23 
    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 		 */
    24 		cancel.onclick = function() {
    56 		cancel.onclick = function() {
    25 			var t = addComment, temp = t.I('wp-temp-form-div'), respond = t.I(t.respondId);
    57 			var t       = addComment,
       
    58 				temp    = t.I( 'wp-temp-form-div' ),
       
    59 				respond = t.I( t.respondId );
    26 
    60 
    27 			if ( ! temp || ! respond )
    61 			if ( ! temp || ! respond ) {
    28 				return;
    62 				return;
       
    63 			}
    29 
    64 
    30 			t.I('comment_parent').value = '0';
    65 			t.I( 'comment_parent' ).value = '0';
    31 			temp.parentNode.insertBefore(respond, temp);
    66 			temp.parentNode.insertBefore( respond, temp );
    32 			temp.parentNode.removeChild(temp);
    67 			temp.parentNode.removeChild( temp );
    33 			this.style.display = 'none';
    68 			this.style.display = 'none';
    34 			this.onclick = null;
    69 			this.onclick = null;
    35 			return false;
    70 			return false;
    36 		};
    71 		};
    37 
    72 
    38 		try { t.I('comment').focus(); }
    73 		/*
    39 		catch(e) {}
    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 ) {}
    40 
   112 
    41 		return false;
   113 		return false;
    42 	},
   114 	},
    43 
   115 
    44 	I : function(e) {
   116 	/**
    45 		return document.getElementById(e);
   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 );
    46 	}
   126 	}
    47 };
   127 };