forked from HeyPuter/puter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIItem.js
1620 lines (1469 loc) · 71 KB
/
UIItem.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 UIWindowPublishWebsite from './UIWindowPublishWebsite.js';
import UIWindowItemProperties from './UIWindowItemProperties.js';
import UIWindowGetCopyLink from './UIWindowGetCopyLink.js';
import UIWindowSaveAccount from './UIWindowSaveAccount.js';
import UIPopover from './UIPopover.js';
import UIWindowEmailConfirmationRequired from './UIWindowEmailConfirmationRequired.js';
import UIContextMenu from './UIContextMenu.js'
import UIAlert from './UIAlert.js'
import path from "../lib/path.js"
function UIItem(options){
const matching_appendto_count = $(options.appendTo).length;
if(matching_appendto_count > 1){
$(options.appendTo).each(function(){
const opts = options;
opts.appendTo = this;
UIItem(opts);
})
return;
}else if(matching_appendto_count === 0){
return;
}
const item_id = global_element_id++;
let last_mousedown_ts = 999999999999999;
let rename_cancelled = false;
// set options defaults
options.disabled = options.disabled ?? false;
options.is_dir = options.is_dir ?? false;
options.is_selected = options.is_selected ?? false;
options.is_shared = options.is_shared ?? false;
options.is_shortcut = options.is_shortcut ?? 0;
options.is_trash = options.is_trash ?? false;
options.metadata = options.metadata ?? '';
options.multiselectable = options.multiselectable ?? true;
options.shortcut_to = options.shortcut_to ?? '';
options.shortcut_to_path = options.shortcut_to_path ?? '';
options.immutable = (options.immutable === false || options.immutable === 0 || options.immutable === undefined ? 0 : 1);
options.sort_container_after_append = (options.sort_container_after_append !== undefined ? options.sort_container_after_append : false);
const is_shared_with_me = (options.path !== '/'+window.user.username && !options.path.startsWith('/'+window.user.username+'/'));
let website_url = determine_website_url(options.path);
// do a quick check to see if the target parent has any file type restrictions
const appendto_allowed_file_types = $(options.appendTo).attr('data-allowed_file_types')
if(!window.check_fsentry_against_allowed_file_types_string({is_dir: options.is_dir, name:options.name, type:options.type}, appendto_allowed_file_types))
options.disabled = true;
// --------------------------------------------------------
// HTML for Item
// --------------------------------------------------------
let h = '';
h += `<div id="item-${item_id}"
class="item${options.is_selected ? ' item-selected':''} ${options.disabled ? 'item-disabled':''}"
data-id="${item_id}"
data-name="${html_encode(options.name)}"
data-metadata="${html_encode(options.metadata)}"
data-uid="${options.uid}"
data-is_dir="${options.is_dir ? 1 : 0}"
data-is_trash="${options.is_trash ? 1 : 0}"
data-has_website="${options.has_website ? 1 : 0 }"
data-website_url = "${website_url ? html_encode(website_url) : ''}"
data-immutable="${options.immutable}"
data-is_shortcut = "${options.is_shortcut}"
data-shortcut_to = "${html_encode(options.shortcut_to)}"
data-shortcut_to_path = "${html_encode(options.shortcut_to_path)}"
data-sortable = "${options.sortable ?? 'true'}"
data-sort_by = "${html_encode(options.sort_by) ?? 'name'}"
data-size = "${options.size ?? ''}"
data-type = "${html_encode(options.type) ?? ''}"
data-modified = "${options.modified ?? ''}"
data-associated_app_name = "${html_encode(options.associated_app_name) ?? ''}"
data-path="${html_encode(options.path)}">`;
// spinner
h += `<div class="item-spinner">`;
h += `</div>`;
// modified
h += `<div class="item-attr item-attr--modified">`;
h += `<span>${options.modified === 0 ? '-' : timeago.format(options.modified*1000)}</span>`;
h += `</div>`;
// size
h += `<div class="item-attr item-attr--size">`;
h += `<span>${options.size ? byte_format(options.size) : '-'}</span>`;
h += `</div>`;
// type
h += `<div class="item-attr item-attr--type">`;
if(options.is_dir)
h += `<span>Folder</span>`;
else
h += `<span>${options.type ? html_encode(options.type) : '-'}</span>`;
h += `</div>`;
// icon
h += `<div class="item-icon">`;
h += `<img src="${html_encode(options.icon.image)}" class="item-icon-${options.icon.type}" data-item-id="${item_id}">`;
h += `</div>`;
// badges
h += `<div class="item-badges">`;
// website badge
h += `<img class="item-badge item-has-website-badge long-hover"
style="${options.has_website ? 'display:block;' : ''}"
src="${html_encode(window.icons['world.svg'])}"
data-item-id="${item_id}"
>`;
// link badge
h += `<img class="item-badge item-has-website-url-badge"
style="${website_url ? 'display:block;' : ''}"
src="${html_encode(window.icons['link.svg'])}"
data-item-id="${item_id}"
>`;
// shared badge
h += `<img class="item-badge item-badge-has-permission"
style="display: ${ is_shared_with_me ? 'block' : 'none'};
background-color: #ffffff;
padding: 2px;" src="${html_encode(window.icons['shared.svg'])}"
data-item-id="${item_id}"
title="A user has shared this item with you.">`;
// owner-shared badge
h += `<img class="item-badge item-is-shared"
style="background-color: #ffffff; padding: 2px; ${!is_shared_with_me && options.is_shared ? 'display:block;' : ''}"
src="${html_encode(window.icons['owner-shared.svg'])}"
data-item-id="${item_id}"
data-item-uid="${options.uid}"
data-item-path="${html_encode(options.path)}"
title="You have shared this item with at least one other user."
>`;
// shortcut badge
h += `<img class="item-badge item-shortcut"
style="background-color: #ffffff; padding: 2px; ${options.is_shortcut !== 0 ? 'display:block;' : ''}"
src="${html_encode(window.icons['shortcut.svg'])}"
data-item-id="${item_id}"
title="Shortcut"
>`;
h += `</div>`;
// name
h += `<span class="item-name" data-item-id="${item_id}" title="${html_encode(options.name)}">${html_encode(truncate_filename(options.name, TRUNCATE_LENGTH)).replaceAll(' ', ' ')}</span>`
// name editor
h += `<textarea class="item-name-editor hide-scrollbar" spellcheck="false" autocomplete="off" autocorrect="off" autocapitalize="off" data-gramm_editor="false">${html_encode(options.name)}</textarea>`
h += `</div>`;
// append to options.appendTo
$(options.appendTo).append(h);
// updte item_container
const item_container = $(options.appendTo).closest('.item-container');
show_or_hide_empty_folder_message(item_container);
// get all the elements needed
const el_item = document.getElementById(`item-${item_id}`);
const el_item_name = document.querySelector(`#item-${item_id} > .item-name`);
const el_item_icon = document.querySelector(`#item-${item_id} .item-icon`);
const el_item_name_editor = document.querySelector(`#item-${item_id} > .item-name-editor`);
const is_trashed = $(el_item).attr('data-path').startsWith(trash_path + '/');
// update parent window's explorer item count if applicable
if(options.appendTo !== undefined){
let el_window = options.appendTo;
if(!$(el_window).hasClass('.window'))
el_window = $(el_window).closest('.window');
update_explorer_footer_item_count(el_window);
}
// --------------------------------------------------------
// Dragster
// allow dragging of local files on this window, if it's is_dir
// --------------------------------------------------------
if(options.is_dir){
$(el_item).dragster({
enter: function () {
$(el_item).not('.item-disabled').addClass('item-selected');
},
leave: function () {
$(el_item).removeClass('item-selected');
},
drop: function (dragsterEvent, event) {
const e = event.originalEvent;
$(el_item).removeClass('item-selected');
// if files were dropped...
if(e.dataTransfer?.items?.length > 0){
upload_items( e.dataTransfer.items, $(el_item).attr('data-path'))
}
e.stopPropagation();
e.preventDefault();
return false;
}
});
}
// --------------------------------------------------------
// Draggable
// --------------------------------------------------------
let longer_hover_timeout;
let last_window_dragged_over;
$(el_item).draggable({
appendTo: "body",
helper: "clone",
revert: "invalid",
//containment: "document",
zIndex: 10000,
scroll:false,
distance: 5,
revertDuration: 100,
start: function(event, ui) {
// select this item and its helper
$(el_item).addClass('item-selected');
$('.ui-draggable-dragging').addClass('item-selected');
//clone other selected items
$(el_item)
.siblings('.item-selected')
.clone()
.addClass('item-selected-clone')
.css('position', 'absolute')
.appendTo('body')
.hide();
// Bring item and clones to front
$('.item-selected-clone, .ui-draggable-dragging').css('z-index', 99999);
// count badge
const item_count = $('.item-selected-clone').length;
if(item_count > 0){
$('body').append(`<span class="draggable-count-badge">${item_count + 1}</span>`);
}
// Disable all droppable UIItems that are not a dir/app to avoid accidental cancellation
// on Items that are not droppables. In general if an item is dropped on another, if the
// target is not a dir, the source needs to be dropped on the target's container.
$(`.item[data-is_dir="0"][data-associated_app_name=""]:not(.item-selected)`).droppable('disable');
// Disable pointer events on all app iframes. This is needed because as soon as
// a dragging event enters the iframe the event is delegated to iframe which makes the item
// stuck at the edge of the iframe not allowing us to move items freely across the screen
$('.window-app-iframe').css('pointer-events', 'none')
// reset longer hover timeout and last window dragged over
longer_hover_timeout = null;
last_window_dragged_over = null;
},
drag: function(event, ui) {
// Only show drag helpers if the item has been moved more than 5px
if( Math.abs(ui.originalPosition.top - ui.offset.top) > 5
||
Math.abs(ui.originalPosition.left - ui.offset.left) > 5 ){
$('.ui-draggable-dragging').show();
$('.item-selected-clone').show();
$('.draggable-count-badge').show();
}
const other_selected_items = $('.item-selected-clone');
const item_count = other_selected_items.length + 1;
// Move count badge with mouse
$('.draggable-count-badge').css({
top: event.pageY,
left: event.pageX + 10,
})
// Move other selected items
for(let i=0; i < item_count - 1; i++){
$(other_selected_items[i]).css({
'left': ui.position.left + 3 * (i+1),
'top': ui.position.top + 3 * (i+1),
'z-index': 999 - (i),
'opacity': 0.5 - i*0.1,
})
}
// remove all item-container active borders
$('.item-container').removeClass('item-container-active');
// if item has changed container, remove timeout for window focus and reset last target
if(longer_hover_timeout && last_window_dragged_over !== window.mouseover_window){
clearTimeout(longer_hover_timeout);
longer_hover_timeout = null;
last_window_dragged_over = window.mouseover_window;
}
// if item hover for more than 1.2s, focus the window
if(!longer_hover_timeout){
longer_hover_timeout = setTimeout(() => {
$(last_window_dragged_over).focusWindow();
}, 1200);
}
// Highlight item container to help user see more clearly where the item is going to be dropped
if($(window.mouseover_item_container).closest('.window').is(window.mouseover_window) &&
// do not highlight if the target is the same as the item being moved
$(el_item).attr('data-path') !== $(window.mouseover_item_container).attr('data-path') &&
// do not highlight if item is being moved to where it already is
$(el_item).attr('data-path') !== $(window.mouseover_item_container).attr('data-path')){
// highlight item container
$(window.mouseover_item_container).addClass('item-container-active');
}
// send drag event to iframe if mouse is inside iframe
if(mouseover_window){
const $app_iframe = $(mouseover_window).find('.window-app-iframe');
if(!$(mouseover_window).hasClass('window-disabled') && $app_iframe.length > 0){
var rect = $app_iframe.get(0).getBoundingClientRect();
// if mouse is inside iframe, send drag message to iframe
if(mouseX > rect.left && mouseX < rect.right && mouseY > rect.top && mouseY < rect.bottom){
$app_iframe.get(0).contentWindow.postMessage({msg: "drag", x: (mouseX - rect.left), y: (mouseY - rect.top)}, '*');
}
}
}
},
stop: function(event, ui){
$('.item-selected-clone').remove();
$('.draggable-count-badge').remove();
// re-enable all droppable UIItems that are not a dir
$(`.item[data-is_dir='0']:not(.item-selected)`).droppable('enable');
// remove active item-container border highlights
$('.item-container').removeClass('item-container-active');
// reset longer hover timeout and last window dragged over
clearTimeout(longer_hover_timeout);
last_window_dragged_over = null;
}
});
// --------------------------------------------------------
// Droppable
// --------------------------------------------------------
$(el_item).droppable({
accept: '.item',
// 'pointer' is very important because of active window tracking is based on the position of cursor.
tolerance: 'pointer',
drop: async function( event, ui ) {
// Check if hovering over an item that is VISIBILE
if($(event.target).closest('.window').attr('data-id') !== $(mouseover_window).attr('data-id'))
return;
// If ctrl is pressed and source is Trashed, cancel whole operation
if(event.ctrlKey && path.dirname($(ui.draggable).attr('data-path')) === window.trash_path)
return;
const items_to_move = []
// First item
items_to_move.push(ui.draggable);
// All subsequent items
const cloned_items = document.getElementsByClassName('item-selected-clone');
for(let i =0; i<cloned_items.length; i++){
const source_item = document.getElementById('item-' + $(cloned_items[i]).attr('data-id'));
if(source_item !== null)
items_to_move.push(source_item);
}
// --------------------------------------------------------
// If dropped on an app, open the app with the dropped
// items as argument
//--------------------------------------------------------
if(options.associated_app_name){
// an array that hold the items to sign
const items_to_open = [];
// prepare items to sign
for(let i=0; i < items_to_move.length; i++){
items_to_open.push({
name: $(items_to_move[i]).attr('data-name'),
uid: $(items_to_move[i]).attr('data-uid'),
action: 'write',
path: $(items_to_move[i]).attr('data-path')
});
}
// open each item
for (let i = 0; i < items_to_open.length; i++) {
const item = items_to_open[i];
launch_app({
name: options.associated_app_name,
file_path: item.path,
// app_obj: open_item_meta.suggested_apps[0],
window_title: item.name,
file_uid: item.uid,
file_signature: item,
});
}
// deselect dragged item
for(let i=0; i < items_to_move.length; i++)
$(items_to_move[i]).removeClass('item-selected');
}
//--------------------------------------------------------
// If dropped on a directory, move items to that directory
//--------------------------------------------------------
else{
// If ctrl key is down, copy items. Except if target or source is Trash
if(event.ctrlKey){
if(options.is_dir && $(el_item).attr('data-path') !== window.trash_path )
copy_items(items_to_move, $(el_item).attr('data-path'))
else if(!options.is_dir)
copy_items(items_to_move, path.dirname($(el_item).attr('data-path')));
}
// If alt key is down, create shortcut items
else if(event.altKey && window.feature_flags.create_shortcut){
items_to_move.forEach((item_to_move) => {
create_shortcut(
path.basename($(item_to_move).attr('data-path')),
$(item_to_move).attr('data-is_dir') === '1',
options.is_dir ? $(el_item).attr('data-path') : path.dirname($(el_item).attr('data-path')),
null,
$(item_to_move).attr('data-shortcut_to') === '' ? $(item_to_move).attr('data-uid') : $(item_to_move).attr('data-shortcut_to'),
$(item_to_move).attr('data-shortcut_to_path') === '' ? $(item_to_move).attr('data-path') : $(item_to_move).attr('data-shortcut_to_path'),
);
});
}
// Otherwise, move items
else if(options.is_dir){
move_items(items_to_move, $(el_item).attr('data-shortcut_to_path') !== '' ? $(el_item).attr('data-shortcut_to_path') : $(el_item).attr('data-path'));
}
}
// Re-enable droppable on all 'item-container's
$('.item-container').droppable('enable')
return false;
},
over: function(event, ui){
// Check hovering over an item that is VISIBILE
const $event_parent_win = $(event.target).closest('.window')
if( $event_parent_win.length > 0 && $event_parent_win.attr('data-id') !== $(mouseover_window).attr('data-id'))
return;
// Don't do anything if the dragged item is NOT a UIItem
if(!$(ui.draggable).hasClass('item'))
return;
// If this is a directory or an app, and an item was dragged over it, highlight it.
if(options.is_dir || options.associated_app_name){
$(el_item).addClass('item-selected');
$('.ui-draggable-dragging .item-name, .item-selected-clone .item-name').css('opacity', 0.1)
// remove all item-container active borders
$('.item-container').addClass('item-container-transparent-border')
}
// Disable all window bodies
$('.item-container').droppable( 'disable' )
},
out: function(event, ui){
// Don't do anything if the dragged item is NOT a UIItem
if(!$(ui.draggable).hasClass('item'))
return;
// Unselect directory/app if item is dragged out
if(options.is_dir || options.associated_app_name){
$(el_item).removeClass('item-selected');
$('.ui-draggable-dragging .item-name, .item-selected-clone .item-name').css('opacity', 'initial')
$('.item-container').removeClass('item-container-transparent-border')
}
$('.item-container').droppable( 'enable' )
}
});
// --------------------------------------------------------
// Double Click/Single Tap on Item
// --------------------------------------------------------
if(isMobile.phone || isMobile.tablet){
$(el_item).on('click', async function (e) {
// if item is disabled, do not allow any action
if($(el_item).hasClass('item-disabled'))
return false;
if($(e.target).hasClass('item-name-editor'))
return false;
open_item({
item: el_item,
maximized: true,
});
});
}else{
$(el_item).on('dblclick', async function (e) {
// if item is disabled, do not allow any action
if($(el_item).hasClass('item-disabled'))
return false;
if($(e.target).hasClass('item-name-editor'))
return false;
open_item({
item: el_item,
new_window: e.metaKey || e.ctrlKey,
});
});
}
// --------------------------------------------------------
// Mousedown
// --------------------------------------------------------
$(el_item).on('mousedown', function (e) {
// if item is disabled, do not allow any action
if($(el_item).hasClass('item-disabled'))
return false;
// if link badge is clicked, don't continue
if($(e.target).hasClass('item-has-website-url-badge'))
return false;
const $el_parent_window = $(el_item).closest('.window');
// first see if this is a ContextMenu call on multiple items
if(e.which === 3 && $(el_item).hasClass('item-selected') && $(el_item).siblings('.item-selected').length > 0){
$(".context-menu").remove();
return false;
}
// unselect other items if neither CTRL nor Command key are held
// or
// if parent is not multiselectable
if((!e.ctrlKey && !e.metaKey && !$(this).hasClass('item-selected')) || ($el_parent_window.length>0 && $el_parent_window.attr('data-multiselectable') !== 'true')){
$(this).closest('.item-container').find('.item-selected').removeClass('item-selected');
}
if((e.ctrlKey || e.metaKey) && $(this).hasClass('item-selected')){
$(this).removeClass('item-selected')
}
else{
$(this).addClass('item-selected')
}
update_explorer_footer_selected_items_count($el_parent_window)
});
// --------------------------------------------------------
// Click
// --------------------------------------------------------
$(el_item).on('click', function (e) {
// if item is disabled, do not allow any action
if($(el_item).hasClass('item-disabled'))
return false;
skip_a_rename_click = false;
const $el_parent_window = $(el_item).closest('.window');
// do not unselect other items if:
// CTRL/Command key is pressed or clicking an item that is already selected
if(!e.ctrlKey && !e.metaKey){
$(this).closest('.item-container').find('.item-selected').not(this).removeClass('item-selected');
update_explorer_footer_selected_items_count($el_parent_window)
}
//----------------------------------------------------------------
// On an OpenFileDialog?
//----------------------------------------------------------------
if($el_parent_window.attr('data-is_openFileDialog') === 'true'){
if(!options.is_dir)
$el_parent_window.find('.openfiledialog-open-btn').removeClass('disabled');
else
$el_parent_window.find('.openfiledialog-open-btn').addClass('disabled');
}
//----------------------------------------------------------------
// On a SaveFileDialog?
//----------------------------------------------------------------
if($el_parent_window.attr('data-is_saveFileDialog') === 'true' && !options.is_dir){
$el_parent_window.find('.savefiledialog-filename').val($(el_item).attr('data-name'));
$el_parent_window.find('.savefiledialog-save-btn').removeClass('disabled');
}
});
$(document).on('click', function(e){
if(!$(e.target).hasClass('item') && !$(e.target).hasClass('item-name') && !$(e.target).hasClass('item-icon')){
skip_a_rename_click = true;
}
if($(e.target).parents('.item').data('id') !== item_id){
skip_a_rename_click = true;
}
})
// --------------------------------------------------------
// Rename
// --------------------------------------------------------
function rename(){
if(rename_cancelled){
rename_cancelled = false;
return;
}
const old_name = $(el_item).attr('data-name');
const old_path = $(el_item).attr('data-path');
const new_name = $(el_item_name_editor).val();
// Don't send a rename request if:
// the new name is the same as the old one,
// or it's empty,
// or editable was not even active at all
if(old_name === new_name || !new_name || new_name === '.' || new_name === '..' || !$(el_item_name_editor).hasClass('item-name-editor-active')){
if(new_name === '.'){
UIAlert(`The name "." is not allowed, because it is a reserved name. Please choose another name.`);
}
else if(new_name === '..'){
UIAlert(`The name ".." is not allowed, because it is a reserved name. Please choose another name.`)
}
$(el_item_name).html(truncate_filename(options.name, TRUNCATE_LENGTH).replaceAll(' ', ' '));
$(el_item_name).show();
$(el_item_name_editor).val($(el_item).attr('data-name'));
$(el_item_name_editor).hide();
return;
}
// deactivate item name editable
$(el_item_name_editor).removeClass('item-name-editor-active');
// Perform rename request
puter.fs.rename({
uid: options.uid === 'null' ? null : options.uid,
new_name: new_name,
excludeSocketID: window.socket.id,
success: async (fsentry)=>{
// Has the extension changed? in that case update options.sugggested_apps
const old_extension = path.extname(old_name);
const new_extension = path.extname(new_name);
if(old_extension !== new_extension){
suggest_apps_for_fsentry({
uid: options.uid,
onSuccess: function(suggested_apps){
options.suggested_apps = suggested_apps;
}
});
}
// Set new item name
$(`.item[data-uid='${$(el_item).attr('data-uid')}'] .item-name`).html(html_encode(truncate_filename(new_name, TRUNCATE_LENGTH)).replaceAll(' ', ' '));
$(el_item_name).show();
// Hide item name editor
$(el_item_name_editor).hide();
// Set new icon
const new_icon = (options.is_dir ? window.icons['folder.svg'] : (await item_icon(fsentry)).image);
$(el_item_icon).find('.item-icon-icon').attr('src', new_icon);
// Set new data-name
options.name = new_name;
$(el_item).attr('data-name', html_encode(new_name));
$(`.item[data-uid='${$(el_item).attr('data-uid')}']`).attr('data-name', html_encode(new_name));
$(`.window-${options.uid}`).attr('data-name', html_encode(new_name));
// Set new title attribute
$(`.item[data-uid='${$(el_item).attr('data-uid')}']`).attr('title', html_encode(new_name));
$(`.window-${options.uid}`).attr('title', html_encode(new_name));
// Set new value for item-name-editor
$(`.item[data-uid='${$(el_item).attr('data-uid')}'] .item-name-editor`).val(html_encode(new_name));
$(`.item[data-uid='${$(el_item).attr('data-uid')}'] .item-name`).attr('title', html_encode(new_name));
// Set new data-path
options.path = path.join( path.dirname(options.path), options.name);
const new_path = options.path;
$(el_item).attr('data-path', new_path);
$(`.item[data-uid='${$(el_item).attr('data-uid')}']`).attr('data-path', new_path);
$(`.window-${options.uid}`).attr('data-path', new_path);
// Update all elements that have matching paths
$(`[data-path="${html_encode(old_path)}" i]`).each(function(){
$(this).attr('data-path', new_path)
if($(this).hasClass('window-navbar-path-dirname'))
$(this).text(new_name);
});
// Update the paths of all elements whose paths start with old_path
$(`[data-path^="${html_encode(old_path) + '/'}"]`).each(function(){
const new_el_path = _.replace($(this).attr('data-path'), old_path + '/', new_path+'/');
$(this).attr('data-path', new_el_path);
});
// Update the 'Sites Cache'
if($(el_item).attr('data-has_website') === '1')
await update_sites_cache();
// Update website_url
website_url = determine_website_url(new_path);
$(el_item).attr('data-website_url', website_url);
// Update all exact-matching windows
$(`.window-${options.uid}`).each(function(){
update_window_path(this, options.path);
})
// Set new name for corresponding open windows
$(`.window-${options.uid} .window-head-title`).text(new_name);
// Re-sort all matching item containers
$(`.item[data-uid='${$(el_item).attr('data-uid')}']`).parent('.item-container').each(function(){
sort_items(this, $(el_item).closest('.item-container').attr('data-sort_by'), $(el_item).closest('.item-container').attr('data-sort_order'));
})
},
error: function (err){
// reset to old name
$(el_item_name).text(truncate_filename(options.name, TRUNCATE_LENGTH));
$(el_item_name).show();
// hide item name editor
$(el_item_name_editor).hide();
$(el_item_name_editor).val(html_encode($(el_item).attr('data-name')));
//show error
if(err.message){
UIAlert(err.message)
}
},
});
}
// --------------------------------------------------------
// Rename if enter pressed on Item Name Editor
// --------------------------------------------------------
$(el_item_name_editor).on('keypress',function(e) {
// If name editor is not active don't continue
if(!$(el_item_name_editor).is(":visible"))
return;
// Enter key = rename
if(e.which === 13) {
e.stopPropagation();
e.preventDefault();
$(el_item_name_editor).blur();
$(el_item).addClass('item-selected');
last_enter_pressed_to_rename_ts = Date.now();
update_explorer_footer_selected_items_count($(el_item).closest('.item-container'));
return false;
}
})
// --------------------------------------------------------
// Cancel and undo if escape pressed on Item Name Editor
// --------------------------------------------------------
$(el_item_name_editor).on('keyup',function(e) {
if(!$(el_item_name_editor).is(":visible"))
return;
// Escape = undo rename
else if(e.which === 27){
e.stopPropagation();
e.preventDefault();
rename_cancelled = true;
$(el_item_name_editor).hide();
$(el_item_name_editor).val(options.name);
$(el_item_name).show();
}
});
$(el_item_name_editor).on('focusout',function(e) {
e.stopPropagation();
e.preventDefault();
rename();
});
/************************************************
* Takes care of 'click to edit item name'
************************************************/
let skip_a_rename_click = true;
$(el_item_name).on('click', function(e){
if( !skip_a_rename_click && e.which !== 3 && $(el_item_name).parent('.item-selected').length > 0){
last_mousedown_ts = Date.now();
setTimeout(() => {
if(!skip_a_rename_click && (Date.now() - last_mousedown_ts) > 400){
if (!e.ctrlKey && !e.metaKey)
activate_item_name_editor(el_item)
last_mousedown_ts = 0
}else{
last_mousedown_ts = Date.now() + 500;
skip_a_rename_click= false;
}
}, 500);
}
skip_a_rename_click = false;
})
$(el_item_name).on('dblclick', function(e){
skip_a_rename_click = true;
})
// --------------------------------------------------------
// ContextMenu
// --------------------------------------------------------
$(el_item).bind("contextmenu taphold", async function (event) {
// if item is disabled, do not allow any action
if($(el_item).hasClass('item-disabled'))
return false;
// if on website link badge, don't continue
if($(event.target).hasClass('item-has-website-url-badge'))
return false;
// dimiss taphold on regular devices
if(event.type==='taphold' && !isMobile.phone && !isMobile.tablet)
return;
// if editing item name, preserve native context menu
if(event.target === el_item_name_editor)
return;
// if ctrl is pressed don't open ctxmenu, ctrl is for drag and copy
if(event.ctrlKey)
return false;
event.preventDefault();
let menu_items;
const $selected_items = $(el_item).closest('.item-container').find('.item-selected').not(el_item).addBack();
// -------------------------------------------------------
// Multiple items selected
// -------------------------------------------------------
if($selected_items.length > 1){
const are_trashed = $selected_items.attr('data-path').startsWith(trash_path + '/');
menu_items = []
// -------------------------------------------
// Restore
// -------------------------------------------
if(are_trashed){
menu_items.push({
html: "Restore",
onClick: function(){
$selected_items.each(function() {
const ell = this;
let metadata = $(ell).attr('data-metadata') === '' ? {} : JSON.parse($(ell).attr('data-metadata'))
move_items([ell], path.dirname(metadata.original_path));
})
}
});
// -------------------------------------------
// -
// -------------------------------------------
menu_items.push('-');
}
if(!are_trashed){
// -------------------------------------------
// Donwload
// -------------------------------------------
menu_items.push({
html: 'Download',
onClick: async function(){
let items = [];
for (let index = 0; index < $selected_items.length; index++) {
items.push($selected_items[index]);
}
zipItems(items, path.dirname($(el_item).attr('data-path')), true);
}
});
// -------------------------------------------
// Zip
// -------------------------------------------
menu_items.push({
html: 'Zip',
onClick: async function(){
let items = [];
for (let index = 0; index < $selected_items.length; index++) {
items.push($selected_items[index]);
}
zipItems(items, path.dirname($(el_item).attr('data-path')), false);
}
});
// -------------------------------------------
// -
// -------------------------------------------
menu_items.push('-');
}
// -------------------------------------------
// Cut
// -------------------------------------------
menu_items.push({
html: "Cut",
onClick: function(){
window.clipboard_op= 'move';
window.clipboard = [];
$selected_items.each(function() {
const ell = this;
window.clipboard.push($(ell).attr('data-path'));
})
}
});
// -------------------------------------------
// Copy
// -------------------------------------------
if(!are_trashed){
menu_items.push({
html: "Copy",
onClick: function(){
window.clipboard_op= 'copy';
window.clipboard = [];
$selected_items.each(function() {
const ell = this;
window.clipboard.push({path: $(ell).attr('data-path')});
})
}
});
}
// -------------------------------------------
// -
// -------------------------------------------
menu_items.push('-');
// -------------------------------------------
// Delete Permanently
// -------------------------------------------
if(are_trashed){
menu_items.push({
html: 'Delete Permanently',
onClick: async function(){
const alert_resp = await UIAlert({
message: `Are you sure you want to permanently delete these items?`,
buttons:[
{
label: 'Delete',
type: 'primary',
},
{
label: 'Cancel'
},
]
})
if((alert_resp) === 'Delete'){
for (let index = 0; index < $selected_items.length; index++) {
const element = $selected_items[index];
await delete_item(element);
}
const trash = await puter.fs.stat(trash_path);
// update other clients
if(window.socket){
window.socket.emit('trash.is_empty', {is_empty: trash.is_empty});
}
if(trash.is_empty){
$(`.item[data-path="${html_encode(trash_path)}" i], .item[data-shortcut_to_path="${trash_path}" i]`).find('.item-icon > img').attr('src', window.icons['trash.svg']);
$(`.window[data-path="${html_encode(trash_path)}"]`).find('.window-head-icon').attr('src', window.icons['trash.svg']);
}
}
}
});
}
// -------------------------------------------
// Create Shortcut
// -------------------------------------------
if(!are_trashed && window.feature_flags.create_shortcut){
menu_items.push({
html: 'Create Shortcut',
onClick: async function(){
$selected_items.each(function() {
let base_dir = path.dirname($(this).attr('data-path'));
// Trash on Desktop is a special case
if($(this).attr('data-path') && $(this).closest('.item-container').attr('data-path') === window.desktop_path){
base_dir = window.desktop_path;
}
// create shortcut
create_shortcut(
path.basename($(this).attr('data-path')),
$(this).attr('data-is_dir') === '1',
base_dir,
$(this).closest('.item-container'),
$(this).attr('data-shortcut_to') === '' ? $(this).attr('data-uid') : $(this).attr('data-shortcut_to'),
$(this).attr('data-shortcut_to_path') === '' ? $(this).attr('data-path') : $(this).attr('data-shortcut_to_path'),
);
})
}
});
}
// -------------------------------------------
// Delete
// -------------------------------------------
if(!are_trashed){
menu_items.push({
html: 'Delete',
onClick: async function(){
move_items($selected_items, trash_path);
}
});
}
}
// -------------------------------------------------------
// One item selected
// -------------------------------------------------------
else{