Advertisement
Guest User

js

a guest
Dec 14th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.42 KB | None | 0 0
  1. var stateCheckbox = document.getElementsByClassName("state");
  2.  
  3. var addCheckbox = document.querySelector("input[name=add]");
  4.  
  5. var addButton = document.querySelector("input[name=addButton]");
  6.  
  7. var editButton = document.getElementsByName("edit");
  8.  
  9. var trashIcons = document.getElementsByName("trash");
  10.  
  11. var priorityCheckboxes = document.getElementsByName("priority");
  12.  
  13. let username = document.querySelector('#register input[name=username]');
  14. username.addEventListener('keyup', validateUsername, false);
  15.  
  16. let password = document.querySelector('#register input[name=password]');
  17. let repeat = document.querySelector('#register input[name=repeat]');
  18. password.addEventListener('keyup', validatePassword, false);
  19. repeat.addEventListener('keyup', validateRepeat.bind(repeat, password), false);
  20.  
  21. let register = document.querySelector('#register form');
  22. register.addEventListener('submit', validateRegister, false);
  23.  
  24. function validateUsername() {
  25. if (!/^[a-z]{3,}$/.test(this.value))
  26. this.classList.add('invalid');
  27. else
  28. this.classList.remove('invalid');
  29. }
  30.  
  31. function validatePassword(other) {
  32. if (!/^.{8,}$/.test(this.value))
  33. this.classList.add('invalid');
  34. else
  35. this.classList.remove('invalid');
  36. }
  37.  
  38. function validateRepeat(password) {
  39. if (this.value !== password.value)
  40. this.classList.add('invalid');
  41. else
  42. this.classList.remove('invalid');
  43. }
  44.  
  45. function validateRegister(event) {
  46. let inputs = this.querySelectorAll('input');
  47. for (let i = 0; i < inputs.length; i++)
  48. if (inputs[i].classList.contains('invalid'))
  49. event.preventDefault();
  50. }
  51.  
  52. //Configure state checkboxes to update objective state with distinct colors
  53. for(var i = 0; i < stateCheckbox.length; i++){
  54. stateCheckbox[i].addEventListener('change', stateCheckboxConfiguration);
  55. }
  56.  
  57. function stateCheckboxConfiguration() {
  58. var objective = this.parentNode;
  59. var description = objective.children[1];
  60.  
  61. if(this.checked) {
  62. description.style.color = "gray";
  63. } else {
  64. description.style.color = "black";
  65. }
  66. }
  67.  
  68. //Configure trash icons to delete content
  69. for(var i = 0; i < trashIcons.length; i++){
  70. trashIcons[i].addEventListener('click', trashConfiguration);
  71. }
  72.  
  73. //Configure New List's Add button
  74. addButton.addEventListener("click", addButtonConfiguration);
  75.  
  76. function addButtonConfiguration() {
  77. var article = this.parentNode.parentNode;
  78. var newArticle = document.createElement("ARTICLE");
  79.  
  80. //Create new header to the list's new title
  81. var headerList = document.createElement("LI");
  82. var TitleBox = this.parentNode.children[0];
  83. var listTitle = document.createElement("H3");
  84. listTitle.innerText = TitleBox.value;
  85.  
  86. //Create new Edit button
  87. var editButton = document.createElement("INPUT");
  88. editButton.id = "editButton";
  89. editButton.type = "button";
  90. editButton.name = "edit";
  91. editButton.value = "Edit";
  92.  
  93. editButton.addEventListener("click", editButtonConfiguration);
  94.  
  95. //Add header elements
  96. headerList.appendChild(listTitle);
  97. headerList.appendChild(editButton);
  98.  
  99. var objectivesList = article.children[1];
  100. var newObjectivesList = document.createElement("UL");
  101.  
  102. for(var i = 0; i < objectivesList.childElementCount-1; i++){
  103. //Create objective <li>
  104. var objectiveLine = document.createElement("LI");
  105.  
  106. //Create state checkbox
  107. var stateCheckbox = document.createElement("INPUT");
  108. stateCheckbox.class = "state";
  109. stateCheckbox.type = "checkbox";
  110. stateCheckbox.name = "state";
  111. stateCheckbox.value = "State";
  112.  
  113. //Configure checkbox for state changes
  114. stateCheckbox.addEventListener( 'change', stateCheckboxConfiguration);
  115.  
  116. //Create new description
  117. var description = document.createElement("P");
  118. description.innerText = objectivesList.children[i].children[0].value;
  119.  
  120. //Add objective info to <li>
  121. objectiveLine.appendChild(stateCheckbox);
  122. objectiveLine.appendChild(description);
  123.  
  124. //Add <li> to the List
  125. newObjectivesList.appendChild(objectiveLine);
  126. }
  127. //Create new trash icon for delete
  128. var trash = document.createElement("IMG");
  129. trash.src = "images/site/trash_icon.png";
  130. trash.name = "trash";
  131.  
  132. trash.addEventListener("click", trashConfiguration);
  133.  
  134. //Add header, list & trash to the new article
  135. newArticle.appendChild(headerList);
  136. newArticle.appendChild(newObjectivesList);
  137. newArticle.appendChild(trash);
  138.  
  139. //Add the new list to the rest
  140. var lists = document.getElementById("lists");
  141. lists.appendChild(newArticle);
  142.  
  143. //Reset New List interface
  144. article.children[0].children[0].value = null;
  145. for(var i = 0; i < objectivesList.childElementCount-1; i+=0)
  146. objectivesList.removeChild(objectivesList.children[0]);
  147.  
  148. article.children[1].children[0].children[1].value = null;
  149.  
  150. //Add new list to the database
  151. let request = new XMLHttpRequest();
  152. request.onload = function() {
  153. var last_id = JSON.parse(this.responseText);
  154. newArticle.id = last_id;
  155.  
  156. //Add objectives to the new list in the database
  157. for(var i = 0; i < newObjectivesList.childElementCount; i++){
  158. let request2 = new XMLHttpRequest();
  159. request2.onload = function() {
  160. var args = JSON.parse(this.responseText);
  161. newObjectivesList.children[args[1]].id = args[0];
  162. };
  163. request2.open("get", "addObjective.php?list=" + last_id +
  164. "&description=" + newObjectivesList.children[i].children[1].innerText +
  165. "&state=" + 0 +
  166. "&i=" + i, true);
  167.  
  168. request2.send();
  169. }
  170. };
  171. request.open("get", "addList.php?user=" + 1 +
  172. "&title=" + listTitle.innerText +
  173. "&priority=" + 0, true);
  174.  
  175. request.send();
  176. }
  177.  
  178. //Configure New List's checkboxes to create new objectives
  179. addCheckbox.addEventListener("change", addCheckboxConfiguration);
  180.  
  181. function addCheckboxConfiguration() {
  182. var objective = this.parentNode;
  183. var objectiveList = objective.parentNode;
  184. var textInput = objective.children[1];
  185.  
  186. //Create TextInput for the description
  187. var description = textInput.value;
  188. var ObjectiveBox = document.createElement("INPUT");
  189. ObjectiveBox.class = "objectiveText";
  190. ObjectiveBox.type = "text";
  191. ObjectiveBox.name = "objective";
  192. ObjectiveBox.value = description;
  193.  
  194. //Reset the TextInput
  195. textInput.value = null;
  196.  
  197. //Create trash icon
  198. var trash = document.createElement("IMG");
  199. trash.src = "images/site/trash_icon.png";
  200. trash.id = "trash";
  201.  
  202. trash.addEventListener("click", trashConfiguration);
  203.  
  204. //Create new <li>
  205. var objectiveLI = document.createElement("LI");
  206. objectiveLI.appendChild(ObjectiveBox);
  207. objectiveLI.appendChild(trash);
  208.  
  209. objectiveList.insertBefore(objectiveLI, objectiveList.lastElementChild);
  210. this.checked = false;
  211. }
  212.  
  213. //Configure Edit button to create the edit interface
  214. for(var i = 0; i < editButton.length; i++){
  215. editButton[i].addEventListener("click", editButtonConfiguration);
  216. }
  217.  
  218. function editButtonConfiguration() {
  219. if(this.value == "Edit"){
  220. var article = this.parentNode.parentNode;
  221. var title = article.children[0].children[0].innerText;
  222.  
  223. //New editable Title
  224. var TitleBox = document.createElement("INPUT");
  225. TitleBox.class = "titleText";
  226. TitleBox.type = "text";
  227. TitleBox.name = "title";
  228. TitleBox.value = title;
  229.  
  230. //Add new TextInput and Delete old Title header
  231. article.children[0].removeChild(article.children[0].children[0]);
  232. article.children[0].insertBefore(TitleBox, article.children[0].firstChild);
  233.  
  234. //Remove Edit button for correct layout
  235. article.children[0].removeChild(article.children[0].children[1]);
  236.  
  237. //Create the Save button
  238. var saveButton = document.createElement("INPUT");
  239. saveButton.id = "saveButton";
  240. saveButton.type = "button";
  241. saveButton.name = "save";
  242. saveButton.value = "Save";
  243.  
  244. //Add the Save button
  245. article.children[0].appendChild(saveButton);
  246.  
  247. //Change each objective to an editable input
  248. var objectivesList = article.children[1];
  249.  
  250. for(var i = 0; i < objectivesList.childElementCount; i++){
  251. //Remove state checkbox
  252. objectivesList.children[i].removeChild(objectivesList.children[i].children[0]);
  253.  
  254. //Create TextInput for the description
  255. var description = objectivesList.children[i].children[0].innerText;
  256. var ObjectiveBox = document.createElement("INPUT");
  257. ObjectiveBox.class = "objectiveText";
  258. ObjectiveBox.type = "text";
  259. ObjectiveBox.name = "objective";
  260. ObjectiveBox.value = description;
  261.  
  262. //Create trash icon
  263. var trash = document.createElement("IMG");
  264. trash.src = "images/site/trash_icon.png";
  265. trash.id = "trash";
  266.  
  267. trash.addEventListener("click", trashConfiguration);
  268.  
  269. //Remove old description
  270. objectivesList.children[i].removeChild(objectivesList.children[i].children[0]);
  271.  
  272. //Add new editable TextInput & trash icon
  273. objectivesList.children[i].appendChild(ObjectiveBox);
  274. objectivesList.children[i].appendChild(trash);
  275. }
  276. //Create New Line interface
  277. var newLine = document.createElement("LI");
  278. var addCheckbox = document.createElement("INPUT");
  279. addCheckbox.class = "add";
  280. addCheckbox.type = "checkbox";
  281. addCheckbox.name = "add";
  282. addCheckbox.value = "Add";
  283.  
  284. addCheckbox.addEventListener("change", addCheckboxConfiguration);
  285.  
  286. var textInput = document.createElement("INPUT");
  287. textInput.class = "text";
  288. textInput.type = "text";
  289. textInput.name = "description";
  290. textInput.placeholder = "Write here...";
  291.  
  292. newLine.appendChild(addCheckbox);
  293. newLine.appendChild(textInput);
  294.  
  295. //Add New Line interface
  296. objectivesList.appendChild(newLine);
  297.  
  298. //Configure Save button to save new content
  299. saveButton.addEventListener("click", saveButtonConfiguration);
  300. }
  301. }
  302.  
  303. function trashConfiguration() {
  304. var element = this.parentNode;
  305. var parent = element.parentNode;
  306.  
  307. if(element.tagName == "ARTICLE"){
  308. let request = new XMLHttpRequest();
  309. request.open("get", "deleteList.php?id=" + element.id, true);
  310. request.send();
  311. }else if(element.tagName == "LI"){
  312. let request = new XMLHttpRequest();
  313. request.open("get", "deleteObjective.php?id=" + element.id, true);
  314. request.send();
  315. }
  316.  
  317. parent.removeChild(element);
  318. }
  319.  
  320. function saveButtonConfiguration() {
  321. //Create new header to the list's new title
  322. var TitleBox = this.parentNode.firstChild;
  323. var article = this.parentNode.parentNode;
  324. var listTitle = document.createElement("H3");
  325. listTitle.innerText = TitleBox.value;
  326.  
  327. //Remove TextInput for the title
  328. article.children[0].removeChild(article.children[0].children[0]);
  329.  
  330. //Add new Title header
  331. article.children[0].insertBefore(listTitle, article.children[0].firstChild);
  332.  
  333. var objectivesList = article.children[1];
  334.  
  335. for(var i = 0; i < objectivesList.childElementCount-1; i++){
  336. //Create state checkbox
  337. var stateCheckbox = document.createElement("INPUT");
  338. stateCheckbox.class = "state";
  339. stateCheckbox.type = "checkbox";
  340. stateCheckbox.name = "state";
  341. stateCheckbox.value = "State";
  342.  
  343. //Configure checkbox for state changes
  344. stateCheckbox.addEventListener( 'change', stateCheckboxConfiguration);
  345.  
  346. //Create new description
  347. var description = document.createElement("P");
  348. description.innerText = objectivesList.children[i].children[0].value;
  349.  
  350. //Remove TextInput for the description & trash icon
  351. objectivesList.children[i].removeChild(objectivesList.children[i].children[0]);
  352. objectivesList.children[i].removeChild(objectivesList.children[i].children[0]);
  353.  
  354. //Add new checkbox and description
  355. objectivesList.children[i].appendChild(stateCheckbox);
  356. objectivesList.children[i].appendChild(description);
  357. }
  358. //Remove new line interface
  359. objectivesList.removeChild(objectivesList.children[objectivesList.childElementCount-1]);
  360.  
  361. //Create new Edit button
  362. var editButton = document.createElement("INPUT");
  363. editButton.id = "editButton";
  364. editButton.type = "button";
  365. editButton.name = "edit";
  366. editButton.value = "Edit";
  367.  
  368. editButton.addEventListener("click", editButtonConfiguration);
  369.  
  370. //Remove Save button
  371. article.children[0].removeChild(article.children[0].children[1]);
  372.  
  373. //Add new Edit button
  374. article.children[0].appendChild(editButton);
  375.  
  376. //Update new List's info in the database
  377. let request = new XMLHttpRequest();
  378. request.onload = function() {
  379. //Update objectives in the database
  380. for(var i = 0; i < objectivesList.childElementCount; i++){
  381. if(objectivesList.children[i].id > 0){
  382. let request2 = new XMLHttpRequest();
  383. request2.onload = function() {
  384. var arg = JSON.parse(this.responseText);
  385. objectivesList.children[arg].id = arg;
  386. };
  387. request2.open("get", "updateObjective.php?id=" + objectivesList.children[i].id +
  388. "&description=" + objectivesList.children[i].children[1].innerText +
  389. "&state=" + 0 +
  390. "&i=" + i, true);
  391. request2.send();
  392. } else{
  393. let request2 = new XMLHttpRequest();
  394. request2.onload = function() {
  395. var args = JSON.parse(this.responseText);
  396. objectivesList.children[args[1]].id = args[0];
  397. };
  398. request2.open("get", "addObjective.php?list=" + objectivesList.parentNode.id +
  399. "&description=" + objectivesList.children[i].children[1].innerText +
  400. "&state=" + 0 +
  401. "&i=" + i, true);
  402. request2.send();
  403. }
  404. }
  405. };
  406. request.open("get", "updateList.php?list=" + article.id +
  407. "&title=" + listTitle.innerText, true);
  408.  
  409. request.send();
  410. }
  411.  
  412. //Configure priority checkboxes
  413. for(var i = 0; i < priorityCheckboxes.length; i++){
  414. priorityCheckboxes[i].addEventListener("change", priorityCheckboxConfiguration);
  415. }
  416.  
  417. function priorityCheckboxConfiguration() {
  418. let request = new XMLHttpRequest();
  419. var article = this.parentNode.parentNode;
  420.  
  421. if(this.checked){
  422. request = new XMLHttpRequest();
  423. request.onload = function() {
  424. var priority = JSON.parse(this.responseText);
  425. if(priority == 1){
  426. var importantSection = document.getElementById("important");
  427. importantSection.appendChild(article);
  428. } else {
  429. var generalSection = document.getElementById("lists");
  430. generalSection.appendChild(article);
  431. }
  432. };
  433. request.open("get", "updatePriority.php?list=" + article.id +
  434. "&priority=" + 1, true);
  435. request.send();
  436. } else {
  437. request = new XMLHttpRequest();
  438. request.onload = function() {
  439. var priority = JSON.parse(this.responseText);
  440. if(priority == 1){
  441. var importantSection = document.getElementById("important");
  442. importantSection.appendChild(article);
  443. } else {
  444. var generalSection = document.getElementById("lists");
  445. generalSection.appendChild(article);
  446. }
  447. };
  448. request.open("get", "updatePriority.php?list=" + article.id +
  449. "&priority=" + 0, true);
  450. request.send();
  451. }
  452. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement