Skip to content

Commit

Permalink
feat: preserve sort order
Browse files Browse the repository at this point in the history
Signed-off-by: Hamza Mahjoubi <[email protected]>
  • Loading branch information
hamza221 committed Apr 18, 2024
1 parent 1e8f2e2 commit 9b538bd
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 9 deletions.
4 changes: 2 additions & 2 deletions js/viewer-main.js

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions js/viewer-main.js.LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,28 @@
*
*/

/**
* @copyright Copyright (c) 2023 Hamza Mahjoubi <[email protected]>
*
* @author Hamza Mahjoubi <[email protected]>
*
* @license AGPL-3.0-or-later
*
* This program 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 <http://www.gnu.org/licenses/>.
*
*/

/**
* @copyright Copyright (c) 2023 John Molakvoæ <[email protected]>
*
Expand Down
2 changes: 1 addition & 1 deletion js/viewer-main.js.map

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

57 changes: 57 additions & 0 deletions src/services/FileSortingConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* @copyright Copyright (c) 2023 Hamza Mahjoubi <[email protected]>
*
* @author Hamza Mahjoubi <[email protected]>
*
* @license AGPL-3.0-or-later
*
* This program 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 <http://www.gnu.org/licenses/>.
*
*/

import axios from '@nextcloud/axios'
import { isPublic } from '../utils/davUtils'

/**
* @return {object}
*/
export default async function getSortingConfig() {
const viewConfigs = await getViewConfigs()

if (!viewConfigs) {
return { key: 'basename', asc: true }
}

const keyMap = { mtime: 'lastmod' }
const key = keyMap[viewConfigs.sorting_mode] || viewConfigs.sorting_mode || 'basename'
const asc = viewConfigs.sorting_direction === 'asc' || !viewConfigs.sorting_direction

return { key, asc }
}

/**
* @return {object}
*/
async function getViewConfigs() {
if (isPublic()) {
return null
}
return await axios.get('/apps/files/api/v1/views')
.then((response) => {
return response.data.data?.files
})
.catch(() => {
return null
})
}
9 changes: 6 additions & 3 deletions src/views/Viewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ import axios from '@nextcloud/axios'
import { showError } from '@nextcloud/dialogs'
import { emit, subscribe, unsubscribe } from '@nextcloud/event-bus'
import { registerFileAction, FileAction, Permission, DefaultType } from '@nextcloud/files'
import getSortingConfig from '../services/FileSortingConfig.ts'
import isFullscreen from '@nextcloud/vue/dist/Mixins/isFullscreen.js'
import isMobile from '@nextcloud/vue/dist/Mixins/isMobile.js'
Expand Down Expand Up @@ -269,6 +270,7 @@ export default {
comparisonFile: null,
nextFile: {},
fileList: [],
sortingConfig: null,
// States
isLoaded: false,
Expand Down Expand Up @@ -580,13 +582,14 @@ export default {
},
methods: {
beforeOpen() {
async beforeOpen() {
// initial loading start
this.initiated = true
if (OCA?.Files?.Sidebar?.setFullScreenMode) {
OCA.Files.Sidebar.setFullScreenMode(true)
}
this.sortingConfig = await getSortingConfig()
},
/**
Expand All @@ -596,7 +599,7 @@ export default {
* @param {string|null} overrideHandlerId the ID of the handler with which to view the files, if any
*/
async openFile(path, overrideHandlerId = null) {
this.beforeOpen()
await this.beforeOpen()
// cancel any previous request
this.cancelRequestFile()
Expand Down Expand Up @@ -709,7 +712,7 @@ export default {
// sort like the files list
// TODO: implement global sorting API
// https://github.com/nextcloud/server/blob/a83b79c5f8ab20ed9b4d751167417a65fa3c42b8/apps/files/lib/Controller/ApiController.php#L247
this.fileList = filteredFiles.sort((a, b) => sortCompare(a, b, 'basename'))
this.fileList = filteredFiles.sort((a, b) => sortCompare(a, b, this.sortingConfig.key, this.sortingConfig.asc))
// store current position
this.currentIndex = this.fileList.findIndex(file => file.filename === fileInfo.filename)
Expand Down

0 comments on commit 9b538bd

Please sign in to comment.