2020-08-26 13:32:52 +00:00
|
|
|
import React from 'react';
|
2021-01-17 18:07:26 +00:00
|
|
|
|
2021-04-12 17:55:02 +00:00
|
|
|
import EventBus from '../../../modules/helpers/eventbus';
|
2021-05-01 17:29:07 +00:00
|
|
|
import fetchJSONP from 'fetch-jsonp';
|
|
|
|
|
|
|
|
import AutocompleteInput from '../../helpers/autocomplete/Autocomplete';
|
2021-04-12 17:55:02 +00:00
|
|
|
|
2020-08-26 13:32:52 +00:00
|
|
|
import SearchIcon from '@material-ui/icons/Search';
|
2020-10-13 14:30:53 +00:00
|
|
|
import MicIcon from '@material-ui/icons/Mic';
|
|
|
|
|
2020-11-29 14:32:08 +00:00
|
|
|
import './search.scss';
|
|
|
|
|
|
|
|
const searchEngines = require('./search_engines.json');
|
2021-05-01 17:29:07 +00:00
|
|
|
const autocompleteProviders = require('./autocomplete_providers.json');
|
2020-08-26 13:32:52 +00:00
|
|
|
|
|
|
|
export default class Search extends React.PureComponent {
|
2021-03-23 13:10:34 +00:00
|
|
|
constructor() {
|
|
|
|
super();
|
2020-10-13 14:30:53 +00:00
|
|
|
this.state = {
|
|
|
|
url: '',
|
2021-01-19 10:56:51 +00:00
|
|
|
query: '',
|
2021-05-01 17:29:07 +00:00
|
|
|
autocompleteURL: '',
|
|
|
|
autocompleteQuery: '',
|
|
|
|
autocompleteCallback: '',
|
|
|
|
microphone: null,
|
|
|
|
suggestions: []
|
2020-10-13 14:30:53 +00:00
|
|
|
};
|
2021-03-19 22:30:29 +00:00
|
|
|
this.language = window.language.widgets.search;
|
2020-10-13 14:30:53 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 13:10:34 +00:00
|
|
|
startSpeechRecognition = () => {
|
2020-10-13 14:30:53 +00:00
|
|
|
const voiceSearch = new window.webkitSpeechRecognition();
|
|
|
|
voiceSearch.start();
|
2021-01-19 10:56:51 +00:00
|
|
|
|
2021-01-16 18:39:03 +00:00
|
|
|
const searchText = document.getElementById('searchtext');
|
2021-01-19 10:56:51 +00:00
|
|
|
|
|
|
|
voiceSearch.onresult = (event) => {
|
|
|
|
searchText.value = event.results[0][0].transcript;
|
2021-04-08 19:02:31 +00:00
|
|
|
};
|
2021-01-19 10:56:51 +00:00
|
|
|
|
2021-04-08 19:02:31 +00:00
|
|
|
voiceSearch.onend = () => {
|
2021-06-21 12:13:16 +00:00
|
|
|
if (searchText.value === '') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-16 22:43:46 +00:00
|
|
|
setTimeout(() => {
|
2021-06-30 10:27:54 +00:00
|
|
|
window.stats.postEvent('feature', 'Voice search');
|
2021-01-16 22:43:46 +00:00
|
|
|
window.location.href = this.state.url + `?${this.state.query}=` + searchText.value;
|
|
|
|
}, 1000);
|
2021-04-16 11:26:56 +00:00
|
|
|
};
|
2020-10-13 14:30:53 +00:00
|
|
|
}
|
|
|
|
|
2021-05-01 21:27:44 +00:00
|
|
|
searchButton = (e) => {
|
|
|
|
let value;
|
|
|
|
|
2021-05-08 09:58:17 +00:00
|
|
|
if (e.target.innerText !== undefined) {
|
|
|
|
value = e.target.innerText;
|
2021-05-01 21:27:44 +00:00
|
|
|
} else {
|
|
|
|
value = document.getElementById('searchtext').value || 'mue fast';
|
|
|
|
}
|
|
|
|
|
2021-06-30 10:27:54 +00:00
|
|
|
window.stats.postEvent('feature', 'Search');
|
2021-03-17 17:55:58 +00:00
|
|
|
window.location.href = this.state.url + `?${this.state.query}=` + value;
|
|
|
|
}
|
2021-01-19 10:56:51 +00:00
|
|
|
|
2021-05-01 17:29:07 +00:00
|
|
|
async getSuggestions(input) {
|
|
|
|
const data = await (await fetchJSONP(this.state.autocompleteURL + this.state.autocompleteQuery + input, {
|
|
|
|
jsonpCallback: this.state.autocompleteCallback
|
|
|
|
})).json();
|
2021-05-01 21:27:44 +00:00
|
|
|
|
2021-05-01 17:29:07 +00:00
|
|
|
this.setState({
|
|
|
|
suggestions: data[1].splice(0, 3)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-12 17:55:02 +00:00
|
|
|
init() {
|
2020-08-26 13:32:52 +00:00
|
|
|
let url;
|
|
|
|
let query = 'q';
|
2021-01-19 10:56:51 +00:00
|
|
|
let microphone = null;
|
2020-08-26 13:32:52 +00:00
|
|
|
|
2020-09-19 13:53:33 +00:00
|
|
|
const setting = localStorage.getItem('searchEngine');
|
2021-04-08 19:02:31 +00:00
|
|
|
const info = searchEngines.find((i) => i.settingsName === setting);
|
2021-01-17 18:07:26 +00:00
|
|
|
|
2020-09-23 09:48:23 +00:00
|
|
|
if (info !== undefined) {
|
|
|
|
url = info.url;
|
2021-04-08 18:52:17 +00:00
|
|
|
if (info.query) {
|
|
|
|
query = info.query;
|
|
|
|
}
|
2020-09-23 09:48:23 +00:00
|
|
|
}
|
2020-08-26 13:32:52 +00:00
|
|
|
|
2021-01-17 16:25:21 +00:00
|
|
|
if (setting === 'custom') {
|
|
|
|
url = localStorage.getItem('customSearchEngine');
|
|
|
|
}
|
2020-09-18 11:58:37 +00:00
|
|
|
|
2020-10-13 14:30:53 +00:00
|
|
|
if (localStorage.getItem('voiceSearch') === 'true') {
|
2021-03-23 13:10:34 +00:00
|
|
|
microphone = <MicIcon className='micIcon' onClick={this.startSpeechRecognition}/>;
|
2020-10-13 14:30:53 +00:00
|
|
|
}
|
|
|
|
|
2021-05-01 17:29:07 +00:00
|
|
|
let autocompleteURL;
|
|
|
|
let autocompleteQuery;
|
|
|
|
let autocompleteCallback;
|
|
|
|
|
|
|
|
if (localStorage.getItem('autocomplete') === 'true') {
|
|
|
|
const info = autocompleteProviders.find((i) => i.value === localStorage.getItem('autocompleteProvider'));
|
|
|
|
autocompleteURL = info.url;
|
|
|
|
autocompleteQuery = info.query;
|
|
|
|
autocompleteCallback = info.callback;
|
|
|
|
}
|
|
|
|
|
2021-01-19 10:56:51 +00:00
|
|
|
this.setState({
|
|
|
|
url: url,
|
|
|
|
query: query,
|
2021-05-01 17:29:07 +00:00
|
|
|
autocompleteURL: autocompleteURL,
|
|
|
|
autocompleteQuery: autocompleteQuery,
|
|
|
|
autocompleteCallback: autocompleteCallback,
|
2021-01-19 10:56:51 +00:00
|
|
|
microphone: microphone
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-12 17:55:02 +00:00
|
|
|
componentDidMount() {
|
|
|
|
EventBus.on('refresh', (data) => {
|
|
|
|
if (data === 'search') {
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
EventBus.remove('refresh');
|
|
|
|
}
|
|
|
|
|
2021-01-19 10:56:51 +00:00
|
|
|
render() {
|
2020-09-16 10:43:58 +00:00
|
|
|
return (
|
2021-03-23 09:44:57 +00:00
|
|
|
<form action={this.state.url} className='searchBar'>
|
|
|
|
{this.state.microphone}
|
2021-03-23 13:10:34 +00:00
|
|
|
<SearchIcon onClick={this.searchButton}/>
|
2021-05-01 17:29:07 +00:00
|
|
|
<AutocompleteInput placeholder={this.language} name={this.state.query} id='searchtext' suggestions={this.state.suggestions} onChange={(e) => this.getSuggestions(e)} onClick={this.searchButton}/>
|
2021-03-23 09:44:57 +00:00
|
|
|
</form>
|
2020-09-16 10:43:58 +00:00
|
|
|
);
|
2020-08-26 13:32:52 +00:00
|
|
|
}
|
2020-10-15 14:23:01 +00:00
|
|
|
}
|