From 1299946b9797022d7ca4e1f749a2eec3cefbc4bb Mon Sep 17 00:00:00 2001 From: Benjamin-Re Date: Sat, 23 Apr 2022 11:35:13 +0200 Subject: [PATCH] Finish data persistence --- dist/main.js | 6 +-- src/modules/controller.js | 42 ++++++++++------- src/modules/entities.js | 9 +++- src/modules/model.js | 95 ++++++++++++++++++++++++--------------- 4 files changed, 98 insertions(+), 54 deletions(-) diff --git a/dist/main.js b/dist/main.js index d531833..8174f09 100644 --- a/dist/main.js +++ b/dist/main.js @@ -157,7 +157,7 @@ eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _sty /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { "use strict"; -eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"Controller\": () => (/* binding */ Controller)\n/* harmony export */ });\n/* harmony import */ var _view__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./view */ \"./src/modules/view.js\");\n/* harmony import */ var _model_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./model.js */ \"./src/modules/model.js\");\n\r\n\r\n\r\nclass Controller {\r\n model;\r\n view;\r\n todoInputField;\r\n addTodoButton;\r\n projectInputField;\r\n addProjectButton;\r\n currentProjectId;\r\n allCurrentProjects;\r\n showPopupButton;\r\n popup;\r\n todoDate;\r\n todoPrio;\r\n isUpdate;\r\n idToUpdate;\r\n\r\n constructor() {\r\n this.model = new _model_js__WEBPACK_IMPORTED_MODULE_1__.Model();\r\n this.view = new _view__WEBPACK_IMPORTED_MODULE_0__.View();\r\n this.todoInputField = document.querySelector(\"#todoInputField\");\r\n this.addTodoButton = document.querySelector(\".addTodoButton\");\r\n this.projectInputField = document.querySelector(\"#projectsInputField\");\r\n this.addProjectButton = document.querySelector(\".addProjectButton\");\r\n this.currentProjectId = 0;\r\n this.showPopupButton = document.querySelector(\".showPopupButton\");\r\n this.popup = document.querySelector(\".popup\");\r\n this.todoDate = document.querySelector(\"#dateInput\");\r\n this.todoPrio = document.querySelector(\"#prio\");\r\n this.isUpdate = false;\r\n this.idToUpdate = -1;\r\n }\r\n\r\n initializeUI() {\r\n // Load stored data when the page is first loaded\r\n let data = this.getData();\r\n console.log(data);\r\n console.log(this.model.getProjects());\r\n this.view.displayProjects(data); \r\n \r\n // this.view.displayProjects(this.model.getProjects());\r\n this.listenForProjects();\r\n this.listenForProjectAdd();\r\n this.listenForTodoAdd();\r\n this.listenForShowPopupButton();\r\n }\r\n\r\n listenForProjects() {\r\n this.allCurrentProjects = Array.from(document.querySelectorAll(\"li\"));\r\n this.allCurrentProjects?.forEach((project) => {\r\n project.addEventListener(\"click\", (e) => {\r\n this.currentProjectId = e.target.getAttribute(\"id\");\r\n // Display all todos for active project\r\n this.view.displayTodos(\r\n this.model.getProject(this.currentProjectId).getTodos()\r\n );\r\n this.listenForTodos();\r\n this.listenForEdit();\r\n this.listenForDelete();\r\n });\r\n });\r\n }\r\n\r\n listenForProjectAdd() {\r\n this.addProjectButton.addEventListener(\"click\", () => {\r\n this.addProjectHelper();\r\n });\r\n // Add keyboard listener but to the input field not to the projects themselves\r\n this.projectInputField.addEventListener(\"keypress\", (e) => {\r\n if (e.key === \"Enter\") {\r\n this.addProjectHelper();\r\n }\r\n });\r\n }\r\n\r\n addProjectHelper() {\r\n let projectTitle = this.projectInputField.value;\r\n this.model.addProject(projectTitle);\r\n this.view.displayProjects(this.model.getProjects());\r\n // Save your current data\r\n this.saveData();\r\n this.listenForProjects();\r\n this.listenForDescriptions();\r\n this.listenForEdit();\r\n }\r\n\r\n listenForTodoAdd() {\r\n this.addTodoButton.addEventListener(\"click\", () => {\r\n this.addTodoHelper();\r\n });\r\n // Listen for keypress also\r\n this.todoInputField.addEventListener(\"keypress\", (e) => {\r\n if (e.key === \"Enter\") {\r\n this.addTodoHelper();\r\n }\r\n });\r\n }\r\n\r\n addTodoHelper() {\r\n let todoTitle = this.todoInputField.value;\r\n let todoDate = this.todoDate.value;\r\n let todoPrio = this.todoPrio.value;\r\n if(this.isUpdate){\r\n // If we update dont create a new todo but overwrite the old one\r\n // The id of the todo to update is saved in a global variable\r\n // Also get the description as the popup is showing at this flow\r\n let todoDescription = document.querySelector(`tr[id='${this.idToUpdate}']`).nextElementSibling.firstElementChild.firstElementChild.value;\r\n this.model.updateTodo(this.currentProjectId, this.idToUpdate, todoTitle, todoDate, todoPrio, todoDescription); \r\n this.isUpdate=false;\r\n } else {\r\n this.model.addTodoToProject(\r\n this.currentProjectId,\r\n todoTitle,\r\n todoDate,\r\n todoPrio\r\n );\r\n }\r\n // Display all todos after the change\r\n this.view.displayTodos(\r\n this.model.getProject(this.currentProjectId).getTodos()\r\n );\r\n this.popup.classList.remove(\"active\");\r\n this.listenForDescriptions();\r\n this.listenForTodos();\r\n this.listenForDelete();\r\n this.listenForEdit();\r\n // Save the current data\r\n this.saveData();\r\n }\r\n\r\n // Listen for delete icon\r\n listenForDelete() {\r\n const deleteIcons = Array.from(document.querySelectorAll(\".trash-icon\"));\r\n deleteIcons.forEach((icon) => {\r\n icon.parentNode.addEventListener(\"click\", () => {\r\n // Get id of corresponding todo\r\n let thisTodoId =\r\n +icon.parentNode.parentNode.previousElementSibling.getAttribute(\"id\");\r\n this.model.getProject(this.currentProjectId).deleteTodo(thisTodoId);\r\n this.view.displayTodos(\r\n this.model.getProject(this.currentProjectId).getTodos()\r\n );\r\n });\r\n });\r\n }\r\n\r\n listenForEdit(){\r\n const editIcons = Array.from(document.querySelectorAll(\".edit-icon\"));\r\n editIcons.forEach((icon)=>{\r\n icon.addEventListener(\"click\", ()=>{\r\n // Set the global update flag to true so the add button handlers can know. \r\n this.isUpdate=true;\r\n // Get id of todo to edit and save it in global variable\r\n this.idToUpdate =\r\n +icon.parentNode.parentNode.previousElementSibling.getAttribute(\"id\");\r\n // Get data from the popup + descr\r\n this.popup.classList.add(\"active\");\r\n })\r\n })\r\n }\r\n\r\n listenForTodos() {\r\n const todos = Array.from(document.querySelectorAll(\"tr[id]\"));\r\n todos.forEach((todo) => {\r\n todo.addEventListener(\"click\", () => {\r\n todo.nextElementSibling.classList.toggle(\"popup\");\r\n this.listenForDescriptions();\r\n });\r\n });\r\n }\r\n\r\n listenForShowPopupButton() {\r\n this.showPopupButton.addEventListener(\"click\", () => {\r\n this.popup.classList.add(\"active\");\r\n });\r\n }\r\n\r\n listenForDescriptions() {\r\n const descriptions = Array.from(document.querySelectorAll(\"textarea\"));\r\n descriptions.forEach((description) => {\r\n description.addEventListener(\"keypress\", (e) => {\r\n if (e.key === \"Enter\") {\r\n // Save the description in the corresponding todo\r\n let correspondingTodoId =\r\n e.target.parentNode.parentNode.previousElementSibling.getAttribute(\r\n \"id\"\r\n );\r\n this.model.addDescriptionToTodo(\r\n this.currentProjectId,\r\n correspondingTodoId,\r\n e.target.value\r\n );\r\n }\r\n });\r\n });\r\n }\r\n\r\n // Store data whenever a project or todo is added\r\n saveData() {\r\n let data = JSON.stringify(this.model.getProjects());\r\n console.log(`stringified data: ${data}`);\r\n localStorage.setItem(\"projects\", data);\r\n }\r\n\r\n getData() {\r\n let data = localStorage.getItem(\"projects\");\r\n return JSON.parse(data);\r\n }\r\n\r\n}\r\n\r\n\r\n\n\n//# sourceURL=webpack://todo/./src/modules/controller.js?"); +eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"Controller\": () => (/* binding */ Controller)\n/* harmony export */ });\n/* harmony import */ var _view__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./view */ \"./src/modules/view.js\");\n/* harmony import */ var _model_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./model.js */ \"./src/modules/model.js\");\n/* harmony import */ var _entities__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./entities */ \"./src/modules/entities.js\");\n\r\n\r\n\r\n\r\nclass Controller {\r\n model;\r\n view;\r\n todoInputField;\r\n addTodoButton;\r\n projectInputField;\r\n addProjectButton;\r\n currentProjectId;\r\n allCurrentProjects;\r\n showPopupButton;\r\n popup;\r\n todoDate;\r\n todoPrio;\r\n isUpdate;\r\n idToUpdate;\r\n\r\n constructor() {\r\n this.model = new _model_js__WEBPACK_IMPORTED_MODULE_1__.Model();\r\n this.view = new _view__WEBPACK_IMPORTED_MODULE_0__.View();\r\n this.todoInputField = document.querySelector(\"#todoInputField\");\r\n this.addTodoButton = document.querySelector(\".addTodoButton\");\r\n this.projectInputField = document.querySelector(\"#projectsInputField\");\r\n this.addProjectButton = document.querySelector(\".addProjectButton\");\r\n this.currentProjectId = 0;\r\n this.showPopupButton = document.querySelector(\".showPopupButton\");\r\n this.popup = document.querySelector(\".popup\");\r\n this.todoDate = document.querySelector(\"#dateInput\");\r\n this.todoPrio = document.querySelector(\"#prio\");\r\n this.isUpdate = false;\r\n this.idToUpdate = -1;\r\n }\r\n\r\n initializeUI() {\r\n // Load stored data when the page is first loaded\r\n let data = this.getData();\r\n if(data){\r\n console.log(\"There is data\");\r\n this.model.revive(data);\r\n } else {\r\n console.log(\"There is NO data\");\r\n }\r\n let currentProjects = this.model.getProjects();\r\n console.log(currentProjects);\r\n this.view.displayProjects(currentProjects);\r\n this.listenForProjects();\r\n this.listenForProjectAdd();\r\n this.listenForTodoAdd();\r\n this.listenForShowPopupButton();\r\n }\r\n\r\n // Store data whenever a project or todo is added\r\n saveData() {\r\n let data = JSON.stringify(this.model.getProjects());\r\n console.log(`stringified data: ${data}`);\r\n localStorage.setItem(\"projects\", data);\r\n }\r\n\r\n getData() {\r\n let data = localStorage.getItem(\"projects\"); \r\n return JSON.parse(data);\r\n }\r\n\r\n\r\n\r\n listenForProjects() {\r\n this.allCurrentProjects = Array.from(document.querySelectorAll(\"li\"));\r\n this.allCurrentProjects?.forEach((project) => {\r\n project.addEventListener(\"click\", (e) => {\r\n this.currentProjectId = e.target.getAttribute(\"id\");\r\n // Display all todos for active project\r\n this.view.displayTodos(\r\n this.model.getProject(this.currentProjectId).getTodos()\r\n );\r\n this.listenForTodos();\r\n this.listenForEdit();\r\n this.listenForDelete();\r\n });\r\n });\r\n }\r\n\r\n listenForProjectAdd() {\r\n this.addProjectButton.addEventListener(\"click\", () => {\r\n this.addProjectHelper();\r\n });\r\n // Add keyboard listener but to the input field not to the projects themselves\r\n this.projectInputField.addEventListener(\"keypress\", (e) => {\r\n if (e.key === \"Enter\") {\r\n this.addProjectHelper();\r\n }\r\n });\r\n }\r\n\r\n addProjectHelper() {\r\n let projectTitle = this.projectInputField.value;\r\n this.model.addProject(projectTitle);\r\n this.view.displayProjects(this.model.getProjects());\r\n // Save your current data\r\n this.saveData();\r\n this.listenForProjects();\r\n this.listenForDescriptions();\r\n this.listenForEdit();\r\n }\r\n\r\n listenForTodoAdd() {\r\n this.addTodoButton.addEventListener(\"click\", () => {\r\n this.addTodoHelper();\r\n });\r\n // Listen for keypress also\r\n this.todoInputField.addEventListener(\"keypress\", (e) => {\r\n if (e.key === \"Enter\") {\r\n this.addTodoHelper();\r\n }\r\n });\r\n }\r\n\r\n addTodoHelper() {\r\n let todoTitle = this.todoInputField.value;\r\n let todoDate = this.todoDate.value;\r\n let todoPrio = this.todoPrio.value;\r\n if(this.isUpdate){\r\n // If we update dont create a new todo but overwrite the old one\r\n // The id of the todo to update is saved in a global variable\r\n // Also get the description as the popup is showing at this flow\r\n let todoDescription = document.querySelector(`tr[id='${this.idToUpdate}']`).nextElementSibling.firstElementChild.firstElementChild.value;\r\n this.model.updateTodo(this.currentProjectId, this.idToUpdate, todoTitle, todoDate, todoPrio, todoDescription); \r\n this.isUpdate=false;\r\n } else {\r\n this.model.addTodoToProject(\r\n this.currentProjectId,\r\n todoTitle,\r\n todoDate,\r\n todoPrio\r\n );\r\n }\r\n // Display all todos after the change\r\n this.view.displayTodos(\r\n this.model.getProject(this.currentProjectId).getTodos()\r\n );\r\n this.popup.classList.remove(\"active\");\r\n this.listenForDescriptions();\r\n this.listenForTodos();\r\n this.listenForDelete();\r\n this.listenForEdit();\r\n // Save the current data\r\n this.saveData();\r\n }\r\n\r\n // Listen for delete icon\r\n listenForDelete() {\r\n const deleteIcons = Array.from(document.querySelectorAll(\".trash-icon\"));\r\n deleteIcons.forEach((icon) => {\r\n icon.parentNode.addEventListener(\"click\", () => {\r\n // Get id of corresponding todo\r\n let thisTodoId =\r\n +icon.parentNode.parentNode.previousElementSibling.getAttribute(\"id\");\r\n this.model.getProject(this.currentProjectId).deleteTodo(thisTodoId);\r\n this.view.displayTodos(\r\n this.model.getProject(this.currentProjectId).getTodos()\r\n );\r\n this.saveData();\r\n });\r\n });\r\n }\r\n\r\n listenForEdit(){\r\n const editIcons = Array.from(document.querySelectorAll(\".edit-icon\"));\r\n editIcons.forEach((icon)=>{\r\n icon.addEventListener(\"click\", ()=>{\r\n // Set the global update flag to true so the add button handlers can know. \r\n this.isUpdate=true;\r\n // Get id of todo to edit and save it in global variable\r\n this.idToUpdate =\r\n +icon.parentNode.parentNode.previousElementSibling.getAttribute(\"id\");\r\n // Get data from the popup + descr\r\n this.popup.classList.add(\"active\");\r\n })\r\n })\r\n }\r\n\r\n listenForTodos() {\r\n const todos = Array.from(document.querySelectorAll(\"tr[id]\"));\r\n todos.forEach((todo) => {\r\n todo.addEventListener(\"click\", () => {\r\n todo.nextElementSibling.classList.toggle(\"popup\");\r\n this.listenForDescriptions();\r\n });\r\n });\r\n }\r\n\r\n listenForShowPopupButton() {\r\n this.showPopupButton.addEventListener(\"click\", () => {\r\n this.popup.classList.add(\"active\");\r\n });\r\n }\r\n\r\n listenForDescriptions() {\r\n const descriptions = Array.from(document.querySelectorAll(\"textarea\"));\r\n descriptions.forEach((description) => {\r\n description.addEventListener(\"keypress\", (e) => {\r\n if (e.key === \"Enter\") {\r\n // Save the description in the corresponding todo\r\n let correspondingTodoId =\r\n e.target.parentNode.parentNode.previousElementSibling.getAttribute(\r\n \"id\"\r\n );\r\n this.model.addDescriptionToTodo(\r\n this.currentProjectId,\r\n correspondingTodoId,\r\n e.target.value\r\n );\r\n // Save the description\r\n this.saveData();\r\n }\r\n });\r\n });\r\n }\r\n\r\n\r\n\r\n}\r\n\r\n\r\n\n\n//# sourceURL=webpack://todo/./src/modules/controller.js?"); /***/ }), @@ -168,7 +168,7 @@ eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpac /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { "use strict"; -eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"Project\": () => (/* binding */ Project),\n/* harmony export */ \"Todo\": () => (/* binding */ Todo)\n/* harmony export */ });\nclass Todo {\r\n static id = 0;\r\n title;\r\n due;\r\n prio;\r\n description;\r\n\r\n constructor(title, due, prio) {\r\n this.id = ++Todo.id;\r\n this.title = title;\r\n this.due = due;\r\n this.prio = prio;\r\n console.log(this.id);\r\n }\r\n\r\n getId(){\r\n return this.id;\r\n }\r\n\r\n setDescription(text){\r\n this.description=text;\r\n console.log(this.description);\r\n }\r\n\r\n getDescription(){\r\n return this.description;\r\n }\r\n\r\n setTitle(title) {\r\n this.title = title;\r\n }\r\n\r\n getTitle() {\r\n return this.title;\r\n }\r\n\r\n\r\n setDue(due) {\r\n this.due = due;\r\n }\r\n\r\n getDue() {\r\n return this.due;\r\n }\r\n\r\n setPriority(prio) {\r\n this.prio = prio;\r\n }\r\n\r\n getPrio() {\r\n return this.prio;\r\n }\r\n}\r\n\r\nclass Project {\r\n title;\r\n todoList = [];\r\n\r\n constructor(title){\r\n this.title = title;\r\n }\r\n\r\n setTitle(title){\r\n this.title = title;\r\n }\r\n\r\n getTitle(){\r\n return this.title;\r\n }\r\n\r\n addTodo(title,due,prio){\r\n this.todoList.push(new Todo(title,due,prio));\r\n }\r\n\r\n getTodo(id){\r\n return this.todoList[id];\r\n }\r\n\r\n setTodo(id, title, due, prio, description){\r\n this.todoList[id].setTitle(title);\r\n this.todoList[id].setDue(due);\r\n this.todoList[id].setPriority(prio);\r\n this.todoList[id].setDescription(description);\r\n }\r\n\r\n getTodos(){\r\n return this.todoList;\r\n }\r\n\r\n deleteTodo(id){\r\n this.todoList.splice(id,1);\r\n }\r\n}\r\n\r\n\n\n//# sourceURL=webpack://todo/./src/modules/entities.js?"); +eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"Project\": () => (/* binding */ Project),\n/* harmony export */ \"Todo\": () => (/* binding */ Todo)\n/* harmony export */ });\nclass Todo {\r\n static id = 0;\r\n title;\r\n due;\r\n prio;\r\n description;\r\n\r\n constructor(title, due, prio) {\r\n this.id = ++Todo.id;\r\n this.title = title;\r\n this.due = due;\r\n this.prio = prio;\r\n }\r\n\r\n setId(id){\r\n this.id = id;\r\n }\r\n\r\n getId(){\r\n return this.id;\r\n }\r\n\r\n setDescription(text){\r\n this.description=text;\r\n console.log(this.description);\r\n }\r\n\r\n getDescription(){\r\n return this.description;\r\n }\r\n\r\n setTitle(title) {\r\n this.title = title;\r\n }\r\n\r\n getTitle() {\r\n return this.title;\r\n }\r\n\r\n\r\n setDue(due) {\r\n this.due = due;\r\n }\r\n\r\n getDue() {\r\n return this.due;\r\n }\r\n\r\n setPriority(prio) {\r\n this.prio = prio;\r\n }\r\n\r\n getPrio() {\r\n return this.prio;\r\n }\r\n}\r\n\r\nclass Project {\r\n title;\r\n todoList = [];\r\n\r\n constructor(title){\r\n this.title = title;\r\n }\r\n\r\n setTitle(title){\r\n this.title = title;\r\n }\r\n\r\n getTitle(){\r\n return this.title;\r\n }\r\n\r\n addTodo(title,due,prio){\r\n this.todoList.push(new Todo(title,due,prio));\r\n }\r\n\r\n addRevivedTodo(todo){\r\n this.todoList.push(todo);\r\n }\r\n\r\n getTodo(id){\r\n return this.todoList[id];\r\n }\r\n\r\n setTodo(id, title, due, prio, description){\r\n this.todoList[id].setTitle(title);\r\n this.todoList[id].setDue(due);\r\n this.todoList[id].setPriority(prio);\r\n this.todoList[id].setDescription(description);\r\n }\r\n\r\n getTodos(){\r\n return this.todoList;\r\n }\r\n\r\n deleteTodo(id){\r\n this.todoList.splice(id,1);\r\n }\r\n}\r\n\r\n\n\n//# sourceURL=webpack://todo/./src/modules/entities.js?"); /***/ }), @@ -179,7 +179,7 @@ eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpac /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { "use strict"; -eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"Model\": () => (/* binding */ Model)\n/* harmony export */ });\n/* harmony import */ var _entities__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./entities */ \"./src/modules/entities.js\");\n\r\n\r\nclass Model {\r\n projectList;\r\n\r\n constructor(){\r\n const defaultProject = new _entities__WEBPACK_IMPORTED_MODULE_0__.Project(\"default\");\r\n this.projectList = [defaultProject];\r\n }\r\n\r\n addProject(title){\r\n this.projectList.push(new _entities__WEBPACK_IMPORTED_MODULE_0__.Project(title));\r\n }\r\n \r\n getProject(id){\r\n return this.projectList[id];\r\n }\r\n \r\n getProjects(){\r\n return this.projectList;\r\n }\r\n \r\n addTodoToProject(id, title, due, prio){\r\n this.projectList[id].addTodo(title,due,prio);\r\n console.log(this.projectList);\r\n }\r\n\r\n addDescriptionToTodo(projectId, todoId, text){\r\n this.projectList[projectId].getTodo(todoId).setDescription(text);\r\n \r\n }\r\n\r\n updateTodo(projectId, todoId, title, due, prio, description){\r\n this.projectList[projectId].setTodo(todoId, title, due, prio, description);\r\n }\r\n\r\n}\r\n\r\n\r\n\n\n//# sourceURL=webpack://todo/./src/modules/model.js?"); +eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"Model\": () => (/* binding */ Model)\n/* harmony export */ });\n/* harmony import */ var _entities__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./entities */ \"./src/modules/entities.js\");\n\r\n\r\nclass Model {\r\n projectList;\r\n\r\n constructor() {\r\n const defaultProject = new _entities__WEBPACK_IMPORTED_MODULE_0__.Project(\"default\");\r\n this.projectList = [defaultProject];\r\n }\r\n\r\n revive(data) {\r\n // When reviving empty the plist we dont want duplicate defautl\r\n this.projectList = [];\r\n data.forEach((project) => {\r\n this.reviveProject(project);\r\n });\r\n }\r\n\r\n reviveProject(project) {\r\n const newProject = new _entities__WEBPACK_IMPORTED_MODULE_0__.Project(project.title);\r\n // revive and add its todos\r\n project.todoList.forEach((todo) => {\r\n newProject.addRevivedTodo(this.reviveTodo(todo));\r\n });\r\n this.addRevivedProject(newProject);\r\n }\r\n\r\n reviveTodo(todo) {\r\n let newTodo = new _entities__WEBPACK_IMPORTED_MODULE_0__.Todo(todo.title, todo.due, todo.prio);\r\n newTodo.setId(todo.id);\r\n newTodo.setDescription(todo.description);\r\n return newTodo;\r\n }\r\n\r\n addRevivedProject(project) {\r\n this.projectList.push(project);\r\n }\r\n\r\n addProject(title) {\r\n this.projectList.push(new _entities__WEBPACK_IMPORTED_MODULE_0__.Project(title));\r\n }\r\n\r\n getProject(id) {\r\n return this.projectList[id];\r\n }\r\n\r\n getProjects() {\r\n return this.projectList;\r\n }\r\n\r\n addTodoToProject(id, title, due, prio) {\r\n this.projectList[id].addTodo(title, due, prio);\r\n\r\n }\r\n\r\n addDescriptionToTodo(projectId, todoId, text) {\r\n this.projectList[projectId].getTodo(todoId).setDescription(text);\r\n }\r\n\r\n updateTodo(projectId, todoId, title, due, prio, description) {\r\n this.projectList[projectId].setTodo(todoId, title, due, prio, description);\r\n }\r\n}\r\n\r\n\r\n\n\n//# sourceURL=webpack://todo/./src/modules/model.js?"); /***/ }), diff --git a/src/modules/controller.js b/src/modules/controller.js index 79da060..9e05d82 100644 --- a/src/modules/controller.js +++ b/src/modules/controller.js @@ -1,5 +1,6 @@ import { View } from "./view"; import { Model } from "./model.js"; +import { Project, Todo } from "./entities"; class Controller { model; @@ -36,17 +37,35 @@ class Controller { initializeUI() { // Load stored data when the page is first loaded let data = this.getData(); - console.log(data); - console.log(this.model.getProjects()); - this.view.displayProjects(data); - - // this.view.displayProjects(this.model.getProjects()); + if(data){ + console.log("There is data"); + this.model.revive(data); + } else { + console.log("There is NO data"); + } + let currentProjects = this.model.getProjects(); + console.log(currentProjects); + this.view.displayProjects(currentProjects); this.listenForProjects(); this.listenForProjectAdd(); this.listenForTodoAdd(); this.listenForShowPopupButton(); } + // Store data whenever a project or todo is added + saveData() { + let data = JSON.stringify(this.model.getProjects()); + console.log(`stringified data: ${data}`); + localStorage.setItem("projects", data); + } + + getData() { + let data = localStorage.getItem("projects"); + return JSON.parse(data); + } + + + listenForProjects() { this.allCurrentProjects = Array.from(document.querySelectorAll("li")); this.allCurrentProjects?.forEach((project) => { @@ -142,6 +161,7 @@ class Controller { this.view.displayTodos( this.model.getProject(this.currentProjectId).getTodos() ); + this.saveData(); }); }); } @@ -192,22 +212,14 @@ class Controller { correspondingTodoId, e.target.value ); + // Save the description + this.saveData(); } }); }); } - // Store data whenever a project or todo is added - saveData() { - let data = JSON.stringify(this.model.getProjects()); - console.log(`stringified data: ${data}`); - localStorage.setItem("projects", data); - } - getData() { - let data = localStorage.getItem("projects"); - return JSON.parse(data); - } } diff --git a/src/modules/entities.js b/src/modules/entities.js index 3c71ae7..a6d2991 100644 --- a/src/modules/entities.js +++ b/src/modules/entities.js @@ -10,7 +10,10 @@ class Todo { this.title = title; this.due = due; this.prio = prio; - console.log(this.id); + } + + setId(id){ + this.id = id; } getId(){ @@ -72,6 +75,10 @@ class Project { this.todoList.push(new Todo(title,due,prio)); } + addRevivedTodo(todo){ + this.todoList.push(todo); + } + getTodo(id){ return this.todoList[id]; } diff --git a/src/modules/model.js b/src/modules/model.js index 07c16bc..0ab36b3 100644 --- a/src/modules/model.js +++ b/src/modules/model.js @@ -1,40 +1,65 @@ -import {Project} from "./entities"; +import { Project, Todo } from "./entities"; class Model { - projectList; - - constructor(){ - const defaultProject = new Project("default"); - this.projectList = [defaultProject]; - } - - addProject(title){ - this.projectList.push(new Project(title)); - } - - getProject(id){ - return this.projectList[id]; - } - - getProjects(){ - return this.projectList; - } - - addTodoToProject(id, title, due, prio){ - this.projectList[id].addTodo(title,due,prio); - console.log(this.projectList); - } - - addDescriptionToTodo(projectId, todoId, text){ - this.projectList[projectId].getTodo(todoId).setDescription(text); - - } - - updateTodo(projectId, todoId, title, due, prio, description){ - this.projectList[projectId].setTodo(todoId, title, due, prio, description); - } + projectList; -} + constructor() { + const defaultProject = new Project("default"); + this.projectList = [defaultProject]; + } + + revive(data) { + // When reviving empty the plist we dont want duplicate defautl + this.projectList = []; + data.forEach((project) => { + this.reviveProject(project); + }); + } + + reviveProject(project) { + const newProject = new Project(project.title); + // revive and add its todos + project.todoList.forEach((todo) => { + newProject.addRevivedTodo(this.reviveTodo(todo)); + }); + this.addRevivedProject(newProject); + } + + reviveTodo(todo) { + let newTodo = new Todo(todo.title, todo.due, todo.prio); + newTodo.setId(todo.id); + newTodo.setDescription(todo.description); + return newTodo; + } + + addRevivedProject(project) { + this.projectList.push(project); + } + + addProject(title) { + this.projectList.push(new Project(title)); + } + getProject(id) { + return this.projectList[id]; + } + + getProjects() { + return this.projectList; + } + + addTodoToProject(id, title, due, prio) { + this.projectList[id].addTodo(title, due, prio); + + } + + addDescriptionToTodo(projectId, todoId, text) { + this.projectList[projectId].getTodo(todoId).setDescription(text); + } + + updateTodo(projectId, todoId, title, due, prio, description) { + this.projectList[projectId].setTodo(todoId, title, due, prio, description); + } +} -export {Model}; \ No newline at end of file +export { Model };