diff --git a/__tests__/newFileMenu.spec.ts b/__tests__/newFileMenu.spec.ts
index 740e96a3..2595873c 100644
--- a/__tests__/newFileMenu.spec.ts
+++ b/__tests__/newFileMenu.spec.ts
@@ -2,6 +2,7 @@ import { describe, expect, test, vi } from 'vitest'
import { NewFileMenu, getNewFileMenu, type Entry } from '../lib/newFileMenu'
import logger from '../lib/utils/logger'
+import { Folder, Permission } from '../lib'
describe('NewFileMenu init', () => {
test('Initializing NewFileMenu', () => {
@@ -244,7 +245,7 @@ describe('NewFileMenu getEntries filter', () => {
displayName: 'Create empty file',
templateName: 'New file',
iconClass: 'icon-file',
- if: fileInfo => fileInfo.permissions.includes('CK'),
+ if: folder => (folder.permissions & Permission.CREATE) !== 0,
handler: () => {},
}
newFileMenu.registerEntry(entry1)
@@ -254,26 +255,18 @@ describe('NewFileMenu getEntries filter', () => {
displayName: 'Create new markdown file',
templateName: 'New text.md',
iconClass: 'icon-filetype-text',
- if: fileInfo => fileInfo.permissions.includes('CK'),
+ if: folder => (folder.permissions & Permission.CREATE) !== 0,
handler: () => {},
}
newFileMenu.registerEntry(entry2)
- const context = {
- basename: 'Folder',
- etag: '63071eedd82fe',
- fileid: '56',
- filename: '/Folder',
- hasPreview: false,
- lastmod: 1661410576,
- mime: 'httpd/unix-directory',
- month: '197001',
- permissions: 'CKGWDR',
- showShared: false,
+ const context = new Folder({
+ id: 56,
+ owner: 'admin',
size: 2610077102,
- timestamp: 1661410,
- type: 'dir',
- }
+ source: 'https://example.com/remote.php/dav/files/admin/Folder',
+ permissions: Permission.ALL,
+ })
const entries = newFileMenu.getEntries(context)
expect(entries).toHaveLength(2)
@@ -288,7 +281,7 @@ describe('NewFileMenu getEntries filter', () => {
displayName: 'Create empty file',
templateName: 'New file',
iconClass: 'icon-file',
- if: fileInfo => fileInfo.permissions.includes('CK'),
+ if: folder => (folder.permissions & Permission.CREATE) !== 0,
handler: () => {},
}
newFileMenu.registerEntry(entry1)
@@ -298,27 +291,18 @@ describe('NewFileMenu getEntries filter', () => {
displayName: 'Create new markdown file',
templateName: 'New text.md',
iconClass: 'icon-filetype-text',
- if: fileInfo => fileInfo.permissions.includes('CK'),
+ if: folder => (folder.permissions & Permission.CREATE) !== 0,
handler: () => {},
}
newFileMenu.registerEntry(entry2)
- const context = {
- basename: 'Shared folder',
- etag: '63071eedd82fe',
- fileid: '56',
- filename: '/Shared folder',
- hasPreview: false,
- lastmod: 1661410576,
- mime: 'httpd/unix-directory',
- month: '197001',
- // Only read and share
- permissions: 'GR',
- showShared: false,
+ const context = new Folder({
+ id: 56,
+ owner: 'admin',
size: 2610077102,
- timestamp: 1661410,
- type: 'dir',
- }
+ source: 'https://example.com/remote.php/dav/files/admin/Folder',
+ permissions: Permission.READ,
+ })
const entries = newFileMenu.getEntries(context)
expect(entries).toHaveLength(0)
@@ -331,7 +315,6 @@ describe('NewFileMenu getEntries filter', () => {
displayName: 'Create template',
templateName: 'New file',
iconClass: 'icon-file',
- // No conditions
handler: () => {},
}
newFileMenu.registerEntry(entry1)
@@ -341,27 +324,19 @@ describe('NewFileMenu getEntries filter', () => {
displayName: 'Create new markdown file',
templateName: 'New text.md',
iconClass: 'icon-filetype-text',
- if: fileInfo => fileInfo.permissions.includes('CK'),
+ if: folder => (folder.permissions & Permission.CREATE) !== 0,
handler: () => {},
}
newFileMenu.registerEntry(entry2)
- const context = {
- basename: 'Shared folder',
- etag: '63071eedd82fe',
- fileid: '56',
- filename: '/Shared folder',
- hasPreview: false,
- lastmod: 1661410576,
- mime: 'httpd/unix-directory',
- month: '197001',
- // Only read and share
- permissions: 'GR',
- showShared: false,
+ const context = new Folder({
+ id: 56,
+ owner: 'admin',
size: 2610077102,
- timestamp: 1661410,
- type: 'dir',
- }
+ source: 'https://example.com/remote.php/dav/files/admin/Foo/Bar',
+ permissions: Permission.NONE,
+ root: '/files/admin',
+ })
const entries = newFileMenu.getEntries(context)
expect(entries).toHaveLength(1)
diff --git a/lib/index.ts b/lib/index.ts
index faa0c032..e5cde906 100644
--- a/lib/index.ts
+++ b/lib/index.ts
@@ -22,6 +22,7 @@
*/
import { type Entry, getNewFileMenu } from './newFileMenu'
+import { Folder } from './files/folder'
export { formatFileSize } from './humanfilesize'
export { FileAction, getFileActions, registerFileAction } from './fileAction'
@@ -63,9 +64,9 @@ export const removeNewFileMenuEntry = function(entry: Entry | string) {
/**
* Get the list of registered entries from the upload menu
*
- * @param {FileInfo} context the creation context. Usually the current folder FileInfo
+ * @param {Folder} context the creation context. Usually the current folder FileInfo
*/
-export const getNewFileMenuEntries = function(context?: object) {
+export const getNewFileMenuEntries = function(context?: Folder) {
const newFileMenu = getNewFileMenu()
return newFileMenu.getEntries(context)
}
diff --git a/lib/newFileMenu.ts b/lib/newFileMenu.ts
index 20d97144..64a1324c 100644
--- a/lib/newFileMenu.ts
+++ b/lib/newFileMenu.ts
@@ -20,6 +20,7 @@
*
*/
+import { Folder } from '.'
import logger from './utils/logger'
export interface Entry {
@@ -27,10 +28,13 @@ export interface Entry {
id: string
/** Translatable string displayed in the menu */
displayName: string
- // Default new file name
+ /** Default new file name */
templateName?: string
- // Condition wether this entry is shown or not
- if?: (context: object) => boolean
+ /**
+ * Condition wether this entry is shown or not
+ * @param {Folder} context the creation context. Usually the current folder
+ */
+ if?: (context: Folder) => boolean
/**
* Either iconSvgInline or iconClass must be defined
* Svg as inline string.
@@ -67,9 +71,9 @@ export class NewFileMenu {
/**
* Get the list of registered entries
*
- * @param {FileInfo} context the creation context. Usually the current folder FileInfo
+ * @param {Folder} context the creation context. Usually the current folder FileInfo
*/
- public getEntries(context?: object): Array {
+ public getEntries(context?: Folder): Array {
if (context) {
return this._entries
.filter(entry => typeof entry.if === 'function' ? entry.if(context) : true)