Advertisement
Guest User

Untitled

a guest
Oct 9th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.51 KB | None | 0 0
  1. // Save all the tasks
  2. var todoList = new Array();
  3.  
  4. // Function to display the box to add a new task
  5. function displayNewItem() {
  6. document.getElementById("main-add-button").style.display = "none";
  7. document.getElementById("cancel-button").style.display = "block";
  8. document.getElementById("new-item-creation").style.display = "block";
  9.  
  10. }
  11.  
  12. // Function to hide the box to add a new task
  13. function cancelAdding() {
  14. document.getElementById("main-add-button").style.display = "block";
  15. document.getElementById("cancel-button").style.display = "none";
  16. document.getElementById("new-item-creation").style.display = "none";
  17. document.getElementById("todoName").value = "";
  18. }
  19.  
  20. // Function to create a new task
  21. function createItem() {
  22. // if the task is correct
  23. if (!isTaskCorrect()) {
  24. return;
  25. }
  26.  
  27. // We get the elements placed in the form of the new task
  28. var todoName = document.getElementById("todoName").value;
  29. var todoDetails = document.getElementById("todoDetails").value;
  30. var todoDate = document.getElementById("todoDate").value;
  31.  
  32. // We add a new task by filling an anonymous object and putting it inside the array
  33. todoList.push({title: todoName, details: todoDetails, deadline: todoDate, done: false, unfolded: false});
  34.  
  35. // We get the ID of the new task
  36. var id = todoList.length -1;
  37.  
  38. var node = document.createElement("LI");
  39. node.classList.add("list-item");
  40.  
  41. // We store the ID in the HTML node
  42. node.value = id;
  43.  
  44. var titleDivNode = document.createElement("DIV");
  45. titleDivNode.value = id;
  46. titleDivNode.appendChild(document.createTextNode(todoName));
  47.  
  48. // We setup a listener in order to display the details when the item will be clicked
  49. node.addEventListener('click', function() {
  50. displayDetails(titleDivNode);
  51. }, false);
  52.  
  53. var deadlineDivNode = document.createElement("DIV");
  54. deadlineDivNode.appendChild(document.createTextNode(todoDate));
  55.  
  56. var actionsDivNode = document.createElement("DIV");
  57. var checkImg = document.createElement("IMG");
  58. checkImg.value = id;
  59.  
  60. // We setup a listener in order to check a task which is now done when the image will be clicked
  61. checkImg.addEventListener('click', function(e) {
  62. checkTask(e, node);
  63. // We stop the event propagation in order to avoid the trigger of the display details event
  64. e.stopPropagation();
  65. }, false);
  66.  
  67. var trashImg = document.createElement("IMG");
  68. trashImg.value = id;
  69.  
  70. // We setup a listener in order to delete a task when the image will be clicked
  71. trashImg.addEventListener('click', function(e) {
  72. deleteTask(e, node);
  73. // We stop the event propagation in order to avoid the trigger of the display details event
  74. e.stopPropagation();
  75. }, false);
  76.  
  77. // We prepare the images
  78. checkImg.src = "img/checkmark.png";
  79. checkImg.alt = "done";
  80. checkImg.width = 20;
  81. checkImg.style.zIndex = 999;
  82. trashImg.src = "img/trash.png";
  83. trashImg.alt = "delete";
  84. trashImg.width = 20;
  85. trashImg.style.zIndex = 999;
  86.  
  87. // We append all the elements together in order to have a new line in our list
  88. // We add both image in the actionsDivNode
  89. actionsDivNode.appendChild(checkImg);
  90. actionsDivNode.appendChild(trashImg);
  91.  
  92. // We append the title, the deadline and the actionsDivNode to a new row of the list
  93. node.appendChild(titleDivNode);
  94. node.appendChild(deadlineDivNode);
  95. node.appendChild(actionsDivNode);
  96.  
  97. // We append the new row of the list in the list
  98. document.getElementById("list").appendChild(node);
  99.  
  100. // We reset the value of the form for the new tasks
  101. resetForm();
  102. }
  103.  
  104. // Empty the fileds of the form
  105. function resetForm() {
  106. document.getElementById("todoName").value = "";
  107. document.getElementById("todoDetails").value = "";
  108. document.getElementById("todoDate").value = "";
  109. }
  110.  
  111. // Function to verify if all the fields have been filled for the new task
  112. function isTaskCorrect() {
  113. var todoName = document.getElementById("todoName").value;
  114.  
  115. if (todoName == '')
  116. return false;
  117.  
  118. var todoDetails = document.getElementById("todoDetails").value;
  119.  
  120. if (todoDetails == '')
  121. return false;
  122.  
  123. var todoDate = document.getElementById("todoDate").value;
  124. if (todoDate == '')
  125. return false;
  126.  
  127. return true;
  128. }
  129.  
  130. // Display the details of the task clicked below its title
  131. function displayDetails(elem) {
  132.  
  133. const index = elem.value;
  134.  
  135. if (!todoList[index].unfolded) {
  136. elem.innerHTML += "<div class='details'>" + todoList[index].details + "</div>";
  137.  
  138. } else {
  139. elem.innerHTML = todoList[index].title;
  140. }
  141. todoList[index].unfolded = !todoList[index].unfolded;
  142. }
  143.  
  144. // Check the task clicked
  145. function checkTask(event, parent) {
  146. const index = event.target.value;
  147.  
  148. todoList[index].done = !todoList[index].done;
  149.  
  150. if (todoList[index].done) {
  151. parent.classList.add("done");
  152. } else {
  153. parent.classList.remove("done");
  154. }
  155. }
  156.  
  157. // Delete the task clicked if the user confirms it
  158. function deleteTask(event, parent) {
  159. const index = event.target.value;
  160. if (!todoList[index].done) {
  161. if(window.confirm('Warning! You are about to delete an uncompleted task... Do you want to remove it anyway?')) {
  162. parent.remove();
  163. }
  164. } else {
  165. parent.remove();
  166. }
  167. }
  168.  
  169. // Delete all the tasks if the user confirms it
  170. function deleteAllTask() {
  171. var wantDelete = false;
  172. var safeDelete = true;
  173.  
  174. for (var i = 0; i < todoList.length; i++) {
  175. if (!todoList[i].done) {
  176. safeDelete = false;
  177. if (window.confirm('Warning! You are about to delete one or more uncompleted task(s)... Do you want to remove all of them anyway?')) {
  178. wantDelete = true;
  179. break;
  180. } else {
  181. break;
  182. }
  183. }
  184. }
  185. if (!safeDelete && !wantDelete)
  186. return;
  187.  
  188. // The array is emptied
  189. todoList = new Array();
  190. var list = document.getElementById("list");
  191.  
  192. // We delete all the HTML nodes contained by the list
  193. while (list.firstChild) {
  194. list.removeChild(list.firstChild);
  195. }
  196. }
  197.  
  198.  
  199.  
  200. function login() {
  201. // Retrieving of the data of the form
  202. var username = document.getElementById("username").value;
  203. var password = document.getElementById("password").value;
  204.  
  205. // We create an HTTP Request
  206. var xhttp = new XMLHttpRequest();
  207.  
  208. // We setup the function which will handle the response
  209. xhttp.onreadystatechange = function() {
  210. // If we received the response and everything is good so far
  211. if (this.readyState == XMLHttpRequest.DONE && this.status == 200) {
  212. // We get the JSON Object
  213. var resp = JSON.parse(xhttp.responseText);
  214. // We compare the value of the field "success" of the JSON Object
  215. if (resp.success == "true") {
  216. document.getElementById("login-form").style.display = "none";
  217. document.getElementById("main").style.display = "block";
  218. document.getElementById("error-message").style.visibility = "hidden";
  219. } else {
  220. document.getElementById("error-message").style.visibility = "visible";
  221. }
  222. }
  223. };
  224. // We open the HTTP request
  225. xhttp.open("POST", "http://mytodolist-env.vgnc2k3pmv.eu-west-1.elasticbeanstalk.com/login.jsp", true);
  226. // We setup the header
  227. xhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  228. // We send the credentials with the request
  229. xhttp.send("username=" + username + "&password=" + password);
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement