Skip to content

Commit a2853c7

Browse files
committed
Preserve sort order
Signed-off-by: hamza mahjoubi <hamzamahjoubi221@gmail.com>
1 parent 8f6cb61 commit a2853c7

5 files changed

Lines changed: 88 additions & 6 deletions

File tree

js/viewer-main.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/viewer-main.js.LICENSE.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,28 @@
266266
*
267267
*/
268268

269+
/**
270+
* @copyright Copyright (c) 2023 Hamza Mahjoubi <hamza.mahjoubi221@proton.me>
271+
*
272+
* @author Hamza Mahjoubi <hamza.mahjoubi221@proton.me>
273+
*
274+
* @license AGPL-3.0-or-later
275+
*
276+
* This program is free software: you can redistribute it and/or modify
277+
* it under the terms of the GNU Affero General Public License as
278+
* published by the Free Software Foundation, either version 3 of the
279+
* License, or (at your option) any later version.
280+
*
281+
* This program is distributed in the hope that it will be useful,
282+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
283+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
284+
* GNU Affero General Public License for more details.
285+
*
286+
* You should have received a copy of the GNU Affero General Public License
287+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
288+
*
289+
*/
290+
269291
/**
270292
* @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com>
271293
*

js/viewer-main.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/services/FileSortingConfig.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* @copyright Copyright (c) 2023 Hamza Mahjoubi <hamza.mahjoubi221@proton.me>
3+
*
4+
* @author Hamza Mahjoubi <hamza.mahjoubi221@proton.me>
5+
*
6+
* @license AGPL-3.0-or-later
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as
10+
* published by the Free Software Foundation, either version 3 of the
11+
* License, or (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
23+
import axios from '@nextcloud/axios'
24+
import { isPublic } from '../utils/davUtils'
25+
26+
/**
27+
* @return {object}
28+
*/
29+
export default async function getSortingConfig() {
30+
const viewConfigs = await getViewConfigs()
31+
32+
if (!viewConfigs) {
33+
return { key: 'basename', asc: true }
34+
}
35+
36+
const keyMap = { mtime: 'lastmod' }
37+
const key = keyMap[viewConfigs.sorting_mode] || viewConfigs.sorting_mode || 'basename'
38+
const asc = viewConfigs.sorting_direction === 'asc' || !viewConfigs.sorting_direction
39+
40+
return { key, asc }
41+
}
42+
43+
/**
44+
* @return {object}
45+
*/
46+
async function getViewConfigs() {
47+
if (isPublic()) {
48+
return null
49+
}
50+
return await axios.get('/apps/files/api/v1/views')
51+
.then((response) => {
52+
return response.data.data?.files
53+
})
54+
.catch(() => {
55+
return null
56+
})
57+
}

src/views/Viewer.vue

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ import axios from '@nextcloud/axios'
184184
import { showError } from '@nextcloud/dialogs'
185185
import { emit, subscribe, unsubscribe } from '@nextcloud/event-bus'
186186
import { registerFileAction, FileAction, Permission, DefaultType } from '@nextcloud/files'
187+
import getSortingConfig from '../services/FileSortingConfig.ts'
187188
188189
import isFullscreen from '@nextcloud/vue/dist/Mixins/isFullscreen.js'
189190
import isMobile from '@nextcloud/vue/dist/Mixins/isMobile.js'
@@ -259,6 +260,7 @@ export default {
259260
comparisonFile: null,
260261
nextFile: {},
261262
fileList: [],
263+
sortingConfig: null,
262264
263265
// States
264266
isLoaded: false,
@@ -570,13 +572,14 @@ export default {
570572
},
571573
572574
methods: {
573-
beforeOpen() {
575+
async beforeOpen() {
574576
// initial loading start
575577
this.initiated = true
576578
577579
if (OCA?.Files?.Sidebar?.setFullScreenMode) {
578580
OCA.Files.Sidebar.setFullScreenMode(true)
579581
}
582+
this.sortingConfig = await getSortingConfig()
580583
},
581584
582585
/**
@@ -586,7 +589,7 @@ export default {
586589
* @param {string|null} overrideHandlerId the ID of the handler with which to view the files, if any
587590
*/
588591
async openFile(path, overrideHandlerId = null) {
589-
this.beforeOpen()
592+
await this.beforeOpen()
590593
591594
// cancel any previous request
592595
this.cancelRequestFile()
@@ -699,7 +702,7 @@ export default {
699702
// sort like the files list
700703
// TODO: implement global sorting API
701704
// https://github.com/nextcloud/server/blob/a83b79c5f8ab20ed9b4d751167417a65fa3c42b8/apps/files/lib/Controller/ApiController.php#L247
702-
this.fileList = filteredFiles.sort((a, b) => sortCompare(a, b, 'basename'))
705+
this.fileList = filteredFiles.sort((a, b) => sortCompare(a, b, this.sortingConfig.key, this.sortingConfig.asc))
703706
704707
// store current position
705708
this.currentIndex = this.fileList.findIndex(file => file.basename === fileInfo.basename)

0 commit comments

Comments
 (0)