-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmc.js
180 lines (166 loc) · 6.5 KB
/
tmc.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
let ColorCode = {
"blue": "#778beb",
"yellow": "#f5cd79",
"green": "#32ff7e",
"black": "#3d3d3d"
}
let allColors = document.querySelectorAll(".color");
let para = document.querySelector(".ticket-content")
let opentab = document.querySelector(".open-plus");
let ticket = document.querySelector(".ticket-content");
let closetab = document.querySelector(".close-cross");
let activemodalfilter = "blue";
for (let i = 0; i < allColors.length; i++) {
allColors[i].addEventListener("click", function (event) {
let clickedcolor = event.target.classList[1];
if (event.target.classList.contains("active-class")) {
event.target.classList.remove("active-class");
para.style.background = "#d2d2d3";
//display all tickets
ticket.innerHTML = "";
let alltickets = JSON.parse(localStorage.getItem("alltickets"));
for (let i = 0; i < alltickets.length; i++) {
appendtoUI(alltickets[i]);
}
return;
}
if (document.querySelector(".active-class")) {
//remove prev classs tickets
document.querySelector(".active-class").classList.remove("active-class");
}
//upload my color tickets
ticket.innerHTML = "";
let alltickets = JSON.parse(localStorage.getItem("alltickets"));
let filteredarr = alltickets.filter(obj => {
return obj.T_filter == clickedcolor;
})
console.log(filteredarr);
for (let i = 0; i < filteredarr.length; i++) {
appendtoUI(filteredarr[i]);
}
event.target.classList.add("active-class");
para.style.background = ColorCode[clickedcolor];
})
}
opentab.addEventListener("click", function (e) {
//1. Add modal on +
if (document.querySelector(".modal")) {
return;
}
let modaldiv = document.createElement("div");
//<div></div>
modaldiv.classList.add("modal");
modaldiv.innerHTML = `<div class="modal-text" contenteditable="true" spellcheck="false" data-typed="false">Enter your Text here !!</div>
<div class="modal-filter-options">
<div class="modal-filter blue active-class"></div>
<div class="modal-filter yellow"></div>
<div class="modal-filter green"></div>
<div class="modal-filter black"></div>
</div>`;
//2.remove Enter your text
let textclick = modaldiv.querySelector(".modal-text");
// // if click then remove
textclick.addEventListener("click", function (e) {
if (e.target.getAttribute("data-typed") == "false") {
e.target.textContent = "";
e.target.setAttribute("data-typed", "true");
}
})
// // if a keypress then also remove
textclick.addEventListener("keypress", function (e) {
if (e.key == "Enter") {
let text = e.target.textContent;
appendModal(text);
}
if (e.target.getAttribute("data-typed") == "false") {
e.target.textContent = "";
e.target.setAttribute("data-typed", "true");
}
})
// //3. Change the active class therfore update activemodalfilter
let modalfilters = modaldiv.querySelector(".modal-filter-options");
modalfilters.addEventListener("click", function (e) {
// already on active class
if (e.target.classList.contains("active-class") || e.target.classList.contains("modal-filter-options")) {
return;
}
// new active class
// // remove current active
let itemtoremove = document.querySelector(".modal-filter.active-class");
itemtoremove.classList.remove("active-class");
// // add active class to clicked element
e.target.classList.add("active-class");
activemodalfilter = e.target.classList[1];
})
ticket.append(modaldiv);
})
// ADD TICKET TO UI WHEN ENTER IS PRESSED
function appendModal(text) {
if (text.length == 0) {
alert("Please Enter A Note!");
return;
}
let ID = uid();
let ticketdiv = document.createElement("div");
ticketdiv.classList.add("ticket");
ticketdiv.innerHTML =
`<div id=${ID} class="ticket-filter ${activemodalfilter}"></div>
<div class="ticket-body">
<div class="ticket-id">
#${ID}
<div>
<i class="fas fa-minus-circle" id=${ID}></i>
</div>
</div>
<div class="ticket-text">
${text}
</div>
</div>`;
ticketdiv.querySelector(".ticket-filter").addEventListener("click", function (e) {
console.log(e);
let arr = ["blue", "yellow", "green", "black"];
let curclass = e.target.classList[1];
let idx = arr.indexOf(curclass);
idx = (idx + 1) % 4;
e.target.classList.remove(curclass);
e.target.classList.add(arr[idx]);
// whenever filter is toggled update it in local strg
let alltickets = JSON.parse(localStorage.getItem("alltickets"));
for (let i = 0; i < alltickets.length; i++) {
if (alltickets[i].T_ID == e.target.id) {
alltickets[i].T_filter = arr[idx];
break;
}
}
localStorage.setItem("alltickets", JSON.stringify(alltickets));
})
let trash=ticketdiv.querySelector(".ticket-id>div");
trash.addEventListener("click",(e)=>{
let id=e.target.id;
//UI
ticketdiv.remove();
// DB
let alltickets = JSON.parse(localStorage.getItem("alltickets"));
let arr=alltickets.filter((obj)=>{
return obj.T_ID!=id;
})
localStorage.setItem("alltickets", JSON.stringify(arr));
})
ticket.append(ticketdiv);
closemodal();
// add it to local storage
let obj = { "T_ID": ID, "T_Text": text, "T_filter": activemodalfilter };
let curstrg = JSON.parse(localStorage.getItem("alltickets"));
curstrg.push(obj);
localStorage.setItem("alltickets", JSON.stringify(curstrg));
}
closetab.addEventListener("click", closemodal)
function closemodal(e) {
if (document.querySelector(".modal")) {
document.querySelector(".modal").remove();
}
}
document.querySelector(".close-cross").addEventListener("click",(e)=>{
ticket.innerHTML="";
localStorage.setItem("alltickets",JSON.stringify([]));
})