-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathview.ts
More file actions
285 lines (226 loc) · 6.27 KB
/
view.ts
File metadata and controls
285 lines (226 loc) · 6.27 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import type { Folder } from '../node/folder.ts'
import type { Node } from '../node/node.ts'
import { emit } from '@nextcloud/event-bus'
import isSvg from 'is-svg'
import { Column } from './column.ts'
export type ContentsWithRoot = {
folder: Folder,
contents: Node[]
}
interface ViewData {
/** Unique view ID */
id: string
/** Translated view name */
name: string
/** Translated accessible description of the view */
caption?: string
/** Translated title of the empty view */
emptyTitle?: string
/** Translated description of the empty view */
emptyCaption?: string
/**
* Custom implementation of the empty view.
* If set and no content is found for the current view,
* then this method is called with the container element
* where to render your empty view implementation.
*
* @param div - The container element to render into
*/
emptyView?: (div: HTMLDivElement) => void
/**
* Method return the content of the provided path
* This ideally should be a cancellable promise.
* promise.cancel(reason) will be called when the directory
* change and the promise is not resolved yet.
* You _must_ also return the current directory
* information alongside with its content.
*/
getContents: (path: string) => Promise<ContentsWithRoot>
/**
* If set then the view will be hidden from the navigation unless its the active view.
*/
hidden?: true
/** The view icon as an inline svg */
icon: string
/**
* The view order.
* If not set will be natural sorted by view name.
*/
order?: number
/**
* Custom params to give to the router on click
* If defined, will be treated as a dummy view and
* will just redirect and not fetch any contents.
*/
params?: Record<string, string>
/**
* This view column(s). Name and actions are
* by default always included
*/
columns?: Column[]
/** The parent unique ID */
parent?: string
/** This view is sticky (sent at the bottom) */
sticky?: boolean
/**
* This view has children and is expanded (by default)
* or not. This will be overridden by user config.
*/
expanded?: boolean
/**
* Will be used as default if the user
* haven't customized their sorting column
*/
defaultSortKey?: string
/**
* Method called to load child views if any
*/
// eslint-disable-next-line no-use-before-define
loadChildViews?: (view: View) => Promise<void>
}
export class View implements ViewData {
private _view: ViewData
constructor(view: ViewData) {
isValidView(view)
this._view = view
}
get id() {
return this._view.id
}
get name() {
return this._view.name
}
get caption() {
return this._view.caption
}
get emptyTitle() {
return this._view.emptyTitle
}
get emptyCaption() {
return this._view.emptyCaption
}
get getContents() {
return this._view.getContents
}
get hidden() {
return this._view.hidden
}
get icon() {
return this._view.icon
}
set icon(icon) {
this._view.icon = icon
}
get order() {
return this._view.order
}
set order(order) {
this._view.order = order
}
get params() {
return this._view.params
}
set params(params) {
this._view.params = params
}
get columns() {
return this._view.columns
}
get emptyView() {
return this._view.emptyView
}
get parent() {
return this._view.parent
}
get sticky() {
return this._view.sticky
}
get expanded() {
return this._view.expanded
}
set expanded(expanded: boolean | undefined) {
this._view.expanded = expanded
}
get defaultSortKey() {
return this._view.defaultSortKey
}
get loadChildViews() {
return this._view.loadChildViews
}
/**
* Allows to update the view data.
* This will throw an error if the view is not valid.
* Warning: the view ID is immutable and cannot be changed after creation.
* @param {Partial<ViewData>} view the view data to update
*/
update(view: Partial<ViewData>) {
if (view.id && view.id !== this._view.id) {
throw new Error('The view ID is immutable and cannot be changed after creation')
}
isValidView({ ...this._view, ...view })
Object.assign(this._view, view)
emit('files:view:updated', this)
}
}
/**
* Typescript cannot validate an interface.
* Please keep in sync with the View interface requirements.
*
* @param {ViewData} view the view to check
* @return {boolean} true if the column is valid
* @throws {Error} if the view is not valid
*/
const isValidView = function(view: ViewData): boolean {
if (!view.id || typeof view.id !== 'string') {
throw new Error('View id is required and must be a string')
}
if (!view.name || typeof view.name !== 'string') {
throw new Error('View name is required and must be a string')
}
if ('caption' in view && typeof view.caption !== 'string') {
throw new Error('View caption must be a string')
}
if (!view.getContents || typeof view.getContents !== 'function') {
throw new Error('View getContents is required and must be a function')
}
if ('hidden' in view && typeof view.hidden !== 'boolean') {
throw new Error('View hidden must be a boolean')
}
if (!view.icon || typeof view.icon !== 'string' || !isSvg(view.icon)) {
throw new Error('View icon is required and must be a valid svg string')
}
if ('order' in view && typeof view.order !== 'number') {
throw new Error('View order must be a number')
}
// Optional properties
if (view.columns) {
view.columns.forEach((column) => {
if (!(column instanceof Column)) {
throw new Error('View columns must be an array of Column. Invalid column found')
}
})
}
if (view.emptyView && typeof view.emptyView !== 'function') {
throw new Error('View emptyView must be a function')
}
if (view.parent && typeof view.parent !== 'string') {
throw new Error('View parent must be a string')
}
if ('sticky' in view && typeof view.sticky !== 'boolean') {
throw new Error('View sticky must be a boolean')
}
if ('expanded' in view && typeof view.expanded !== 'boolean') {
throw new Error('View expanded must be a boolean')
}
if (view.defaultSortKey && typeof view.defaultSortKey !== 'string') {
throw new Error('View defaultSortKey must be a string')
}
if (view.loadChildViews && typeof view.loadChildViews !== 'function') {
throw new Error('View loadChildViews must be a function')
}
return true
}