-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
UnifiedSearch.vue
205 lines (180 loc) · 4.96 KB
/
UnifiedSearch.vue
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
<!--
- SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<div class="header-menu unified-search-menu">
<NcButton v-show="!showLocalSearch"
class="header-menu__trigger"
:aria-label="t('core', 'Unified search')"
type="tertiary-no-background"
@click="toggleUnifiedSearch">
<template #icon>
<Magnify class="header-menu__trigger-icon" :size="20" />
</template>
</NcButton>
<UnifiedSearchLocalSearchBar v-if="supportsLocalSearch"
:open.sync="showLocalSearch"
:query.sync="queryText"
@global-search="openModal" />
<UnifiedSearchModal :local-search="supportsLocalSearch"
:query.sync="queryText"
:open.sync="showUnifiedSearch" />
</div>
</template>
<script lang="ts">
import { emit, subscribe } from '@nextcloud/event-bus'
import { translate } from '@nextcloud/l10n'
import { useBrowserLocation } from '@vueuse/core'
import { defineComponent } from 'vue'
import NcButton from '@nextcloud/vue/dist/Components/NcButton.js'
import Magnify from 'vue-material-design-icons/Magnify.vue'
import UnifiedSearchModal from '../components/UnifiedSearch/UnifiedSearchModal.vue'
import UnifiedSearchLocalSearchBar from '../components/UnifiedSearch/UnifiedSearchLocalSearchBar.vue'
import debounce from 'debounce'
import logger from '../logger'
export default defineComponent({
name: 'UnifiedSearch',
components: {
NcButton,
Magnify,
UnifiedSearchModal,
UnifiedSearchLocalSearchBar,
},
setup() {
const currentLocation = useBrowserLocation()
return {
currentLocation,
t: translate,
}
},
data() {
return {
/** The current search query */
queryText: '',
/** Open state of the modal */
showUnifiedSearch: false,
/** Open state of the local search bar */
showLocalSearch: false,
}
},
computed: {
/**
* Debounce emitting the search query by 250ms
*/
debouncedQueryUpdate() {
return debounce(this.emitUpdatedQuery, 250)
},
/**
* Current page (app) supports local in-app search
*/
supportsLocalSearch() {
// TODO: Make this an API
const providerPaths = ['/settings/users', '/apps/deck', '/settings/apps']
return providerPaths.some((path) => this.currentLocation.pathname?.includes?.(path))
},
},
watch: {
/**
* Emit the updated query as eventbus events
* (This is debounced)
*/
queryText() {
this.debouncedQueryUpdate()
},
},
mounted() {
// register keyboard listener for search shortcut
if (window.OCP.Accessibility.disableKeyboardShortcuts() === false) {
window.addEventListener('keydown', this.onKeyDown)
}
// Allow external reset of the search / close local search
subscribe('nextcloud:unified-search:reset', () => {
this.showLocalSearch = false
this.queryText = ''
})
// Deprecated events to be removed
subscribe('nextcloud:unified-search:reset', () => {
emit('nextcloud:unified-search.reset', { query: '' })
})
subscribe('nextcloud:unified-search:search', ({ query }) => {
emit('nextcloud:unified-search.search', { query })
})
// all done
logger.debug('Unified search initialized!')
},
beforeDestroy() {
// keep in mind to remove the event listener
window.removeEventListener('keydown', this.onKeyDown)
},
methods: {
/**
* Handle the key down event to open search on `ctrl + F`
* @param event The keyboard event
*/
onKeyDown(event: KeyboardEvent) {
if (event.ctrlKey && event.code === 'KeyF') {
// only handle search if not already open - in this case the browser native search should be used
if (!this.showLocalSearch && !this.showUnifiedSearch) {
event.preventDefault()
}
this.toggleUnifiedSearch()
}
},
/**
* Toggle the local search if available - otherwise open the unified search modal
*/
toggleUnifiedSearch() {
if (this.supportsLocalSearch) {
this.showLocalSearch = !this.showLocalSearch
} else {
this.showUnifiedSearch = !this.showUnifiedSearch
this.showLocalSearch = false
}
},
/**
* Open the unified search modal
*/
openModal() {
this.showUnifiedSearch = true
this.showLocalSearch = false
},
/**
* Emit the updated search query as eventbus events
*/
emitUpdatedQuery() {
if (this.queryText === '') {
emit('nextcloud:unified-search:reset')
} else {
emit('nextcloud:unified-search:search', { query: this.queryText })
}
},
},
})
</script>
<style lang="scss" scoped>
// this is needed to allow us overriding component styles (focus-visible)
#header {
.header-menu {
display: flex;
align-items: center;
justify-content: center;
&__trigger {
height: var(--header-height);
width: var(--header-height) !important;
&:focus-visible {
// align with other header menu entries
outline: none !important;
box-shadow: none !important;
}
&:not(:hover,:focus,:focus-visible) {
opacity: .85;
}
&-icon {
// ensure the icon has the correct color
color: var(--color-background-plain-text) !important;
}
}
}
}
</style>