Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. var todoContainer = document.getElementById("todo-container");
  2. var todos = [];
  3. function addTodo(todo){
  4. todo = todo || {};
  5. if (typeof todo.label !== "string"){
  6. throw new Error("todo.label must be a string");
  7. }
  8. if (typeof todo.done !== "boolean"){
  9. throw new Error("todo.done must be a boolean");
  10. }
  11.  
  12. todos.push(todo);
  13.  
  14. const todoElem=document.createElement("div");
  15.  
  16. todoElem.className = "listitem";
  17. const todoText = document.createTextNode(todo.label);
  18. todoElem.id = todo.label;
  19.  
  20. if(todo.done){
  21. todoElem.style.textDecoration = "line-through";
  22. todoElem.style.textTransform = "lowercase";
  23. }
  24. else{
  25. todoElem.style.textTransform = "uppercase";
  26. }
  27. todoElem.appendChild(todoText);
  28.  
  29. todoElem.addEventListener("click", function(){
  30. todo.done = !todo.done;
  31. if(todo.done){
  32. todoElem.style.textDecoration ="line-through";
  33. todoElem.style.textTransform = "lowercase";
  34. }else{
  35. todoElem.style.textDecoration ="";
  36. todoElem.style.textTransform = "uppercase";
  37. }
  38. save();
  39. })
  40. todoContainer.appendChild(todoElem);
  41. save();
  42.  
  43.  
  44.  
  45. }
  46. function save(){
  47. const json = JSON.stringify(todos);
  48.  
  49. localStorage.setItem("todo-state",json);
  50. }
  51.  
  52. function rmTodo(rm_label){
  53.  
  54. todos = todos.filter(function(todo) {
  55. return !(todo.label === rm_label );})
  56.  
  57. todoContainer.removeChild( document.getElementById(rm_label));
  58. save();
  59.  
  60. }
  61.  
  62.  
  63. document.getElementById("remove-button").addEventListener("click", function(){
  64. const rmItem= document.getElementById("remove-input").value.trim();
  65. if(!rmItem){
  66. return;
  67. }
  68. if(rmItem === "all"){
  69. while (todoContainer.firstChild) {
  70. todoContainer.removeChild(todoContainer.firstChild);
  71. }
  72. todos=[];
  73. return;
  74. }
  75. rmTodo(rmItem);
  76. })
  77. document.getElementById("add-button").addEventListener("click", function(){
  78. const newTodo = document.getElementById("todo-input").value.trim();
  79. if(!newTodo){
  80. return;
  81. }
  82. addTodo({
  83. label: newTodo,
  84. done: false
  85. });
  86. })
  87.  
  88. const savedJson = localStorage.getItem("todo-state");
  89.  
  90. if(savedJson){
  91. const lastState = JSON.parse(savedJson);
  92. for(let idx =0; idx<Object.keys(lastState).length;idx+=1){
  93.  
  94. addTodo(lastState[idx]);
  95. }
  96. }else{
  97. addTodo({
  98. label: "CODE",
  99. done: false
  100. });
  101.  
  102. addTodo({
  103. label: "learn Chinese",
  104. done: true
  105. });
  106. }
  107. //save();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement