Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. var todoList = {
  2. todos: [],
  3. addTodo: function(todoText) {
  4. this.todos.push({
  5. todoText: todoText,
  6. completed: false
  7. });
  8. },
  9. changeTodo: function(position, todoText) {
  10. this.todos[position].todoText = todoText;
  11. },
  12. deleteTodo: function(position) {
  13. this.todos.splice(position, 1);
  14. },
  15. toggleCompleted: function(position) {
  16. var todo = this.todos[position];
  17. todo.completed = !todo.completed;
  18. },
  19. toggleAll: function() {
  20. var totalTodos = this.todos.length;
  21. var completedTodos = 0;
  22.  
  23. // Get number of completed todos.
  24. for (var i = 0; i < totalTodos; i++) {
  25. if (this.todos[i].completed === true) {
  26. completedTodos++;
  27. }
  28. }
  29.  
  30. // Case 1: If everything’s true, make everything false.
  31. if (completedTodos === totalTodos) {
  32. for (var i = 0; i < totalTodos; i++) {
  33. this.todos[i].completed = false;
  34. }
  35. // Case 2: Otherwise, make everything true.
  36. } else {
  37. for (var i = 0; i < totalTodos; i++) {
  38. this.todos[i].completed = true;
  39. }
  40. }
  41. }
  42. };
  43.  
  44. var handlers = {
  45. addTodo: function() {
  46. var addTodoTextInput = document.getElementById('addTodoTextInput');
  47. todoList.addTodo(addTodoTextInput.value);
  48. addTodoTextInput.value = '';
  49. view.displayTodos();
  50. },
  51. changeTodo: function() {
  52. var changeTodoPositionInput = document.getElementById('changeTodoPositionInput');
  53. var changeTodoTextInput = document.getElementById('changeTodoTextInput');
  54. todoList.changeTodo(changeTodoPositionInput.valueAsNumber, changeTodoTextInput.value);
  55. changeTodoPositionInput.value = '';
  56. changeTodoTextInput.value = '';
  57. view.displayTodos();
  58. },
  59. deleteTodo: function() {
  60. var deleteTodoPositionInput = document.getElementById('deleteTodoPositionInput');
  61. todoList.deleteTodo(deleteTodoPositionInput.valueAsNumber);
  62. deleteTodoPositionInput.value = '';
  63. view.displayTodos();
  64. },
  65. toggleCompleted: function() {
  66. var toggleCompletedPositionInput = document.getElementById('toggleCompletedPositionInput');
  67. todoList.toggleCompleted(toggleCompletedPositionInput.valueAsNumber);
  68. toggleCompletedPositionInput.value = '';
  69. view.displayTodos();
  70. },
  71. toggleAll: function() {
  72. todoList.toggleAll();
  73. view.displayTodos();
  74. }
  75. };
  76.  
  77. var view = {
  78. displayTodos: function() {
  79. var todosUl = document.querySelector('ul');
  80. todosUl.innerHTML = '';
  81. for (var i = 0; i < todoList.todos.length; i++) {
  82. var todoLi = document.createElement('li');
  83. var todo = todoList.todos[i];
  84. var todoTextWithCompletion = '';
  85.  
  86. if (todo.completed === true) {
  87. todoTextWithCompletion = '(x) ' + todo.todoText;
  88. } else {
  89. todoTextWithCompletion = '( ) ' + todo.todoText;
  90. }
  91.  
  92.  
  93. todoLi.id = i;
  94. todoLi.textContent = todoTextWithCompletion;
  95. todoLi.appendChild(this.createDeleteButton());
  96. todosUl.appendChild(todoLi);
  97. }
  98. },
  99. createDeleteButton: function() {
  100. var deleteButton = document.createElement('button');
  101. deleteButton.textContent = 'Delete';
  102. deleteButton.className = 'deleteButton';
  103. return deleteButton;
  104. }
  105. };
  106.  
  107.  
  108. var todosUl = document.querySelector('ul');
  109.  
  110. todosUl.addEventListener('click', function() {});
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement