aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files/src/filters/SearchFilter.ts
blob: 4c7231fd26ae05b9def3ce40c2aaa686cf94b97a (plain)
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
/*!
 * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

import type { INode } from '@nextcloud/files'
import type { ComponentPublicInstance } from 'vue'

import { subscribe } from '@nextcloud/event-bus'
import { FileListFilter, registerFileListFilter } from '@nextcloud/files'
import Vue from 'vue'
import FileListFilterToSearch from '../components/FileListFilter/FileListFilterToSearch.vue'

class SearchFilter extends FileListFilter {

	private currentInstance?: ComponentPublicInstance<typeof FileListFilterToSearch>

	constructor() {
		super('files:filter-to-search', 999)
		subscribe('files:search:updated', ({ query, scope }) => {
			if (query && scope === 'filter') {
				this.currentInstance?.showButton()
			} else {
				this.currentInstance?.hideButton()
			}
		})
	}

	public mount(el: HTMLElement) {
		if (this.currentInstance) {
			this.currentInstance.$destroy()
		}

		const View = Vue.extend(FileListFilterToSearch)
		this.currentInstance = new View().$mount(el) as unknown as ComponentPublicInstance<typeof FileListFilterToSearch>
	}

	public filter(nodes: INode[]): INode[] {
		return nodes
	}

}

/**
 * Register a file list filter to only show hidden files if enabled by user config
 */
export function registerFilterToSearchToggle() {
	registerFileListFilter(new SearchFilter())
}