| 1 |
<template> |
| 2 |
<cdx-typeahead-search |
| 3 |
:id="id" |
| 4 |
ref="searchForm" |
| 5 |
class="vector-typeahead-search" |
| 6 |
:class="rootClasses" |
| 7 |
:search-results-label="$i18n( 'searchresults' ).text()" |
| 8 |
:accesskey="searchAccessKey" |
| 9 |
:autocapitalize="autocapitalizeValue" |
| 10 |
:title="searchTitle" |
| 11 |
:placeholder="searchPlaceholder" |
| 12 |
:aria-label="searchPlaceholder" |
| 13 |
:initial-input-value="searchQuery" |
| 14 |
:button-label="$i18n( 'searchbutton' ).text()" |
| 15 |
:form-action="action" |
| 16 |
:show-thumbnail="showThumbnail" |
| 17 |
:highlight-query="highlightQuery" |
| 18 |
:auto-expand-width="autoExpandWidth" |
| 19 |
:search-results="suggestions" |
| 20 |
:search-footer-url="searchFooterUrl" |
| 21 |
:visible-item-limit="visibleItemLimit" |
| 22 |
@load-more="onLoadMore" |
| 23 |
@input="onInput" |
| 24 |
@search-result-click="instrumentation.onSuggestionClick" |
| 25 |
@submit="onSubmit" |
| 26 |
@focus="onFocus" |
| 27 |
@blur="onBlur" |
| 28 |
> |
| 29 |
<template #default> |
| 30 |
<input |
| 31 |
type="hidden" |
| 32 |
name="title" |
| 33 |
:value="searchPageTitle" |
| 34 |
> |
| 35 |
<input |
| 36 |
type="hidden" |
| 37 |
name="wprov" |
| 38 |
:value="wprov" |
| 39 |
> |
| 40 |
</template> |
| 41 |
<template #search-results-pending> |
| 42 |
{{ $i18n( 'vector-search-loader' ).text() }} |
| 43 |
</template> |
| 44 |
<!-- eslint-disable-next-line vue/no-template-shadow --> |
| 45 |
<template #search-footer-text="{ searchQuery }"> |
| 46 |
<span v-i18n-html:vector-searchsuggest-containing="[ searchQuery ]"></span> |
| 47 |
</template> |
| 48 |
</cdx-typeahead-search> |
| 49 |
</template> |
| 50 |
|
| 51 |
<script> |
| 52 |
const { CdxTypeaheadSearch } = mw.loader.require( 'skins.vector.search.codex.scripts' ), |
| 53 |
{ defineComponent, nextTick } = require( 'vue' ), |
| 54 |
client = require( './restSearchClient.js' ), |
| 55 |
restClient = client( mw.config ), |
| 56 |
urlGenerator = require( './urlGenerator.js' )( mw.config ), |
| 57 |
instrumentation = require( './instrumentation.js' ); |
| 58 |
|
| 59 |
// @vue/component |
| 60 |
module.exports = exports = defineComponent( { |
| 61 |
name: 'App', |
| 62 |
compatConfig: { |
| 63 |
MODE: 3 |
| 64 |
}, |
| 65 |
compilerOptions: { |
| 66 |
whitespace: 'condense' |
| 67 |
}, |
| 68 |
components: { CdxTypeaheadSearch }, |
| 69 |
props: { |
| 70 |
id: { |
| 71 |
type: String, |
| 72 |
required: true |
| 73 |
}, |
| 74 |
autocapitalizeValue: { |
| 75 |
type: String, |
| 76 |
default: undefined |
| 77 |
}, |
| 78 |
searchPageTitle: { |
| 79 |
type: String, |
| 80 |
default: 'Special:Search' |
| 81 |
}, |
| 82 |
autofocusInput: { |
| 83 |
type: Boolean, |
| 84 |
default: false |
| 85 |
}, |
| 86 |
action: { |
| 87 |
type: String, |
| 88 |
default: '' |
| 89 |
}, |
| 90 |
/** The keyboard shortcut to focus search. */ |
| 91 |
searchAccessKey: { |
| 92 |
type: String, |
| 93 |
default: undefined |
| 94 |
}, |
| 95 |
/** The access key informational tip for search. */ |
| 96 |
searchTitle: { |
| 97 |
type: String, |
| 98 |
default: undefined |
| 99 |
}, |
| 100 |
/** The ghost text shown when no search query is entered. */ |
| 101 |
searchPlaceholder: { |
| 102 |
type: String, |
| 103 |
default: undefined |
| 104 |
}, |
| 105 |
/** |
| 106 |
* The search query string taken from the server-side rendered input immediately before |
| 107 |
* client render. |
| 108 |
*/ |
| 109 |
searchQuery: { |
| 110 |
type: String, |
| 111 |
default: undefined |
| 112 |
}, |
| 113 |
showThumbnail: { |
| 114 |
type: Boolean, |
| 115 |
required: true, |
| 116 |
default: false |
| 117 |
}, |
| 118 |
showDescription: { |
| 119 |
type: Boolean, |
| 120 |
default: false |
| 121 |
}, |
| 122 |
highlightQuery: { |
| 123 |
type: Boolean, |
| 124 |
default: false |
| 125 |
}, |
| 126 |
autoExpandWidth: { |
| 127 |
type: Boolean, |
| 128 |
default: false |
| 129 |
} |
| 130 |
}, |
| 131 |
data() { |
| 132 |
return { |
| 133 |
// -1 here is the default "active suggestion index". |
| 134 |
wprov: instrumentation.getWprovFromResultIndex( -1 ), |
| 135 |
|
| 136 |
// Suggestions to be shown in the TypeaheadSearch menu. |
| 137 |
suggestions: [], |
| 138 |
|
| 139 |
// Link to the search page for the current search query. |
| 140 |
searchFooterUrl: '', |
| 141 |
|
| 142 |
// The current search query. Used to detect whether a fetch response is stale. |
| 143 |
currentSearchQuery: '', |
| 144 |
|
| 145 |
// Whether to apply a CSS class that disables the CSS transitions on the text input |
| 146 |
disableTransitions: this.autofocusInput, |
| 147 |
|
| 148 |
instrumentation: instrumentation.listeners, |
| 149 |
|
| 150 |
isFocused: false |
| 151 |
}; |
| 152 |
}, |
| 153 |
computed: { |
| 154 |
rootClasses() { |
| 155 |
return { |
| 156 |
'vector-search-box-disable-transitions': this.disableTransitions, |
| 157 |
'vector-typeahead-search--active': this.isFocused |
| 158 |
}; |
| 159 |
}, |
| 160 |
visibleItemLimit() { |
| 161 |
// if the search client supports loading more results, |
| 162 |
// show 7 out of 10 results at first (arbitrary number), |
| 163 |
// so that scroll events are fired and trigger onLoadMore() |
| 164 |
return restClient.loadMore ? 7 : null; |
| 165 |
} |
| 166 |
}, |
| 167 |
methods: { |
| 168 |
/** |
| 169 |
* Fetch suggestions when new input is received. |
| 170 |
* |
| 171 |
* @param {string} value |
| 172 |
*/ |
| 173 |
onInput: function ( value ) { |
| 174 |
const query = value.trim(); |
| 175 |
|
| 176 |
this.currentSearchQuery = query; |
| 177 |
|
| 178 |
if ( query === '' ) { |
| 179 |
this.suggestions = []; |
| 180 |
this.searchFooterUrl = ''; |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
this.updateUIWithSearchClientResult( |
| 185 |
restClient.fetchByTitle( query, 10, this.showDescription ), |
| 186 |
true |
| 187 |
); |
| 188 |
}, |
| 189 |
|
| 190 |
/** |
| 191 |
* Fetch additional suggestions. |
| 192 |
* |
| 193 |
* This should only be called if visibleItemLimit is non-null, |
| 194 |
* i.e. if the search client supports loading more results. |
| 195 |
*/ |
| 196 |
onLoadMore() { |
| 197 |
if ( !restClient.loadMore ) { |
| 198 |
mw.log.warn( 'onLoadMore() should not have been called for this search client' ); |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
this.updateUIWithSearchClientResult( |
| 203 |
restClient.loadMore( |
| 204 |
this.currentSearchQuery, |
| 205 |
this.suggestions.length, |
| 206 |
10, |
| 207 |
this.showDescription |
| 208 |
), |
| 209 |
false |
| 210 |
); |
| 211 |
}, |
| 212 |
|
| 213 |
/** |
| 214 |
* @param {AbortableSearchFetch} search |
| 215 |
* @param {boolean} replaceResults |
| 216 |
*/ |
| 217 |
updateUIWithSearchClientResult( search, replaceResults ) { |
| 218 |
const query = this.currentSearchQuery; |
| 219 |
instrumentation.listeners.onFetchStart(); |
| 220 |
|
| 221 |
search.fetch |
| 222 |
.then( ( data ) => { |
| 223 |
// Only use these results if they're still relevant |
| 224 |
// If currentSearchQuery !== query, these results are for a previous search |
| 225 |
// and we shouldn't show them. |
| 226 |
if ( this.currentSearchQuery === query ) { |
| 227 |
if ( replaceResults ) { |
| 228 |
this.suggestions = []; |
| 229 |
} |
| 230 |
this.suggestions.push( |
| 231 |
...instrumentation.addWprovToSearchResultUrls( |
| 232 |
data.results, this.suggestions.length |
| 233 |
) |
| 234 |
); |
| 235 |
this.searchFooterUrl = urlGenerator.generateUrl( query ); |
| 236 |
} |
| 237 |
|
| 238 |
const event = { |
| 239 |
numberOfResults: data.results.length, |
| 240 |
query: query |
| 241 |
}; |
| 242 |
instrumentation.listeners.onFetchEnd( event ); |
| 243 |
} ) |
| 244 |
.catch( () => { |
| 245 |
// TODO: error handling |
| 246 |
} ); |
| 247 |
}, |
| 248 |
|
| 249 |
/** |
| 250 |
* @param {SearchSubmitEvent} event |
| 251 |
*/ |
| 252 |
onSubmit( event ) { |
| 253 |
this.wprov = instrumentation.getWprovFromResultIndex( event.index ); |
| 254 |
|
| 255 |
instrumentation.listeners.onSubmit( event ); |
| 256 |
}, |
| 257 |
|
| 258 |
onFocus() { |
| 259 |
this.isFocused = true; |
| 260 |
}, |
| 261 |
|
| 262 |
onBlur() { |
| 263 |
this.isFocused = false; |
| 264 |
} |
| 265 |
}, |
| 266 |
mounted() { |
| 267 |
if ( this.autofocusInput ) { |
| 268 |
this.$refs.searchForm.focus(); |
| 269 |
nextTick( () => { |
| 270 |
this.disableTransitions = false; |
| 271 |
} ); |
| 272 |
} |
| 273 |
} |
| 274 |
} ); |
| 275 |
</script> |