-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathdrag.js
More file actions
393 lines (317 loc) · 11 KB
/
drag.js
File metadata and controls
393 lines (317 loc) · 11 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
const dragcontainer = document.getElementById("dragcontainer");
const actions = document.getElementById("actions");
const addBtn = document.getElementById("add-btn");
const edit = document.getElementById("edit");
const fieldTypes = document.querySelector(".field-types");
dragcontainer.addEventListener('dragstart', dragStart);
dragcontainer.addEventListener('dragend', dragEnd);
dragcontainer.addEventListener('dragover', dragOver);
dragcontainer.addEventListener('dragenter', dragEnter);
dragcontainer.addEventListener('dragleave', dragLeave);
dragcontainer.addEventListener('drop', drop);
dragcontainer.addEventListener('mouseover', over);
//dragcontainer.addEventListener('mouseout', out);
actions.addEventListener('click', actionsClick);
edit.addEventListener('click', editClick);
edit.addEventListener('change', editChange);
addBtn.addEventListener('click', addRow);
fieldTypes.addEventListener('click', addRow);
let dragged = null;
let highlighted = null;
let editElement = null;
function addRow(event) {
let type = event.target.closest("a")?.dataset.type ?? "text";
let template = document.getElementById("template").firstElementChild.cloneNode(true);
console.log(type);
template =template.outerHTML;
let newId = Math.floor(Math.random() * 10000);
template = template.replaceAll(name + '[0]', name + '[' + newId + ']').
replaceAll(name + '[#]', name + '[' + newId + ']').
replaceAll(name + '#', name + newId ).
replaceAll("[" + name +"][#]", "[" + name +"][" + newId + "]").
replace('d-none', '');
template = generateElements(template)[0];
dragcontainer.querySelector(".dragzone").append(template);
if (type != "text") {
editElement = template.querySelector(".col");
highlighted = editElement;
highlighted.querySelector("[data-v-field-type]").value = type;
renderField(template);
}
}
function addCol(event) {
const template = document.getElementById("template").firstElementChild.cloneNode(true);
dragcontainer.querySelector(".dragzone").append(template);
}
function buildParams( prefix, obj, add ) {
let rbracket = /\[\]$/;
if ( Array.isArray( obj ) ) {
// Serialize array item.
for(const key in obj) {
let v = obj[key];
if ( rbracket.test( prefix ) ) {
// Treat each array item as a scalar.
add( prefix, v );
} else {
// Item is non-scalar (array or object), encode its numeric index.
buildParams(
prefix + "[" + ( typeof v === "object" && v != null ? key : "" ) + "]",
v,
add
);
}
}
} else if ( typeof obj === "object" ) {
// Serialize object item.
for (const name in obj ) {
buildParams( prefix + "[" + name + "]", obj[ name ], add );
}
} else {
// Serialize scalar item.
add( prefix, obj );
}
}
// Serialize an array of form elements or a set of
// key/values into a query string
function nestedFormData( a ) {
let prefix,
s = [],
add = function( key, valueOrFunction ) {
// If value is a function, invoke it and use its return value
let value = typeof valueOrFunction === "function" ?
valueOrFunction() :
valueOrFunction;
s[ s.length ] = encodeURIComponent( key ) + "=" +
encodeURIComponent( value == null ? "" : value );
};
if ( a == null ) {
return "";
}
// If an array was passed in, assume that it is an array of form elements.
if ( Array.isArray( a ) || ( Object.is( a ) ) ) {
// Serialize the form elements
for(const key in object) {
let v = object[key];
//jQuery.each( a, function() {
add( key, v );
};
} else {
// If traditional, encode the "old" way (the way 1.3.2 or older
// did it), otherwise encode params recursively.
for (const prefix in a ) {
buildParams( prefix, a[ prefix ], add );
}
}
// Return the resulting serialization
return s.join( "&" );
};
function serialize(parentElement, prefix) {
let data = {}
parentElement.querySelectorAll("input[type=text],input[type=number],input[type=hidden],input[type=checkbox]:checked,input[type=radio]:checked,input[name=image],select:not(:disabled), textarea").forEach( (el, i) => {
//if (el.offsetParent)
let name = el.name;//.replace('field[', '[field][');
prefix = "";
data[prefix + name] = el.value;
});
return data;
}
function editChange(event) {
let element = event.target;
let name = element.name;
let value = element.value;
let fieldName = name.replace(/.+\[|\]/g,'');
if (fieldName == "cols") {
highlighted.setAttribute("class", "col " + value);
}
renderField(edit);
}
function renderField(editContainer) {
let data = {};
editContainer.querySelectorAll('.tab-pane').forEach((e) => {
let name = e.getAttribute('aria-labelledby');
if (name == 'conditions') return;
data = {...data, ...serialize(e, name)};
});
data["field_id"] = highlighted.dataset.id;//edit.querySelector("[data-v-field-id]")?.value;
data["type"] = editContainer.querySelector('[data-v-field-type]')?.value;//edit.querySelector("[data-v-field-id]")?.value;
data["csrf"] = document.querySelector('[data-v-csrf]')?.value;//edit.querySelector("[data-v-field-id]")?.value;
fetch(window.location.pathname + "?module=field/field-group&action=field", {
method: "POST",
headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
//body: new URLSearchParams(data)
body: nestedFormData(data)
})
.then((response) => {
if (!response.ok) { throw new Error(response) }
return response.json()
})
.then((data) => {
let focusName = document.activeElement?.name ?? "";
editElement.querySelector(".input").innerHTML = data['field'];
editContainer.querySelectorAll(".tab-pane")?.forEach(e => {
let name = e.getAttribute('aria-labelledby');
if (name && data[name]) {
e.innerHTML = data[name];
}
});
if (focusName) editContainer.querySelector("[name='" + focusName + "']")?.focus();
})
.catch(error => {
console.log(error);
displayToast("danger", "Error", "Error loading translations!");
});
/*
if (element.name == "type") {
template = document.getElementById(value + "-field").cloneNode(true);
highlighted.querySelector(".input").replaceChildren(...template.children);
}*/
}
function editClick(event) {
let element;
if (element = event.target.closest(".btn-edit")) {
}
if (element = event.target.closest(".nav-item > .nav-link")) {
let id = element.getAttribute("aria-controls");
let tab = edit.querySelector('[aria-labelledby="' + id + '"]');
console.log("[aria-labelledby=" + id + "]");
element.closest(".nav-tabs")?.querySelectorAll(".nav-link")?.forEach(e => e.classList.remove("active"));
element?.classList.add("active");
tab?.parentNode.querySelectorAll(".tab-pane")?.forEach(e => e.classList.remove("active"));
tab?.classList.add("active");
}
}
function actionsClick(event) {
let element;
if (element = event.target.closest(".btn-edit")) {
let editContainer = highlighted.querySelector(".edit");
if (edit.classList.contains("show")) {
element.querySelector("i").setAttribute("class", "la la-angle-down");
edit.classList.remove("show");
/*
editContainer.replaceWith(edit.cloneNode(true));
editContainer.removeAttribute("id");
*/
//editContainer.querySelectorAll(".tab-pane").forEach(e => editContainer.replaceChildren(...edit.children);
editContainer.replaceChildren(...edit.children);
editElement = null;
} else {
element.querySelector("i").setAttribute("class", "la la-angle-up");
edit.classList.add("show")
edit.replaceChildren(...editContainer.children);
/*
edit.replaceWith(editContainer.cloneNode(true));
edit.setAttribute("id", "edit");
*/
editElement = highlighted;
}
event.preventDefault();
}
if (element = event.target.closest(".btn-add")) {
let template = document.getElementById("template").firstElementChild.cloneNode(true);
if (highlighted.classList.contains("col")) {
template = template.querySelector(".col");
console.log("col");
}
highlighted.after(template);
}
if (element = event.target.closest(".btn-clone")) {
if (highlighted.classList.contains("col")) {
}
let template = highlighted.cloneNode(true);
highlighted.after(template);
}
if (element = event.target.closest(".btn-remove")) {
if (confirm("Are you sure?")) {
if (highlighted) {
let form = document.getElementById('field-form');
console.log(form);
form.append(generateElements('<input type="hidden" name="delete[field_id][]" value="' + highlighted.dataset.id + '">')[0]);
}
highlighted.remove();
highlighted = null;
actions.style.display = "none";
}
}
}
function out(event) {
}
function over(event) {
//if (event.target.parentNode == actions) return;
let classList = event.target.classList;
//if (! (classList.contains("row") || classList.contains("col")) ) {
if (! (classList.contains("col")) ) {
return;
}
if (edit.classList.contains("show")) {
return;
}
highlighted = event.target;
const rect = highlighted.getBoundingClientRect();
actions.style.opacity = "0";
actions.style.top = (rect.top + window.scrollY) + "px";
actions.style.left = (rect.x + rect.width - actions.clientWidth + window.scrollX - 1 ) + "px";
if (!edit.classList.contains("show")) {
edit.style.top = (rect.bottom + window.scrollY) + "px";
}
if (rect.top < 80) {
actions.style.display = "none";
highlighted = null;
} else {
actions.style.display = "block";
actions.style.opacity = "1";
}
}
function dragStart(event) {
event.target.classList.add("dragstart");
dragged = event.target;
actions.style.display = "none";
}
function dragEnd(event) {
event.target.classList.remove("dragstart");
}
function dragEnter(event) {
event.target.classList.add("drop");
}
function dragLeave(event) {
event.target.classList.remove("drop");
}
function dragOver(event) {
event.preventDefault();
}
function drop(event) {
let container = event.target;
let element = event.currentTarget;
event.target.classList.remove("drop");
//console.log("drop", dragged, container, element);
let parentRow = dragged.parentNode;
if (dragged.classList.contains("row")) {//row
if (container.classList.contains("container-fluid")) {//container
container.appendChild(dragged);
} else if (container.classList.contains("row")) {//row
container.after(dragged);
} else if (container.classList.contains("col")) {//col
//if row dragged inside a col create a parent col and add after
let col = generateElements('<div class="col" draggable="true"></div>')[0];
col.append(dragged);
container.after(col);
//container.append(dragged);
}
} else
if (dragged.classList.contains("col")) {//col
if (container.classList.contains("row")) {
container.appendChild(dragged);
} else if (container.classList.contains("col")) {
container.after(dragged);
} else if (container.classList.contains("container-fluid")) {//container
//if col dragged outiside a row (container) create a parent row
let row = generateElements('<div class="row" draggable="true"></div>')[0];
row.append(dragged);
container.append(row);
}
}
//remove empty rows/cols
if (!parentRow.childElementCount) {
parentRow.remove();
}
//event.dataTransfer.clearData();
}