-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
filelist.js
4042 lines (3633 loc) · 120 KB
/
filelist.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) 2014
*
* This file is licensed under the Affero General Public License version 3
* or later.
*
* See the COPYING-README file.
*
*/
(function() {
/**
* @class OCA.Files.FileList
* @classdesc
*
* The FileList class manages a file list view.
* A file list view consists of a controls bar and
* a file list table.
*
* @param $el container element with existing markup for the .files-controls
* and a table
* @param {Object} [options] map of options, see other parameters
* @param {Object} [options.scrollContainer] scrollable container, defaults to $(window)
* @param {Object} [options.dragOptions] drag options, disabled by default
* @param {Object} [options.folderDropOptions] folder drop options, disabled by default
* @param {boolean} [options.detailsViewEnabled=true] whether to enable details view
* @param {boolean} [options.enableUpload=false] whether to enable uploader
* @param {OC.Files.Client} [options.filesClient] files client to use
*/
var FileList = function($el, options) {
this.initialize($el, options);
};
/**
* @memberof OCA.Files
*/
FileList.prototype = {
SORT_INDICATOR_ASC_CLASS: 'icon-triangle-n',
SORT_INDICATOR_DESC_CLASS: 'icon-triangle-s',
id: 'files',
appName: t('files', 'Files'),
isEmpty: true,
useUndo:true,
/**
* Top-level container with controls and file list
*/
$el: null,
/**
* Files table
*/
$table: null,
/**
* List of rows (table tbody)
*/
$fileList: null,
$header: null,
headers: [],
$footer: null,
footers: [],
/**
* @type OCA.Files.BreadCrumb
*/
breadcrumb: null,
/**
* @type OCA.Files.FileSummary
*/
fileSummary: null,
/**
* @type OCA.Files.DetailsView
*/
_detailsView: null,
/**
* Files client instance
*
* @type OC.Files.Client
*/
filesClient: null,
/**
* Whether the file list was initialized already.
* @type boolean
*/
initialized: false,
/**
* Wheater the file list was already shown once
* @type boolean
*/
shown: false,
/**
* Number of files per page
* Always show a minimum of 1
*
* @return {number} page size
*/
pageSize: function() {
var isGridView = this.$table.hasClass('view-grid');
var columns = 1;
var rows = Math.ceil(this.$container.height() / 50);
if (isGridView) {
columns = Math.ceil(this.$container.width() / 160);
rows = Math.ceil(this.$container.height() / 160);
}
return Math.max(columns*rows, columns);
},
/**
* Array of files in the current folder.
* The entries are of file data.
*
* @type Array.<OC.Files.FileInfo>
*/
files: [],
/**
* Current directory entry
*
* @type OC.Files.FileInfo
*/
dirInfo: null,
/**
* Whether to prevent or to execute the default file actions when the
* file name is clicked.
*
* @type boolean
*/
_defaultFileActionsDisabled: false,
/**
* File actions handler, defaults to OCA.Files.FileActions
* @type OCA.Files.FileActions
*/
fileActions: null,
/**
* File selection menu, defaults to OCA.Files.FileSelectionMenu
* @type OCA.Files.FileSelectionMenu
*/
fileMultiSelectMenu: null,
/**
* Whether selection is allowed, checkboxes and selection overlay will
* be rendered
*/
_allowSelection: true,
/**
* Map of file id to file data
* @type Object.<int, Object>
*/
_selectedFiles: {},
/**
* Summary of selected files.
* @type OCA.Files.FileSummary
*/
_selectionSummary: null,
/**
* If not empty, only files containing this string will be shown
* @type String
*/
_filter: '',
/**
* @type UserConfig
* @see /apps/files/lib/Service/UserConfig.php
*/
_filesConfig: undefined,
/**
* Sort attribute
* @type String
*/
_sort: 'name',
/**
* Sort direction: 'asc' or 'desc'
* @type String
*/
_sortDirection: 'asc',
/**
* Sort comparator function for the current sort
* @type Function
*/
_sortComparator: null,
/**
* Whether to do a client side sort.
* When false, clicking on a table header will call reload().
* When true, clicking on a table header will simply resort the list.
*/
_clientSideSort: true,
/**
* Whether or not users can change the sort attribute or direction
*/
_allowSorting: true,
/**
* Current directory
* @type String
*/
_currentDirectory: null,
_dragOptions: null,
_folderDropOptions: null,
/**
* @type OC.Uploader
*/
_uploader: null,
/**
* Initialize the file list and its components
*
* @param $el container element with existing markup for the .files-controls
* and a table
* @param options map of options, see other parameters
* @param options.scrollContainer scrollable container, defaults to $(window)
* @param options.dragOptions drag options, disabled by default
* @param options.folderDropOptions folder drop options, disabled by default
* @param options.scrollTo name of file to scroll to after the first load
* @param [options.dir='/'] current directory
* @param {OC.Files.Client} [options.filesClient] files API client
* @param {OC.Backbone.Model} [options.filesConfig] files app configuration
* @private
*/
initialize: function($el, options) {
var self = this;
options = options || {};
if (this.initialized) {
return;
}
if (options.shown) {
this.shown = options.shown;
}
if (options.config) {
this._filesConfig = options.config;
} else if (!_.isUndefined(OCA.Files) && !_.isUndefined(OCA.Files.App)) {
this._filesConfig = OCA.Files.App.getFilesConfig();
} else {
this._filesConfig = OCP.InitialState.loadState('files', 'config', {})
}
if (options.dragOptions) {
this._dragOptions = options.dragOptions;
}
if (options.folderDropOptions) {
this._folderDropOptions = options.folderDropOptions;
}
if (options.filesClient) {
this.filesClient = options.filesClient;
} else {
// default client if not specified
this.filesClient = OC.Files.getClient();
}
this.$el = $el;
if (options.id) {
this.id = options.id;
}
this.$container = options.scrollContainer || $('#app-content');
this.$table = $el.find('table:first');
this.$fileList = $el.find('.files-fileList');
this.$header = $el.find('.filelist-header');
this.$footer = $el.find('.filelist-footer');
// Legacy mapper for new vue components
window._nc_event_bus.subscribe('files:config:updated', ({ key, value }) => {
// Replace existing config with new one
Object.assign(this._filesConfig, { [key]: value })
if (key === 'show_hidden') {
self.$el.toggleClass('hide-hidden-files', !value);
self.updateSelectionSummary();
// hiding files could make the page too small, need to try rendering next page
if (!value) {
self._onScroll();
}
}
if (key === 'crop_image_previews') {
self.reload();
}
})
var config = OCP.InitialState.loadState('files', 'config', {})
if (config.show_hidden === false) {
this.$el.addClass('hide-hidden-files');
}
if (_.isUndefined(options.detailsViewEnabled) || options.detailsViewEnabled) {
this._detailsView = new OCA.Files.DetailsView();
this._detailsView.$el.addClass('disappear');
}
if (options && options.defaultFileActionsDisabled) {
this._defaultFileActionsDisabled = options.defaultFileActionsDisabled
}
this._initFileActions(options.fileActions);
if (this._detailsView) {
this._detailsView.addDetailView(new OCA.Files.MainFileInfoDetailView({fileList: this, fileActions: this.fileActions}));
}
this.files = [];
this._selectedFiles = {};
this._selectionSummary = new OCA.Files.FileSummary(undefined, {config: this._filesConfig});
// dummy root dir info
this.dirInfo = new OC.Files.FileInfo({});
this.fileSummary = this._createSummary();
if (options.multiSelectMenu) {
this.multiSelectMenuItems = options.multiSelectMenu;
for (var i=0; i<this.multiSelectMenuItems.length; i++) {
if (_.isFunction(this.multiSelectMenuItems[i])) {
this.multiSelectMenuItems[i] = this.multiSelectMenuItems[i](this);
}
}
this._updateMultiSelectFileActions()
}
if (options.sorting) {
this.setSort(options.sorting.mode, options.sorting.direction, false, false);
} else {
this.setSort('name', 'asc', false, false);
}
var breadcrumbOptions = {
onClick: _.bind(this._onClickBreadCrumb, this),
getCrumbUrl: function(part) {
return self.linkTo(part.dir);
}
};
// if dropping on folders is allowed, then also allow on breadcrumbs
if (this._folderDropOptions) {
breadcrumbOptions.onDrop = _.bind(this._onDropOnBreadCrumb, this);
breadcrumbOptions.onOver = function() {
self.$el.find('td.filename.ui-droppable').droppable('disable');
};
breadcrumbOptions.onOut = function() {
self.$el.find('td.filename.ui-droppable').droppable('enable');
};
}
this.breadcrumb = new OCA.Files.BreadCrumb(breadcrumbOptions);
var $controls = this.$el.find('.files-controls');
if ($controls.length > 0) {
$controls.prepend(this.breadcrumb.$el);
this.$table.addClass('has-controls');
}
this._renderNewButton();
this.$el.find('thead th .columntitle').click(_.bind(this._onClickHeader, this));
this._onResize = _.debounce(_.bind(this._onResize, this), 250);
$('#app-content').on('appresized', this._onResize);
$(window).resize(this._onResize);
this.$el.on('show', this._onResize);
// reload files list on share accept
$('body').on('OCA.Notification.Action', function(eventObject) {
if (eventObject.notification.app === 'files_sharing' && eventObject.action.type === 'POST') {
self.reload()
}
});
window._nc_event_bus.subscribe('files_sharing:share:created', () => { self.reload(true) });
window._nc_event_bus.subscribe('files_sharing:share:deleted', () => { self.reload(true) });
this.$fileList.on('click','td.filename>a.name, td.filesize, td.date', _.bind(this._onClickFile, this));
this.$fileList.on("droppedOnFavorites", function (event, file) {
self.fileActions.triggerAction('Favorite', self.getModelForFile(file), self);
});
this.$fileList.on('droppedOnTrash', function (event, filename, directory) {
self.do_delete(filename, directory);
});
this.$fileList.on('change', 'td.selection>.selectCheckBox', _.bind(this._onClickFileCheckbox, this));
this.$fileList.on('mouseover', 'td.selection', _.bind(this._onMouseOverCheckbox, this));
this.$el.on('show', _.bind(this._onShow, this));
this.$el.on('urlChanged', _.bind(this._onUrlChanged, this));
this.$el.find('.select-all').click(_.bind(this._onClickSelectAll, this));
this.$el.find('.actions-selected').click(function () {
self.fileMultiSelectMenu.show(self);
return false;
});
this.$container.on('scroll', _.bind(this._onScroll, this));
if (options.scrollTo) {
this.$fileList.one('updated', function() {
self.scrollTo(options.scrollTo);
});
}
if (!_.isUndefined(options.dir)) {
this._setCurrentDir(options.dir || '/', false);
}
if (options.openFile) {
// Wait for some initialisation process to be over before triggering the default action.
_.defer(() => {
try {
var fileInfo = JSON.parse(atob($('#initial-state-files-openFileInfo').val()))
var spec = this.fileActions.getDefaultFileAction(fileInfo.mime, fileInfo.type, fileInfo.permissions)
if (spec && spec.action) {
spec.action(fileInfo.name, {
fileId: fileInfo.id,
fileList: this,
fileActions: this.fileActions,
dir: fileInfo.directory
});
} else {
var url = this.getDownloadUrl(fileInfo.name, fileInfo.dir, true);
OCA.Files.Files.handleDownload(url);
}
if (document.documentElement.clientWidth > 1024) {
OCA.Files.Sidebar.open(fileInfo.path);
}
} catch (error) {
console.error(`Failed to trigger default action on the file for URL: ${location.href}`, error)
}
})
}
this._operationProgressBar = new OCA.Files.OperationProgressBar();
this._operationProgressBar.render();
this.$el.find('#uploadprogresswrapper').replaceWith(this._operationProgressBar.$el);
if (options.enableUpload) {
// TODO: auto-create this element
var $uploadEl = this.$el.find('#file_upload_start');
if ($uploadEl.exists()) {
this._uploader = new OC.Uploader($uploadEl, {
progressBar: this._operationProgressBar,
fileList: this,
filesClient: this.filesClient,
dropZone: $('#content'),
maxChunkSize: options.maxChunkSize
});
this.setupUploadEvents(this._uploader);
}
}
this.triedActionOnce = false;
OC.Plugins.attach('OCA.Files.FileList', this);
OCA.Files.App && OCA.Files.App.updateCurrentFileList(this);
this.initHeadersAndFooters()
},
initHeadersAndFooters: function() {
this.headers.sort(function(a, b) {
return a.order - b.order;
})
this.footers.sort(function(a, b) {
return a.order - b.order;
})
var uniqueIds = [];
var self = this;
this.headers.forEach(function(header) {
if (header.id) {
if (uniqueIds.indexOf(header.id) !== -1) {
return
}
uniqueIds.push(header.id)
}
self.$header.append(header.el)
setTimeout(function() {
header.render(self)
}, 0)
})
uniqueIds = [];
this.footers.forEach(function(footer) {
if (footer.id) {
if (uniqueIds.indexOf(footer.id) !== -1) {
return
}
uniqueIds.push(footer.id)
}
self.$footer.append(footer.el)
setTimeout(function() {
footer.render(self)
}, 0)
})
},
/**
* Destroy / uninitialize this instance.
*/
destroy: function() {
if (this._newFileMenu) {
this._newFileMenu.remove();
}
if (this._newButton) {
this._newButton.remove();
}
if (this._detailsView) {
this._detailsView.remove();
}
// TODO: also unregister other event handlers
this.fileActions.off('registerAction', this._onFileActionsUpdated);
this.fileActions.off('setDefault', this._onFileActionsUpdated);
OC.Plugins.detach('OCA.Files.FileList', this);
$('#app-content').off('appresized', this._onResize);
},
_selectionMode: 'single',
_getCurrentSelectionMode: function () {
return this._selectionMode;
},
_onClickToggleSelectionMode: function () {
this._selectionMode = (this._selectionMode === 'range') ? 'single' : 'range';
if (this._selectionMode === 'single') {
this._removeHalfSelection();
}
},
multiSelectMenuClick: function (ev, action) {
var actionFunction = _.find(this.multiSelectMenuItems, function (item) {return item.name === action;}).action;
if (actionFunction) {
actionFunction(this.getSelectedFiles());
return;
}
switch (action) {
case 'delete':
this._onClickDeleteSelected(ev)
break;
case 'download':
this._onClickDownloadSelected(ev);
break;
case 'copyMove':
this._onClickCopyMoveSelected(ev);
break;
case 'restore':
this._onClickRestoreSelected(ev);
break;
case 'tags':
this._onClickTagSelected(ev);
break;
}
},
/**
* Initializes the file actions, set up listeners.
*
* @param {OCA.Files.FileActions} fileActions file actions
*/
_initFileActions: function(fileActions) {
var self = this;
this.fileActions = fileActions;
if (!this.fileActions) {
this.fileActions = new OCA.Files.FileActions();
this.fileActions.registerDefaultActions();
}
if (this._detailsView) {
this.fileActions.registerAction({
name: 'Details',
displayName: t('files', 'Details'),
mime: 'all',
order: -50,
iconClass: 'icon-details',
permissions: OC.PERMISSION_NONE,
actionHandler: function(fileName, context) {
self._updateDetailsView(fileName);
}
});
}
this._onFileActionsUpdated = _.debounce(_.bind(this._onFileActionsUpdated, this), 100);
this.fileActions.on('registerAction', this._onFileActionsUpdated);
this.fileActions.on('setDefault', this._onFileActionsUpdated);
},
/**
* Returns a unique model for the given file name.
*
* @param {string|object} fileName file name or jquery row
* @return {OCA.Files.FileInfoModel} file info model
*/
getModelForFile: function(fileName) {
var self = this;
var $tr;
// jQuery object ?
if (fileName.is) {
$tr = fileName;
fileName = $tr.attr('data-file');
} else {
$tr = this.findFileEl(fileName);
}
if (!$tr || !$tr.length) {
return null;
}
// if requesting the selected model, return it
if (this._currentFileModel && this._currentFileModel.get('name') === fileName) {
return this._currentFileModel;
}
// TODO: note, this is a temporary model required for synchronising
// state between different views.
// In the future the FileList should work with Backbone.Collection
// and contain existing models that can be used.
// This method would in the future simply retrieve the matching model from the collection.
var model = new OCA.Files.FileInfoModel(this.elementToFile($tr), {
filesClient: this.filesClient
});
if (!model.get('path')) {
model.set('path', this.getCurrentDirectory(), {silent: true});
}
model.on('change', function(model) {
// re-render row
var highlightState = $tr.hasClass('highlighted');
$tr = self.updateRow(
$tr,
model.toJSON(),
{updateSummary: true, silent: false, animate: true}
);
// restore selection state
var selected = !!self._selectedFiles[$tr.data('id')];
self._selectFileEl($tr, selected);
$tr.toggleClass('highlighted', highlightState);
});
model.on('busy', function(model, state) {
self.showFileBusyState($tr, state);
});
return model;
},
/**
* Displays the details view for the given file and
* selects the given tab
*
* @param {string|OCA.Files.FileInfoModel} fileName file name or FileInfoModel for which to show details
* @param {string} [tabId] optional tab id to select
*/
showDetailsView: function(fileName, tabId) {
OC.debug && console.warn('showDetailsView is deprecated! Use OCA.Files.Sidebar.activeTab. It will be removed in nextcloud 20.');
this._updateDetailsView(fileName);
if (tabId) {
OCA.Files.Sidebar.setActiveTab(tabId);
}
},
/**
* Update the details view to display the given file
*
* @param {string|OCA.Files.FileInfoModel} fileName file name from the current list or a FileInfoModel object
* @param {boolean} [show=true] whether to open the sidebar if it was closed
*/
_updateDetailsView: function(fileName, show) {
if (!(OCA.Files && OCA.Files.Sidebar)) {
console.error('No sidebar available');
return;
}
if (!fileName && OCA.Files.Sidebar.close) {
OCA.Files.Sidebar.close()
return
} else if (typeof fileName !== 'string') {
fileName = ''
}
// this is the old (terrible) way of getting the context.
// don't use it anywhere else. Just provide the full path
// of the file to the sidebar service
var tr = this.findFileEl(fileName)
var model = this.getModelForFile(tr)
var path = model.attributes.path + '/' + model.attributes.name
// make sure the file list has the correct context available
if (this._currentFileModel) {
this._currentFileModel.off();
}
this.$fileList.children().removeClass('highlighted');
tr.addClass('highlighted');
this._currentFileModel = model;
const secondaryActionsOpen = Boolean(tr.find('.actions-secondary-vue').length)
// open sidebar and set file
if (!secondaryActionsOpen && (typeof show === 'undefined' || !!show || (OCA.Files.Sidebar.file !== ''))) {
OCA.Files.Sidebar.open(path.replace('//', '/'))
}
},
/**
* Replaces the current details view element with the details view
* element of this file list.
*
* Each file list has its own DetailsView object, and each one has its
* own root element, but there can be just one details view/sidebar
* element in the document. This helper method replaces the current
* details view/sidebar element in the document with the element from
* the DetailsView object of this file list.
*/
_replaceDetailsViewElementIfNeeded: function() {
var $appSidebar = $('#app-sidebar');
if ($appSidebar.length === 0) {
this._detailsView.$el.insertAfter($('#app-content'));
} else if ($appSidebar[0] !== this._detailsView.el) {
// "replaceWith()" can not be used here, as it removes the old
// element instead of just detaching it.
this._detailsView.$el.insertBefore($appSidebar);
$appSidebar.detach();
}
},
/**
* Event handler for when the window size changed
*/
_onResize: function() {
var containerWidth = this.$el.width();
var actionsWidth = 0;
$.each(this.$el.find('.files-controls .actions'), function(index, action) {
actionsWidth += $(action).outerWidth();
});
this.breadcrumb._resize();
},
setGridView: function(isGridView) {
this.$table.toggleClass('view-grid', isGridView);
if (isGridView) {
// If switching into grid view from list view, too few files might be displayed
// Try rendering the next page
this._onScroll();
}
},
/**
* Event handler when leaving previously hidden state
*/
_onShow: function(e) {
OCA.Files.App && OCA.Files.App.updateCurrentFileList(this);
if (e.itemId === this.id) {
this._setCurrentDir('/', false);
}
// Only reload if we don't navigate to a different directory
if (typeof e.dir === 'undefined' || e.dir === this.getCurrentDirectory()) {
this.reload();
}
},
/**
* Event handler for when the URL changed
*/
_onUrlChanged: function(e) {
if (e && _.isString(e.dir)) {
var currentDir = this.getCurrentDirectory();
// this._currentDirectory is NULL when fileList is first initialised
if(this._currentDirectory && currentDir === e.dir) {
return;
}
this.changeDirectory(e.dir, true, true, undefined, true);
}
},
/**
* Selected/deselects the given file element and updated
* the internal selection cache.
*
* @param {Object} $tr single file row element
* @param {boolean} state true to select, false to deselect
*/
_selectFileEl: function($tr, state) {
var $checkbox = $tr.find('td.selection>.selectCheckBox');
var oldData = !!this._selectedFiles[$tr.data('id')];
var data;
$checkbox.prop('checked', state);
$tr.toggleClass('selected', state);
// already selected ?
if (state === oldData) {
return;
}
data = this.elementToFile($tr);
if (state) {
this._selectedFiles[$tr.data('id')] = data;
this._selectionSummary.add(data);
}
else {
delete this._selectedFiles[$tr.data('id')];
this._selectionSummary.remove(data);
}
if (this._detailsView && !this._detailsView.$el.hasClass('disappear')) {
// hide sidebar
this._updateDetailsView(null);
}
this.$el.find('.select-all').prop('checked', this._selectionSummary.getTotal() === this.files.length);
},
_selectRange: function($tr) {
var checked = $tr.hasClass('selected');
var $lastTr = $(this._lastChecked);
var lastIndex = $lastTr.index();
var currentIndex = $tr.index();
var $rows = this.$fileList.children('tr');
// last clicked checkbox below current one ?
if (lastIndex > currentIndex) {
var aux = lastIndex;
lastIndex = currentIndex;
currentIndex = aux;
}
// auto-select everything in-between
for (var i = lastIndex; i <= currentIndex; i++) {
this._selectFileEl($rows.eq(i), !checked);
}
this._removeHalfSelection();
this._selectionMode = 'single';
},
_selectSingle: function($tr) {
var state = !$tr.hasClass('selected');
this._selectFileEl($tr, state);
},
_onMouseOverCheckbox: function(e) {
if (this._getCurrentSelectionMode() !== 'range') {
return;
}
var $currentTr = $(e.target).closest('tr');
var $lastTr = $(this._lastChecked);
var lastIndex = $lastTr.index();
var currentIndex = $currentTr.index();
var $rows = this.$fileList.children('tr');
// last clicked checkbox below current one ?
if (lastIndex > currentIndex) {
var aux = lastIndex;
lastIndex = currentIndex;
currentIndex = aux;
}
// auto-select everything in-between
this._removeHalfSelection();
for (var i = 0; i <= $rows.length; i++) {
var $tr = $rows.eq(i);
var $checkbox = $tr.find('td.selection>.selectCheckBox');
if(lastIndex <= i && i <= currentIndex) {
$tr.addClass('halfselected');
$checkbox.prop('checked', true);
}
}
},
_removeHalfSelection: function() {
var $rows = this.$fileList.children('tr');
for (var i = 0; i <= $rows.length; i++) {
var $tr = $rows.eq(i);
$tr.removeClass('halfselected');
var $checkbox = $tr.find('td.selection>.selectCheckBox');
$checkbox.prop('checked', !!this._selectedFiles[$tr.data('id')]);
}
},
/**
* Event handler for when clicking on files to select them
*/
_onClickFile: function(event) {
var $tr = $(event.target).closest('tr');
if ($tr.hasClass('dragging')) {
return;
}
if (this._allowSelection && event.shiftKey) {
event.preventDefault();
this._selectRange($tr);
this._lastChecked = $tr;
this.updateSelectionSummary();
} else if (!event.ctrlKey) {
// clicked directly on the name
if (!this._detailsView || $(event.target).is('.nametext, .name, .thumbnail') || $(event.target).closest('.nametext').length) {
var filename = $tr.attr('data-file');
var renaming = $tr.data('renaming');
if (this._defaultFileActionsDisabled) {
event.preventDefault();
} else if (!renaming) {
this.fileActions.currentFile = $tr.find('td');
var spec = this.fileActions.getCurrentDefaultFileAction();
if (spec && spec.action) {
event.preventDefault();
spec.action(filename, {
$file: $tr,
fileList: this,
fileActions: this.fileActions,
dir: $tr.attr('data-path') || this.getCurrentDirectory()
});
}
// deselect row
$(event.target).closest('a').blur();
}
} else {
// Even if there is no Details action the default event
// handler is prevented for consistency (although there
// should always be a Details action); otherwise the link
// would be downloaded by the browser when the user expected
// the details to be shown.
event.preventDefault();
var filename = $tr.attr('data-file');
this.fileActions.currentFile = $tr.find('td');
var mime = this.fileActions.getCurrentMimeType();
var type = this.fileActions.getCurrentType();
var permissions = this.fileActions.getCurrentPermissions();
var action = this.fileActions.get(mime, type, permissions, filename)['Details'];
if (action) {
action(filename, {
$file: $tr,
fileList: this,
fileActions: this.fileActions,
dir: $tr.attr('data-path') || this.getCurrentDirectory()
});
}
}
}
},
/**
* Event handler for when clicking on a file's checkbox
*/
_onClickFileCheckbox: function(e) {
var $tr = $(e.target).closest('tr');
if(this._getCurrentSelectionMode() === 'range') {
this._selectRange($tr);
} else {
this._selectSingle($tr);
}
this._lastChecked = $tr;
this.updateSelectionSummary();
if (this._detailsView && !this._detailsView.$el.hasClass('disappear')) {
// hide sidebar
this._updateDetailsView(null);
}
},
/**
* Event handler for when selecting/deselecting all files
*/
_onClickSelectAll: function(e) {
var hiddenFiles = this.$fileList.find('tr.hidden');
var checked = e.target.checked;
if (hiddenFiles.length > 0) {
// set indeterminate alongside checked
e.target.indeterminate = checked;
} else {
e.target.indeterminate = false
}
// Select only visible checkboxes to filter out unmatched file in search
this.$fileList.find('td.selection > .selectCheckBox:visible').prop('checked', checked)
.closest('tr').toggleClass('selected', checked);
// For prevents the selection of encrypted folders when clicking on the "Select all" checkbox
this.$fileList.find('tr[data-e2eencrypted="true"]').find('td.selection > .selectCheckBox:visible').prop('checked', false).closest('tr').toggleClass('selected', false);
if (checked) {
for (var i = 0; i < this.files.length; i++) {
// a search will automatically hide the unwanted rows
// let's only select the matches
var fileData = this.files[i];
var fileRow = this.$fileList.find('tr[data-id=' + fileData.id + ']');
// do not select already selected ones
if (!fileRow.hasClass('hidden') && _.isUndefined(this._selectedFiles[fileData.id]) && (!fileData.isEncrypted)) {
this._selectedFiles[fileData.id] = fileData;
this._selectionSummary.add(fileData);
}
}
} else {