Skip to content

Commit d939be4

Browse files
PVince81nextcloud-command
authored andcommitted
Fix grid view toggle
Move grid view logic to the files app itself because the button does not belong to any specific file list. Then inject the grid view state when switching file lists. Signed-off-by: Vincent Petry <vincent@nextcloud.com> Signed-off-by: nextcloud-command <nextcloud-command@users.noreply.github.com>
1 parent a09077c commit d939be4

2 files changed

Lines changed: 44 additions & 29 deletions

File tree

apps/files/js/app.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@
6161
var cropImagePreviews = $('#cropImagePreviews').val() === "1";
6262
this.$cropImagePreviews.prop('checked', cropImagePreviews);
6363

64+
// Toggle for grid view
65+
this.$showGridView = $('input#showgridview');
66+
this.$showGridView.on('change', _.bind(this._onGridviewChange, this));
67+
$('#view-toggle').tooltip({placement: 'bottom', trigger: 'hover'});
68+
6469
if ($('#fileNotFound').val() === "1") {
6570
OC.Notification.show(t('files', 'File could not be found'), {type: 'error'});
6671
}
@@ -190,7 +195,16 @@
190195
* @param {OCA.Files.FileList} newFileList -
191196
*/
192197
updateCurrentFileList: function(newFileList) {
198+
if (this.currentFileList === newFileList) {
199+
return
200+
}
201+
193202
this.currentFileList = newFileList;
203+
if (this.currentFileList !== null) {
204+
// update grid view to the current value
205+
const isGridView = this.$showGridView.is(':checked');
206+
this.currentFileList.setGridView(isGridView);
207+
}
194208
},
195209

196210
/**
@@ -394,7 +408,34 @@
394408
} else {
395409
OC.Util.History.pushState(this._makeUrlParams(params));
396410
}
397-
}
411+
},
412+
413+
/**
414+
* Toggle showing gridview by default or not
415+
*
416+
* @returns {undefined}
417+
*/
418+
_onGridviewChange: function() {
419+
const isGridView = this.$showGridView.is(':checked');
420+
// only save state if user is logged in
421+
if (OC.currentUser) {
422+
$.post(OC.generateUrl('/apps/files/api/v1/showgridview'), {
423+
show: isGridView,
424+
});
425+
}
426+
this.$showGridView.next('#view-toggle')
427+
.removeClass('icon-toggle-filelist icon-toggle-pictures')
428+
.addClass(isGridView ? 'icon-toggle-filelist' : 'icon-toggle-pictures')
429+
this.$showGridView.next('#view-toggle').attr(
430+
'data-original-title',
431+
isGridView ? t('files', 'Show list view') : t('files', 'Show grid view'),
432+
)
433+
434+
if (this.currentFileList) {
435+
this.currentFileList.setGridView(isGridView);
436+
}
437+
},
438+
398439
};
399440
})();
400441

apps/files/js/filelist.js

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
* @return {number} page size
106106
*/
107107
pageSize: function() {
108-
var isGridView = this.$showGridView.is(':checked');
108+
var isGridView = this.$table.hasClass('view-grid');
109109
var columns = 1;
110110
var rows = Math.ceil(this.$container.height() / 50);
111111
if (isGridView) {
@@ -368,12 +368,6 @@
368368

369369
this.$el.find('thead th .columntitle').click(_.bind(this._onClickHeader, this));
370370

371-
// Toggle for grid view, only register once
372-
this.$showGridView = $('input#showgridview:not(.registered)');
373-
this.$showGridView.on('change', _.bind(this._onGridviewChange, this));
374-
this.$showGridView.addClass('registered');
375-
$('#view-toggle').tooltip({placement: 'bottom', trigger: 'hover'});
376-
377371
this._onResize = _.debounce(_.bind(this._onResize, this), 250);
378372
$('#app-content').on('appresized', this._onResize);
379373
$(window).resize(this._onResize);
@@ -747,27 +741,7 @@
747741
this.breadcrumb._resize();
748742
},
749743

750-
/**
751-
* Toggle showing gridview by default or not
752-
*
753-
* @returns {undefined}
754-
*/
755-
_onGridviewChange: function() {
756-
const isGridView = this.$showGridView.is(':checked');
757-
// only save state if user is logged in
758-
if (OC.currentUser) {
759-
$.post(OC.generateUrl('/apps/files/api/v1/showgridview'), {
760-
show: isGridView,
761-
});
762-
}
763-
this.$showGridView.next('#view-toggle')
764-
.removeClass('icon-toggle-filelist icon-toggle-pictures')
765-
.addClass(isGridView ? 'icon-toggle-filelist' : 'icon-toggle-pictures')
766-
this.$showGridView.next('#view-toggle').attr(
767-
'data-original-title',
768-
isGridView ? t('files', 'Show list view') : t('files', 'Show grid view'),
769-
)
770-
744+
setGridView: function(isGridView) {
771745
this.$table.toggleClass('view-grid', isGridView);
772746
if (isGridView) {
773747
// If switching into grid view from list view, too few files might be displayed

0 commit comments

Comments
 (0)