-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
223 lines (195 loc) Β· 5.57 KB
/
main.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
const URL = "./request.php?method=";
const ROOT_TITLE = "root";
const ROOT_MESSAGE = "Create root";
const CHILDREN_HIDE = "fa-angle-right";
const CHILDREN_SHOW = "fa-angle-down";
const ITEM_BTN_ADD = "fa-plus";
const ITEM_BTN_DELETE = "fa-minus";
class Request {
constructor() {
this.method = "read";
this.data = {};
}
create(data) {
this.method = "create";
this.data = data;
return this;
}
update(id, data) {
this.method = "update";
this.data = { id, ...data };
return this;
}
delete(id) {
this.method = "delete";
this.data = { id };
return this;
}
async execute() {
const response = await fetch(URL + this.method, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(this.data),
});
return await response.json();
}
}
class App {
constructor() {
this.application = document.getElementById("app");
this.modalAdd = document.getElementById("add");
this.modalRemove = document.getElementById("remove");
this.backDrop = document.getElementById("backdrop");
this.modalItemId = null;
this.modalItemUpdate = false;
}
request() {
return new Request();
}
loading() {
this.application.innerHTML = '<i class="fa-solid fa-spinner fa-spin"></i>';
}
btnRoot(message = ROOT_MESSAGE) {
this.application.innerHTML = `<span class="btn btn-primary" onclick="app.createRoot()">${message}</span>`;
}
callbackAfterExecute(data) {
if (data && data.length > 0) {
this.application.innerHTML = this.template(this.generateTreeFromItems(data));
} else {
this.btnRoot();
}
this.closeModal();
}
createRequest(data) {
this.loading();
this.request()
.create(data)
.execute()
.then((data) => this.callbackAfterExecute(data));
}
updateRequest(id, data) {
this.loading();
this.request()
.update(id, data)
.execute()
.then((data) => this.callbackAfterExecute(data));
}
deleteRequest() {
this.loading();
this.request()
.delete(this.modalItemId)
.execute()
.then((data) => this.callbackAfterExecute(data));
}
readItems() {
this.loading();
this.request()
.execute()
.then((data) => this.callbackAfterExecute(data));
}
generateTreeFromItems(params) {
let values = Object.values(params);
let map = {};
values.forEach(function (row) {
map[row.id] = {
title: row.title,
id: row.id,
parent: row.parent_id,
children: [],
};
});
values.forEach(function (row) {
if (map[row.parent_id]) {
map[row.parent_id].children.push(map[row.id]);
}
});
Object.keys(map).forEach((k) => {
if (map[k].parent != 0) {
delete map[k];
}
});
return map;
}
template(params) {
let li = [];
Object.values(params).forEach((row) => {
li.push(
`<li>
<div class="d-flex justify-content-between my-1">
<div>
<span class="branch-name" onclick="app.update(${row.id}, '${row.title}')">${row.title}</span>
</div>
<div class="btn-group" role="group">
<span class="btn btn-outline-secondary btn-sm" onclick="app.add(${row.id}, '${row.title}')"><i class="fa-solid ${ITEM_BTN_ADD}"></i></span>
<span class="btn btn-outline-secondary btn-sm" onclick="app.remove(${row.id}, '${row.title}')"><i class="fa-solid ${ITEM_BTN_DELETE}"></i></span>
</div>
</div>
${row.children.length ? this.template(row.children) : ""}
</li>`
);
});
return li.length ? `<ul>${li.join("")}</ul>` : "";
}
openModal(modal, id) {
this.modalItemId = id;
if (!modal.classList.contains("show")) {
modal.classList.add("show", "d-block");
}
if (this.backDrop.classList.contains("d-none")) {
this.backDrop.classList.remove("d-none");
this.backDrop.classList.add("show");
}
}
closeModal() {
document.querySelectorAll(".modal input").forEach(input => input.value = "");
document.querySelectorAll(".modal").forEach(modal => {
if (modal.classList.contains("show")) {
modal.classList.remove("show", "d-block");
}
});
if (!this.backDrop.classList.contains("d-none")) {
this.backDrop.classList.add("d-none");
this.backDrop.classList.remove("show");
}
this.modalItemId = null;
}
createRoot() {
this.createRequest({
title: ROOT_TITLE,
});
}
add(id, title) {
this.modalItemUpdate = false;
this.modalAdd.querySelector(".modal-title").textContent = `Add item in "${title}"`;
this.modalAdd.querySelector(".btn-primary").textContent = "Add item";
this.openModal(this.modalAdd, id);
}
addSubmit() {
let data = {
title: this.modalAdd.querySelector("input").value,
};
if (this.modalItemUpdate) {
this.updateRequest(this.modalItemId, data);
} else {
data.parent_id = this.modalItemId;
this.createRequest(data);
}
}
remove(id, title) {
this.modalRemove.querySelector(".modal-body").textContent = `Would you like to delete "${title}"? Are you sure?`;
this.openModal(this.modalRemove, id);
}
update(id, title) {
this.modalItemUpdate = true;
this.modalAdd.querySelector(".modal-title").textContent = `Edit item "${title}"`;
this.modalAdd.querySelector(".btn-primary").textContent = "Edit item";
this.modalAdd.querySelector("input").value = title;
this.openModal(this.modalAdd, id);
}
}
var app = new App();
window.onload = function () {
app.readItems();
};