-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
171 lines (139 loc) · 5.61 KB
/
index.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
var rgtClickContextMenu = document.getElementById('div-context-menu');
var rgtClickContextMenuFolder = document.getElementById('div-context-menu-folder');
/** close the right click context menu on click anywhere else in the page*/
document.onclick = function(e){
rgtClickContextMenu.style.display = 'none';
rgtClickContextMenuFolder.style.display = 'none';
}
/**
present the right click context menu ONLY for the elements having the right class
by replacing the 0 or any digit after the "to-" string with the element id , which
triggered the event
*/
document.oncontextmenu = function(e){
var elmnt = e.target;
rgtClickContextMenu.style.display = 'none';
// alert(elmnt.className);
if ( elmnt.className === "cls-context-menu-image") {
// alert("Is image");
var infoLink = "/viewphoto.php?id=" + elmnt.id;
e.preventDefault();
rgtClickContextMenu.style.left = e.pageX + 'px'
rgtClickContextMenu.style.top = e.pageY + 'px'
rgtClickContextMenu.style.display = 'block'
rgtClickContextMenu.innerHTML = rgtClickContextMenu.innerHTML.replace("theLink",infoLink)
} else if ( elmnt.className === "cls-context-menu-folder") {
var deleteFolderLink = "/deleteFolder.php?id=" + elmnt.getAttribute("data-id");
e.preventDefault();
rgtClickContextMenuFolder.style.left = e.pageX + 'px'
rgtClickContextMenuFolder.style.top = e.pageY + 'px'
rgtClickContextMenuFolder.style.display = 'block'
rgtClickContextMenuFolder.innerHTML = rgtClickContextMenuFolder.innerHTML.replace("theLink",deleteFolderLink)
}
}
var uploadModal = document.getElementById("newImageModal");
var modal = document.getElementById("newFolderModal");
// Get the button that opens the modal
var btn = document.getElementById("folderBtn");
// Get the <span> element that closes the modal
var span = document.getElementById("closeFolder");
var uploadSpan = document.getElementById("closeUpload");
// When the user clicks the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks on <span> (x), close the modal
uploadSpan.onclick = function() {
uploadModal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
if (event.target == uploadModal) {
uploadModal.style.display = "none";
}
}
var uploadInput = document.getElementById("imageUploadInput");
var uploadBtn = document.getElementById("uploadBtn");
// When the user clicks the button, open the modal
uploadBtn.onclick = function() {
uploadInput.click();
}
uploadInput.onchange = function() {
uploadModal.style.display = "block";
}
/////////////////////////////////
// DRAG AND DROP ELEMENTS //
/////////////////////////////////
var draggables = [];
var droppableFolders = document.getElementsByClassName("cls-context-menu-folder");
var draggableFolders = document.getElementsByClassName("gridBoxFolder");
var draggableImages = document.getElementsByClassName("gridBoxImage");
var droppedId = 0;
var droppedType = "";
for (var droppableFolderIndex = 0; droppableFolderIndex < droppableFolders.length; droppableFolderIndex++) {
droppableFolders[droppableFolderIndex].addEventListener('dragenter', function(event) {
setTimeout(function(){
document.getElementById("folder" + event.target.getAttribute("data-id")).style.background = "#CCCCCC";
}, 1);
event.preventDefault();
}, true);
droppableFolders[droppableFolderIndex].addEventListener('dragover', function(event) {
event.preventDefault();
return false;
}, true);
droppableFolders[droppableFolderIndex].addEventListener('dragleave', function(event) {
if (event.target.getAttribute("data-largest")) {
document.getElementById("folder" + event.target.getAttribute("data-id")).style.background = "#555";
}
}, true);
droppableFolders[droppableFolderIndex].addEventListener('drop', function(event) {
droppedId = parseInt(event.target.getAttribute("data-id"));
event.stopPropagation();
//event.target.style.background = 'white';
dnd_successful = true;
event.preventDefault(); //Prevent Firefox from redirecting the page
}, true);
}
for (var draggableFolderIndex = 0; draggableFolderIndex < draggableFolders.length; draggableFolderIndex++) {
draggables.push(draggableFolders[draggableFolderIndex]);
}
for (var draggableImageIndex = 0; draggableImageIndex < draggableImages.length; draggableImageIndex++) {
draggables.push(draggableImages[draggableImageIndex]);
}
for (var i = 0; i < draggables.length; i++) {
draggables[i].addEventListener('dragstart', function(event) {
setTimeout(function(){
event.target.style.opacity = "0.5";
}, 1);
/* Allow specific type of transfers */
dnd_successful = false;
}, false);
draggables[i].addEventListener('dragend', function(event) {
if (dnd_successful) {
// did drop in folder
if (event.target.className == "gridBoxFolder") {
droppedType = "folder";
} else {
droppedType = "image";
}
var didConfirm = confirm("Are you sure you want to move the " + droppedType + " with ID " + parseInt(event.target.getAttribute("data-id")) + " into the folder with ID " + droppedId + "?");
if (didConfirm == true) {
window.location = "/moveItem.php?from=" + event.target.getAttribute("data-id") + "&to=" + droppedId + "&type=" + droppedType;
} else {
document.getElementById("folder" + droppedId).style.background = "#555";
event.target.style.opacity = "1.0";
droppedID = 0;
droppedType = "";
}
} else {
event.target.style.opacity = "1.0";
}
}, false);
}