From cf8ea841f5e0dfc4b888fdc07adf1246ad6c167e Mon Sep 17 00:00:00 2001 From: Benjamin-Re Date: Tue, 19 Apr 2022 20:28:24 +0200 Subject: [PATCH] Implement update function --- dist/main.js | 10 ++++---- src/index.js | 2 +- src/modules/controller.js | 54 ++++++++++++++++++--------------------- src/modules/entities.js | 7 +++++ src/modules/model.js | 4 +++ src/style.css | 4 --- 6 files changed, 42 insertions(+), 39 deletions(-) diff --git a/dist/main.js b/dist/main.js index 70d7bcb..9859d40 100644 --- a/dist/main.js +++ b/dist/main.js @@ -36,7 +36,7 @@ eval("/*!\n * Font Awesome Free 6.1.1 by @fontawesome - https://fontawesome.com\ /***/ ((module, __webpack_exports__, __webpack_require__) => { "use strict"; -eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"default\": () => (__WEBPACK_DEFAULT_EXPORT__)\n/* harmony export */ });\n/* harmony import */ var _node_modules_css_loader_dist_runtime_noSourceMaps_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../node_modules/css-loader/dist/runtime/noSourceMaps.js */ \"./node_modules/css-loader/dist/runtime/noSourceMaps.js\");\n/* harmony import */ var _node_modules_css_loader_dist_runtime_noSourceMaps_js__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_node_modules_css_loader_dist_runtime_noSourceMaps_js__WEBPACK_IMPORTED_MODULE_0__);\n/* harmony import */ var _node_modules_css_loader_dist_runtime_api_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../node_modules/css-loader/dist/runtime/api.js */ \"./node_modules/css-loader/dist/runtime/api.js\");\n/* harmony import */ var _node_modules_css_loader_dist_runtime_api_js__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_node_modules_css_loader_dist_runtime_api_js__WEBPACK_IMPORTED_MODULE_1__);\n// Imports\n\n\nvar ___CSS_LOADER_EXPORT___ = _node_modules_css_loader_dist_runtime_api_js__WEBPACK_IMPORTED_MODULE_1___default()((_node_modules_css_loader_dist_runtime_noSourceMaps_js__WEBPACK_IMPORTED_MODULE_0___default()));\n// Module\n___CSS_LOADER_EXPORT___.push([module.id, \"body {\\r\\n background-color: red;\\r\\n height: 100vh;\\r\\n display: grid;\\r\\n grid-template-columns: 1fr 4fr;\\r\\n grid-template-rows: 1fr 3fr 1fr;\\r\\n font-family: monospace;\\r\\n}\\r\\n\\r\\nheader {\\r\\n background-color: aliceblue;\\r\\n grid-column: 1/3;\\r\\n grid-row: 1/2;\\r\\n}\\r\\n\\r\\naside {\\r\\n grid-column: 1/2;\\r\\n grid-row: 2/3;\\r\\n}\\r\\n.content {\\r\\n background-color: coral;\\r\\n grid-column: 2/3;\\r\\n grid-row: 2/3;\\r\\n}\\r\\n\\r\\nfooter {\\r\\n background-color: aqua;\\r\\n grid-column: 1/3;\\r\\n grid-row: 3/4;\\r\\n}\\r\\n\\r\\n.popup {\\r\\n display: none;\\r\\n}\\r\\n\\r\\n.active {\\r\\n display: block;\\r\\n}\\r\\n\\r\\nsvg {\\r\\n pointer-events: auto;\\r\\n}\", \"\"]);\n// Exports\n/* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = (___CSS_LOADER_EXPORT___);\n\n\n//# sourceURL=webpack://todo/./src/style.css?./node_modules/css-loader/dist/cjs.js"); +eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"default\": () => (__WEBPACK_DEFAULT_EXPORT__)\n/* harmony export */ });\n/* harmony import */ var _node_modules_css_loader_dist_runtime_noSourceMaps_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../node_modules/css-loader/dist/runtime/noSourceMaps.js */ \"./node_modules/css-loader/dist/runtime/noSourceMaps.js\");\n/* harmony import */ var _node_modules_css_loader_dist_runtime_noSourceMaps_js__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_node_modules_css_loader_dist_runtime_noSourceMaps_js__WEBPACK_IMPORTED_MODULE_0__);\n/* harmony import */ var _node_modules_css_loader_dist_runtime_api_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../node_modules/css-loader/dist/runtime/api.js */ \"./node_modules/css-loader/dist/runtime/api.js\");\n/* harmony import */ var _node_modules_css_loader_dist_runtime_api_js__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_node_modules_css_loader_dist_runtime_api_js__WEBPACK_IMPORTED_MODULE_1__);\n// Imports\n\n\nvar ___CSS_LOADER_EXPORT___ = _node_modules_css_loader_dist_runtime_api_js__WEBPACK_IMPORTED_MODULE_1___default()((_node_modules_css_loader_dist_runtime_noSourceMaps_js__WEBPACK_IMPORTED_MODULE_0___default()));\n// Module\n___CSS_LOADER_EXPORT___.push([module.id, \"body {\\r\\n background-color: red;\\r\\n height: 100vh;\\r\\n display: grid;\\r\\n grid-template-columns: 1fr 4fr;\\r\\n grid-template-rows: 1fr 3fr 1fr;\\r\\n font-family: monospace;\\r\\n}\\r\\n\\r\\nheader {\\r\\n background-color: aliceblue;\\r\\n grid-column: 1/3;\\r\\n grid-row: 1/2;\\r\\n}\\r\\n\\r\\naside {\\r\\n grid-column: 1/2;\\r\\n grid-row: 2/3;\\r\\n}\\r\\n.content {\\r\\n background-color: coral;\\r\\n grid-column: 2/3;\\r\\n grid-row: 2/3;\\r\\n}\\r\\n\\r\\nfooter {\\r\\n background-color: aqua;\\r\\n grid-column: 1/3;\\r\\n grid-row: 3/4;\\r\\n}\\r\\n\\r\\n.popup {\\r\\n display: none;\\r\\n}\\r\\n\\r\\n.active {\\r\\n display: block;\\r\\n}\\r\\n\", \"\"]);\n// Exports\n/* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = (___CSS_LOADER_EXPORT___);\n\n\n//# sourceURL=webpack://todo/./src/style.css?./node_modules/css-loader/dist/cjs.js"); /***/ }), @@ -146,7 +146,7 @@ eval("\n\n/* istanbul ignore next */\nfunction styleTagTransform(css, styleElem /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { "use strict"; -eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _style_css__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./style.css */ \"./src/style.css\");\n/* harmony import */ var _fortawesome_fontawesome_free_js_fontawesome__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! @fortawesome/fontawesome-free/js/fontawesome */ \"./node_modules/@fortawesome/fontawesome-free/js/fontawesome.js\");\n/* harmony import */ var _fortawesome_fontawesome_free_js_fontawesome__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_fortawesome_fontawesome_free_js_fontawesome__WEBPACK_IMPORTED_MODULE_1__);\n/* harmony import */ var _fortawesome_fontawesome_free_js_solid__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! @fortawesome/fontawesome-free/js/solid */ \"./node_modules/@fortawesome/fontawesome-free/js/solid.js\");\n/* harmony import */ var _fortawesome_fontawesome_free_js_solid__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(_fortawesome_fontawesome_free_js_solid__WEBPACK_IMPORTED_MODULE_2__);\n/* harmony import */ var _modules_controller__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./modules/controller */ \"./src/modules/controller.js\");\n\r\n\r\n\r\n\r\n\r\n\r\nwindow.addEventListener(\"load\", () => {\r\n const controller = new _modules_controller__WEBPACK_IMPORTED_MODULE_3__.Controller();\r\n controller.initializeUI();\r\n});\r\n\r\n/* Next step: Display Todos associated with the active project */\n\n//# sourceURL=webpack://todo/./src/index.js?"); +eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _style_css__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./style.css */ \"./src/style.css\");\n/* harmony import */ var _fortawesome_fontawesome_free_js_fontawesome__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! @fortawesome/fontawesome-free/js/fontawesome */ \"./node_modules/@fortawesome/fontawesome-free/js/fontawesome.js\");\n/* harmony import */ var _fortawesome_fontawesome_free_js_fontawesome__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_fortawesome_fontawesome_free_js_fontawesome__WEBPACK_IMPORTED_MODULE_1__);\n/* harmony import */ var _fortawesome_fontawesome_free_js_solid__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! @fortawesome/fontawesome-free/js/solid */ \"./node_modules/@fortawesome/fontawesome-free/js/solid.js\");\n/* harmony import */ var _fortawesome_fontawesome_free_js_solid__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(_fortawesome_fontawesome_free_js_solid__WEBPACK_IMPORTED_MODULE_2__);\n/* harmony import */ var _modules_controller__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./modules/controller */ \"./src/modules/controller.js\");\n\r\n\r\n\r\n\r\n\r\n\r\nwindow.addEventListener(\"load\", () => {\r\n const controller = new _modules_controller__WEBPACK_IMPORTED_MODULE_3__.Controller();\r\n controller.initializeUI();\r\n});\r\n\r\n/* Next step: Enable update function */\n\n//# sourceURL=webpack://todo/./src/index.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\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 }\r\n\r\n initializeUI() {\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 });\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 this.listenForProjects();\r\n this.listenForDescriptions();\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 this.model.addTodoToProject(\r\n this.currentProjectId,\r\n todoTitle,\r\n todoDate,\r\n todoPrio\r\n );\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 }\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 // Get id of todo to edit\r\n let thisTodoId =\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 // Hide the Add button (its listening to add a new todo)\r\n this.popup.lastElementChild.style.display=\"none\";\r\n // Disable the trash can while editing\r\n icon.parentElement.nextElementSibling.firstChild.style.display=\"none\";\r\n // Add an update button (which will update not add a new one)\r\n const updateButton = document.createElement(\"button\");\r\n updateButton.textContent=\"update\";\r\n this.popup.appendChild(updateButton);\r\n let todoTitle = this.todoInputField.value;\r\n let todoDate = this.todoDate.value;\r\n let todoPrio = this.todoPrio.value;\r\n let todoDescription = icon.parentNode.previousElementSibling.firstChild.value;\r\n this.model.getProject(this.currentProjectId).setTodo(\r\n thisTodoId,\r\n todoTitle,\r\n todoDate,\r\n todoPrio,\r\n todoDescription,\r\n );\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\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\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 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 });\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 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 }\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\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 title;\r\n due;\r\n prio;\r\n description;\r\n\r\n constructor(title, due, prio) {\r\n this.title = title;\r\n this.due = due;\r\n this.prio = prio;\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 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?"); /***/ }), @@ -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}\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 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?"); /***/ }), diff --git a/src/index.js b/src/index.js index d46bfec..7581d5a 100644 --- a/src/index.js +++ b/src/index.js @@ -9,4 +9,4 @@ window.addEventListener("load", () => { controller.initializeUI(); }); -/* Next step: Display Todos associated with the active project */ \ No newline at end of file +/* Next step: Enable update function */ \ No newline at end of file diff --git a/src/modules/controller.js b/src/modules/controller.js index 654e0e8..ae452c4 100644 --- a/src/modules/controller.js +++ b/src/modules/controller.js @@ -14,6 +14,8 @@ class Controller { popup; todoDate; todoPrio; + isUpdate; + idToUpdate; constructor() { this.model = new Model(); @@ -27,6 +29,8 @@ class Controller { this.popup = document.querySelector(".popup"); this.todoDate = document.querySelector("#dateInput"); this.todoPrio = document.querySelector("#prio"); + this.isUpdate = false; + this.idToUpdate = -1; } initializeUI() { @@ -69,6 +73,7 @@ class Controller { this.view.displayProjects(this.model.getProjects()); this.listenForProjects(); this.listenForDescriptions(); + this.listenForEdit(); } listenForTodoAdd() { @@ -87,12 +92,22 @@ class Controller { let todoTitle = this.todoInputField.value; let todoDate = this.todoDate.value; let todoPrio = this.todoPrio.value; - this.model.addTodoToProject( - this.currentProjectId, - todoTitle, - todoDate, - todoPrio - ); + if(this.isUpdate){ + // If we update dont create a new todo but overwrite the old one + // The id of the todo to update is saved in a global variable + // Also get the description as the popup is showing at this flow + let todoDescription = document.querySelector(`tr[id='${this.idToUpdate}']`).nextElementSibling.firstElementChild.firstElementChild.value; + this.model.updateTodo(this.currentProjectId, this.idToUpdate, todoTitle, todoDate, todoPrio, todoDescription); + this.isUpdate=false; + } else { + this.model.addTodoToProject( + this.currentProjectId, + todoTitle, + todoDate, + todoPrio + ); + } + // Display all todos after the change this.view.displayTodos( this.model.getProject(this.currentProjectId).getTodos() ); @@ -123,32 +138,13 @@ class Controller { const editIcons = Array.from(document.querySelectorAll(".edit-icon")); editIcons.forEach((icon)=>{ icon.addEventListener("click", ()=>{ - // Get id of todo to edit - let thisTodoId = + // Set the global update flag to true so the add button handlers can know. + this.isUpdate=true; + // Get id of todo to edit and save it in global variable + this.idToUpdate = +icon.parentNode.parentNode.previousElementSibling.getAttribute("id"); // Get data from the popup + descr this.popup.classList.add("active"); - // Hide the Add button (its listening to add a new todo) - this.popup.lastElementChild.style.display="none"; - // Disable the trash can while editing - icon.parentElement.nextElementSibling.firstChild.style.display="none"; - // Also disable other todos to be clicked - // Add an update button (which will update not add a new one) - const updateButton = document.createElement("button"); - updateButton.textContent="update"; - this.popup.appendChild(updateButton); - // Add event listener to the update button - let todoTitle = this.todoInputField.value; - let todoDate = this.todoDate.value; - let todoPrio = this.todoPrio.value; - let todoDescription = icon.parentNode.previousElementSibling.firstChild.value; - this.model.getProject(this.currentProjectId).setTodo( - thisTodoId, - todoTitle, - todoDate, - todoPrio, - todoDescription, - ); }) }) } diff --git a/src/modules/entities.js b/src/modules/entities.js index 7dbb628..3c71ae7 100644 --- a/src/modules/entities.js +++ b/src/modules/entities.js @@ -1,13 +1,20 @@ class Todo { + static id = 0; title; due; prio; description; constructor(title, due, prio) { + this.id = ++Todo.id; this.title = title; this.due = due; this.prio = prio; + console.log(this.id); + } + + getId(){ + return this.id; } setDescription(text){ diff --git a/src/modules/model.js b/src/modules/model.js index 2664a80..07c16bc 100644 --- a/src/modules/model.js +++ b/src/modules/model.js @@ -30,6 +30,10 @@ class Model { } + updateTodo(projectId, todoId, title, due, prio, description){ + this.projectList[projectId].setTodo(todoId, title, due, prio, description); + } + } diff --git a/src/style.css b/src/style.css index e2103e7..32daf68 100644 --- a/src/style.css +++ b/src/style.css @@ -36,7 +36,3 @@ footer { .active { display: block; } - -svg { - pointer-events: auto; -} \ No newline at end of file