Pearlfromsu

js Test task

Jul 4th, 2024
1,003
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3.   <head>
  4.     <title>Hello World!</title>
  5.     <link rel="stylesheet" href="styles.css" />
  6.   </head>
  7.   <body>
  8.       <div>
  9.         <label for="text">Enter your Todo text</label>
  10.         <input id="text" placeholder="something"/>
  11.         <button onclick="parseTodo()">Add</button>
  12.       </div>
  13.       <div id="todolist"></div>
  14.       <script src="script.js"></script>
  15.   </body>
  16. </html>
  17.  
  18. let todos = []
  19. let ids = 0;
  20.  
  21. function parseTodo() {
  22.   let textInput = document.querySelector("#text");
  23.   addTodo(textInput.value)
  24. }
  25. function addTodo(text) {
  26.   todos.push({id: ++ids, text: text});
  27.   render();
  28. }
  29. function startEdit(id) {
  30.   todos = todos.map(todo => todo.id === id ? {id, isEditing: true, ...todo} : todo);
  31.   render();
  32. }
  33. function editTodo(id, newTodo) {
  34.   todos = todos.map(todo => todo.id === id ? {id, isEditing: false, ...newTodo} : todo);
  35.   render();
  36. }
  37. function removeTodo(id) {
  38.   todos = todos.filter(todo => todo.id !== id);
  39.   render();
  40. }
  41.  
  42. function render() { //Отрисовываем наши данные
  43.   let todolist = document.querySelector("#todolist"); //Получаем узел списка
  44.   console.log(...todos) //Любуемся в консоли на наши данные
  45.   todolist.innerHTML = ""; //Стираем текущий список
  46.   todos.forEach(todo => { //Идём по нашим тудушкам
  47.     let todoBox = document.createElement("div"); //Создаём контейнер тудушки
  48.     let removeButton = document.createElement("button"); //Создаём кнопку удаления тудушки
  49.    
  50.     removeButton.textContent = "X"; //Текст кнопки
  51.     removeButton.addEventListener("click", () => removeTodo(todo.id)) //При нажатии удаляем
  52.     todoBox.appendChild(removeButton); //Цепляем кнопку к контейнеру
  53.    
  54.     if(!todo?.isEditing) { //Если Тудушка сейчас не редактируется
  55.       let textLabel = document.createElement("a"); //Создаём элемент тега текста
  56.       textLabel.textContent = todo.text; //Заполняем текстом
  57.       todoBox.appendChild(textLabel); //Цепляем текст к нашему контейнеру
  58.      
  59.       let editButton = document.createElement("button"); //Создаём кнопку редактирования
  60.      
  61.       editButton.textContent = "Edit"; //Указываем текст
  62.       editButton.addEventListener("click", () => startEdit(todo.id)) //Начинаем редактирование при нажатии
  63.      
  64.       todoBox.appendChild(editButton); //цепляем кнопку редактирования к контейнеру тудушки
  65.     } else { //Если же тудушка сейчас редактируется
  66.       let editInput = document.createElement("input"); //Создаём поле ввода
  67.       editInput.value = todo.text; //Заполняем его изначально текстом самой тудушки
  68.       todoBox.appendChild(editInput); //Цепляем инпут к контейнеру
  69.      
  70.       let editButton = document.createElement("button"); //Создаём кнопку завершения редактирования
  71.      
  72.       editButton.textContent = "V"; //Текст кнопки
  73.       editButton.addEventListener("click", () => editTodo(todo.id, {text: editInput.value})) //Редактируем тудушку
  74.      
  75.       todoBox.appendChild(editButton); //Цепляем кнопку завершения редактирования к контейнеру
  76.     }
  77.     todolist.appendChild(todoBox) //Цепляем контейнер к списку
  78.   });
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment