-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathfilelist.js
3461 lines (3098 loc) · 100 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() {
var TEMPLATE_ADDBUTTON = '<a href="#" class="button new">' +
'<span class="icon {{iconClass}}"></span>' +
'<span class="hidden-visually">{{addText}}</span>' +
'</a>';
/**
* @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 #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,
/**
* @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,
/**
* Last clicked row
*/
$currentRow: null,
/**
* Number of files per page
*
* @return {int} page size
*/
pageSize: function() {
return Math.ceil(this.$container.height() / 50);
},
/**
* 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,
/**
* File actions handler, defaults to OCA.Files.FileActions
* @type OCA.Files.FileActions
*/
fileActions: 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 Backbone.Model
*/
_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,
/**
* Stores shareTree items and infos
*
* @type Array
*/
_shareTreeCache: {},
/**
* 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,
/**
* 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 #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 {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.config) {
this._filesConfig = options.config;
} else if (!_.isUndefined(OCA.Files) && !_.isUndefined(OCA.Files.App)) {
this._filesConfig = OCA.Files.App.getFilesConfig();
} else {
this._filesConfig = new OC.Backbone.Model({
'showhidden': false
});
}
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 || $(window);
this.$table = $el.find('table:first');
this.$fileList = $el.find('#fileList');
if (!_.isUndefined(this._filesConfig)) {
this._filesConfig.on('change:showhidden', function() {
var showHidden = this.get('showhidden');
self.$el.toggleClass('hide-hidden-files', !showHidden);
self.updateSelectionSummary();
if (!showHidden) {
// hiding files could make the page too small, need to try rendering next page
self._onScroll();
}
});
this.$el.toggleClass('hide-hidden-files', !this._filesConfig.get('showhidden'));
}
if (_.isUndefined(options.detailsViewEnabled) || options.detailsViewEnabled) {
this._detailsView = new OCA.Files.DetailsView();
this._detailsView.$el.insertBefore(this.$el);
this._detailsView.$el.addClass('disappear');
}
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.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);
},
rootName: this.appName === t('files', 'Files') ? t('files', 'All files') : this.appName,
rootIconClass: 'nav-icon-' + this.id,
};
// 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('#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), 100);
$('#app-content').on('appresized', this._onResize);
$(window).resize(this._onResize);
this.$el.on('show', this._onResize);
this.updateSearch();
this.$fileList.on('click','td.filename>a.name, td.filesize, td.date', _.bind(this._onClickFile, this));
this.$fileList.on('change', 'td.filename>.selectCheckBox', _.bind(this._onClickFileCheckbox, this));
// use namespaced event because "bind" will get in the way to remove the event later
// note that the jquery element won't be removed, so it's possible to register
// the same listener twice or more. We'll remove the listener in the destroy method.
this.$el.on('urlChanged.filelistbound', _.bind(this._onUrlChanged, this));
this.$el.find('.select-all').click(_.bind(this._onClickSelectAll, this));
this.$el.find('.download').click(_.bind(this._onClickDownloadSelected, this));
this.$el.find('.delete-selected').click(_.bind(this._onClickDeleteSelected, this));
this.$el.find('.selectedActions a').tooltip({placement:'top'});
// namespaced event based on this jquery element, to be removed when is destroyed
// note that the listener is attached to the container, not to the element
this.$container.on('scroll.' + this.$el.attr('id'), _.bind(this._onScroll, this));
if (options.scrollTo) {
this.$fileList.one('updated', function() {
self.scrollTo(options.scrollTo, options.detailTabId);
});
}
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, {
fileList: this,
filesClient: this.filesClient,
dropZone: $('#content'),
maxChunkSize: options.maxChunkSize,
uploadStallTimeout: options.uploadStallTimeout,
uploadStallRetries: options.uploadStallRetries
});
this.setupUploadEvents(this._uploader);
}
}
OC.Plugins.attach('OCA.Files.FileList', this);
},
/**
* 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);
// HACK: this will make reload work when reused
this.$el.find('#dir').val('');
// remove summary
this.$el.find('tfoot tr.summary').remove();
// detach click event handler, this ensures that older _onClickFile events won't be triggered
this.$fileList.off('click');
this.$fileList.empty();
// remove events attached to the $el
this.$el.off('show', this._onResize);
this.$el.off('urlChanged.filelistbound');
// remove events attached to the $container
this.$container.off('scroll.' + this.$el.attr('id'));
},
/**
* 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_READ,
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
// also take the file id into account if possible because
// the file name may not be unique
if (this._currentFileModel &&
this._currentFileModel.get('name') === fileName &&
(!this.$currentRow || this.$currentRow.data('id') === this._currentFileModel.id)
) {
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} fileName file name for which to show details
* @param {string} [tabId] optional tab id to select
*/
showDetailsView: function(fileName, tabId) {
if (!this._detailsView) {
return;
}
this._updateDetailsView(fileName, true);
if (tabId) {
this._detailsView.selectTab(tabId);
}
},
/**
* Update the details view to display the given file
*
* @param {string} fileName file name from the current list
* @param {boolean} [show=true] whether to open the sidebar if it was closed
*/
_updateDetailsView: function(fileName, show) {
if (!this._detailsView) {
return;
}
// show defaults to true
show = _.isUndefined(show) || !!show;
var oldFileInfo = this._currentFileModel;
if (oldFileInfo) {
// TODO: use more efficient way, maybe track the highlight
this.$fileList.children().filterAttr('data-id', '' + oldFileInfo.get('id')).removeClass('highlighted');
oldFileInfo.off('change', this._onSelectedModelChanged, this);
}
if (!fileName) {
this._detailsView.setFileInfo(null);
if (this._currentFileModel) {
this._currentFileModel.off();
}
this._currentFileModel = null;
OC.Apps.hideAppSidebar(this._detailsView.$el);
return;
}
if (show && this._detailsView.$el.hasClass('disappear')) {
OC.Apps.showAppSidebar(this._detailsView.$el);
} else if (show === false) {
OC.Apps.hideAppSidebar(this._detailsView.$el);
}
var $tr = this.findFileEl(fileName);
var model = this.getModelForFile($tr);
this._currentFileModel = model;
$tr.addClass('highlighted');
if (show === false) {
this._detailsView.setFileInfo(null);
} else {
this._detailsView.setFileInfo(model);
this._detailsView.$el.scrollTop(0);
}
},
/**
* Event handler for when the window size changed
*/
_onResize: function() {
var containerWidth = this.$el.width();
var actionsWidth = 0;
$.each(this.$el.find('#controls .actions'), function(index, action) {
actionsWidth += $(action).outerWidth();
});
// subtract app navigation toggle when visible
containerWidth -= $('#app-navigation-toggle').width();
this.breadcrumb.setMaxWidth(containerWidth - actionsWidth - 10);
this.$table.find('>thead').width($('#app-content').width() - OC.Util.getScrollBarWidth());
},
/**
* 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 (!e.force) {
if( (this._currentDirectory || this.$el.find('#dir').val()) && currentDir === e.dir) {
return;
}
}
this.changeDirectory(e.dir, false, true);
}
},
/**
* Selected/deselects the given file element and updated
* the internal selection cache.
*
* @param {Object} $tr single file row element
* @param {bool} state true to select, false to deselect
*/
_selectFileEl: function($tr, state, showDetailsView) {
var $checkbox = $tr.find('td.filename>.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);
},
/**
* Event handler for when clicking on files to select them
*/
_onClickFile: function(event) {
this._setCurrentRow($(event.currentTarget).closest('tr'));
var $link = $(event.target).closest('a');
if ($link.attr('href') === '#' || $link.hasClass('disable-click')) {
event.preventDefault();
return;
}
var $tr = $(event.target).closest('tr');
if ($tr.hasClass('dragging')) {
return;
}
if (this._allowSelection && (event.ctrlKey || event.shiftKey)) {
event.preventDefault();
if (event.shiftKey) {
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 + 1; i < currentIndex; i++) {
this._selectFileEl($rows.eq(i), true);
}
}
else {
this._lastChecked = $tr;
}
var $checkbox = $tr.find('td.filename>.selectCheckBox');
this._selectFileEl($tr, !$checkbox.prop('checked'));
this.updateSelectionSummary();
} else {
// clicked directly on the name
if (!this._detailsView || $(event.target).is('.nametext') || $(event.target).closest('.nametext').length) {
var filename = $tr.attr('data-file');
var renaming = $tr.data('renaming');
if (!renaming) {
this.fileActions.currentFile = $tr.find('td');
var mime = this.fileActions.getCurrentMimeType();
var type = this.fileActions.getCurrentType();
var permissions = this.fileActions.getCurrentPermissions();
var defaultActions = this.fileActions.getDefaultFileActions(mime,type, permissions);
var context = {
$file: $tr,
fileList: this,
fileActions: this.fileActions,
dir: $tr.attr('data-path') || this.getCurrentDirectory()
};
// don't show app drawer for directories as we want to open them per default
if (defaultActions.length > 1 && type !== 'dir') {
var appSelectMenu = new OCA.Files.FileActionsApplicationSelectMenu();
appSelectMenu.show(context, $tr.find('td.filename'));
event.preventDefault();
return;
}
var action = this.fileActions.getDefault(mime,type, permissions);
if (action) {
event.preventDefault();
// also set on global object for legacy apps
window.FileActions.currentFile = this.fileActions.currentFile;
action(filename, context);
}
// deselect row
$(event.target).closest('a').blur();
}
} else {
this._updateDetailsView($tr.attr('data-file'));
event.preventDefault();
}
}
},
/**
* Event handler for when clicking on a file's checkbox
*/
_onClickFileCheckbox: function(e) {
var $tr = $(e.target).closest('tr');
var state = !$tr.hasClass('selected');
this._selectFileEl($tr, state);
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
* modifiedFiles if not passed then this.files would be used.
* modifiedFiles was introduced, so that if we have to alter
* any fields of this.files, its better to use modifiedFiles
* instead of directly making change in this.files.
*/
_onClickSelectAll: function(e, modifiedFiles) {
var filesViewed = modifiedFiles ? modifiedFiles : this.files;
var checked = $(e.target).prop('checked');
this.$fileList.find('td.filename>.selectCheckBox').prop('checked', checked)
.closest('tr').toggleClass('selected', checked);
this._selectedFiles = {};
this._selectionSummary.clear();
if (checked) {
for (var i = 0; i < filesViewed.length; i++) {
var fileData = filesViewed[i];
this._selectedFiles[fileData.id] = fileData;
this._selectionSummary.add(fileData);
}
}
this.updateSelectionSummary();
if (this._detailsView && !this._detailsView.$el.hasClass('disappear')) {
// hide sidebar
this._updateDetailsView(null);
}
},
/**
* Event handler for when clicking on "Download" for the selected files
*/
_onClickDownloadSelected: function(event) {
var files;
var dir = this.getCurrentDirectory();
if (this.isAllSelected() && this.getSelectedFiles().length > 1) {
files = OC.basename(dir);
dir = OC.dirname(dir) || '/';
}
else {
files = _.pluck(this.getSelectedFiles(), 'name');
}
var downloadFileaction = $('#selectedActionsList').find('.download');
// don't allow a second click on the download action
if(downloadFileaction.hasClass('disabled')) {
event.preventDefault();
return;
}
var disableLoadingState = function(){
OCA.Files.FileActions.updateFileActionSpinner(downloadFileaction, false);
};
OCA.Files.FileActions.updateFileActionSpinner(downloadFileaction, true);
if(this.getSelectedFiles().length > 1) {
OCA.Files.Files.handleDownload(this.getDownloadUrl(files, dir, true), disableLoadingState);
}
else {
first = this.getSelectedFiles()[0];
OCA.Files.Files.handleDownload(this.getDownloadUrl(first.name, dir, true), disableLoadingState);
}
return false;
},
/**
* Event handler for when clicking on "Delete" for the selected files
*/
_onClickDeleteSelected: function(event) {
var files = null;
if (!this.isAllSelected()) {
files = _.pluck(this.getSelectedFiles(), 'name');
}
this.do_delete(files);
event.preventDefault();
return false;
},
/**
* Event handler when clicking on a table header
*/
_onClickHeader: function(e) {
if (this.$table.hasClass('multiselect')) {
return;
}
var $target = $(e.target);
var sort;
if (!$target.is('a')) {
$target = $target.closest('a');
}
sort = $target.attr('data-sort');
if (sort) {
if (this._sort === sort) {
this.setSort(sort, (this._sortDirection === 'desc')?'asc':'desc', true, true);
}
else {
if ( sort === 'name' ) { //default sorting of name is opposite to size and mtime
this.setSort(sort, 'asc', true, true);
}
else {
this.setSort(sort, 'desc', true, true);
}
}
}
},
/**
* Event handler when clicking on a bread crumb
*/
_onClickBreadCrumb: function(e) {
var $el = $(e.target).closest('.crumb'),
$targetDir = $el.data('dir');
if ($targetDir !== undefined && e.which === 1) {
e.preventDefault();
this.changeDirectory($targetDir);
this.updateSearch();
}
},
/**
* Event handler for when scrolling the list container.
* This appends/renders the next page of entries when reaching the bottom.
*/
_onScroll: function(e) {
if (this.$container.scrollTop() + this.$container.height() > this.$el.height() - 300) {
this._nextPage(true);
}
},
/**
* Event handler when dropping on a breadcrumb
*/
_onDropOnBreadCrumb: function( event, ui ) {
var self = this;
var $target = $(event.target);
if (!$target.is('.crumb')) {
$target = $target.closest('.crumb');
}
var targetPath = $(event.target).data('dir');
var dir = this.getCurrentDirectory();
while (dir.substr(0,1) === '/') {//remove extra leading /'s
dir = dir.substr(1);
}
dir = '/' + dir;
if (dir.substr(-1,1) !== '/') {
dir = dir + '/';
}
// do nothing if dragged on current dir
if (targetPath === dir || targetPath + '/' === dir) {
return;
}
var files = this.getSelectedFiles();
if (files.length === 0) {
// single one selected without checkbox?
files = _.map(ui.helper.find('tr'), function(el) {
return self.elementToFile($(el));
});
}
this.move(_.pluck(files, 'name'), targetPath);
// re-enable td elements to be droppable
// sometimes the filename drop handler is still called after re-enable,
// it seems that waiting for a short time before re-enabling solves the problem
setTimeout(function() {
self.$el.find('td.filename.ui-droppable').droppable('enable');
}, 10);
},
/**
* Sets a new page title
*/
setPageTitle: function(title){
if (title) {
title += ' - ';
} else {
title = '';
}
title += this.appName;
// Sets the page title with the " - ownCloud" suffix as in templates
window.document.title = title + ' - ' + oc_defaults.title;
return true;
},
/**
* Returns the file info for the given file name from the internal collection.
*
* @param {string} fileName file name
* @return {OCA.Files.FileInfo} file info or null if it was not found
*
* @since 8.2
*/
findFile: function(fileName) {
return _.find(this.files, function(aFile) {
return (aFile.name === fileName);
}) || null;
},
/**
* Returns the tr element for a given file name, but only if it was already rendered.
*
* @param {string} fileName file name
* @return {Object} jQuery object of the matching row
*/
findFileEl: function(fileName){
// use filterAttr to avoid escaping issues
var $fileEl = this.$fileList.find('tr').filterAttr('data-file', fileName);
// take the file id into account if possible because the file name
// may not be unique which results in multiple elements
if (this.$currentRow && $fileEl.length > 1) {
$fileEl = $fileEl.filter('[data-id="' + this.$currentRow.data('id') + '"]');
}
return $fileEl;
},
/**
* Returns the file data from a given file element.
* @param $el file tr element
* @return file data
*/
elementToFile: function($el){
$el = $($el);
var data = {
id: parseInt($el.attr('data-id'), 10),
name: $el.attr('data-file'),
mimetype: $el.attr('data-mime'),
mtime: parseInt($el.attr('data-mtime'), 10),
type: $el.attr('data-type'),
etag: $el.attr('data-etag'),
permissions: parseInt($el.attr('data-permissions'), 10)
};
var size = $el.attr('data-size');
if (size) {
data.size = parseInt(size, 10);
}
var icon = $el.attr('data-icon');
if (icon) {
data.icon = icon;
}
var mountType = $el.attr('data-mounttype');
if (mountType) {
data.mountType = mountType;
}
var path = $el.attr('data-path');
if (path) {
data.path = path;
}
return data;
},
/**
* Appends the next page of files into the table
* @param animate true to animate the new elements
* @return array of DOM elements of the newly added files
*/
_nextPage: function(animate) {
var index = this.$fileList.children().length,