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
|
/**
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import type { Folder, Node } from '@nextcloud/files'
import { subscribe } from '@nextcloud/event-bus'
import { FileType, View, getFavoriteNodes, getNavigation } from '@nextcloud/files'
import { getLanguage, translate as t } from '@nextcloud/l10n'
import { client } from '../services/WebdavClient.ts'
import FolderSvg from '@mdi/svg/svg/folder.svg?raw'
import StarSvg from '@mdi/svg/svg/star.svg?raw'
import { getContents } from '../services/Favorites'
import { hashCode } from '../utils/hashUtils'
import logger from '../logger'
const generateFavoriteFolderView = function(folder: Folder, index = 0): View {
return new View({
id: generateIdFromPath(folder.path),
name: folder.displayname,
icon: FolderSvg,
order: index,
params: {
dir: folder.path,
fileid: String(folder.fileid),
view: 'favorites',
},
parent: 'favorites',
columns: [],
getContents,
})
}
const generateIdFromPath = function(path: string): string {
return `favorite-${hashCode(path)}`
}
export const registerFavoritesView = async () => {
const Navigation = getNavigation()
Navigation.register(new View({
id: 'favorites',
name: t('files', 'Favorites'),
caption: t('files', 'List of favorite files and folders.'),
emptyTitle: t('files', 'No favorites yet'),
emptyCaption: t('files', 'Files and folders you mark as favorite will show up here'),
icon: StarSvg,
order: 15,
columns: [],
getContents,
}))
const favoriteFolders = (await getFavoriteNodes(client)).filter(node => node.type === FileType.Folder) as Folder[]
const favoriteFoldersViews = favoriteFolders.map((folder, index) => generateFavoriteFolderView(folder, index)) as View[]
logger.debug('Generating favorites view', { favoriteFolders })
favoriteFoldersViews.forEach(view => Navigation.register(view))
/**
* Update favorites 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
}
addToFavorites(node as Folder)
})
/**
* Remove favorites 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)
})
/**
* Update favorites navigation when a folder is renamed
*/
subscribe('files:node:renamed', (node: Node) => {
if (node.type !== FileType.Folder) {
return
}
if (node.attributes.favorite !== 1) {
return
}
updateNodeFromFavorites(node as Folder)
})
/**
* Sort the favorites paths array and
* update the order property of the existing views
*/
const updateAndSortViews = function() {
favoriteFolders.sort((a, b) => a.path.localeCompare(b.path, getLanguage(), { ignorePunctuation: true }))
favoriteFolders.forEach((folder, index) => {
const view = favoriteFoldersViews.find((view) => view.id === generateIdFromPath(folder.path))
if (view) {
view.order = index
}
})
}
// Add a folder to the favorites paths array and update the views
const addToFavorites = function(node: Folder) {
const view = generateFavoriteFolderView(node)
// Skip if already exists
if (favoriteFolders.find((folder) => folder.path === node.path)) {
return
}
// Update arrays
favoriteFolders.push(node)
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((folder) => folder.path === path)
// Skip if not exists
if (index === -1) {
return
}
// Update arrays
favoriteFolders.splice(index, 1)
favoriteFoldersViews.splice(index, 1)
// Update and sort views
Navigation.remove(id)
updateAndSortViews()
}
// Update a folder from the favorites paths array and update the views
const updateNodeFromFavorites = function(node: Folder) {
const favoriteFolder = favoriteFolders.find((folder) => folder.fileid === node.fileid)
// Skip if it does not exists
if (favoriteFolder === undefined) {
return
}
removePathFromFavorites(favoriteFolder.path)
addToFavorites(node)
}
}
|