-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
739 lines (630 loc) · 23.8 KB
/
script.js
File metadata and controls
739 lines (630 loc) · 23.8 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
// Clock functionality
function updateClock() {
const now = new Date();
let hours = now.getHours();
const minutes = now.getMinutes();
const ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12; // 0 should be 12
const minutesStr = minutes < 10 ? '0' + minutes : minutes;
const timeString = hours + ':' + minutesStr + ' ' + ampm;
document.getElementById('clock').textContent = timeString;
}
updateClock();
setInterval(updateClock, 1000);
// Window dragging and resizing functionality
let activeWindow = null;
let isDragging = false;
let isResizing = false;
let resizeDirection = null;
let currentX;
let currentY;
let initialX;
let initialY;
let xOffset = 0;
let yOffset = 0;
let initialWidth;
let initialHeight;
const windows = document.querySelectorAll('.window');
windows.forEach(win => {
const titleBar = win.querySelector('.title-bar');
const closeButton = win.querySelector('.close-button');
const minimizeButton = win.querySelectorAll('.title-bar-button')[0];
const maximizeButton = win.querySelectorAll('.title-bar-button')[1];
// Add resize handles
addResizeHandles(win);
// Make window active when clicked
win.addEventListener('mousedown', (e) => {
makeWindowActive(win);
});
// Dragging
titleBar.addEventListener('mousedown', dragStart);
// Close button
closeButton.addEventListener('click', (e) => {
e.stopPropagation();
win.style.display = 'none';
win.classList.remove('minimized'); // Remove minimized state when closing
updateTaskbar();
});
// Minimize button
minimizeButton.addEventListener('click', (e) => {
e.stopPropagation();
win.classList.add('minimized');
win.style.display = 'none';
updateTaskbar();
});
// Maximize button (toggle between maximized and normal)
maximizeButton.addEventListener('click', (e) => {
e.stopPropagation();
if (win.style.width === '100%') {
// Restore
win.style.width = win.dataset.normalWidth || '600px';
win.style.height = win.dataset.normalHeight || '450px';
win.style.left = win.dataset.normalLeft || '150px';
win.style.top = win.dataset.normalTop || '80px';
} else {
// Maximize
win.dataset.normalWidth = win.style.width;
win.dataset.normalHeight = win.style.height;
win.dataset.normalLeft = win.style.left;
win.dataset.normalTop = win.style.top;
win.style.width = '100%';
win.style.height = 'calc(100% - 28px)';
win.style.left = '0';
win.style.top = '0';
}
});
});
function dragStart(e) {
if (e.target.classList.contains('title-bar-button')) {
return;
}
const win = e.target.closest('.window');
if (win.style.width === '100%') {
return; // Don't allow dragging maximized windows
}
activeWindow = win;
initialX = e.clientX - xOffset;
initialY = e.clientY - yOffset;
const transform = window.getComputedStyle(win).transform;
if (transform !== 'none') {
const matrix = new DOMMatrix(transform);
xOffset = matrix.m41;
yOffset = matrix.m42;
} else {
xOffset = parseInt(win.style.left) || 0;
yOffset = parseInt(win.style.top) || 0;
}
initialX = e.clientX - xOffset;
initialY = e.clientY - yOffset;
isDragging = true;
}
document.addEventListener('mousemove', drag);
document.addEventListener('mouseup', dragEnd);
function drag(e) {
if (isDragging && activeWindow) {
e.preventDefault();
currentX = e.clientX - initialX;
currentY = e.clientY - initialY;
xOffset = currentX;
yOffset = currentY;
setTranslate(currentX, currentY, activeWindow);
}
if (isResizing && activeWindow) {
e.preventDefault();
const deltaX = e.clientX - initialX;
const deltaY = e.clientY - initialY;
if (resizeDirection === 'right' || resizeDirection === 'corner') {
const newWidth = initialWidth + deltaX;
if (newWidth >= 200) {
activeWindow.style.width = newWidth + 'px';
}
}
if (resizeDirection === 'left') {
const newWidth = initialWidth - deltaX;
if (newWidth >= 200) {
activeWindow.style.width = newWidth + 'px';
activeWindow.style.left = (parseInt(activeWindow.dataset.initialLeft) + deltaX) + 'px';
}
}
if (resizeDirection === 'bottom' || resizeDirection === 'corner') {
const newHeight = initialHeight + deltaY;
if (newHeight >= 100) {
activeWindow.style.height = newHeight + 'px';
}
}
if (resizeDirection === 'top') {
const newHeight = initialHeight - deltaY;
if (newHeight >= 100) {
activeWindow.style.height = newHeight + 'px';
activeWindow.style.top = (parseInt(activeWindow.dataset.initialTop) + deltaY) + 'px';
}
}
}
}
function setTranslate(xPos, yPos, el) {
el.style.left = xPos + 'px';
el.style.top = yPos + 'px';
}
function dragEnd(e) {
initialX = currentX;
initialY = currentY;
isDragging = false;
isResizing = false;
resizeDirection = null;
}
function makeWindowActive(win) {
// Remove active class from all windows
windows.forEach(w => w.classList.remove('active'));
// Add active class to clicked window
win.classList.add('active');
// Bring to front
let maxZ = 0;
windows.forEach(w => {
const z = parseInt(window.getComputedStyle(w).zIndex) || 0;
if (z > maxZ) maxZ = z;
});
win.style.zIndex = maxZ + 1;
updateTaskbar();
}
// Add resize handles to a window
function addResizeHandles(win) {
// Check if handles already exist
if (win.querySelector('.resize-handle')) {
return;
}
// Left edge
const leftHandle = document.createElement('div');
leftHandle.className = 'resize-handle left';
leftHandle.addEventListener('mousedown', (e) => resizeStart(e, win, 'left'));
win.appendChild(leftHandle);
// Right edge
const rightHandle = document.createElement('div');
rightHandle.className = 'resize-handle right';
rightHandle.addEventListener('mousedown', (e) => resizeStart(e, win, 'right'));
win.appendChild(rightHandle);
// Top edge
const topHandle = document.createElement('div');
topHandle.className = 'resize-handle top';
topHandle.addEventListener('mousedown', (e) => resizeStart(e, win, 'top'));
win.appendChild(topHandle);
// Bottom edge
const bottomHandle = document.createElement('div');
bottomHandle.className = 'resize-handle bottom';
bottomHandle.addEventListener('mousedown', (e) => resizeStart(e, win, 'bottom'));
win.appendChild(bottomHandle);
// Corner
const cornerHandle = document.createElement('div');
cornerHandle.className = 'resize-handle corner';
cornerHandle.addEventListener('mousedown', (e) => resizeStart(e, win, 'corner'));
win.appendChild(cornerHandle);
}
// Start resizing
function resizeStart(e, win, direction) {
e.stopPropagation();
e.preventDefault();
if (win.style.width === '100%') {
return; // Don't allow resizing maximized windows
}
activeWindow = win;
isResizing = true;
resizeDirection = direction;
initialX = e.clientX;
initialY = e.clientY;
initialWidth = win.offsetWidth;
initialHeight = win.offsetHeight;
// Store initial position for left/top resizing
win.dataset.initialLeft = parseInt(win.style.left) || 0;
win.dataset.initialTop = parseInt(win.style.top) || 0;
makeWindowActive(win);
}
// Desktop icon functionality
const desktopIcons = document.querySelectorAll('.desktop-icon');
desktopIcons.forEach(icon => {
let clickCount = 0;
let clickTimer = null;
icon.addEventListener('click', (e) => {
clickCount++;
if (clickCount === 1) {
clickTimer = setTimeout(() => {
// Single click - select icon
desktopIcons.forEach(i => i.classList.remove('selected'));
icon.classList.add('selected');
clickCount = 0;
}, 300);
} else if (clickCount === 2) {
// Double click - open window
clearTimeout(clickTimer);
clickCount = 0;
const iconType = icon.dataset.icon;
openIconWindow(iconType);
}
});
});
// Function to open windows based on icon type
function openIconWindow(iconType) {
switch(iconType) {
case 'computer':
openMyComputerWindow();
break;
case 'documents':
openMyDocumentsWindow();
break;
case 'network':
openNetworkWindow();
break;
case 'recycle':
openRecycleBinWindow();
break;
case 'ie':
openInternetExplorerWindow();
break;
case 'normal-view':
// Navigate to the normal view
window.location.href = 'ematth.dev/index.html';
break;
}
}
// Unified Explorer window for My Documents, My Network Places, Recycle Bin, and My Computer
window.openExplorerWindow = function(location, title, src) {
const windowId = 'window-explorer';
const existingWindow = document.getElementById(windowId);
if (existingWindow) {
// Window exists - navigate to new location
existingWindow.style.display = 'flex';
makeWindowActive(existingWindow);
// Update title
const titleBar = existingWindow.querySelector('.title-bar-text');
if (titleBar) {
titleBar.textContent = title;
}
// Navigate iframe to new location
const iframe = existingWindow.querySelector('iframe');
if (iframe) {
iframe.src = src + '?t=' + Date.now();
}
updateTaskbar();
return;
}
// Create new window
createWindowWithIframe(windowId, title, src, 220, 140);
}
function openMyComputerWindow() {
openExplorerWindow('my-computer', 'My Computer', 'mycomputer.html');
}
window.openWelcomeTxt = function() {
createWindowWithIframe('window-welcome-txt', 'Welcome.txt - Notepad', 'documents/welcome-txt.html', 240, 160);
}
// Generic function to open any HTML document
window.openHTMLDocument = function(id, title, src, left, top) {
// Check if src is an external URL (http:// or https://)
if (src.startsWith('http://') || src.startsWith('https://')) {
// Open external links in a new tab
window.open(src, '_blank');
} else {
// Open local documents in an iframe window
createWindowWithIframe(id, title, src, left || 260, top || 180);
}
}
// Deprecated: kept for backwards compatibility
window.openExampleDocument = function() {
window.openHTMLDocument('window-example-doc', 'Example Document', 'documents/example-document.html', 260, 180);
}
function createWindowWithIframe(id, title, src, left = 200, top = 100) {
const existingWindow = document.getElementById(id);
if (existingWindow) {
existingWindow.style.display = 'flex';
makeWindowActive(existingWindow);
// Reload the iframe to show latest content
const iframe = existingWindow.querySelector('iframe');
if (iframe) {
iframe.src = iframe.src.split('?')[0] + '?t=' + Date.now();
}
return;
}
// Add cache-busting parameter to iframe src
const cacheBustedSrc = src + '?t=' + Date.now();
const windowHTML = `
<div class="window" id="${id}" style="left: ${left}px; top: ${top}px; width: 500px; height: 380px;">
<div class="title-bar">
<div class="title-bar-text">${title}</div>
<div class="title-bar-controls">
<button class="title-bar-button" aria-label="Minimize"></button>
<button class="title-bar-button" aria-label="Maximize"></button>
<button class="title-bar-button close-button" aria-label="Close"></button>
</div>
</div>
<div class="window-content">
<div class="menu-bar">
<span class="menu-item"><u>F</u>ile</span>
<span class="menu-item"><u>E</u>dit</span>
<span class="menu-item"><u>V</u>iew</span>
<span class="menu-item">F<u>a</u>vorites</span>
<span class="menu-item"><u>T</u>ools</span>
<span class="menu-item"><u>H</u>elp</span>
</div>
<iframe src="${cacheBustedSrc}" frameborder="0" style="width: 100%; flex: 1; border: none; background: white;"></iframe>
</div>
</div>
`;
const desktop = document.querySelector('.desktop');
desktop.insertAdjacentHTML('beforeend', windowHTML);
const newWindow = document.getElementById(id);
setupWindowEvents(newWindow);
makeWindowActive(newWindow);
updateTaskbar();
}
function openMyDocumentsWindow() {
openExplorerWindow('my-documents', 'My Documents', 'mydocuments.html');
}
function openNetworkWindow() {
openExplorerWindow('my-network', 'My Network Places', 'mynetwork.html');
}
function openRecycleBinWindow() {
openExplorerWindow('recycle-bin', 'Recycle Bin', 'recyclebin.html');
}
function openInternetExplorerWindow() {
// If IE window already exists, just show it
const existingWindow = document.getElementById('window1');
if (existingWindow) {
existingWindow.style.display = 'flex';
makeWindowActive(existingWindow);
updateTaskbar();
// Reload the iframe to reinitialize the animation
const ieFrame = document.getElementById('ie-frame');
if (ieFrame) {
const currentSrc = ieFrame.src || 'http://ematth.dev/ematth.dev/index.html';
ieFrame.src = currentSrc;
}
return;
}
}
function createWindow(id, title, content, left = 200, top = 100) {
const existingWindow = document.getElementById(id);
if (existingWindow) {
existingWindow.style.display = 'flex';
makeWindowActive(existingWindow);
return;
}
const windowHTML = `
<div class="window" id="${id}" style="left: ${left}px; top: ${top}px; width: 500px; height: 380px;">
<div class="title-bar">
<div class="title-bar-text">${title}</div>
<div class="title-bar-controls">
<button class="title-bar-button" aria-label="Minimize"></button>
<button class="title-bar-button" aria-label="Maximize"></button>
<button class="title-bar-button close-button" aria-label="Close"></button>
</div>
</div>
<div class="window-content">
<div class="menu-bar">
<span class="menu-item">File</span>
<span class="menu-item">Edit</span>
<span class="menu-item">View</span>
<span class="menu-item">Help</span>
</div>
${content}
</div>
</div>
`;
const desktop = document.querySelector('.desktop');
desktop.insertAdjacentHTML('beforeend', windowHTML);
const newWindow = document.getElementById(id);
setupWindowEvents(newWindow);
makeWindowActive(newWindow);
updateTaskbar();
}
function setupWindowEvents(win) {
const titleBar = win.querySelector('.title-bar');
const closeButton = win.querySelector('.close-button');
const minimizeButton = win.querySelectorAll('.title-bar-button')[0];
const maximizeButton = win.querySelectorAll('.title-bar-button')[1];
// Add resize handles
addResizeHandles(win);
win.addEventListener('mousedown', (e) => {
makeWindowActive(win);
});
titleBar.addEventListener('mousedown', dragStart);
closeButton.addEventListener('click', (e) => {
e.stopPropagation();
win.style.display = 'none';
win.classList.remove('minimized'); // Remove minimized state when closing
updateTaskbar();
});
minimizeButton.addEventListener('click', (e) => {
e.stopPropagation();
win.classList.add('minimized');
win.style.display = 'none';
updateTaskbar();
});
maximizeButton.addEventListener('click', (e) => {
e.stopPropagation();
if (win.style.width === '100%') {
win.style.width = win.dataset.normalWidth || '500px';
win.style.height = win.dataset.normalHeight || '380px';
win.style.left = win.dataset.normalLeft || '200px';
win.style.top = win.dataset.normalTop || '100px';
} else {
win.dataset.normalWidth = win.style.width;
win.dataset.normalHeight = win.style.height;
win.dataset.normalLeft = win.style.left;
win.dataset.normalTop = win.style.top;
win.style.width = '100%';
win.style.height = 'calc(100% - 28px)';
win.style.left = '0';
win.style.top = '0';
}
});
}
// Click on desktop to deselect icons
document.querySelector('.desktop').addEventListener('click', (e) => {
if (e.target.classList.contains('desktop')) {
desktopIcons.forEach(i => i.classList.remove('selected'));
}
});
// Taskbar button functionality
function updateTaskbar() {
const taskbarButtons = document.querySelector('.taskbar-buttons');
taskbarButtons.innerHTML = '';
// Get all windows (including dynamically created ones)
const allWindows = document.querySelectorAll('.window');
allWindows.forEach(win => {
// Show button if window exists and hasn't been closed (not minimized counts)
const isClosed = win.style.display === 'none' && !win.classList.contains('minimized');
if (!isClosed) {
const button = document.createElement('button');
button.className = 'taskbar-button';
button.textContent = win.querySelector('.title-bar-text').textContent;
button.dataset.window = win.id;
if (win.classList.contains('active') && win.style.display !== 'none') {
button.classList.add('active');
}
button.addEventListener('click', () => {
if (win.style.display === 'none') {
// Restore minimized window
win.style.display = 'flex';
win.classList.remove('minimized');
makeWindowActive(win);
} else if (win.classList.contains('active')) {
// Minimize if clicking on already active window
win.classList.add('minimized');
win.style.display = 'none';
updateTaskbar();
} else {
// Activate if not active
makeWindowActive(win);
}
});
taskbarButtons.appendChild(button);
}
});
}
// Initialize
updateTaskbar();
if (windows.length > 0 && windows[0].style.display !== 'none') {
makeWindowActive(windows[0]);
}
// Start Menu functionality
const startButton = document.querySelector('.start-button');
const startMenu = document.getElementById('start-menu');
startButton.addEventListener('click', (e) => {
e.stopPropagation();
startMenu.classList.toggle('active');
startButton.classList.toggle('active');
});
// Close start menu when clicking outside
document.addEventListener('click', (e) => {
if (!startMenu.contains(e.target) && !startButton.contains(e.target)) {
startMenu.classList.remove('active');
startButton.classList.remove('active');
}
});
// Handle start menu item clicks
const startMenuItems = document.querySelectorAll('.start-menu-item');
startMenuItems.forEach(item => {
item.addEventListener('click', (e) => {
const action = item.getAttribute('data-action');
handleStartMenuAction(action);
startMenu.classList.remove('active');
startButton.classList.remove('active');
});
});
function handleStartMenuAction(action) {
switch(action) {
case 'programs':
alert('Programs menu - Coming soon!');
break;
case 'documents':
// Open My Documents
const documentsIcon = document.querySelector('[data-icon="documents"]');
if (documentsIcon) {
documentsIcon.click();
documentsIcon.click();
}
break;
case 'settings':
alert('Settings - Coming soon!');
break;
case 'search':
alert('Search - Coming soon!');
break;
case 'help':
alert('Help - Coming soon!');
break;
case 'run':
alert('Run dialog - Coming soon!');
break;
case 'shutdown':
if (confirm('Are you sure you want to shut down?')) {
alert('Shutting down... (you can close the window now!)')
}
break;
}
}
// Internet Explorer navigation
const ieFrame = document.getElementById('ie-frame');
const ieAddress = document.getElementById('ie-address');
const ieBack = document.getElementById('ie-back');
const ieForward = document.getElementById('ie-forward');
const ieRefresh = document.getElementById('ie-refresh');
const ieStop = document.getElementById('ie-stop');
const ieHome = document.getElementById('ie-home');
if (ieFrame && ieBack && ieForward && ieRefresh && ieHome) {
// Back button
ieBack.addEventListener('click', () => {
try {
ieFrame.contentWindow.history.back();
} catch (e) {
console.log('Cannot navigate back');
}
});
// Forward button
ieForward.addEventListener('click', () => {
try {
ieFrame.contentWindow.history.forward();
} catch (e) {
console.log('Cannot navigate forward');
}
});
// Refresh button
ieRefresh.addEventListener('click', () => {
ieFrame.contentWindow.location.reload();
});
// Stop button
ieStop.addEventListener('click', () => {
try {
ieFrame.contentWindow.stop();
} catch (e) {
console.log('Cannot stop loading');
}
});
// Home button
ieHome.addEventListener('click', () => {
ieFrame.src = 'http://ematth.dev/ematth.dev/index.html';
ieAddress.value = 'http://ematth.dev/ematth.dev/index.html';
});
// Update address bar when iframe navigates
ieFrame.addEventListener('load', () => {
try {
const currentPath = ieFrame.contentWindow.location.pathname;
const currentSearch = ieFrame.contentWindow.location.search;
if (currentPath) {
// Extract just the relevant part of the path
const pathParts = currentPath.split('/');
const relevantPath = pathParts.slice(-2).join('/');
// Include query parameters if they exist
ieAddress.value = relevantPath + currentSearch;
}
} catch (e) {
// Cross-origin restrictions may prevent reading the location
console.log('Cannot read iframe location');
}
});
}
// Automatically open welcome.txt on page load
window.addEventListener('load', function() {
// Small delay to ensure everything is initialized
setTimeout(function() {
openWelcomeTxt();
}, 300);
});