Skip to content

Commit

Permalink
feat: Add reset method to file list filters
Browse files Browse the repository at this point in the history
This allows the files app to reset filters if needed (e.g. on
navigation) instead all filters to listen  to different events.

Signed-off-by: Ferdinand Thiessen <[email protected]>
  • Loading branch information
susnux committed Nov 13, 2024
1 parent 45d7733 commit a435b8a
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions lib/fileListFilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,28 @@ export interface IFileListFilter extends TypedEventTarget<IFileListFilterEvents>
*/
readonly order: number

/**
* Filter function to decide if a node is shown.
*
* @param nodes Nodes to filter
* @return Subset of the `nodes` parameter to show
*/
filter(nodes: INode[]): INode[]

/**
* If the filter needs a visual element for settings it can provide a function to mount it.
* @param el The DOM element to mount to
*/
readonly mount?: (el: HTMLElement) => void
mount?(el: HTMLElement): void

/**
* Filter function to decide if a node is shown
* @return The nodes to be shown
* Reset the filter to the initial state.
* This is called by the files app.
* Implementations should make sure,that if they provide chips they need to emit the `update:chips` event.
*
* @since 3.10.0
*/
filter(node: INode[]): INode[]
reset?(): void
}

export class FileListFilter extends TypedEventTarget<IFileListFilterEvents> implements IFileListFilter {
Expand All @@ -96,6 +108,16 @@ export class FileListFilter extends TypedEventTarget<IFileListFilterEvents> impl
return nodes
}

/**
* Reset the filter to the initial state.
*
* Make sure to override this method when extending the class.
*/
public reset(): void {
this.filterUpdated()
this.updateChips([])

Check warning on line 118 in lib/fileListFilters.ts

View check run for this annotation

Codecov / codecov/patch

lib/fileListFilters.ts#L117-L118

Added lines #L117 - L118 were not covered by tests
}

protected updateChips(chips: IFileListFilterChip[]) {
this.dispatchTypedEvent('update:chips', new CustomEvent('update:chips', { detail: chips }) as FilterUpdateChipsEvent)
}
Expand Down

0 comments on commit a435b8a

Please sign in to comment.