Advertisement
Guest User

Untitled

a guest
Nov 17th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     this.todos.forEach(function(todo) {
  25.       if (todo.completed === true) {
  26.         completedTodos++;
  27.       }
  28.     });
  29.  
  30.     this.todos.forEach(function(todo) {
  31.       // Case 1: If everything’s true, make everything false.
  32.       if (completedTodos === totalTodos) {
  33.         todo.completed = false;
  34.       // Case 2: Otherwise, make everything true.
  35.       } else {
  36.         todo.completed = true;
  37.       }
  38.     });
  39.   }
  40. };
  41.  
  42. var handlers = {
  43.   addTodo: function() {
  44.     var addTodoTextInput = document.getElementById('addTodoTextInput');
  45.     todoList.addTodo(addTodoTextInput.value);
  46.     addTodoTextInput.value = '';
  47.     view.displayTodos();
  48.   },
  49.   changeTodo: function() {
  50.     var changeTodoPositionInput = document.getElementById('changeTodoPositionInput');
  51.     var changeTodoTextInput = document.getElementById('changeTodoTextInput');
  52.     todoList.changeTodo(changeTodoPositionInput.valueAsNumber, changeTodoTextInput.value);
  53.     changeTodoPositionInput.value = '';
  54.     changeTodoTextInput.value = '';
  55.     view.displayTodos();
  56.   },
  57.   deleteTodo: function(position) {
  58.     todoList.deleteTodo(position);
  59.     view.displayTodos();
  60.   },
  61.   toggleCompleted: function() {
  62.     var toggleCompletedPositionInput = document.getElementById('toggleCompletedPositionInput');
  63.     todoList.toggleCompleted(toggleCompletedPositionInput.valueAsNumber);
  64.     toggleCompletedPositionInput.value = '';
  65.     view.displayTodos();
  66.   },
  67.   toggleAll: function() {
  68.     todoList.toggleAll();
  69.     view.displayTodos();
  70.   }  
  71. };
  72.  
  73. var view = {
  74.   displayTodos: function() {
  75.     var todosUl = document.querySelector('ul');
  76.     todosUl.innerHTML = '';
  77.    
  78.     todoList.todos.forEach(function(todo,position) {
  79.       var todoLi = document.createElement('li');
  80.       var todoTextWithCompletion = '';
  81.  
  82.       if (todo.completed === true) {
  83.         todoTextWithCompletion = '(x) ' + todo.todoText;
  84.       } else {
  85.         todoTextWithCompletion = '( ) ' + todo.todoText;
  86.       }
  87.      
  88.       todoLi.id = position;
  89.       todoLi.textContent = todoTextWithCompletion;
  90.       todoLi.appendChild(this.createDeleteButton());
  91.       todosUl.appendChild(todoLi);      
  92.     }, this);
  93.   },
  94.   createDeleteButton: function() {
  95.     var deleteButton = document.createElement('button');
  96.     deleteButton.textContent = "Delete";
  97.     deleteButton.className = "deleteButton";
  98.     return deleteButton;
  99.   }
  100. };
  101.  
  102. var todoUl = document.querySelector('ul');
  103.  
  104. todoUl.addEventListener('click', function(event) {
  105.   var elementClicked = event.target;
  106.  
  107.   if (elementClicked.className === "deleteButton") {
  108.     handlers.deleteTodo(elementClicked.parentNode.id);
  109.   }
  110. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement