1/** Author Pham Khien ***/ 2jQuery('document').ready(function ($j) { 3 // If user in Back-End then return. Function only use in Front-End 4 if ($j('body.wp-admin').length > 0) { 5 return; 6 } 7 var commentForm = $j('#commentform'); // Find the comment form 8 9 var submitButton = $j('#commentform #submit'); 10 var imgLoading = ajaxCommentImgUrl + "loading.gif"; 11 submitButton.after('<img src="' + imgLoading + '" class="loading-comment" alt="Loading" style="display: none" />'); 12 var imgLoading = $j('.loading-comment'); 13 14 /*-------------------------------------------------------------------------- 15 Because theme 16 Twenty Fourteen, 17 Twenty Ten, 18 Twenty Fifteen, 19 Twenty Twelve, 20 Twenty Eleven, 21 Twenty Thirteen, 22 have different class name or id name 23 --------------------------------------------------------------------------*/ 24 function getElementCommentsTitle() { 25 var isCheck = $j('body').find("#comments .comments-title").length; 26 if (isCheck > 0) { 27 return ".comments-title"; 28 } else { 29 return "#comments-title" 30 } 31 } 32 33 function getElementCommentsList() { 34 var isCheck = $j('body').find("#comments .comment-list").length; 35 if (isCheck > 0) { 36 return ".comment-list"; 37 } else { 38 return ".commentlist" 39 } 40 } 41 42 function getElementCommentNavigation(above) { 43 var isCheck = $j('body').find("#comments .navigation").length; 44 if (isCheck > 0) { 45 return ".navigation"; 46 } else { 47 if (above) { 48 return "#comment-nav-above" 49 } else { 50 return "#comment-nav-below" 51 } 52 53 } 54 } 55 56 var elementCommentsTitle = getElementCommentsTitle(); 57 var elementCommentsList = getElementCommentsList(); 58 var elementCommentNavigationAbove = getElementCommentNavigation(true); 59 var elementCommentNavigationBelow = getElementCommentNavigation(false); 60 /*-------------------------------------------------------------------------- 61 End - Get element Title, List, Navigation for different theme 62 --------------------------------------------------------------------------*/ 63 64 $j("body").on("click", '#comments #respond #submit', function () { 65 /* Check validation of form with html5 */ 66 if (!$j("form")[0].checkValidity()) { 67 return; 68 } 69 /** 70 * When customer submit comment successfully then system (php code) will redirect to new url 71 * Get this url redirect after call ajax. Get param anchor for scroll 72 */ 73 var xhr; 74 var _orgAjax = jQuery.ajaxSettings.xhr; 75 jQuery.ajaxSettings.xhr = function () { 76 xhr = _orgAjax(); 77 return xhr; 78 }; 79 80 81 var formData = commentForm.serialize(); // Serialize and store form data to formData 82 var formUrl = commentForm.attr('action'); // Store url action of form to formUrl 83 84 submitButton.hide(); 85 imgLoading.show(); 86 87 // Ajax submit form to action 88 $j.ajax({ 89 url: formUrl, 90 data: formData, 91 type: 'POST', 92 93 error: function (XMLHttpRequest) { 94 if (XMLHttpRequest.status == 429) { 95 swal("Error!", "You are posting too quickly!", "error"); 96 } else if (XMLHttpRequest.status == 409) { 97 swal("Error!", "Duplicate comment!", "error"); 98 } else { 99 swal("Error!", "There was an error, please try again!", "error"); 100 } 101 }, 102 103 success: function (data, textStatus) { 104 105 var commentList = $j(elementCommentsList, data); 106 var commentTitle = $j(elementCommentsTitle, data); 107 var navigationAbove = $j('#comments >' + elementCommentNavigationAbove, data).first(); 108 if (elementCommentNavigationAbove == elementCommentNavigationBelow) { 109 // Theme Twenty Thirteen has only navigation below 110 var navigationBelow = $j('#comments >' + elementCommentNavigationBelow, data).eq(1); 111 if (navigationBelow.length <= 0) { 112 var navigationBelow = $j('#comments >' + elementCommentNavigationBelow, data).first(); 113 } 114 } else { 115 var navigationBelow = $j('#comments >' + elementCommentNavigationBelow, data).first(); 116 } 117 118 // When some required fields don't have value 119 if (commentTitle.length <= 0) { 120 swal("Error!", "Please fill the required fields or incorrect email!", "error"); 121 return; 122 } 123 124 /* When customer reply comment */ 125 if ($j('#comments > #respond').length <= 0 || $j(elementCommentsTitle).length <= 0) { 126 $j('#cancel-comment-reply-link').hide(); 127 $j('#comment_parent').attr('value', 0); 128 var formRespond = $j('#respond'); 129 } 130 131 /* Reset content of comment */ 132 $j('#comment').val(''); 133 if (typeof(formRespond) != "undefined" && formRespond !== null) { 134 $j(formRespond).find('#comment').val(''); 135 } 136 137 /* Reload comment-title, navigation, comment-list */ 138 if ($j(elementCommentsTitle).length > 0) { 139 $j(elementCommentsTitle).replaceWith(commentTitle); 140 } else { 141 $j('#comments').append(commentTitle); 142 } 143 144 if ($j('#comments >' + elementCommentNavigationAbove).length > 0) { 145 $j('#comments >' + elementCommentNavigationAbove).replaceWith(navigationAbove); 146 } else { 147 $j('#comments' + elementCommentsTitle).after(navigationAbove); 148 } 149 150 if ($j(elementCommentsList).length > 0) { 151 $j(elementCommentsList).replaceWith(commentList); 152 } else { 153 $j('#comments').append(commentList); 154 } 155 156 if ($j('#comments >' + elementCommentNavigationBelow).length > 0) { 157 $j('#comments >' + elementCommentNavigationBelow).replaceWith(navigationBelow);
158 } else { 159 $j('#comments' + elementCommentsList).after(navigationBelow); 160 } 161 162 /* Append form comment */ 163 if (typeof(formRespond) != "undefined" && formRespond !== null) { 164 $j('#comments').append(formRespond); 165 } 166 167 if (textStatus == "success") 168 swal("Success!", "Thanks for your comment. We appreciate your response!", "success"); 169 else 170 swal("Error!", "Please wait a while before posting your next comment!", "error"); 171 }, 172 complete: function (jqXHR, textStatus) { 173 if (jqXHR.status == 200) { 174 var urlRedirect = xhr.responseURL; 175 var anchorComment = $j.urlParam('anchor', urlRedirect); 176 scrollToCommentAnchor(anchorComment, true); 177 } else { 178 scrollToCommentAnchor(null, false); 179 } 180 181 /* Show - Hide button submit + image */ 182 submitButton.show(); 183 imgLoading.hide(); 184 185 } 186 187 }); 188 return false; 189 }); 190 191 function scrollToCommentAnchor(anchor, $statusComment) { 192 if ($statusComment && $j('#' + anchor).length > 0) { 193 $j('html, body').animate( 194 {scrollTop: $j('#' + anchor).offset().top - 100}, 195 '500', 'swing'); 196 } else if ($j("#respond").length > 0) { 197 $j('html, body').animate( 198 {scrollTop: $j("#respond").offset().top - 100}, 199 '500', 'swing'); 200 } 201 202 } 203 204 /** 205 * Function get value of param in url 206 * @param nameOfParam 207 * @param url 208 * @returns {*|number} 209 */ 210 $j.urlParam = function (nameOfParam, url) { 211 var results = new RegExp('[\?&]' + nameOfParam + '=([^&#]*)').exec(url); 212 if (typeof(results) != "undefined" && results !== null) { 213 return results[1]; 214 } else { 215 return null; 216 } 217 }; 218 219});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.