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
|
/**
* @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com>
*
* @author John Molakvoæ <skjnldsv@protonmail.com>
*
* @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 type NavigationService from '../services/Navigation.ts'
import type { Navigation } from '../services/Navigation.ts'
import { getLanguage, translate as t } from '@nextcloud/l10n'
import StarSvg from '@mdi/svg/svg/star.svg?raw'
import FolderSvg from '@mdi/svg/svg/folder.svg?raw'
import { getContents } from '../services/Favorites.ts'
import { loadState } from '@nextcloud/initial-state'
import { basename } from 'path'
import { hashCode } from '../utils/hashUtils'
import { subscribe } from '@nextcloud/event-bus'
import { Node, FileType } from '@nextcloud/files'
import logger from '../logger'
const generateFolderView = function(folder: string, index = 0): Navigation {
return {
id: generateIdFromPath(folder),
name: basename(folder),
icon: FolderSvg,
order: index,
params: {
dir: folder,
view: 'favorites',
},
parent: 'favorites',
columns: [],
getContents,
} as Navigation
}
const generateIdFromPath = function(path: string): string {
return `favorite-${hashCode(path)}`
}
const favoriteFolders = loadState('files', 'favoriteFolders', []) as string[]
const favoriteFoldersViews = favoriteFolders.map((folder, index) => generateFolderView(folder, index))
export default () => {
const Navigation = window.OCP.Files.Navigation as NavigationService
Navigation.register({
id: 'favorites',
name: t('files', 'Favorites'),
caption: t('files', 'List of favorites files and folders.'),
icon: StarSvg,
order: 5,
columns: [],
getContents,
} as Navigation)
favoriteFoldersViews.forEach(view => Navigation.register(view))
/**
* Update favourites navigation when a new folder is added
*/
subscribe('files:favorites:added', (node: Node) => {
if (node.type !== FileType.Folder) {
return
}
// Sanity check
if (node.path === null || !node.root?.startsWith('/files')) {
logger.error('Favorite folder is not within user files root', { node })
return
}
addPathToFavorites(node.path)
})
/**
* Remove favourites navigation when a folder is removed
*/
subscribe('files:favorites:removed', (node: Node) => {
if (node.type !== FileType.Folder) {
return
}
// Sanity check
if (node.path === null || !node.root?.startsWith('/files')) {
logger.error('Favorite folder is not within user files root', { node })
return
}
removePathFromFavorites(node.path)
})
/**
* Sort the favorites paths array and
* update the order property of the existing views
*/
const updateAndSortViews = function() {
favoriteFolders.sort((a, b) => a.localeCompare(b, getLanguage(), { ignorePunctuation: true }))
favoriteFolders.forEach((folder, index) => {
const view = favoriteFoldersViews.find(view => view.id === generateIdFromPath(folder))
if (view) {
view.order = index
}
})
}
// Add a folder to the favorites paths array and update the views
const addPathToFavorites = function(path: string) {
const view = generateFolderView(path)
// Update arrays
favoriteFolders.push(path)
favoriteFoldersViews.push(view)
// Update and sort views
updateAndSortViews()
Navigation.register(view)
}
// Remove a folder from the favorites paths array and update the views
const removePathFromFavorites = function(path: string) {
const id = generateIdFromPath(path)
const index = favoriteFolders.findIndex(f => f === path)
// Update arrays
favoriteFolders.splice(index, 1)
favoriteFoldersViews.splice(index, 1)
Navigation.remove(id)
updateAndSortViews()
}
}
|