-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add FileListHeader #717
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { describe, expect, test, beforeEach, vi } from 'vitest' | ||
import { Header, getFileListHeaders, registerFileListHeaders } from '../lib/fileListHeaders' | ||
import logger from '../lib/utils/logger' | ||
import { Folder } from '../lib/files/folder' | ||
|
||
describe('FileListHeader init', () => { | ||
|
||
beforeEach(() => { | ||
delete window._nc_filelistheader | ||
}) | ||
|
||
test('Getting empty uninitialized FileListHeader', () => { | ||
logger.debug = vi.fn() | ||
const headers = getFileListHeaders() | ||
expect(window._nc_filelistheader).toBeUndefined() | ||
expect(headers).toHaveLength(0) | ||
expect(logger.debug).toHaveBeenCalledTimes(0) | ||
}) | ||
|
||
test('Initializing FileListHeader', () => { | ||
logger.debug = vi.fn() | ||
const header = new Header({ | ||
id: 'test', | ||
order: 1, | ||
enabled: () => true, | ||
render: () => {}, | ||
updated: () => {}, | ||
}) | ||
|
||
expect(header.id).toBe('test') | ||
expect(header.order).toBe(1) | ||
expect(header.enabled!({} as Folder, {})).toBe(true) | ||
|
||
registerFileListHeaders(header) | ||
|
||
expect(window._nc_filelistheader).toHaveLength(1) | ||
expect(getFileListHeaders()).toHaveLength(1) | ||
expect(getFileListHeaders()[0]).toStrictEqual(header) | ||
expect(logger.debug).toHaveBeenCalled() | ||
}) | ||
|
||
test('Duplicate Header gets rejected', () => { | ||
logger.error = vi.fn() | ||
const header = new Header({ | ||
id: 'test', | ||
order: 1, | ||
render: () => {}, | ||
updated: () => {}, | ||
}) | ||
|
||
registerFileListHeaders(header) | ||
expect(getFileListHeaders()).toHaveLength(1) | ||
expect(getFileListHeaders()[0]).toStrictEqual(header) | ||
|
||
const header2 = new Header({ | ||
id: 'test', | ||
order: 2, | ||
render: () => {}, | ||
updated: () => {}, | ||
}) | ||
|
||
registerFileListHeaders(header2) | ||
expect(getFileListHeaders()).toHaveLength(1) | ||
expect(getFileListHeaders()[0]).toStrictEqual(header) | ||
expect(logger.error).toHaveBeenCalledWith('Header test already registered', { header: header2 }) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/** | ||
* @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 { Folder } from './files/folder' | ||
import logger from './utils/logger' | ||
|
||
export interface HeaderData { | ||
/** Unique ID */ | ||
id: string | ||
/** Order */ | ||
order: number | ||
/** Condition wether this header is shown or not */ | ||
enabled?: (folder: Folder, view) => boolean | ||
/** Executed when file list is initialized */ | ||
render: (el: HTMLElement, folder: Folder, view) => void | ||
/** Executed when root folder changed */ | ||
updated(folder: Folder, view) | ||
} | ||
|
||
export class Header { | ||
|
||
private _header: HeaderData | ||
|
||
constructor(header: HeaderData) { | ||
this.validateHeader(header) | ||
this._header = header | ||
} | ||
|
||
get id() { | ||
return this._header.id | ||
} | ||
|
||
get order() { | ||
return this._header.order | ||
} | ||
|
||
get enabled() { | ||
return this._header.enabled | ||
} | ||
|
||
get render() { | ||
return this._header.render | ||
} | ||
|
||
get updated() { | ||
return this._header.updated | ||
} | ||
|
||
private validateHeader(header: HeaderData) { | ||
if (!header.id || !header.render || !header.updated) { | ||
throw new Error('Invalid header: id, render and updated are required') | ||
} | ||
|
||
if (typeof header.id !== 'string') { | ||
throw new Error('Invalid id property') | ||
} | ||
|
||
if (header.enabled !== undefined && typeof header.enabled !== 'function') { | ||
throw new Error('Invalid enabled property') | ||
} | ||
|
||
if (header.render && typeof header.render !== 'function') { | ||
throw new Error('Invalid render property') | ||
} | ||
|
||
if (header.updated && typeof header.updated !== 'function') { | ||
throw new Error('Invalid updated property') | ||
} | ||
} | ||
|
||
} | ||
|
||
export const registerFileListHeaders = function(header: Header): void { | ||
if (typeof window._nc_filelistheader === 'undefined') { | ||
window._nc_filelistheader = [] | ||
logger.debug('FileActions initialized') | ||
} | ||
|
||
// Check duplicates | ||
if (window._nc_filelistheader.find(search => search.id === header.id)) { | ||
logger.error(`Header ${header.id} already registered`, { header }) | ||
return | ||
} | ||
|
||
window._nc_filelistheader.push(header) | ||
} | ||
|
||
export const getFileListHeaders = function(): Header[] { | ||
return window._nc_filelistheader || [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
/// <reference types="@nextcloud/typings" /> | ||
|
||
import type { DavProperty } from './lib/dav/davProperties' | ||
import type { FileAction } from './lib/fileAction' | ||
import type { Header } from './lib/fileListHeaders' | ||
import type { NewFileMenu } from './lib/newFileMenu' | ||
import type { DavProperty } from './lib/dav/davProperties' | ||
|
||
export {} | ||
|
||
declare global { | ||
interface Window { | ||
OC: Nextcloud.v25.OC | Nextcloud.v26.OC | Nextcloud.v27.OC; | ||
_nc_newfilemenu?: NewFileMenu | ||
_nc_fileactions?: FileAction[] | ||
_nc_dav_properties?: string[] | ||
_nc_dav_namespaces?: DavProperty | ||
_nc_dav_properties?: string[] | ||
_nc_fileactions?: FileAction[] | ||
_nc_filelistheader?: Header[] | ||
_nc_newfilemenu?: NewFileMenu | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the view be typed somehow?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree we should have the view types (if possible)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we haven't migrated it from server yet! Was maing sure no changes were needed (I had to adjust quite a lot at the beginning)