forked from HeyPuter/puter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIWindow.js
3252 lines (2977 loc) · 148 KB
/
UIWindow.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
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
/**
* Copyright (C) 2024 Puter Technologies Inc.
*
* This file is part of Puter.
*
* Puter is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import UIAlert from './UIAlert.js';
import UIContextMenu from './UIContextMenu.js';
import path from '../lib/path.js';
import UITaskbarItem from './UITaskbarItem.js';
import UIWindowLogin from './UIWindowLogin.js';
import UIWindowPublishWebsite from './UIWindowPublishWebsite.js';
import UIWindowItemProperties from './UIWindowItemProperties.js';
const el_body = document.getElementsByTagName('body')[0];
async function UIWindow(options) {
const win_id = global_element_id++;
last_window_zindex++;
// options.dominant places the window in center close to top.
options.dominant = options.dominant ?? false;
// in case of file dialogs, the window is automatically dominant
if(options.is_openFileDialog || options.is_saveFileDialog || options.is_directoryPicker)
options.dominant = true;
// we don't want to increment window_counter for dominant windows
if(!options.dominant)
window.window_counter++;
// add this window's id to the window_stack
window_stack.push(win_id);
// =====================================
// set options defaults
// =====================================
// indicates if sidebar is hidden, only applies to directory windows
let sidebar_hidden = false;
const default_window_top = ('calc(15% + ' + ((window.window_counter-1) % 10 * 20) + 'px)');
// list of file types that are allowed, other types will be disabled but still shown
options.allowed_file_types = options.allowed_file_types ?? '';
options.app = options.app ?? '';
options.allow_context_menu = options.allow_context_menu ?? true;
options.allow_native_ctxmenu = options.allow_native_ctxmenu ?? false;
options.allow_user_select = options.allow_user_select ?? false;
options.backdrop = options.backdrop ?? false;
options.body_css = options.body_css ?? {};
options.border_radius = options.border_radius ?? undefined;
options.draggable_body = options.draggable_body ?? false;
options.element_uuid = options.element_uuid ?? uuidv4();
options.center = options.center ?? false;
options.close_on_backdrop_click = options.close_on_backdrop_click ?? true;
options.disable_parent_window = options.disable_parent_window ?? false;
options.has_head = options.has_head ?? true;
options.height = options.height ?? 380;
options.icon = options.icon ?? null;
options.iframe_msg_uid = options.iframe_msg_uid ?? null;
options.is_droppable = options.is_droppable ?? true;
options.is_draggable = options.is_draggable ?? true;
options.is_dir = options.is_dir ?? false;
options.is_minimized = options.is_minimized ?? false;
options.is_maximized = options.is_maximized ?? false;
options.is_openFileDialog = options.is_openFileDialog ?? false;
options.is_resizable = options.is_resizable ?? true;
// if this is a fullpage window, it won't be resizable
if(options.is_fullpage)
options.is_resizable = false;
// in the embedded/fullpage mode every window is on top since there is no taskbar to switch between windows
// if user has specifically asked for this window to NOT stay on top, honor it.
if((is_embedded || window.is_fullpage_mode) && !options.parent_uuid && options.stay_on_top !== false)
options.stay_on_top = true;
// Keep the window on top of all previously opened windows
options.stay_on_top = options.stay_on_top ?? false;
options.is_saveFileDialog = options.is_saveFileDialog ?? false;
options.show_minimize_button = options.show_minimize_button ?? true;
options.on_close = options.on_close ?? undefined;
options.parent_uuid = options.parent_uuid ?? null;
options.selectable_body = options.selectable_body ?? true;
options.show_in_taskbar = options.show_in_taskbar ?? true;
options.show_maximize_button = options.show_maximize_button ?? true;
options.single_instance = options.single_instance ?? false;
options.sort_by = options.sort_by ?? 'name';
options.sort_order = options.sort_order ?? 'asc';
options.title = options.title ?? null;
options.top = options.top ?? default_window_top;
options.type = options.type ?? null;
options.update_window_url = options.update_window_url ?? false;
options.layout = options.layout ?? 'icons';
options.width = options.width ?? 680;
options.window_css = options.window_css ?? {};
options.window_class = (options.window_class !== undefined ? ' ' + options.window_class : '');
// if only one instance is allowed, bring focus to the window that is already open
if(options.single_instance && options.app !== ''){
let $already_open_window = $(`.window[data-app="${html_encode(options.app)}"]`);
if($already_open_window.length){
$(`.window[data-app="${html_encode(options.app)}"]`).focusWindow();
return;
}
}
// left
if(!options.dominant && !options.center){
options.left = options.left ?? ((window.innerWidth/2 - options.width/2) +(window.window_counter-1) % 10 * 30) + 'px';
}else if(!options.dominant && options.center){
options.left = options.left ?? ((window.innerWidth/2 - options.width/2)) + 'px';
}
else if(options.dominant){
options.left = (window.innerWidth/2 - options.width/2) + 'px';
}
else
options.left = options.left ?? ((window.innerWidth/2 - options.width/2) + 'px');
// top
if(!options.dominant && !options.center){
options.top = options.top ?? ((window.innerHeight/2 - options.height/2) +(window.window_counter-1) % 10 * 30) + 'px';
}else if(!options.dominant && options.center){
options.top = options.top ?? ((window.innerHeight/2 - options.height/2)) + 'px';
}
else if(options.dominant){
options.top = (window.innerHeight * 0.15);
}
else if(isMobile.phone)
options.top = 100;
if(isMobile.phone){
options.left = 0;
options.top = window.toolbar_height + 'px';
options.width = '100%';
options.height = 'calc(100% - ' + window.toolbar_height + 'px)';
}else{
options.width += 'px'
options.height += 'px'
}
// =====================================
// cover page
// =====================================
if(options.cover_page){
options.left = 0;
options.top = 0;
options.width = '100%';
options.height = '100%';
}
// --------------------------------------------------------
// HTML for Window
// --------------------------------------------------------
let h = '';
// Window
let zindex = options.stay_on_top ? (99999999 + last_window_zindex + 1 + ' !important') : last_window_zindex;
h += `<div class="window window-active
${options.cover_page ? 'window-cover-page' : ''}
${options.uid !== undefined ? 'window-'+options.uid : ''}
${options.window_class}
${options.allow_user_select ? ' allow-user-select' : ''}
${options.is_openFileDialog || options.is_saveFileDialog || options.is_directoryPicker ? 'window-filedialog' : ''}"
id="window-${win_id}"
data-allowed_file_types = "${html_encode(options.allowed_file_types)}"
data-app="${html_encode(options.app)}"
data-app_uuid="${html_encode(options.app_uuid ?? '')}"
data-disable_parent_window = "${html_encode(options.disable_parent_window)}"
data-name="${html_encode(options.title)}"
data-path ="${html_encode(options.path)}"
data-uid ="${options.uid}"
data-element_uuid="${options.element_uuid}"
data-parent_uuid="${options.parent_uuid}"
data-id ="${win_id}"
data-iframe_msg_uid ="${options.iframe_msg_uid}"
data-is_dir ="${options.is_dir}"
data-return_to_parent_window = "${options.return_to_parent_window}"
data-initiating_app_uuid = "${options.initiating_app_uuid}"
data-is_openFileDialog ="${options.is_openFileDialog}"
data-is_saveFileDialog ="${options.is_saveFileDialog}"
data-is_directoryPicker ="${options.is_directoryPicker}"
data-is_fullpage ="${options.is_fullpage ? 1 : 0}"
data-is_minimized ="${options.is_minimized ? 1 : 0}"
data-is_maximized ="${options.is_maximized ? 1 : 0}"
data-layout ="${options.layout}"
data-stay_on_top ="${options.stay_on_top}"
data-sort_by ="${options.sort_by ?? 'name'}"
data-sort_order ="${options.sort_order ?? 'asc'}"
data-multiselectable = "${options.selectable_body}"
data-update_window_url = "${options.update_window_url}"
data-initial_zindex = "${zindex}"
style=" z-index: ${zindex};
${options.width !== undefined ? 'width: ' + html_encode(options.width) +'; ':''}
${options.height !== undefined ? 'height: ' + html_encode(options.height) +'; ':''}
${options.border_radius !== undefined ? 'border-radius: ' + html_encode(options.border_radius) +'; ':''}
"
>`;
// window mask
h += `<div class="window-disable-mask">`;
//busy indicator
h += `<div class="busy-indicator">BUSY</div>`;
h += `</div>`;
// Head
if(options.has_head){
h += `<div class="window-head">`;
// draggable handle which also contains icon and title
h+=`<div class="window-head-draggable">`;
// icon
if(options.icon)
h += `<img class="window-head-icon" />`;
// title
h += `<span class="window-head-title" title="${html_encode(options.title)}"></span>`;
h += `</div>`;
// Minimize button, only if window is resizable and not embedded
if(options.is_resizable && options.show_minimize_button && !is_embedded)
h += `<span class="window-action-btn window-minimize-btn" style="margin-left:0;"><img src="${html_encode(window.icons['minimize.svg'])}" draggable="false"></span>`;
// Maximize button
if(options.is_resizable && options.show_maximize_button)
h += `<span class="window-action-btn window-scale-btn"><img src="${html_encode(window.icons['scale.svg'])}" draggable="false"></span>`;
// Close button
h += `<span class="window-action-btn window-close-btn"><img src="${html_encode(window.icons['close.svg'])}" draggable="false"></span>`;
h += `</div>`;
}
// Sidebar
if(options.is_dir && !isMobile.phone){
h += `<div class="window-sidebar disable-user-select hide-scrollbar"
style="${window.window_sidebar_width ? 'width: ' + html_encode(window.window_sidebar_width) + 'px !important;' : ''}"
draggable="false"
>`;
// favorites
h += `<h2 class="window-sidebar-title disable-user-select">Favorites</h2>`;
h += `<div draggable="false" title="Home" class="window-sidebar-item disable-user-select ${options.path === window.home_path ? 'window-sidebar-item-active' : ''}" data-path="${html_encode(window.home_path)}"><img draggable="false" class="window-sidebar-item-icon" src="${html_encode(window.icons['folder-home.svg'])}">Home</div>`;
h += `<div draggable="false" title="Documents" class="window-sidebar-item disable-user-select ${options.path === window.docs_path ? 'window-sidebar-item-active' : ''}" data-path="${html_encode(window.docs_path)}"><img draggable="false" class="window-sidebar-item-icon" src="${html_encode(window.icons['folder-documents.svg'])}">Documents</div>`;
h += `<div draggable="false" title="Pictures" class="window-sidebar-item disable-user-select ${options.path === window.pictures_path ? 'window-sidebar-item-active' : ''}" data-path="${html_encode(window.pictures_path)}"><img draggable="false" class="window-sidebar-item-icon" src="${html_encode(window.icons['folder-pictures.svg'])}">Pictures</div>`;
h += `<div draggable="false" title="Desktop" class="window-sidebar-item disable-user-select ${options.path === window.desktop_path ? 'window-sidebar-item-active' : ''}" data-path="${html_encode(window.desktop_path)}"><img draggable="false" class="window-sidebar-item-icon" src="${html_encode(window.icons['folder-desktop.svg'])}">Desktop</div>`;
h += `<div draggable="false" title="Videos" class="window-sidebar-item disable-user-select ${options.path === window.videos_path ? 'window-sidebar-item-active' : ''}" data-path="${html_encode(window.videos_path)}"><img draggable="false" class="window-sidebar-item-icon" src="${html_encode(window.icons['folder-videos.svg'])}">Videos</div>`;
h += `</div>`;
}
// Navbar
if(options.is_dir){
h += `<div class="window-navbar">`;
h += `<div style="float:left; margin-left:5px; margin-right:5px;">`;
// Back
h += `<img draggable="false" class="window-navbar-btn window-navbar-btn-back window-navbar-btn-disabled" src="${html_encode(window.icons['arrow-left.svg'])}" title="Click to go back.">`;
// Forward
h += `<img draggable="false" class="window-navbar-btn window-navbar-btn-forward window-navbar-btn-disabled" src="${html_encode(window.icons['arrow-right.svg'])}" title="Click to go forward.">`;
// Up
h += `<img draggable="false" class="window-navbar-btn window-navbar-btn-up ${options.path === '/' ? 'window-navbar-btn-disabled' : ''}" src="${html_encode(window.icons['arrow-up.svg'])}" title="Click to go one directory up.">`;
h += `</div>`;
// Path
h += `<div class="window-navbar-path">${navbar_path(options.path, window.user.username)}</div>`;
// Path editor
h += `<input class="window-navbar-path-input" data-path="${html_encode(options.path)}" value="${html_encode(options.path)}" spellcheck="false"/>`;
// Layout settings
h += `<img class="window-navbar-layout-settings" src="${html_encode(options.layout === 'icons' ? window.icons['layout-icons.svg'] : window.icons['layout-list.svg'])}" draggable="false">`;
h += `</div>`;
}
// Body
h += `<div
class="window-body${options.is_dir ? ' item-container' : ''}${options.iframe_url !== undefined || options.iframe_srcdoc !== undefined ? ' window-body-app' : ''}${options.is_saveFileDialog || options.is_openFileDialog || options.is_directoryPicker ? ' window-body-filedialog' : ''}"
data-allowed_file_types="${html_encode(options.allowed_file_types)}"
data-path="${html_encode(options.path)}"
data-multiselectable = "${options.selectable_body}"
data-sort_by ="${options.sort_by ?? 'name'}"
data-sort_order ="${options.sort_order ?? 'asc'}"
data-uid ="${options.uid}"
id="window-body-${win_id}"
style="${!options.has_head ? ' height: 100%;' : ''}">`;
// iframe, for apps
if(options.iframe_url || options.iframe_srcdoc){
// iframe
h += `<iframe tabindex="-1"
data-app="${html_encode(options.app)}"
class="window-app-iframe"
allowtransparency="true" allowpaymentrequest="true" allowfullscreen="true"
frameborder="0" webkitallowfullscreen="webkitallowfullscreen" mozallowfullscreen="mozallowfullscreen"
${options.iframe_url ? 'src="'+ html_encode(options.iframe_url)+'"' : ''}
${options.iframe_srcdoc ? 'srcdoc="'+ html_encode(options.iframe_srcdoc) +'"' : ''}
allow = "accelerometer; camera; encrypted-media; display-capture; geolocation; gyroscope; microphone; midi; clipboard-read; clipboard-write; web-share; fullscreen;"
sandbox="allow-forms allow-modals allow-pointer-lock allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-top-navigation-by-user-activation allow-downloads allow-presentation"></iframe>`;
}
// custom body
else if(options.body_content !== undefined){
h += options.body_content;
}
// Directory
if(options.is_dir){
// Detail layout header
h += window.explore_table_headers();
// Add 'This folder is empty' message by default
h += `<div class="explorer-empty-message">This folder is empty</div>`;
// Loading spinner
h += `<div class="explorer-loading-spinner">`;
h +=`<svg style="display:block; margin: 0 auto; " xmlns="http://www.w3.org/2000/svg" height="24" width="24" viewBox="0 0 24 24"><title>circle anim</title><g fill="#212121" class="nc-icon-wrapper"><g class="nc-loop-circle-24-icon-f"><path d="M12 24a12 12 0 1 1 12-12 12.013 12.013 0 0 1-12 12zm0-22a10 10 0 1 0 10 10A10.011 10.011 0 0 0 12 2z" fill="#212121" opacity=".4"></path><path d="M24 12h-2A10.011 10.011 0 0 0 12 2V0a12.013 12.013 0 0 1 12 12z" data-color="color-2"></path></g><style>.nc-loop-circle-24-icon-f{--animation-duration:0.5s;transform-origin:12px 12px;animation:nc-loop-circle-anim var(--animation-duration) infinite linear}@keyframes nc-loop-circle-anim{0%{transform:rotate(0)}100%{transform:rotate(360deg)}}</style></g></svg>`;
h += `<p class="explorer-loading-spinner-msg">Loading...</p>`;
h += `</div>`;
}
h += `</div>`;
// Explorer footer
if(options.is_dir && !options.is_saveFileDialog && !options.is_openFileDialog && !options.is_directoryPicker){
h += `<div class="explorer-footer">`
h += `<span class="explorer-footer-item-count"></span>`;
h += `<span class="explorer-footer-seperator">|</span>`;
h += `<span class="explorer-footer-selected-items-count"></span>`;
h += `</div>`;
}
// is_saveFileDialog
if(options.is_saveFileDialog){
h += `<div class="window-filedialog-prompt">`;
h += `<div style="display:flex;">`;
h += `<input type="text" class="savefiledialog-filename" autocorrect="off" spellcheck="false" value="${html_encode(options.saveFileDialog_default_filename) ?? ''}">`;
h += `<button class="button button-small filedialog-cancel-btn">Cancel</button>`;
h += `<button class="button `;
if(options.saveFileDialog_default_filename === undefined || options.saveFileDialog_default_filename === '')
h+= `disabled `;
h += `button-small button-primary savefiledialog-save-btn">Save</button>`;
h += `</div>`;
h += `</div>`;
}
// is_openFileDialog
else if(options.is_openFileDialog){
h += `<div class="window-filedialog-prompt">`;
h += `<div style="text-align:right;">`;
h += `<button class="button button-small filedialog-cancel-btn">Cancel</button>`;
h += `<button class="button disabled button-small button-primary openfiledialog-open-btn">Open</button>`;
h += `</div>`;
h += `</div>`;
}
// is_directoryPicker
else if(options.is_directoryPicker){
h += `<div class="window-filedialog-prompt">`;
h += `<div style="text-align:right;">`;
h += `<button class="button button-small filedialog-cancel-btn">Cancel</button>`;
h += `<button class="button button-small button-primary directorypicker-select-btn" style="margin-left:10px;">Select</button>`;
h += `</div>`;
h += `</div>`;
}
h += `</div>`;
// backdrop
if(options.backdrop){
let backdrop_zindex;
// backdrop should also cover over taskbar
let taskbar_zindex = $('.taskbar').css('z-index');
if(taskbar_zindex === null || taskbar_zindex === undefined)
backdrop_zindex = zindex;
else{
taskbar_zindex = parseInt(taskbar_zindex);
backdrop_zindex = taskbar_zindex > zindex ? taskbar_zindex : zindex;
}
h = `<div class="window-backdrop" style="z-index:${backdrop_zindex};">` + h + `</div>`;
}
// Append
$(el_body).append(h);
// disable_parent_window
if(options.disable_parent_window && options.parent_uuid !== null){
const $el_parent_window = $(`.window[data-element_uuid="${options.parent_uuid}"]`);
const $el_parent_disable_mask = $el_parent_window.find('.window-disable-mask');
//disable parent window
$el_parent_window.addClass('window-disabled')
$el_parent_disable_mask.show();
$el_parent_disable_mask.css('z-index', parseInt($el_parent_window.css('z-index')) + 1);
$el_parent_window.find('iframe').blur();
}
// Add Taskbar Item
if(!options.is_openFileDialog && !options.is_saveFileDialog && !options.is_directoryPicker && options.show_in_taskbar){
// add icon if there is no similar app already open
if($(`.taskbar-item[data-app="${options.app}"]`).length === 0){
UITaskbarItem({
icon: options.icon,
name: options.title,
app: options.app,
open_windows_count: 1,
onClick: function(){
let open_window_count = parseInt($(`.taskbar-item[data-app="${options.app}"]`).attr('data-open-windows'));
if(open_window_count === 0){
launch_app({
name: options.app,
})
}else{
return false;
}
}
});
if(options.app)
$(`.taskbar-item[data-app="${options.app}"] .active-taskbar-indicator`).show();
}else{
if(options.app){
$(`.taskbar-item[data-app="${options.app}"]`).attr('data-open-windows', parseInt($(`.taskbar-item[data-app="${options.app}"]`).attr('data-open-windows')) + 1);
$(`.taskbar-item[data-app="${options.app}"] .active-taskbar-indicator`).show();
}
}
}
// if directory, set window_nav_history and window_nav_history_current_position
if(options.is_dir){
window_nav_history[win_id] = [options.path];
window_nav_history_current_position[win_id] = 0;
}
// get all the elements needed
const el_window = document.querySelector(`#window-${win_id}`);
const el_window_head = document.querySelector(`#window-${win_id} > .window-head`);
const el_window_sidebar = document.querySelector(`#window-${win_id} > .window-sidebar`);
const el_window_head_title = document.querySelector(`#window-${win_id} > .window-head .window-head-title`);
const el_window_head_icon = document.querySelector(`#window-${win_id} > .window-head .window-head-icon`);
const el_window_head_scale_btn = document.querySelector(`#window-${win_id} > .window-head > .window-scale-btn`);
const el_window_navbar_back_btn = document.querySelector(`#window-${win_id} .window-navbar-btn-back`);
const el_window_navbar_forward_btn = document.querySelector(`#window-${win_id} .window-navbar-btn-forward`);
const el_window_navbar_up_btn = document.querySelector(`#window-${win_id} .window-navbar-btn-up`);
const el_window_body = document.querySelector(`#window-${win_id} > .window-body`);
const el_window_app_iframe = document.querySelector(`#window-${win_id} > .window-body > .window-app-iframe`);
const el_savefiledialog_filename = document.querySelector(`#window-${win_id} .savefiledialog-filename`);
const el_savefiledialog_save_btn = document.querySelector(`#window-${win_id} .savefiledialog-save-btn`);
const el_filedialog_cancel_btn = document.querySelector(`#window-${win_id} .filedialog-cancel-btn`);
const el_openfiledialog_open_btn = document.querySelector(`#window-${win_id} .openfiledialog-open-btn`);
const el_directorypicker_select_btn = document.querySelector(`#window-${win_id} .directorypicker-select-btn`);
if(options.is_maximized){
// save original size and position
$(el_window).attr({
'data-left-before-maxim': ((window.innerWidth/2 - 680/2) +(window.window_counter-1) % 10 * 30) + 'px',
'data-top-before-maxim': default_window_top,
'data-width-before-maxim': '680px',
'data-height-before-maxim': '350px',
'data-is_maximized': '1',
});
// shrink icon
$(el_window).find('.window-scale-btn>img').attr('src', window.icons['scale-down-3.svg']);
// set new size and position
$(el_window).css({
'top': window.toolbar_height + 'px',
'left': '0',
'width': '100%',
'height': `calc(100% - ${window.taskbar_height + window.toolbar_height + 1}px)`,
'transform': 'none',
});
}
// when a window is created, focus is brought to it and
// therefore it is the current active element
window.active_element = el_window;
// set name
$(el_window_head_title).html(html_encode(options.title));
// set icon
if(options.icon)
$(el_window_head_icon).attr('src', options.icon.image ?? options.icon);
// root folder of a shared user?
if(options.is_dir && (options.path.split('/').length - 1) === 1 && options.path !== '/'+window.user.username){
$(el_window_head_icon).attr('src', window.icons['shared.svg']);
}
// focus on this window and deactivate other windows
$(el_window).focusWindow();
if (animate_window_opening) {
// animate window opening
$(el_window).css({
'opacity': '0',
'transition': 'opacity 70ms ease-in-out',
});
// Use requestAnimationFrame to schedule a function to run at the next repaint of the browser window
requestAnimationFrame(() => {
// Change the window's opacity to 1 and scale to 1 to create an opening effect
$(el_window).css({
'opacity': '1',
})
// Set a timeout to run after the transition duration (100ms)
setTimeout(function () {
// Remove the transition property, so future CSS changes won't be animated
$(el_window).css({
'transition': 'none',
})
}, 70);
});
}
// onAppend() - using show() is a hack to make sure window is visible AND onAppend is called when
// window is actually appended and usable.
$(el_window).show(0, function(e){
// if SaveFileDialog, bring focus to the el_savefiledialog_filename and select all
if(options.is_saveFileDialog){
let item_name = el_savefiledialog_filename.value;
const extname = path.extname('/' + item_name);
if(extname !== '')
el_savefiledialog_filename.setSelectionRange(0, item_name.length - extname.length)
else
$(el_savefiledialog_filename).select();
$(el_savefiledialog_filename).get(0).focus({preventScroll:true});
}
//set custom window css
$(el_window).css(options.window_css);
// onAppend()
if(options.onAppend && typeof options.onAppend === 'function'){
options.onAppend(el_window);
}
})
if(options.is_saveFileDialog){
//------------------------------------------------
// SaveFileDialog > Save button
//------------------------------------------------
$(el_savefiledialog_save_btn).on('click', function(e){
const filename = $(el_savefiledialog_filename).val();
try{
validate_fsentry_name(filename)
}catch(err){
UIAlert(err.message, 'error', 'OK')
return;
}
const target_path = path.join($(el_window).attr('data-path'), filename);
if(options.onSaveFileDialogSave && typeof options.onSaveFileDialogSave === 'function')
options.onSaveFileDialogSave(target_path, el_window)
})
//------------------------------------------------
// SaveFileDialog > Enter
//------------------------------------------------
$(el_savefiledialog_filename).on('keypress', function(event) {
if(event.which === 13){
$(el_savefiledialog_save_btn).trigger('click');
}
})
//------------------------------------------------
// Enable/disable Save button based on input
//------------------------------------------------
$(el_savefiledialog_filename).bind('keydown change input paste', function(){
if($(this).val() !== '')
$(el_savefiledialog_save_btn).removeClass('disabled');
else
$(el_savefiledialog_save_btn).addClass('disabled');
})
$(el_savefiledialog_filename).get(0).focus({preventScroll:true});
}
if(options.is_openFileDialog){
//------------------------------------------------
// OpenFileDialog > Open button
//------------------------------------------------
$(el_openfiledialog_open_btn).on('click', async function(e){
const selected_els = $(el_window).find('.item-selected[data-is_dir="0"]');
let selected_files;
// No item selected
if(selected_els.length === 0)
return;
// ------------------------------------------------
// Item(s) selected
// ------------------------------------------------
else{
selected_files = []
// an array that hold the items to sign
const items_to_sign = [];
// prepare items to sign
for(let i=0; i<selected_els.length; i++)
items_to_sign.push({uid: $(selected_els[i]).attr('data-uid'), action: 'write', path: $(selected_els[i]).attr('data-path')});
// sign items
selected_files = await puter.fs.sign(options.initiating_app_uuid, items_to_sign);
selected_files = selected_files.items;
selected_files = Array.isArray(selected_files) ? selected_files : [selected_files];
// change path of each item to preserve privacy
for(let i=0; i<selected_files.length; i++)
selected_files[i].path = `~/` + selected_files[i].path.split('/').slice(2).join('/');
}
const ifram_msg_uid = $(el_window).attr('data-iframe_msg_uid');
if(options.return_to_parent_window){
window.opener.postMessage({
msg: "fileOpenPicked",
original_msg_id: ifram_msg_uid,
items: Array.isArray(selected_files) ? [...selected_files] : [selected_files],
// LEGACY SUPPORT, remove this in the future when Polotno uses the new SDK
// this is literally put in here to support Polotno's legacy code
...(selected_files.length === 1 && selected_files[0])
}, '*');
window.close();
window.open('','_self').close();
}
else if(options.parent_uuid){
// send event to iframe
const target_iframe = $(`.window[data-element_uuid="${options.parent_uuid}"]`).find('.window-app-iframe').get(0);
if(target_iframe){
target_iframe.contentWindow.postMessage({
msg: "fileOpenPicked",
original_msg_id: ifram_msg_uid,
items: Array.isArray(selected_files) ? [...selected_files] : [selected_files],
// LEGACY SUPPORT, remove this in the future when Polotno uses the new SDK
// this is literally put in here to support Polotno's legacy code
...(selected_files.length === 1 && selected_files[0])
}, '*');
}
// focus on iframe
$(target_iframe).get(0)?.focus({preventScroll:true});
// send file_opened event
const file_opened_event = new CustomEvent('file_opened', {detail: Array.isArray(selected_files) ? [...selected_files] : [selected_files]});
// dispatch event to parent window
$(`.window[data-element_uuid="${options.parent_uuid}"]`).get(0)?.dispatchEvent(file_opened_event);
$(el_window).close();
}
})
}
else if(options.is_directoryPicker){
//------------------------------------------------
// DirectoryPicker > Select button
//------------------------------------------------
$(el_directorypicker_select_btn).on('click', async function(e){
const selected_els = $(el_window).find('.item-selected[data-is_dir="1"]');
let selected_dirs;
// ------------------------------------------------
// No item selected, return current directory
// ------------------------------------------------
if(selected_els.length === 0){
selected_dirs = await puter.fs.sign(options.initiating_app_uuid, {uid: $(el_window).attr('data-uid'), action: 'write'})
selected_dirs = selected_dirs.items;
}
// ------------------------------------------------
// directorie(s) selected
// ------------------------------------------------
else{
selected_dirs = []
// an array that hold the items to sign
const items_to_sign = [];
// prepare items to sign
for(let i=0; i<selected_els.length; i++)
items_to_sign.push({uid: $(selected_els[i]).attr('data-uid'), action: 'write', path: $(selected_els[i]).attr('data-path')});
// sign items
selected_dirs = await puter.fs.sign(options.initiating_app_uuid, items_to_sign);
selected_dirs = selected_dirs.items;
selected_dirs = Array.isArray(selected_dirs) ? selected_dirs : [selected_dirs];
// change path of each item to preserve privacy
for(let i=0; i<selected_dirs.length; i++)
selected_dirs[i].path = `~/` + selected_dirs[i].path.split('/').slice(2).join('/');
}
const ifram_msg_uid = $(el_window).attr('data-iframe_msg_uid');
if(options.return_to_parent_window){
window.opener.postMessage({
msg: "directoryPicked",
original_msg_id: ifram_msg_uid,
items: Array.isArray(selected_dirs) ? [...selected_dirs] : [selected_dirs],
// LEGACY SUPPORT, remove this in the future when Polotno uses the new SDK
// this is literally put in here to support Polotno's legacy code
...(selected_dirs.length === 1 && selected_dirs[0])
}, '*');
window.close();
window.open('','_self').close();
}
if(options.parent_uuid){
// Send directoryPicked event to iframe
const target_iframe = $(`.window[data-element_uuid="${options.parent_uuid}"]`).find('.window-app-iframe').get(0);
if(target_iframe){
target_iframe.contentWindow.postMessage({
msg: "directoryPicked",
original_msg_id: ifram_msg_uid,
items: Array.isArray(selected_dirs) ? [...selected_dirs] : [selected_dirs],
}, '*');
}
$(target_iframe).get(0).focus({preventScroll:true});
$(el_window).close();
}
})
}
if(options.is_saveFileDialog || options.is_openFileDialog || options.is_directoryPicker){
//------------------------------------------------
// FileDialog > Cancel button
//------------------------------------------------
$(el_filedialog_cancel_btn).on('click', function(e){
if(options.return_to_parent_window){
window.close();
window.open('','_self').close();
}
$(el_window).hide(0, ()=>{
// re-anable parent window
$(`.window[data-element_uuid="${options.parent_uuid}"]`).removeClass('window-disabled');
$(`.window[data-element_uuid="${options.parent_uuid}"]`).find('.window-disable-mask').hide();
$(el_window).close();
})
})
}
if(options.is_dir){
navbar_path_droppable(el_window);
sidebar_item_droppable(el_window);
// --------------------------------------------------------
// Back button
// --------------------------------------------------------
$(el_window_navbar_back_btn).on('click', function(e){
// if history menu is open don't continue
if($(el_window_navbar_back_btn).hasClass('has-open-contextmenu'))
return;
// if ctrl/cmd are pressed, open in new window
if(e.ctrlKey || e.metaKey){
const dirpath = window_nav_history[win_id].at(window_nav_history_current_position[win_id] - 1);
UIWindow({
path: dirpath,
title: dirpath === '/' ? root_dirname : path.basename(dirpath),
icon: window.icons['folder.svg'],
// uid: $(el_item).attr('data-uid'),
is_dir: true,
});
}
// ... otherwise, open in same window
else{
window_nav_history_current_position[win_id] > 0 && window_nav_history_current_position[win_id]--;
const new_path = window_nav_history[win_id].at(window_nav_history_current_position[win_id]);
// update window path
update_window_path(el_window, new_path);
}
})
// --------------------------------------------------------
// Back button click-hold
// --------------------------------------------------------
$(el_window_navbar_back_btn).on('taphold', function() {
let items = [];
const pos = el_window_navbar_back_btn.getBoundingClientRect();
for(let index = window_nav_history_current_position[win_id] - 1; index >= 0; index--){
const history_item = window_nav_history[win_id].at(index);
// build item for context menu
items.push({
html: `<span>${history_item === window.home_path ? 'Home' : path.basename(history_item)}</span>`,
val: index,
onClick: async function(e){
let history_index = e.value;
window_nav_history_current_position[win_id] = history_index;
const new_path = window_nav_history[win_id].at(window_nav_history_current_position[win_id]);
// if ctrl/cmd are pressed, open in new window
if(e.ctrlKey || e.metaKey && (new_path !== undefined && new_path !== null)){
UIWindow({
path: new_path,
title: new_path === '/' ? root_dirname : path.basename(new_path),
icon: window.icons['folder.svg'],
is_dir: true,
});
}
// update window path
else{
update_window_path(el_window, new_path);
}
}
})
}
// Menu
UIContextMenu({
position: {top: pos.top + pos.height + 3, left: pos.left},
parent_element: el_window_navbar_back_btn,
items: items,
})
})
// --------------------------------------------------------
// Forward button
// --------------------------------------------------------
$(el_window_navbar_forward_btn).on('click', function(e){
// if history menu is open don't continue
if($(el_window_navbar_forward_btn).hasClass('has-open-contextmenu'))
return;
// if ctrl/cmd are pressed, open in new window
if(e.ctrlKey || e.metaKey){
const dirpath = window_nav_history[win_id].at(window_nav_history_current_position[win_id] + 1);
UIWindow({
path: dirpath,
title: dirpath === '/' ? root_dirname : path.basename(dirpath),
icon: window.icons['folder.svg'],
// uid: $(el_item).attr('data-uid'),
is_dir: true,
});
}
// ... otherwise, open in same window
else{
window_nav_history_current_position[win_id]++;
// get last path in history
const target_path = window_nav_history[win_id].at(window_nav_history_current_position[win_id]);
// update window path
if(target_path !== undefined){
update_window_path(el_window, target_path);
}
}
})
// --------------------------------------------------------
// forward button click-hold
// --------------------------------------------------------
$(el_window_navbar_forward_btn).on('taphold', function() {
let items = [];
const pos = el_window_navbar_forward_btn.getBoundingClientRect();
for(let index = window_nav_history_current_position[win_id] + 1; index < window_nav_history[win_id].length; index++){
const history_item = window_nav_history[win_id].at(index);
// build item for context menu
items.push({
html: `<span>${history_item === window.home_path ? 'Home' : path.basename(history_item)}</span>`,
val: index,
onClick: async function(e){
let history_index = e.value;
window_nav_history_current_position[win_id] = history_index;
const new_path = window_nav_history[win_id].at(window_nav_history_current_position[win_id]);
// if ctrl/cmd are pressed, open in new window
if(e.ctrlKey || e.metaKey && (new_path !== undefined && new_path !== null)){
UIWindow({
path: new_path,
title: new_path === '/' ? root_dirname : path.basename(new_path),
icon: window.icons['folder.svg'],
is_dir: true,
});
}
// update window path
else{
update_window_path(el_window, new_path);
}
}
})
}
// Menu
UIContextMenu({
parent_element: el_window_navbar_forward_btn,
position: {top: pos.top + pos.height + 3, left: pos.left},
items: items,
})
})
// --------------------------------------------------------
// Up button
// --------------------------------------------------------
$(el_window_navbar_up_btn).on('click', function(e){
const target_path = path.resolve(path.join($(el_window).attr('data-path'), '..'));
// if ctrl/cmd are pressed, open in new window
if(e.ctrlKey || e.metaKey && (target_path !== undefined && target_path !== null)){
UIWindow({
path: target_path,
title: target_path === '/' ? root_dirname : path.basename(target_path),
icon: window.icons['folder.svg'],
// uid: $(el_item).attr('data-uid'),
is_dir: true,
});
}
// ... otherwise, open in same window
else if(target_path !== undefined && target_path !== null){
// update history
window_nav_history[win_id] = window_nav_history[win_id].slice(0, window_nav_history_current_position[win_id]+1);
window_nav_history[win_id].push(target_path);
window_nav_history_current_position[win_id]++;
// update window path
update_window_path(el_window, target_path);
}
})
const layouts = ['icons', 'list', 'details'];
$(el_window).find('.window-navbar-layout-settings').on('contextmenu taphold', function() {
let cur_layout = $(el_window).attr('data-layout');
let items = [];
for(let i=0; i<layouts.length; i++){
items.push({
html: `<span style="text-transform: capitalize;">${layouts[i]}</span>`,
icon: cur_layout === layouts[i] ? '✓' : '',
onClick: async function(e){
update_window_layout(el_window, layouts[i]);
window.set_layout($(el_window).attr('data-uid'), layouts[i]);
}
})
}
UIContextMenu({
parent_element: this,
items: items,
})
})
$(el_window).find('.window-navbar-layout-settings').on('click', function() {
let cur_layout = $(el_window).attr('data-layout');
for(let i=0; i<layouts.length; i++){
if(cur_layout === layouts[i]){
if(i === layouts.length - 1){
update_window_layout(el_window, layouts[0]);
window.set_layout($(el_window).attr('data-uid'), layouts[0]);
}else{
update_window_layout(el_window, layouts[i+1]);
window.set_layout($(el_window).attr('data-uid'), layouts[i+1]);
}
break;
}
}
})
// --------------------------------------------------------
// directory content
// --------------------------------------------------------
//auth
if(!is_auth() && !(await UIWindowLogin()))
return;
// get directory content
refresh_item_container(el_window_body, options);
}
// set iframe url
if (options.iframe_url){
$(el_window_app_iframe).attr('src', options.iframe_url)
//bring focus to iframe
el_window_app_iframe.contentWindow.focus();
}
// set the position of window
if(!options.is_maximized){
$(el_window).css('top', options.top)
$(el_window).css('left', options.left)
}
$(el_window).css('display', 'block');
// mousedown on the window body will unselect selected items if neither ctrl nor command are pressed
$(el_window_body).on('mousedown', function(e){
if($(e.target).hasClass('window-body') && !e.ctrlKey && !e.metaKey){
$(el_window_body).find('.item-selected').removeClass('item-selected');
update_explorer_footer_selected_items_count(el_window);
// if this is openFileDialog, disable the Open button
if(options.is_openFileDialog)
$(el_openfiledialog_open_btn).addClass('disabled')
}
})
// on_close event
$(el_window).on('remove', function(e){
// if on_close callback is set, call it
options.on_close?.();
})
// --------------------------------------------------------
// Backdrop click
// --------------------------------------------------------
if(options.backdrop && options.close_on_backdrop_click){
$(el_window).closest('.window-backdrop').on('mousedown', function(e){
if($(e.target).hasClass('window-backdrop')){
$(el_window).close();
}
})
}
// --------------------------------------------------------
// Selectable
// only for Desktop screens
// --------------------------------------------------------
if(options.is_dir && options.selectable_body && !isMobile.phone && !isMobile.tablet){
let selected_ctrl_items = [];
// init viselect
const selection = new SelectionArea({
selectionContainerClass: '.selection-area-container',
container: `#window-body-${win_id}`,
selectables: [`#window-body-${win_id} .item`],
startareas: [`#window-body-${win_id}`],
boundaries: [`#window-body-${win_id}`],