Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
337 lines (271 loc) · 11.1 KB
/
Copy pathscript.js
File metadata and controls
337 lines (271 loc) · 11.1 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
// Create an array to store window elements
var windows = [];
var taskbar = [];
let index = 1;
// Function to initialize a window and its event listeners
function initWindow(windowElement) {
var isMouseDown = false;
var offsetX, offsetY, windowAmount;
var titleBar = windowElement.querySelector('.titleBar');
var closeButton = windowElement.querySelector('.close'); // Get the close button element
var leftResize = windowElement.querySelector('.left')
var rightResize = windowElement.querySelector('.right')
var topResize = windowElement.querySelector('.top')
var bottomResize = windowElement.querySelector('.bottom')
var initialWidth = windowElement.offsetWidth
var initialHeight = windowElement.offsetHeight
var initialLeft, initialRight, initialTop, initialBottom
var leftDown, rightDown, topDown, bottomDown;
windowElement.addEventListener('mousedown', function (e) {
index += 1;
windowElement.style.zIndex = index
})
titleBar.addEventListener('mousedown', function (e) {
isMouseDown = true;
offsetX = e.clientX - windowElement.getBoundingClientRect().left;
offsetY = e.clientY - windowElement.getBoundingClientRect().top;
});
closeButton.addEventListener('click', function () {
// Remove the window element from the DOM
windowElement.remove();
// Remove the window element from the windows array
var indexToRemove = windows.indexOf(windowElement);
if (indexToRemove !== -1) {
windows.splice(indexToRemove, 1);
}
});
document.addEventListener('mouseup', function () {
isMouseDown = false;
leftDown = false
rightDown = false
topDown = false
bottomDown = false
});
document.addEventListener('mousemove', function (e) {
if (isMouseDown) {
// Check if the mouse is within the viewport
if (
e.clientX >= 0 && e.clientX <= window.innerWidth &&
e.clientY >= 0 && e.clientY <= window.innerHeight
) {
windowElement.style.left = (e.clientX - offsetX) + 'px';
windowElement.style.top = (e.clientY - offsetY) + 'px';
}
}
if (leftDown) {
const newWidth = initialWidth + (offsetX - e.clientX); // Calculate the new width
const newLeft = initialLeft - (offsetX - e.clientX); // Calculate the new left position
if (newWidth >= 200) {
windowElement.style.width = newWidth + 'px';
windowElement.style.left = newLeft + 'px';
}
}
if (rightDown) {
const newWidth = initialWidth + (e.clientX - offsetX); // Calculate the new width
if (newWidth >= 200) {
windowElement.style.width = newWidth + 'px';
}
}
if (topDown) {
const newHeight = initialHeight - (e.clientY - offsetY);
const newTop = initialTop + (e.clientY - offsetY)
if (newHeight >= 200) {
windowElement.style.height = newHeight + 'px'
windowElement.style.top = newTop + 'px'
}
}
if (bottomDown) {
const newHeight = initialHeight + (e.clientY - offsetY);
if (newHeight >= 200) {
windowElement.style.height = newHeight + 'px'
}
}
});
leftResize.addEventListener('mousedown', function (e) {
leftDown = true;
offsetX = e.clientX;
initialWidth = windowElement.offsetWidth; // Store the initial width
initialLeft = windowElement.getBoundingClientRect().left; // Store the initial left position
});
rightResize.addEventListener('mousedown', function (e) {
rightDown = true;
offsetX = e.clientX;
initialWidth = windowElement.offsetWidth; // Store the initial width
initialRight = windowElement.getBoundingClientRect().right; // Store the initial left position
});
topResize.addEventListener('mousedown', function(e) {
topDown = true;
offsetY = e.clientY
initialHeight = windowElement.offsetHeight
initialTop = windowElement.getBoundingClientRect().top
})
bottomResize.addEventListener('mousedown', function(e) {
bottomDown = true
offsetY = e.clientY
initialHeight = windowElement.offsetHeight
initialBottom = windowElement.getBoundingClientRect().bottom
})
}
// Initialize windows
windows.push(document.getElementById('window1'));
windowAmount = windows.length;
// Initialize event listeners for each window
windows.forEach(function (windowElement) {
initWindow(windowElement);
});
taskbar.push(document.getElementById('test'))
taskbar.push(document.getElementById('test2'))
taskbar.forEach(function (windowElement) {
buttonPress(windowElement);
});
function buttonPress(windowElement) {
windowElement.addEventListener('click', function() {
var windoww = windowElement.getAttribute('windowName')
var windowMain = document.createElement('div')
windowMain.classList = "window"
// windowMain.id = "window" + windowAmount
// windowAmount =+ 1
var windowTitle = document.createElement('div')
windowTitle.classList = "titleBar"
var windowContent = document.createElement('div')
windowContent.classList = "windowContent"
var windowName = document.createElement('span')
windowName.innerText = windowElement.getAttribute('windowName')
var windowX = document.createElement('div')
windowX.classList = "close"
var windowXName = document.createElement('span')
windowXName.innerText = "X"
var leftResize = document.createElement('div')
leftResize.classList = "left"
var rightResize = document.createElement('div')
rightResize.classList = "right"
var topResize = document.createElement('div')
topResize.classList = "top"
var bottomResize = document.createElement('div')
bottomResize.classList = "bottom"
document.body.appendChild(windowMain)
windowMain.appendChild(windowTitle)
windowMain.appendChild(windowContent)
windowTitle.appendChild(windowName)
windowTitle.appendChild(windowX)
windowX.appendChild(windowXName)
windowMain.appendChild(leftResize)
windowMain.appendChild(rightResize)
windowMain.appendChild(topResize)
windowMain.appendChild(bottomResize)
if (windowElement.getAttribute('type') === "file") {
// Use fetch to load the file content
fetch(windowElement.getAttribute('fileSrc'))
.then(response => response.text())
.then(content => {
const windowContentText = document.createElement('p')
windowContentText.innerText = content
windowContent.appendChild(windowContentText)
})
.catch(error => {
console.error('Error loading file:', error);
});
} else if (windowElement.getAttribute('type') === 'fileSystem') {
fetch('./files.json')
.then((response) => response.json())
.then((json) => {
var fileSystem = document.createElement('div')
fileSystem.classList = 'fileSystem'
var search = document.createElement('input')
search.type = 'text'
search.classList = 'fileSearch'
search.placeholder = "address"
windowMain.appendChild(search)
search.addEventListener('change', function(e) {
var answer = []
if (search.value.length > 0) {
search.value = search.value + '/'
}
for (const x in search.value.split('/')) {
answer.push(search.value.split('/')[x])
}
var path = json
for (const x of answer) {
if (path[x]) {
path = path[x]
}
}
windowContent.innerHTML = ``
var fileSystem = document.createElement('div')
fileSystem.classList = 'fileSystem'
createFiles(path, windowContent, fileSystem, search)
})
createFiles(json, windowContent, fileSystem, search)
});
}
windows.push(windowMain)
initWindow(windowMain)
})
}
function createFiles(data, append, fileSystem, search) {
var folders = [];
for (const x in data) {
if (data[x].type === 'folder') {
folders.push(data[x])
}
}
var files = []
for (const x in data) {
if (data[x].type === 'file') {
files.push(data[x])
}
}
for (var f = 0; f < folders.length; f++) {
var whole = document.createElement('div')
whole.classList = 'folderWhole'
var ico = document.createElement('img')
ico.src = "./icos/folder.png"
var text = document.createElement('span')
var name = folders[f].name
if (name.length <= 8) {
text.innerText = name
} else {
text.innerText = name.slice(0, 5) + '...'
}
text.title = name
append.appendChild(fileSystem)
fileSystem.appendChild(whole)
whole.appendChild(ico)
whole.appendChild(text)
folderClick(whole, folders[f], text, append, search)
}
for (var f = 0; f < files.length; f++) {
var whole = document.createElement('div')
whole.classList = "fileWhole"
var ico = document.createElement('img')
ico.src = `./icos/${files[f].file}.png`
var text = document.createElement('span')
var name = files[f].name + ".js"
if (name.length <= 8) {
text.innerText = name
} else {
text.innerText = name.slice(0, 5) + '...'
}
text.title = name
append.appendChild(fileSystem)
fileSystem.appendChild(whole)
whole.appendChild(ico)
whole.appendChild(text)
}
}
function folderClick(folderElement, data, text, append, search) {
folderElement.addEventListener('click', function (e) {
var folderClicked;
for (const x in data) {
if (text.innerText === data[x].name) {
folderClicked = data[x]
console.log(text.innerText)
}
}
search.value = search.value + text.title + '/'
append.innerHTML = ''
var fileSystem = document.createElement('div')
fileSystem.classList = 'fileSystem'
createFiles(data, append, fileSystem, search)
})
}