This repository was archived by the owner on May 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
87 lines (68 loc) · 2.66 KB
/
app.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
/*
drag and drop events :
drag ondrag
dragend ondragend
dragenter ondragenter
dragexit ondragexit
dragleave ondragleave
dragover ondragover
dragstart ondragstart
drop ondrop
*/
// all elements in main div ' <div class="dropzone"></div> ' // i for loop
var AllDropZone = document.querySelectorAll(".dropzone") , i = 1;
// count of imgs i have in imgs folder
const imgsCount = 6;
// this loop for making all div's dropzone
for(zone of AllDropZone){
zone.dropzone = true;
// new element as icon content
let Newcontent = document.createElement("div");
// access to dragable
Newcontent.draggable = true;
// just id + class for usage :)
Newcontent.setAttribute("class","content");
Newcontent.setAttribute("id" , "content" + i); i+=1;
// if dropzone is greater than imgsCount that mean no image available
// only if still there image then should append image as icon
if(i < imgsCount){
Newcontent.style.cssText = `background-image: url("./imgs/icons (${i}).svg");`;
}
// on 'dragstart' event we sending reference of element ' ID '
Newcontent.addEventListener("dragstart" , e =>{
// sending id by using 'dataTransfer'
e.dataTransfer.setData("text/plain" , Newcontent.id);
});
// append directly with icon or without icon
zone.append(Newcontent);
}
// giving necessary events for each dropzone
for(let zone = 0; zone < AllDropZone.length ; zone+=1){
// in dragover 'preventDefault' just for avoiding error or exception happen
AllDropZone[zone].addEventListener("dragover" , e =>{
e.preventDefault();
// adding animation
AllDropZone[zone].style.cssText = "animation: onDragOver infinite 1s;";
});
// when drag leave we clear css animation
AllDropZone[zone].addEventListener("dragleave" , e =>{
AllDropZone[zone].style.cssText = "animation:'';";
});
// when element dropped in dropzone area
AllDropZone[zone].addEventListener("drop" , e => {
e.preventDefault();
// !!! old child & new child just swapping parents :)
// old child
let oldContent = AllDropZone[zone].firstChild;
// new child
let newChild = document.getElementById(e.dataTransfer.getData("text/plain"));
// parent new child
let ParentNCH = newChild.parentElement;
// append new child to zone
AllDropZone[zone].append(newChild);
// append old child to old zone
ParentNCH.append(oldContent);
// in drop clearing animation to
AllDropZone[zone].style.cssText = "animation:'';";
});
}