-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
198 lines (177 loc) · 6.26 KB
/
script.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// localStorage
const todos = JSON.parse(localStorage.getItem("todos"));
let ta = $(".add-input");
// get textarea font-size
let taLineHeight = parseInt(ta.css("font-size").slice(0, 2));
// textarea rows
let totalRows = 3;
// textarea maxlength
let maxWidth = ta.attr("cols");
let maxLength = parseInt(ta.attr("cols") * totalRows);
ta.attr("maxlength", maxLength);
let checkbtn = `<label>
<input type="checkbox" value="first_checkbox" class="check-hide"></input>
<span class="add-check">
<svg viewBox="0 0 512 512">
<path d="M173.898 439.404l-166.4-166.4c-9.997-9.997-9.997-26.206 0-36.204l36.203-36.204c9.997-9.998 26.207-9.998 36.204 0L192 312.69 432.095 72.596c9.997-9.997 26.207-9.997 36.204 0l36.203 36.204c9.997 9.997 9.997 26.206 0 36.204l-294.4 294.401c-9.998 9.997-26.207 9.997-36.204-.001z" />
</svg>
</span>
</label>`;
ta.on("input keyup change", function (e) {
let textareaLength = ta.val().length;
let numberOfLines = Math.floor(textareaLength / maxWidth);
let textareaVal = ta.val();
let nodeSelectVal = $(".node-select").html();
// append item
let addVal = `<li><div class="burger"><span></span><span></span><span></span></div>${checkbtn}<p>${textareaVal}</p><div class="node-select-item">${nodeSelectVal}</div></li>`;
expandheight(numberOfLines);
if (e.key == "Enter") {
// empty textarea
ta.val("");
// add item
$(".unfinished-list").append(addVal);
// remove lineheight class
ta.removeClass("row-2 row-3");
// check todos numbers
if ($(".unfinished-list li").length > 0) {
$(".edit-btn").addClass("active");
}
// update localstorage
updateLs();
}
});
// check todos numbers
$(document).ready(function () {
if ($(".unfinished-list li").length > 0) {
$(".edit-btn").addClass("active");
}
});
// expand textarea height
function expandheight(numberOfLines) {
if (numberOfLines == 1) {
ta.removeClass("row-2 row-3").addClass("row-2");
} else if (numberOfLines >= 2) {
ta.removeClass("row-2 row-3").addClass("row-3");
} else {
ta.removeClass("row-2 row-3");
}
}
// dropdown button
$(".node-select").on("click", function (e) {
e.preventDefault();
$(".node-select-scroll").slideToggle();
});
$(".node-select-scroll li").on("click", function (e) {
let newVal = $(e.target).html();
$(".node-select").html(newVal);
$(".node-select-scroll").slideUp();
$(".node-select").removeClass("work busy home");
if (newVal == "work") {
$(".node-select").addClass("work");
} else if (newVal == "busy") {
$(".node-select").addClass("busy");
} else if (newVal == "home") {
$(".node-select").addClass("home");
}
});
// input focus(checkbox confrim)
$(".unfinished-list").on("click", function (e) {
if (e.target.type == "checkbox") {
var target = e.target.closest("label");
target.nextElementSibling.classList.toggle("slash");
target.parentNode.classList.toggle("completed");
updateLs();
}
});
// edit
$(".edit-btn").on("click", function () {
if ($(this).hasClass("active")) {
$(".unfinished-list").toggleClass("edit");
$(".unfinished-list li").find(".burger").toggleClass("active");
$(".trash-box").toggleClass("active");
if ($(".unfinished-list").hasClass("edit")) {
$(".unfinished-list li").addClass("draggable").attr("draggable", "true");
$(".add-input").attr("disabled", "disabled");
} else {
$(".unfinished-list li")
.removeClass("draggable")
.attr("draggable", "false");
$(".add-input").removeAttr("disabled");
}
}
// drag and drop
let removeBtn = $(".remove-btn");
function dragStart(e) {
this.style.opacity = "0.4";
dragSrcEl = this;
e.dataTransfer.effectAllowed = "move";
e.dataTransfer.setData("text/html", this.innerHTML);
}
function dragEnter(e) {
this.classList.add("over");
}
function dragLeave(e) {
e.stopPropagation();
this.classList.remove("over");
}
function dragOver(e) {
e.preventDefault();
e.dataTransfer.dropEffect = "move";
return false;
}
function dragDrop(e) {
if (dragSrcEl != this && !this.classList.contains("trash-box")) {
dragSrcEl.innerHTML = this.innerHTML;
this.innerHTML = e.dataTransfer.getData("text/html");
} else if (this.classList.contains("trash-box")) {
dragSrcEl.remove();
console.log($(".unfinished-list li").length);
if ($(".unfinished-list li").length == 0) {
$(".edit-btn").removeClass("active");
$(".trash-box").removeClass("active");
$(".add-input").removeAttr("disabled");
}
}
return false;
}
function dragEnd(e) {
var listItens = document.querySelectorAll(".draggable");
[].forEach.call(listItens, function (item) {
item.classList.remove("over");
});
this.style.opacity = "1";
}
function addEventsDragAndDrop(el) {
el.addEventListener("dragstart", dragStart);
el.addEventListener("dragenter", dragEnter);
el.addEventListener("dragover", dragOver);
el.addEventListener("dragleave", dragLeave);
el.addEventListener("drop", dragDrop);
el.addEventListener("dragend", dragEnd);
updateLs();
}
var listItens = document.querySelectorAll(".draggable");
[].forEach.call(listItens, function (item) {
addEventsDragAndDrop(item);
});
});
// update localstorage
function updateLs() {
const todosArr = [];
let todoEl = document.querySelectorAll(".unfinished-list li");
todoEl.forEach((todoEl) => {
todosArr.push({
content: todoEl.innerHTML,
completed: todoEl.classList.contains("completed")
});
});
localStorage.setItem("todos", JSON.stringify(todosArr));
return todos;
}
if (todos) {
for (let i = 0; i < todos.length; i++) {
let element = document.createElement("li");
element.innerHTML = todos[i]["content"];
$(".unfinished-list").append(element);
}
}