Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drag n drop #6

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added dist/3508d620efd5a52bb4ce.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added dist/4b4de43949f46052a146.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added dist/645bd127083ef66fbfb9.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added dist/73e47dbb723ebc772ef0.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions dist/a8fb15d0278c8e312df5.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
185 changes: 183 additions & 2 deletions dist/index.bundle.js

Large diffs are not rendered by default.

57 changes: 46 additions & 11 deletions dist/index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,47 @@
<!DOCTYPE html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Wbpack Exercise</title>
<script defer src="runtime.bundle.js"></script><script defer src="index.bundle.js"></script></head>
<body>
<h1>I am working!</h1>
<h2>I am red</h2>
</body>
</html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link
href="https://fonts.googleapis.com/css2?family=Josefin+Sans"
rel="stylesheet"
/>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.3/font/bootstrap-icons.css"
/>
<title>To Do List</title>
<script defer src="runtime.bundle.js"></script><script defer src="index.bundle.js"></script></head>
<body class="">
<main class="wrapper">
<h1 class="heading">TO DO</h1>
<form class="field-input-to-do">
<input type="text" class="input-to-do" placeholder="What to do?"/>
<button type="submit" class="submit-btn">
<i class="bi bi-plus"></i>
</button>
</form>
<ul class="tasks">

</ul>
<ul class="ul-foot">
<li class="tasks-left">
<span class="tasks-left-num"></span> tasks remaining
</li>
<li>
<input type="checkbox" class="mode-toggler" id="checkbox">
<label for="checkbox" class="toggler-label">
<i class="bi bi-sun-fill"></i>
<i class="bi bi-moon"></i>
<span class="ball"></span>
</label>
</li>
<li>
<a href="#" class="clear-completed">Clear Completed</a>
</li>
</ul>
</main>
</body>
</html>
76 changes: 74 additions & 2 deletions dist/runtime.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = __webpack_module_cache__[moduleId] = {
/******/ // no module.id needed
/******/ id: moduleId,
/******/ // no module.loaded needed
/******/ exports: {}
/******/ };
Expand Down Expand Up @@ -70,14 +70,81 @@
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/compat get default export */
/******/ (() => {
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = (module) => {
/******/ var getter = module && module.__esModule ?
/******/ () => (module['default']) :
/******/ () => (module);
/******/ __webpack_require__.d(getter, { a: getter });
/******/ return getter;
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/define property getters */
/******/ (() => {
/******/ // define getter functions for harmony exports
/******/ __webpack_require__.d = (exports, definition) => {
/******/ for(var key in definition) {
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ }
/******/ }
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/global */
/******/ (() => {
/******/ __webpack_require__.g = (function() {
/******/ if (typeof globalThis === 'object') return globalThis;
/******/ try {
/******/ return this || new Function('return this')();
/******/ } catch (e) {
/******/ if (typeof window === 'object') return window;
/******/ }
/******/ })();
/******/ })();
/******/
/******/ /* webpack/runtime/hasOwnProperty shorthand */
/******/ (() => {
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
/******/ })();
/******/
/******/ /* webpack/runtime/make namespace object */
/******/ (() => {
/******/ // define __esModule on exports
/******/ __webpack_require__.r = (exports) => {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/publicPath */
/******/ (() => {
/******/ var scriptUrl;
/******/ if (__webpack_require__.g.importScripts) scriptUrl = __webpack_require__.g.location + "";
/******/ var document = __webpack_require__.g.document;
/******/ if (!scriptUrl && document) {
/******/ if (document.currentScript)
/******/ scriptUrl = document.currentScript.src
/******/ if (!scriptUrl) {
/******/ var scripts = document.getElementsByTagName("script");
/******/ if(scripts.length) scriptUrl = scripts[scripts.length - 1].src
/******/ }
/******/ }
/******/ // When supporting browsers where an automatic publicPath is not supported you must specify an output.publicPath manually via configuration
/******/ // or pass an empty string ("") and set the __webpack_public_path__ variable from your code to use your own logic.
/******/ if (!scriptUrl) throw new Error("Automatic publicPath is not supported in this browser");
/******/ scriptUrl = scriptUrl.replace(/#.*$/, "").replace(/\?.*$/, "").replace(/\/[^\/]+$/, "/");
/******/ __webpack_require__.p = scriptUrl;
/******/ })();
/******/
/******/ /* webpack/runtime/jsonp chunk loading */
/******/ (() => {
/******/ // no baseURI
/******/ __webpack_require__.b = document.baseURI || self.location.href;
/******/
/******/ // object to store loaded and loading chunks
/******/ // undefined = chunk not loaded, null = chunk preloaded/prefetched
Expand Down Expand Up @@ -128,6 +195,11 @@
/******/ chunkLoadingGlobal.push = webpackJsonpCallback.bind(null, chunkLoadingGlobal.push.bind(chunkLoadingGlobal));
/******/ })();
/******/
/******/ /* webpack/runtime/nonce */
/******/ (() => {
/******/ __webpack_require__.nc = undefined;
/******/ })();
/******/
/************************************************************************/
/******/
/******/
Expand Down
11 changes: 8 additions & 3 deletions src/add_remove_update.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ export class Store {
const checkbox = el;
const taskList = document.querySelector('.tasks');
const tasks = Store.getTasks();
const nodes = Array.prototype.slice.call(taskList.children);
const nodes =Array.prototype.slice.call(taskList.children);;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spread operator can be used to make the code cleaner instead of Array.prototype.slice.call(taskList.children);

console.log(nodes)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kindly remove the console.log to follow the best practices

for (let i = 0; i < tasks.length; i += 1) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider using (.forEach(), .map(), .filter(), .reduce() and .find()) functions instead of (for loop) for better clean code .

if (
tasks[i].index === nodes.indexOf(checkbox.parentElement.parentElement)
Expand Down Expand Up @@ -93,7 +94,8 @@ export class Store {
static updateTask(description) {
const tasks = Store.getTasks();
const taskList = document.querySelector('.tasks');
const nodes = Array.prototype.slice.call(taskList.children); // convert list in array
const nodes =Array.prototype.slice.call(taskList.children);; // convert list in array
console.log(nodes)
for (let i = 0; i < tasks.length; i += 1) {
// iterate over tasks to find completed tasks
if (
Expand Down Expand Up @@ -123,6 +125,8 @@ export class UI {

const row = document.createElement('li');
row.classList.add('task');
row.classList.add('draggable');
row.setAttribute('draggable', 'true');

row.innerHTML = `
<div class="task-field">
Expand Down Expand Up @@ -213,14 +217,15 @@ export class UI {
Store.updateTask(description);
}
});

updateBtn.addEventListener('click', () => {
description.setAttribute('contentEditable', 'false');
description.parentElement.parentElement.classList.remove('active');
// to remove edit btn
editBtn.style.display = 'block';
// to display update btn
updateBtn.style.display = 'none';
Store.updateTask(description);
});
}
}
Expand Down
76 changes: 53 additions & 23 deletions src/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ document.querySelector('.clear-completed').addEventListener('click', () => {
Store.clearCompleted();
// TODO> CHANGE THIS TO SOMTHING THAT DO NOT RELOAD AND STILL CLEAR THE UI
document.location.reload(true); // for reloading after clearing s completed items
}
);
});

// Load tasks
UI.displayTasks();
Expand All @@ -83,30 +82,61 @@ document.querySelectorAll('input[type="checkbox"]').forEach((box) => {
}
});


const toggler = document.querySelector(".mode-toggler")
toggler.addEventListener("change", () => {
document.body.classList.toggle("dark")
document.querySelector(".ball").classList.toggle("active")
if(localStorage.getItem('darkMode') === null){
localStorage.setItem('darkMode', 'true')
const toggler = document.querySelector('.mode-toggler');
toggler.addEventListener('change', () => {
document.body.classList.toggle('dark');
document.querySelector('.ball').classList.toggle('active');
if (localStorage.getItem('darkMode') === null) {
localStorage.setItem('darkMode', 'true');
} else if (localStorage.getItem('darkMode') === 'false') {
localStorage.setItem('darkMode', 'true');
} else if (localStorage.getItem('darkMode') === 'true') {
localStorage.setItem('darkMode', 'false');
}
else if(localStorage.getItem('darkMode') === 'false'){
localStorage.setItem('darkMode', 'true')
});

document.addEventListener('DOMContentLoaded', () => {
if (localStorage.getItem('darkMode') === 'true') {
document.body.classList.add('dark');
document.querySelector('.ball').classList.add('active');
}
else if(localStorage.getItem('darkMode') === 'true'){
localStorage.setItem('darkMode', 'false')
if (localStorage.getItem('darkMode') === 'false') {
document.body.classList.remove('dark');
document.querySelector('.ball').classList.remove('active');
}
})
});

const draggables = document.querySelectorAll('.draggable');
const taskContainer = document.querySelector('.tasks');
draggables.forEach((draggable) => {
draggable.addEventListener('dragstart', () => {
draggable.classList.add('dragging');
});
draggable.addEventListener('dragend', () => {
draggable.classList.remove('dragging');
});
});

document.addEventListener('DOMContentLoaded', ()=>{
if(localStorage.getItem('darkMode') === 'true'){
document.body.classList.add("dark")
document.querySelector(".ball").classList.add("active")
}
if(localStorage.getItem('darkMode') === 'false'){
document.body.classList.remove("dark")
document.querySelector(".ball").classList.remove("active")
taskContainer.addEventListener('dragover', (e) => {
e.preventDefault();
const afterElement = getDragAfterElement(e.clientY);
const draggable = document.querySelector('.dragging');
if (afterElement == null) {
taskContainer.appendChild(draggable);
} else {
taskContainer.insertBefore(draggable, afterElement);
}
})
});

function getDragAfterElement(y) {
const draggableElements = [...taskContainer.querySelectorAll('.draggable:not(.dragging)')];

return draggableElements.reduce((closest, child) => {
const box = child.getBoundingClientRect();
const offset = y - box.top - box.height / 2;
if (offset < 0 && offset > closest.offset) {
return { offset, element: child };
}
return closest;
}, { offset: Number.NEGATIVE_INFINITY }).element;
}
12 changes: 9 additions & 3 deletions src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,17 @@ body.dark .submit-btn .bi {
box-shadow: 7px 21px 47px -28px var(--VeryDarkGrayishBlue);
}

body.dark .tasks {
background: var(--VeryDarkDesaturatedBlue);
}

body.dark .task {
background: var(--VeryDarkDesaturatedBlue);
}

.task {
padding: 15px;
border-bottom: 1px solid rgb(200, 200, 213);
padding: 15px;
border-bottom: 1px solid rgb(200, 200, 213);
display: flex;
gap: 20px;
align-items: center;
Expand All @@ -135,7 +139,9 @@ body.dark .task {
body.dark .task {
border-bottom: 1px solid var(--VeryDarkGrayishBlue);
}

.draggable.dragging {
opacity: .5;
}
.task.active {
border: 2px solid rgb(103, 103, 103);
border-radius: 3px;
Expand Down