-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
1080 lines (953 loc) · 43.5 KB
/
index.js
File metadata and controls
1080 lines (953 loc) · 43.5 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
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
document.addEventListener('DOMContentLoaded', function() {
// DOM elements
const desktop = document.querySelector('.desktop');
const startButton = document.querySelector('.start-button');
const startMenu = document.querySelector('.start-menu');
const clockElement = document.querySelector('.clock');
const dateElement = document.querySelector('.date');
const windowsContainer = document.querySelector('.windows-container');
const taskbarIcons = document.querySelector('.taskbar-icons');
const appItems = document.querySelectorAll('.app-item');
const desktopIcons = document.querySelectorAll('.icon');
const contextMenu = document.querySelector('.context-menu');
const notificationCenter = document.querySelector('.notification-center');
const notificationList = document.querySelector('.notification-list');
const notificationIcon = document.querySelector('.notification-icon');
const clearAllBtn = document.querySelector('.clear-all');
// Virtual File System
const fileSystem = {
root: {
name: 'This PC',
type: 'folder',
children: {
documents: {
name: 'Documents',
type: 'folder',
children: {
file1: {
name: 'Document 1.txt',
type: 'file',
content: 'This is a sample document.'
},
file2: {
name: 'Notes.txt',
type: 'file',
content: 'Important notes go here.'
}
}
},
pictures: {
name: 'Pictures',
type: 'folder',
children: {
wallpaper: {
name: 'Wallpaper.jpg',
type: 'image',
url: 'https://wallpaperaccess.com/full/1567674.jpg'
},
vacation: {
name: 'Vacation.png',
type: 'image',
url: 'https://wallpaperaccess.com/full/1567675.jpg'
}
}
},
music: {
name: 'Music',
type: 'folder',
children: {
song1: {
name: 'Sample Song.mp3',
type: 'audio',
url: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3'
}
}
}
}
},
currentPath: ['root']
};
// Wallpaper options
const wallpapers = [
{ name: 'Blue', url: './files/Wallspaper/1.jpg' },
{ name: 'Blue1', url: './files/Wallspaper/2.jpg' },
{ name: 'Blue2', url: './files/Wallspaper/3.jpg' },
{ name: 'Blue3', url: './files/Wallspaper/4.jpg' },
{ name: 'Blue4', url: './files/Wallspaper/5.jpg' },
{ name: 'Blue5', url: './files/Wallspaper/6.jpg' },
{ name: 'Blue6', url: './files/Wallspaper/7.jpg' },
{ name: 'Blue7', url: './files/Wallspaper/8.jpg' },
{ name: 'Blue8', url: './files/Wallspaper/9.jpg' },
{ name: 'Blue9', url: './files/Wallspaper/10.jpg' },
{ name: 'Blue10', url: './files/Wallspaper/11.jpg' },
{ name: 'Blue11', url: './files/Wallspaper/12.jpg' },
{ name: 'Blue12', url: './files/Wallspaper/13.jpg' },
];
// Notifications
let notifications = [
{ id: 1, title: 'Welcome', message: 'Welcome to WebOS!', time: new Date() },
{ id: 2, title: 'Tip', message: 'Right-click on desktop for context menu', time: new Date() }
];
// Update clock
function updateClock() {
const now = new Date();
clockElement.textContent = now.toLocaleTimeString();
dateElement.textContent = now.toLocaleDateString();
}
setInterval(updateClock, 1000);
updateClock();
// Start menu toggle
startButton.addEventListener('click', function(e) {
e.stopPropagation();
startMenu.classList.toggle('active');
hideContextMenu();
hideNotificationCenter();
});
// Close start menu when clicking elsewhere
document.addEventListener('click', function() {
startMenu.classList.remove('active');
});
// Prevent start menu from closing when clicking inside it
startMenu.addEventListener('click', function(e) {
e.stopPropagation();
});
// App templates
const apps = {
explorer: {
title: 'File Explorer',
icon: 'https://cdn-icons-png.flaticon.com/512/732/732220.png',
createContent: function() {
return `
<div class="explorer-container">
<div class="explorer-nav">
<button class="back">← Back</button>
<button class="forward">Forward →</button>
<button class="up">↑ Up</button>
<div class="explorer-path">${getCurrentPath()}</div>
</div>
<div class="explorer-content">
${renderExplorerContent()}
</div>
</div>
`;
},
init: function(windowElement) {
const backBtn = windowElement.querySelector('.back');
const forwardBtn = windowElement.querySelector('.forward');
const upBtn = windowElement.querySelector('.up');
const contentArea = windowElement.querySelector('.explorer-content');
backBtn.addEventListener('click', () => {
if (fileSystem.currentPath.length > 1) {
fileSystem.currentPath.pop();
contentArea.innerHTML = renderExplorerContent();
windowElement.querySelector('.explorer-path').textContent = getCurrentPath();
}
});
upBtn.addEventListener('click', () => {
if (fileSystem.currentPath.length > 1) {
fileSystem.currentPath.pop();
contentArea.innerHTML = renderExplorerContent();
windowElement.querySelector('.explorer-path').textContent = getCurrentPath();
}
});
// Forward button would require maintaining a navigation history
forwardBtn.addEventListener('click', () => {
showNotification('Explorer', 'Forward navigation not implemented yet');
});
// Handle item clicks
contentArea.addEventListener('click', (e) => {
const item = e.target.closest('.explorer-item');
if (item) {
const itemName = item.dataset.name;
const currentLocation = getCurrentLocation();
if (currentLocation.children[itemName].type === 'folder') {
fileSystem.currentPath.push(itemName);
contentArea.innerHTML = renderExplorerContent();
windowElement.querySelector('.explorer-path').textContent = getCurrentPath();
} else {
showNotification('Explorer', `Opening ${itemName}`);
// In a real implementation, you would open the file with the appropriate app
}
}
});
}
},
notepad: {
title: 'Notepad',
icon: 'https://cdn-icons-png.flaticon.com/512/837/837548.png',
content: '<textarea style="width:100%;height:100%;border:none;resize:none;padding:10px;font-family:monospace;"></textarea>'
},
calculator: {
title: 'Calculator',
icon: 'https://cdn-icons-png.flaticon.com/512/210/210426.png',
content: `
<div class="calculator">
<input type="text" class="calculator-display" readonly>
<div class="calculator-buttons">
<button>C</button>
<button>+/-</button>
<button>%</button>
<button>/</button>
<button>7</button>
<button>8</button>
<button>9</button>
<button>*</button>
<button>4</button>
<button>5</button>
<button>6</button>
<button>-</button>
<button>1</button>
<button>2</button>
<button>3</button>
<button>+</button>
<button class="span-2">0</button>
<button>.</button>
<button>=</button>
</div>
</div>
`,
init: function(windowElement) {
const display = windowElement.querySelector('.calculator-display');
const buttons = windowElement.querySelectorAll('.calculator-buttons button');
let currentInput = '0';
let previousInput = '';
let operation = null;
let resetInput = false;
function updateDisplay() {
display.value = currentInput;
}
buttons.forEach(button => {
button.addEventListener('click', () => {
const value = button.textContent;
if (value >= '0' && value <= '9') {
if (currentInput === '0' || resetInput) {
currentInput = value;
resetInput = false;
} else {
currentInput += value;
}
} else if (value === '.') {
if (!currentInput.includes('.')) {
currentInput += '.';
}
} else if (value === 'C') {
currentInput = '0';
previousInput = '';
operation = null;
} else if (value === '+/-') {
currentInput = (parseFloat(currentInput) * -1).toString();
} else if (value === '%') {
currentInput = (parseFloat(currentInput) / 100).toString();
} else if (['+', '-', '*', '/'].includes(value)) {
if (operation !== null) calculate();
previousInput = currentInput;
operation = value;
resetInput = true;
} else if (value === '=') {
calculate();
}
updateDisplay();
});
});
function calculate() {
let result;
const prev = parseFloat(previousInput);
const current = parseFloat(currentInput);
if (isNaN(prev) || isNaN(current)) return;
switch (operation) {
case '+':
result = prev + current;
break;
case '-':
result = prev - current;
break;
case '*':
result = prev * current;
break;
case '/':
result = prev / current;
break;
default:
return;
}
currentInput = result.toString();
operation = null;
resetInput = true;
}
updateDisplay();
}
},
paint: {
title: 'Paint',
icon: 'https://cdn-icons-png.flaticon.com/512/2819/2819194.png',
content: `
<div class="canvas-container">
<div class="canvas-tools">
<button class="tool-pencil active" data-tool="pencil">✏️ Pencil</button>
<button class="tool-brush" data-tool="brush">🖌️ Brush</button>
<button class="tool-eraser" data-tool="eraser">🧽 Eraser</button>
<div class="color-picker">
<span>Color:</span>
<input type="color" value="#000000">
</div>
<button class="clear-canvas">Clear</button>
</div>
<div class="canvas-wrapper">
<canvas id="paintCanvas"></canvas>
</div>
</div>
`,
init: function(windowElement) {
const canvas = windowElement.querySelector('#paintCanvas');
const ctx = canvas.getContext('2d');
const toolButtons = windowElement.querySelectorAll('[data-tool]');
const colorPicker = windowElement.querySelector('input[type="color"]');
const clearButton = windowElement.querySelector('.clear-canvas');
// Set canvas size
function resizeCanvas() {
const container = windowElement.querySelector('.canvas-wrapper');
canvas.width = container.clientWidth;
canvas.height = container.clientHeight;
// Redraw content if needed
if (!canvas.isEmpty) {
// In a real app, you would redraw the saved drawing
}
}
// Initial resize
resizeCanvas();
// Handle window resize
new ResizeObserver(resizeCanvas).observe(windowElement.querySelector('.canvas-wrapper'));
// Drawing variables
let isDrawing = false;
let currentTool = 'pencil';
let currentColor = '#000000';
let lineWidth = 2;
// Set active tool
function setActiveTool(tool) {
currentTool = tool;
toolButtons.forEach(btn => {
btn.classList.toggle('active', btn.dataset.tool === tool);
});
switch (tool) {
case 'pencil':
lineWidth = 2;
break;
case 'brush':
lineWidth = 5;
break;
case 'eraser':
lineWidth = 10;
currentColor = '#ffffff';
break;
}
}
// Tool button events
toolButtons.forEach(button => {
button.addEventListener('click', () => {
setActiveTool(button.dataset.tool);
});
});
// Color picker event
colorPicker.addEventListener('input', (e) => {
currentColor = e.target.value;
});
// Clear canvas
clearButton.addEventListener('click', () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
});
// Drawing events
canvas.addEventListener('mousedown', startDrawing);
canvas.addEventListener('mousemove', draw);
canvas.addEventListener('mouseup', stopDrawing);
canvas.addEventListener('mouseout', stopDrawing);
function startDrawing(e) {
isDrawing = true;
draw(e);
}
function draw(e) {
if (!isDrawing) return;
ctx.lineWidth = lineWidth;
ctx.lineCap = 'round';
ctx.strokeStyle = currentColor;
// Get mouse position
const rect = canvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
if (ctx.lastX === undefined) {
ctx.lastX = x;
ctx.lastY = y;
}
ctx.beginPath();
ctx.moveTo(ctx.lastX, ctx.lastY);
ctx.lineTo(x, y);
ctx.stroke();
ctx.lastX = x;
ctx.lastY = y;
}
function stopDrawing() {
isDrawing = false;
ctx.lastX = undefined;
ctx.lastY = undefined;
}
// Mark canvas as not empty when drawing starts
canvas.isEmpty = true;
canvas.addEventListener('mousedown', () => {
canvas.isEmpty = false;
});
}
},
browser: {
title: 'Web Browser',
icon: 'https://cdn-icons-png.flaticon.com/512/2991/2991148.png',
content: `
<div class="browser-container">
<div class="browser-nav">
<button class="browser-back">←</button>
<button class="browser-forward">→</button>
<button class="browser-refresh">↻</button>
<input type="text" class="browser-url" value="https://www.example.com">
<button class="browser-go">Go</button>
</div>
<iframe class="browser-content" src="https://www.example.com"></iframe>
</div>
`,
init: function(windowElement) {
const backBtn = windowElement.querySelector('.browser-back');
const forwardBtn = windowElement.querySelector('.browser-forward');
const refreshBtn = windowElement.querySelector('.browser-refresh');
const urlInput = windowElement.querySelector('.browser-url');
const goBtn = windowElement.querySelector('.browser-go');
const iframe = windowElement.querySelector('.browser-content');
// Navigation functions
function goToUrl(url) {
try {
if (!url.startsWith('http://') && !url.startsWith('https://')) {
url = 'https://' + url;
}
iframe.src = url;
urlInput.value = url;
} catch (e) {
showNotification('Browser', 'Invalid URL');
}
}
// Event listeners
goBtn.addEventListener('click', () => goToUrl(urlInput.value));
urlInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') goToUrl(urlInput.value);
});
backBtn.addEventListener('click', () => {
showNotification('Browser', 'Back button clicked');
// In a real implementation, you would use iframe.contentWindow.history.back()
});
forwardBtn.addEventListener('click', () => {
showNotification('Browser', 'Forward button clicked');
// In a real implementation, you would use iframe.contentWindow.history.forward()
});
refreshBtn.addEventListener('click', () => {
iframe.src = iframe.src;
});
// Update URL when iframe navigates
iframe.addEventListener('load', () => {
try {
// For same-origin iframes, we can get the URL
urlInput.value = iframe.contentWindow.location.href;
} catch (e) {
// Cross-origin iframe - can't access location
}
});
}
},
media: {
title: 'Media Player',
icon: 'https://cdn-icons-png.flaticon.com/512/3106/3106921.png',
content: `
<div class="media-container">
<div class="media-display">
<p>Media Player</p>
</div>
<div class="media-controls">
<button class="media-play">▶️</button>
<button class="media-pause">⏸️</button>
<button class="media-stop">⏹️</button>
</div>
</div>
`,
init: function(windowElement) {
const playBtn = windowElement.querySelector('.media-play');
const pauseBtn = windowElement.querySelector('.media-pause');
const stopBtn = windowElement.querySelector('.media-stop');
const display = windowElement.querySelector('.media-display');
let audio = null;
playBtn.addEventListener('click', () => {
if (!audio) {
audio = new Audio("https://soundcloud.com/eminemofficial/mockingbird?utm_source=clipboard&utm_medium=text&utm_campaign=social_sharing");
audio.play();
display.innerHTML = '<p>Now playing: Sample Song</p>';
} else {
audio.play();
}
});
pauseBtn.addEventListener('click', () => {
if (audio) {
audio.pause();
}
});
stopBtn.addEventListener('click', () => {
if (audio) {
audio.pause();
audio.currentTime = 0;
display.innerHTML = '<p>Media Player</p>';
}
});
}
},
settings: {
title: 'Settings',
icon: 'https://cdn-icons-png.flaticon.com/512/2092/2092653.png',
content: `
<div class="settings-container">
<div class="settings-sidebar">
<div class="settings-sidebar-item active" data-tab="personalization">Personalization</div>
<div class="settings-sidebar-item" data-tab="system">System</div>
<div class="settings-sidebar-item" data-tab="about">About</div>
</div>
<div class="settings-content">
<div class="settings-tab active" data-tab="personalization">
<h2>Personalization</h2>
<h3>Wallpaper</h3>
<div class="wallpaper-grid">
${wallpapers.map((wp, i) => `
<div class="wallpaper-item ${i === 0 ? 'selected' : ''}"
data-url="${wp.url}"
style="background-image: url('${wp.url}')"
title="${wp.name}">
</div>
`).join('')}
</div>
</div>
<div class="settings-tab" data-tab="system">
<h2>System Settings</h2>
<p>System settings would go here</p>
</div>
<div class="settings-tab" data-tab="about">
<h2>About WebOS</h2>
<p>This is a web-based operating system demo built with HTML, CSS, and JavaScript.</p>
<p>Version 1.0</p>
</div>
</div>
</div>
`,
init: function(windowElement) {
const sidebarItems = windowElement.querySelectorAll('.settings-sidebar-item');
const tabs = windowElement.querySelectorAll('.settings-tab');
const wallpaperItems = windowElement.querySelectorAll('.wallpaper-item');
// Tab switching
sidebarItems.forEach(item => {
item.addEventListener('click', () => {
const tabName = item.dataset.tab;
// Update sidebar
sidebarItems.forEach(i => i.classList.remove('active'));
item.classList.add('active');
// Update tabs
tabs.forEach(tab => tab.classList.remove('active'));
windowElement.querySelector(`.settings-tab[data-tab="${tabName}"]`).classList.add('active');
});
});
// Wallpaper selection
wallpaperItems.forEach(item => {
item.addEventListener('click', () => {
wallpaperItems.forEach(i => i.classList.remove('selected'));
item.classList.add('selected');
const newWallpaper = item.dataset.url;
document.body.style.backgroundImage = `url('${newWallpaper}')`;
showNotification('Settings', 'Wallpaper changed');
});
});
}
}
};
// Helper functions for file explorer
function getCurrentLocation() {
let location = fileSystem.root;
for (const path of fileSystem.currentPath.slice(1)) {
location = location.children[path];
}
return location;
}
function getCurrentPath() {
return fileSystem.currentPath.map(p => {
if (p === 'root') return 'This PC';
return fileSystem.root.children[p]?.name || p;
}).join(' > ');
}
function renderExplorerContent() {
const location = getCurrentLocation();
if (!location.children) return '';
return Object.entries(location.children).map(([key, item]) => {
let icon;
switch (item.type) {
case 'folder':
icon = 'https://cdn-icons-png.flaticon.com/512/732/732220.png';
break;
case 'image':
icon = 'https://cdn-icons-png.flaticon.com/512/2965/2965300.png';
break;
case 'audio':
icon = 'https://cdn-icons-png.flaticon.com/512/3106/3106921.png';
break;
default:
icon = 'https://cdn-icons-png.flaticon.com/512/2965/2965278.png';
}
return `
<div class="explorer-item" data-name="${key}">
<img src="${icon}" alt="${item.type}">
<span>${item.name}</span>
</div>
`;
}).join('');
}
// Create window function
function createWindow(appId) {
const app = apps[appId];
if (!app) return;
// Check if window already exists
const existingWindow = document.querySelector(`.window[data-app="${appId}"]`);
if (existingWindow) {
existingWindow.style.display = 'flex';
bringToFront(existingWindow);
return;
}
const windowElement = document.createElement('div');
windowElement.className = 'window';
windowElement.dataset.app = appId;
// Set content based on whether the app has a createContent function
const content = app.createContent ? app.createContent() : app.content;
windowElement.innerHTML = `
<div class="window-header">
<div class="window-title">${app.title}</div>
<div class="window-controls">
<button class="minimize">_</button>
<button class="maximize">□</button>
<button class="close">X</button>
</div>
</div>
<div class="window-content">${content}</div>
`;
windowsContainer.appendChild(windowElement);
// Position window randomly but within viewport
const maxLeft = window.innerWidth - 500;
const maxTop = window.innerHeight - 440; // 400 height + 40 taskbar
windowElement.style.left = `${Math.min(Math.max(0, Math.random() * maxLeft), maxLeft)}px`;
windowElement.style.top = `${Math.min(Math.max(0, Math.random() * maxTop), maxTop)}px`;
// Make window draggable
makeDraggable(windowElement);
// Window controls
const minimizeBtn = windowElement.querySelector('.minimize');
const maximizeBtn = windowElement.querySelector('.maximize');
const closeBtn = windowElement.querySelector('.close');
minimizeBtn.addEventListener('click', () => {
windowElement.style.display = 'none';
});
maximizeBtn.addEventListener('click', () => {
if (windowElement.style.width === '100%') {
windowElement.style.width = '500px';
windowElement.style.height = '400px';
windowElement.style.top = '';
windowElement.style.left = '';
} else {
windowElement.style.width = '100%';
windowElement.style.height = 'calc(100% - 40px)';
windowElement.style.top = '0';
windowElement.style.left = '0';
}
});
closeBtn.addEventListener('click', () => {
windowsContainer.removeChild(windowElement);
const taskbarIcon = document.querySelector(`.taskbar-icon[data-app="${appId}"]`);
if (taskbarIcon) {
taskbarIcons.removeChild(taskbarIcon);
}
});
// Add to taskbar
const taskbarIcon = document.createElement('div');
taskbarIcon.className = 'taskbar-icon';
taskbarIcon.dataset.app = appId;
taskbarIcon.innerHTML = `<img src="${app.icon}" alt="${app.title}" width="10">`;
taskbarIcon.addEventListener('click', () => {
if (windowElement.style.display === 'none') {
windowElement.style.display = 'flex';
bringToFront(windowElement);
} else {
windowElement.style.display = 'none';
}
});
taskbarIcons.appendChild(taskbarIcon);
// Bring window to front when clicked
windowElement.addEventListener('mousedown', () => {
bringToFront(windowElement);
});
// Initialize app-specific functionality
if (app.init) {
app.init(windowElement);
}
}
// Make elements draggable with snapping
function makeDraggable(element) {
const header = element.querySelector('.window-header');
let pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
let snapGuide = null;
header.onmousedown = dragMouseDown;
function dragMouseDown(e) {
e = e || window.event;
e.preventDefault();
// Bring to front when dragging
bringToFront(element);
// Get the mouse cursor position at startup
pos3 = e.clientX;
pos4 = e.clientY;
document.onmouseup = closeDragElement;
document.onmousemove = elementDrag;
// Create snap guide
snapGuide = document.createElement('div');
snapGuide.className = 'snap-guide';
document.body.appendChild(snapGuide);
}
function elementDrag(e) {
e = e || window.event;
e.preventDefault();
// Calculate the new cursor position
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
// Calculate new position
let newTop = element.offsetTop - pos2;
let newLeft = element.offsetLeft - pos1;
// Check for snapping
let snapPosition = checkSnapPosition(newLeft, newTop, element.offsetWidth, element.offsetHeight);
if (snapPosition) {
// Show snap guide
snapGuide.style.display = 'block';
snapGuide.className = `snap-guide ${snapPosition}`;
// Adjust position based on snap
switch (snapPosition) {
case 'left':
newLeft = 0;
element.style.width = '50%';
element.style.height = '100%';
break;
case 'right':
newLeft = window.innerWidth / 2;
element.style.width = '50%';
element.style.height = '100%';
break;
case 'top':
newTop = 0;
element.style.width = '100%';
element.style.height = '50%';
break;
case 'bottom':
newTop = window.innerHeight / 2;
element.style.width = '100%';
element.style.height = '50%';
break;
case 'full':
newTop = 0;
newLeft = 0;
element.style.width = '100%';
element.style.height = '100%';
break;
}
} else {
// Hide snap guide if not snapping
snapGuide.style.display = 'none';
// Restore original size if not snapping
if (element.style.width === '50%' || element.style.width === '100%') {
element.style.width = '500px';
element.style.height = '400px';
}
}
// Set the element's new position
element.style.top = newTop + "px";
element.style.left = newLeft + "px";
}
function closeDragElement() {
// Stop moving when mouse button is released
document.onmouseup = null;
document.onmousemove = null;
// Remove snap guide
if (snapGuide && snapGuide.parentNode) {
snapGuide.parentNode.removeChild(snapGuide);
}
}
}
// Check if window should snap to edges
function checkSnapPosition(left, top, width, height) {
const snapThreshold = 20;
const windowWidth = window.innerWidth;
const windowHeight = window.innerHeight;
// Check left edge
if (Math.abs(left) < snapThreshold) {
return 'left';
}
// Check right edge
if (Math.abs(left + width - windowWidth) < snapThreshold) {
return 'right';
}
// Check top edge
if (Math.abs(top) < snapThreshold) {
return 'top';
}
// Check bottom edge
if (Math.abs(top + height - windowHeight) < snapThreshold) {
return 'bottom';
}
// Check top-left corner (for full screen)
if (Math.abs(left) < snapThreshold && Math.abs(top) < snapThreshold) {
return 'full';
}
return null;
}
// Bring window to front
function bringToFront(element) {
const windows = document.querySelectorAll('.window');
let highestZ = 0;
windows.forEach(window => {
const z = parseInt(window.style.zIndex || '0');
if (z > highestZ) highestZ = z;
});
element.style.zIndex = highestZ + 1;
}
// Desktop icons drag and drop
desktopIcons.forEach(icon => {
icon.addEventListener('dragstart', function(e) {
e.dataTransfer.setData('text/plain', this.dataset.app);
this.style.opacity = '0.5';
});
icon.addEventListener('dragend', function() {
this.style.opacity = '1';
});
});
desktop.addEventListener('dragover', function(e) {
e.preventDefault();
});
desktop.addEventListener('drop', function(e) {
e.preventDefault();
const appId = e.dataTransfer.getData('text/plain');
if (appId) {
const icon = document.querySelector(`.icon[data-app="${appId}"]`);
if (icon) {
icon.style.left = (e.clientX - 40) + 'px';
icon.style.top = (e.clientY - 40) + 'px';
}
}
});
// Open apps from start menu
appItems.forEach(item => {
item.addEventListener('click', function() {
const appId = this.dataset.app;
createWindow(appId);
startMenu.classList.remove('active');
});
});
// Open apps from desktop icons (double click)
desktopIcons.forEach(icon => {
icon.addEventListener('dblclick', function() {
const appId = this.dataset.app;
createWindow(appId);
});
});
// Context menu
function showContextMenu(x, y) {
contextMenu.style.display = 'block';
contextMenu.style.left = `${x}px`;
contextMenu.style.top = `${y}px`;
bringToFront(contextMenu);
}
function hideContextMenu() {
contextMenu.style.display = 'none';
}
desktop.addEventListener('contextmenu', function(e) {
e.preventDefault();
showContextMenu(e.clientX, e.clientY);
});
document.addEventListener('click', function() {
hideContextMenu();
});
contextMenu.addEventListener('click', function(e) {
e.stopPropagation();
});
// Context menu actions
document.querySelectorAll('.context-item').forEach(item => {
item.addEventListener('click', function() {
const action = this.dataset.action;