forked from assembler-institute/filesystem-explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
executable file
·153 lines (103 loc) · 4 KB
/
script.js
File metadata and controls
executable file
·153 lines (103 loc) · 4 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
//------------------------ CREATE FOLDER -----------------------------
const createFolder = document.querySelector('.create-btn');
const folderNameToCreate = document.querySelector('.folder-name');
createFolder.addEventListener("click", createNewFolder);
let folder = document.querySelectorAll(".folder");
folder.forEach( element => element.addEventListener("click", nameFolder));
let nombre = "";
function nameFolder(e){
nombre = e.srcElement.getAttribute("value");
console.log(nombre);
return nombre;
}
function createNewFolder() {
// fetch("check-request.php")
// .then(response => response.json())
// .then(data => nombre = data)
let folderName = folderNameToCreate.value;
if (nombre === "") {
fetch ("../filesystem-explorer/create-folder.php?nameFolder="+folderName)
.then(response => response.json())
.then(data => console.log(data))
} else {
fetch ("../filesystem-explorer/create-folder.php?nameFolder="+folderName+"&name="+nombre)
.then(response => response.json())
.then(data => console.log(data))
}
window.location.reload();
}
// -------------------- DELETE FILES & FOLDERS ----------------------
const deleteFiles = document.querySelectorAll(".delete-btn");
deleteFiles.forEach(element => {
element.addEventListener("click", deleteFolder)
});
function deleteFolder(event){
const popUpDeleteConfirm = confirm("Do you want to delete this folder?");
console.log(popUpDeleteConfirm);
if (popUpDeleteConfirm){
let actualFolderName = event.srcElement.getAttribute("actual-folder");
console.log(actualFolderName);
if (!actualFolderName.includes(".")) {
fetch("delete-folder.php?actualFolderName=./"+actualFolderName)
.then(response=>response.json())
.then(data =>console.log(data));
} else {
fetch("delete-file.php?actualFolderName=./"+actualFolderName)
.then(response=>response.json())
.then(data =>console.log(data));
}
}
window.location.reload();
}
// ------------------- MODIFY FOLDER -------------------
const edit = document.querySelectorAll(".edit-btn");
for (let i of edit){
i.addEventListener("click", editName);
let oldname = i.getAttribute("actualFolder");
}
// -------------- EDIT NAME -------------------------
function editName(){
newname = prompt("Write the new name of the folder or file:");
let dataform = new FormData();
dataform.append("oldName", oldname);
dataform.append("newname", newname);
fetch ("./edit-folder.php",{
method : "POST",
body : dataform
})
.then(response=>response.json())
.then(data =>console.log(data));
window.location.reload();
}
// ------------ SEARCH FUNCTION ------------------------
const buscador = document.querySelector(".buscador");
const buscadorValue = document.querySelector(".buscadorValue");
const listSearch = document.querySelector("#searchResults");
buscador.addEventListener("click", search);
function search(e){
e.preventDefault();
let searchValue = buscadorValue.value;
fetch("search.php?searchParam="+searchValue)
.then(response=>response.json())
.then(data =>displayResults(data));
}
function displayResults(data){
if (listSearch.firstChild){
while(listSearch.firstChild){
listSearch.removeChild(listSearch.firstChild);
}
}
data.forEach(element => {
if(element.includes(".")){
const li = document.createElement("li");
li.innerHTML="<a href="+element+">"+element+"</a>";
listSearch.appendChild(li);
} else {
let file = element.split("/");
let fileName = file[file.length-1];
const li = document.createElement("li");
li.innerHTML="<a href=?name="+fileName+">"+fileName+"</a>";
listSearch.appendChild(li);
}
});
}