You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

271 lines
8.7 KiB

  1. /* globals NodeList, HTMLCollection */
  2. (function () {
  3. var postId = null;
  4. //#region Helpers
  5. function objectToUrl(obj) {
  6. var string = '';
  7. for (var prop in obj) {
  8. if (obj.hasOwnProperty(prop)) {
  9. string += prop + '=' + obj[prop].replace(/ /g, '+') + '&';
  10. }
  11. }
  12. return string.substring(0, string.length - 1);
  13. }
  14. var AsynObject = AsynObject ? AsynObject : {};
  15. AsynObject.ajax = function (url, callback) {
  16. var ajaxRequest = AsynObject.getAjaxRequest(callback);
  17. ajaxRequest.open("GET", url, true);
  18. ajaxRequest.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
  19. ajaxRequest.send(null);
  20. };
  21. AsynObject.postAjax = function (url, callback, data) {
  22. var ajaxRequest = AsynObject.getAjaxRequest(callback);
  23. ajaxRequest.open("POST", url, true);
  24. ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  25. ajaxRequest.send(objectToUrl(data));
  26. };
  27. AsynObject.getAjaxRequest = function (callback) {
  28. var ajaxRequest = new XMLHttpRequest();
  29. ajaxRequest.onreadystatechange = function () {
  30. if (ajaxRequest.readyState > 1 && ajaxRequest.status > 0) {
  31. callback(ajaxRequest.readyState, ajaxRequest.status, ajaxRequest.responseText);
  32. }
  33. };
  34. return ajaxRequest;
  35. };
  36. function hasClass(elem, className) {
  37. return new RegExp(' ' + className + ' ').test(' ' + elem.className + ' ');
  38. }
  39. function addClass(elem, className) {
  40. if (!hasClass(elem, className)) {
  41. elem.className += ' ' + className;
  42. }
  43. }
  44. function removeClass(elem, className) {
  45. var newClass = ' ' + elem.className.replace(/[\t\r\n]/g, ' ') + ' ';
  46. if (hasClass(elem, className)) {
  47. while (newClass.indexOf(' ' + className + ' ') >= 0) {
  48. newClass = newClass.replace(' ' + className + ' ', ' ');
  49. }
  50. elem.className = newClass.replace(/^\s+|\s+$/g, '');
  51. }
  52. }
  53. function toDOM(htmlString) {
  54. var wrapper = document.createElement('div');
  55. wrapper.innerHTML = htmlString;
  56. return wrapper.children;
  57. }
  58. function getParentsByAttribute(element, attr, value) {
  59. var arr = [];
  60. while (element) {
  61. element = element.parentNode;
  62. if (element.hasAttribute(attr) && element.getAttribute(attr) === value) {
  63. arr.push(element);
  64. }
  65. if (!element.parentNode.parentNode) {
  66. break;
  67. }
  68. }
  69. return arr;
  70. }
  71. Element.prototype.remove = function () {
  72. this.parentElement.removeChild(this);
  73. };
  74. NodeList.prototype.remove = HTMLCollection.prototype.remove = function () {
  75. for (var i = 0, len = this.length; i < len; i++) {
  76. if (this[i] && this[i].parentElement) {
  77. this[i].parentElement.removeChild(this[i]);
  78. }
  79. }
  80. };
  81. function slide(thisObj, direction, callback) {
  82. if (direction === "Up") {
  83. thisObj.style.height = '0px';
  84. } else {
  85. var clone = thisObj.cloneNode(true);
  86. clone.style.position = 'absolute';
  87. clone.style.visibility = 'hidden';
  88. clone.style.height = 'auto';
  89. addClass(clone, 'slideClone col-md-6 col-md-offset-3');
  90. document.body.appendChild(clone);
  91. var slideClone = document.getElementsByClassName("slideClone")[0];
  92. var newHeight = slideClone.clientHeight;
  93. slideClone.remove();
  94. thisObj.style.height = newHeight + 'px';
  95. if (callback) {
  96. setTimeout(function () {
  97. callback();
  98. }, 500);
  99. }
  100. }
  101. }
  102. //#endregion
  103. var endpoint = "/comment.ashx";
  104. function deleteComment(commentId, element) {
  105. if (confirm("Do you want to delete this comment?")) {
  106. AsynObject.postAjax(endpoint, function (state, status) {
  107. if (state === 4 && status === 200) {
  108. slide(element, "Up", function () {
  109. element.remove();
  110. });
  111. return;
  112. } else if (status !== 200) {
  113. alert("Something went wrong. Please try again");
  114. }
  115. }, {
  116. mode: "delete",
  117. postId: postId,
  118. commentId: commentId,
  119. token: document.querySelector("[data-token]").getAttribute("data-token")
  120. });
  121. }
  122. }
  123. function approveComment(commentId, element) {
  124. AsynObject.postAjax(endpoint, function (state, status) {
  125. if (state === 4 && status === 200) {
  126. element.remove();
  127. return;
  128. } else if (status !== 200) {
  129. alert("Something went wrong. Please try again");
  130. }
  131. }, {
  132. mode: "approve",
  133. postId: postId,
  134. commentId: commentId,
  135. token: document.querySelector("[data-token]").getAttribute("data-token")
  136. });
  137. }
  138. function saveComment(name, email, website, content, callback) {
  139. if (localStorage) {
  140. localStorage.setItem("name", name);
  141. localStorage.setItem("email", email);
  142. localStorage.setItem("website", website);
  143. }
  144. AsynObject.postAjax(endpoint, function (state, status, data) {
  145. var elemStatus = document.getElementById("status");
  146. if (state === 4 && status === 200) {
  147. elemStatus.innerHTML = "Your comment has been added";
  148. removeClass(elemStatus, "alert-danger");
  149. addClass(elemStatus, "alert-success");
  150. document.getElementById("commentcontent").value = "";
  151. var comment = toDOM(data)[0];
  152. comment.style.height = "0px";
  153. var elemComments = document.getElementById("comments");
  154. elemComments.appendChild(comment);
  155. slide(comment, "Down");
  156. callback(true);
  157. return;
  158. } else if (status !== 200) {
  159. addClass(elemStatus, "alert-danger");
  160. elemStatus.innerText = "Unable to add comment";
  161. callback(false);
  162. }
  163. }, {
  164. mode: "save",
  165. postId: postId,
  166. name: name,
  167. email: email,
  168. website: website,
  169. content: content,
  170. token: document.querySelector("[data-token]").getAttribute("data-token"),
  171. });
  172. }
  173. function initialize() {
  174. postId = document.querySelector("[itemprop=blogPost]").getAttribute("data-id");
  175. var email = document.getElementById("commentemail");
  176. var name = document.getElementById("commentname");
  177. var website = document.getElementById("commenturl");
  178. var content = document.getElementById("commentcontent");
  179. var commentForm = document.getElementById("commentform");
  180. var allComments = document.querySelectorAll("[itemprop=comment]");
  181. for (var i = 0; i < allComments.length; ++i) {
  182. allComments[i].style.height = allComments[i].clientHeight + 'px';
  183. }
  184. commentForm.onsubmit = function (e) {
  185. e.preventDefault();
  186. var button = e.target;
  187. button.setAttribute("disabled", true);
  188. saveComment(name.value, email.value, website.value, content.value, function () {
  189. button.removeAttribute("disabled");
  190. });
  191. };
  192. website.addEventListener("keyup", function (e) {
  193. var w = e.target;
  194. if (w.value.trim().length >= 4 && w.value.indexOf("http") === -1) {
  195. w.value = "http://" + w.value;
  196. }
  197. });
  198. window.addEventListener("click", function (e) {
  199. var tag = e.target;
  200. if (hasClass(tag, "deletecomment")) {
  201. var comment = getParentsByAttribute(tag, "itemprop", "comment")[0];
  202. deleteComment(comment.getAttribute("data-id"), comment);
  203. }
  204. if (hasClass(tag, "approvecomment")) {
  205. var comment = getParentsByAttribute(tag, "itemprop", "comment")[0];
  206. approveComment(comment.getAttribute("data-id"), tag);
  207. }
  208. });
  209. if (localStorage) {
  210. email.value = localStorage.getItem("email");
  211. website.value = localStorage.getItem("website");
  212. if (name.value.length === 0) {
  213. name.value = localStorage.getItem("name");
  214. }
  215. }
  216. }
  217. if (document.getElementById("commentform")) {
  218. initialize();
  219. }
  220. })();