-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs.js
More file actions
125 lines (116 loc) · 3.87 KB
/
js.js
File metadata and controls
125 lines (116 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const inputBox=document.getElementById("input-box")
const list=document.getElementById("list")
const DateTime=document.getElementById("DateTime")
const quote=document.getElementById("quote")
const author=document.getElementById("author")
const api="https://api.quotable.io/random";
new Sortable(list, {
animation: 300 ,
ghostClass: 'sortable',
});
function dateTime(){
var now = new Date();
DateTime.innerHTML = now.toLocaleString();
}
async function Quote(api){
const response = await fetch(api);
var data=await response.json();
quote.innerHTML = data.content;
author.innerHTML = data.author;
}
function addTask() {
if (inputBox.value === '') {
alert("empty");
} else {
let li = document.createElement("li");
li.contentEditable=false;
let input = document.createElement("p");
input.textContent= inputBox.value ;
input.classList.add("col-sm-8");
li.appendChild(input);
let span = document.createElement("span");
span.classList.add("span");
span.innerHTML = "\u00d7";
li.appendChild(span);
let editButton = document.createElement("span");
editButton.innerHTML = "\u270E";
editButton.classList.add("edit-button");
li.appendChild(editButton);
editButton.contentEditable = false;
list.appendChild(li);
inputBox.value = "";
applyEditButtonListener(editButton,input);
save();
}
}
var isEditing = false;
function applyEditButtonListener(editButton,input) {
editButton.onclick = function (e) {
isEditing = true;
const li = editButton.parentElement;
const textNode = input.childNodes[0];
const textLength = textNode.textContent.length;
input.contentEditable = true;
li.contentEditable=true;
input.classList.add("col-sm-8");
const range = document.createRange();
const sel = window.getSelection();
range.setStart(textNode, textLength);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
li.addEventListener("blur", function () {
input.contentEditable = false;
li.contentEditable = false;
save();
isEditing = false;
});
input.focus();
li.focus();
};
}
list.addEventListener("click", function (e) {
const target = e.target;
if (target.tagName === "LI" || target.classList.contains("col-sm-8")) {
handleCheckClick(target);
} else if (target.tagName === "SPAN" && target.classList.contains("span")) {
handleDeleteClick(target);
} else if (target.tagName === "SPAN" && target.classList.contains("edit-button")) {
handleEditClick(target);
}
}, false);
function handleCheckClick(element) {
if (!isEditing) {
const li = element.tagName === "LI" ? element : element.closest("li");
li.classList.toggle("checked");
save();
}
}
function handleDeleteClick(span) {
span.parentElement.remove();
save();
}
function handleEditClick(editButton) {
if (!isEditing) {
var listItem = editButton.parentElement;
const input = listItem.querySelector(".col-sm-8");
applyEditButtonListener(editButton, input);
save();
}
}
function save(){
localStorage.setItem("data",list.innerHTML);
}
function showList() {
isEditing = false;
list.innerHTML = localStorage.getItem("data");
const editButtons = document.querySelectorAll(".edit-button");
editButtons.forEach((editButton) => {
const listItem = editButton.parentElement;
const input = listItem.querySelector(".col-sm-8");
applyEditButtonListener(editButton, input);
});
}
showList();
dateTime();
Quote(api);