-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirectory_handler.js
179 lines (165 loc) · 7.58 KB
/
directory_handler.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
const directorySelectButton = document.querySelector('.directorySelect')
const directorySelectWindow = (function() {
const window = document.createElement('div')
window.className = 'window'
const windowHeader = document.createElement('p')
windowHeader.textContent = 'Select File'
window.appendChild(windowHeader)
const entriesWrapper = document.createElement('div')
entriesWrapper.className = 'entriesWrapper'
window.appendChild(entriesWrapper)
const newDirButton = document.createElement('div')
newDirButton.className = 'windowButton'
newDirButton.textContent = '+'
newDirButton.addEventListener('click', event => {
save.newDir().then(response => {
makeFileRequest('/filelist').then(response => {
while(entriesWrapper.firstChild) entriesWrapper.removeChild(entriesWrapper.firstChild)
JSON.parse(response.data).forEach(file => createEntry(file))
})
})
})
window.appendChild(newDirButton)
return window
})()
const entriesWrapper = directorySelectWindow.querySelector('.entriesWrapper')
directorySelectButton.addEventListener('click', () => openDirectorySelect())
let _bold
function closeWindow(event) {
if(event.target != directorySelectWindow &&
!directorySelectWindow.contains(event.target) &&
!event.target != contextMenus.pane &&
!contextMenus.pane.contains(event.target)) {
document.body.removeChild(directorySelectWindow)
while(entriesWrapper.firstChild) entriesWrapper.removeChild(entriesWrapper.firstChild)
removeEventListener('mousedown', closeWindow)
}
}
function openDirectorySelect(bold = false) {
_bold = bold
if(!bold) addEventListener('mousedown', closeWindow)
directorySelectWindow.style.zIndex = bold ? 7 : 5
document.body.appendChild(directorySelectWindow)
refreshEntries()
}
let disabled = false
const screen = (function() {
const block = document.createElement('div')
block.className = 'block'
function disableScreen(loading = true) {
if(loading) {
block.style.cursor = 'wait'
disabled = true
}
else block.style.cursor = 'default'
document.body.appendChild(block)
}
function enableScreen() {
document.body.removeChild(block)
disabled = false
}
return { enable: enableScreen, disable: disableScreen }
})()
function createEntry(file) {
const entry = document.createElement('div')
entry.className = 'fileOption'
entry.textContent = file.replace(/_/g, ' ')
entry.addEventListener('click', event => {
if(_bold) addEventListener('mousedown', closeWindow)
if(!disabled) {
screen.disable()
unload()
if(load('file/' + file) !== errors.PARSE_ISSUE) {
directorySelectButton.textContent = file.replace(/_/g, ' ')
mainDirectory = 'file/' + file
}
}
})
entry.addEventListener('mousedown', event => {
if(event.which == 3 && !disabled) {
contextMenus.open(4, [
() => {
},
() => {
},
() => {
makeFileRequest('/filelist').then(response => {
const list = JSON.parse(response.data)
if(list.length > 1) {
contextMenus.close()
save.deleteDir(file).then(refreshEntries)
if(mainDirectory == 'file/' + file) {
const newFile = (list[0] == file ? list[1] : list[0])
mainDirectory = 'file/' + newFile
unload()
load(mainDirectory)
directorySelectButton.textContent = newFile.replace(/_/g, ' ')
}
}
})
}
], event, pane => {
const e0 = pane.querySelector('.e0')
e0.addEventListener('click', event => {
e0.textContent = file
inputify(e0, newName => {
const sanitisedText = newName.replace(/[ :?\\/*<>"|]/g, '_')
let duplicated = false
makeFileRequest('/filelist').then(response => {
JSON.parse(response.data).forEach(name => {
if(name == sanitisedText) duplicated = true
})
if(!duplicated) {
if(mainDirectory.endsWith(file)) {
mainDirectory = 'file/' + sanitisedText
directorySelectButton.textContent = newName
}
save.renameDir(file, sanitisedText)
entry.textContent = newName
file = sanitisedText
}
})
})
})
const e1 = pane.querySelector('.e1')
e1.addEventListener('click', event => {
save.duplicateDir(file).then(refreshEntries).then(() => {
e1.textContent = file
makeFileRequest('duplicatedfile').then(response => {
file = JSON.parse(response.data)
unload()
if(load('file/' + file) !== errors.PARSE_ISSUE) {
directorySelectButton.textContent = file.replace(/_/g, ' ')
mainDirectory = 'file/' + file
}
inputify(e1, newName => {
const sanitisedText = newName.replace(/[ :?\\/*<>"|]/g, '_')
let duplicated = false
makeFileRequest('/filelist').then(response => {
JSON.parse(response.data).forEach(name => {
if(name == sanitisedText) duplicated = true
})
if(!duplicated) {
save.renameDir(file, sanitisedText)
entry.textContent = newName
file = sanitisedText
mainDirectory = 'file/' + file
refreshEntries()
directorySelectButton.textContent = sanitisedText
}
})
})
})
})
})
})
}
})
entriesWrapper.appendChild(entry)
}
function refreshEntries() {
makeFileRequest('/filelist').then(response => {
while(entriesWrapper.firstChild) entriesWrapper.removeChild(entriesWrapper.firstChild)
JSON.parse(response.data).forEach(file => createEntry(file))
})
}