2021-09-10 15:38:53 +00:00
|
|
|
import variables from 'modules/variables';
|
2022-04-08 13:48:36 +00:00
|
|
|
import { PureComponent, Fragment } from 'react';
|
|
|
|
import { MdSearch, MdMic, MdSettings } from 'react-icons/md';
|
|
|
|
import Tooltip from 'components/helpers/tooltip/Tooltip';
|
2021-11-17 21:50:34 +00:00
|
|
|
//import Hotkeys from 'react-hot-keys';
|
2021-05-01 17:29:07 +00:00
|
|
|
|
2021-08-28 14:34:12 +00:00
|
|
|
import AutocompleteInput from 'components/helpers/autocomplete/Autocomplete';
|
2021-04-12 17:55:02 +00:00
|
|
|
|
2021-08-28 14:34:12 +00:00
|
|
|
import EventBus from 'modules/helpers/eventbus';
|
2020-10-13 14:30:53 +00:00
|
|
|
|
2020-11-29 14:32:08 +00:00
|
|
|
import './search.scss';
|
|
|
|
|
2022-04-13 14:25:50 +00:00
|
|
|
import searchEngines from 'components/widgets/search/search_engines.json';
|
|
|
|
import * as autocompleteProviders from 'components/widgets/search/autocomplete_providers.json';
|
2020-08-26 13:32:52 +00:00
|
|
|
|
2021-08-14 19:10:48 +00:00
|
|
|
export default class Search extends 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,
|
2021-08-25 12:55:20 +00:00
|
|
|
suggestions: [],
|
2022-04-08 13:48:36 +00:00
|
|
|
searchDropdown: 'hidden',
|
|
|
|
classList:
|
|
|
|
localStorage.getItem('widgetStyle') === 'legacy' ? 'searchIcons old' : 'searchIcons',
|
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
|
|
|
|
2022-04-12 09:52:59 +00:00
|
|
|
// todo: use ref, stop being lazy
|
|
|
|
const micIcon = document.getElementById('micBtn');
|
|
|
|
micIcon.classList.add('micActive');
|
|
|
|
|
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 = () => {
|
2022-04-12 09:52:59 +00:00
|
|
|
micIcon.classList.remove('micActive');
|
2021-06-21 12:13:16 +00:00
|
|
|
if (searchText.value === '') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-16 22:43:46 +00:00
|
|
|
setTimeout(() => {
|
2021-09-28 22:04:04 +00:00
|
|
|
variables.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
|
|
|
};
|
2022-04-08 13:48:36 +00:00
|
|
|
};
|
2020-10-13 14:30:53 +00:00
|
|
|
|
2021-05-01 21:27:44 +00:00
|
|
|
searchButton = (e) => {
|
2021-08-16 13:06:57 +00:00
|
|
|
e.preventDefault();
|
|
|
|
const value = e.target.value || document.getElementById('searchtext').value || 'mue fast';
|
2021-09-28 22:04:04 +00:00
|
|
|
variables.stats.postEvent('feature', 'Search');
|
2021-03-17 17:55:58 +00:00
|
|
|
window.location.href = this.state.url + `?${this.state.query}=` + value;
|
2022-04-08 13:48:36 +00:00
|
|
|
};
|
2021-01-19 10:56:51 +00:00
|
|
|
|
2021-05-01 17:29:07 +00:00
|
|
|
async getSuggestions(input) {
|
2022-04-08 13:48:36 +00:00
|
|
|
window.setResults = (results) => {
|
2021-07-11 13:29:32 +00:00
|
|
|
window.searchResults = results;
|
2021-07-12 10:34:05 +00:00
|
|
|
};
|
2021-05-01 21:27:44 +00:00
|
|
|
|
2021-07-12 10:34:05 +00:00
|
|
|
const script = document.createElement('script');
|
2022-04-08 13:48:36 +00:00
|
|
|
script.src = `${this.state.autocompleteURL + this.state.autocompleteQuery + input}&${
|
|
|
|
this.state.autocompleteCallback
|
|
|
|
}=window.setResults`;
|
2021-07-11 13:29:32 +00:00
|
|
|
document.head.appendChild(script);
|
|
|
|
|
|
|
|
try {
|
|
|
|
this.setState({
|
2022-04-08 13:48:36 +00:00
|
|
|
suggestions: window.searchResults[1].splice(0, 3),
|
2021-07-11 13:29:32 +00:00
|
|
|
});
|
2021-07-18 09:43:25 +00:00
|
|
|
} catch (e) {
|
|
|
|
// ignore error if empty
|
|
|
|
}
|
2021-07-11 13:29:32 +00:00
|
|
|
|
|
|
|
document.head.removeChild(script);
|
2021-05-01 17:29:07 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 17:55:02 +00:00
|
|
|
init() {
|
2021-07-12 10:34:05 +00:00
|
|
|
let url, microphone;
|
2020-08-26 13:32:52 +00:00
|
|
|
let query = 'q';
|
|
|
|
|
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') {
|
2022-04-08 13:48:36 +00:00
|
|
|
microphone = (
|
2022-04-12 09:52:59 +00:00
|
|
|
<button onClick={this.startSpeechRecognition} id='micBtn'>
|
2022-04-08 13:48:36 +00:00
|
|
|
<MdMic className="micIcon" />
|
|
|
|
</button>
|
|
|
|
);
|
2020-10-13 14:30:53 +00:00
|
|
|
}
|
|
|
|
|
2021-07-12 10:34:05 +00:00
|
|
|
let autocompleteURL, autocompleteQuery, autocompleteCallback;
|
2021-05-01 17:29:07 +00:00
|
|
|
|
|
|
|
if (localStorage.getItem('autocomplete') === 'true') {
|
2022-04-08 13:48:36 +00:00
|
|
|
const info = autocompleteProviders.find(
|
|
|
|
(i) => i.value === localStorage.getItem('autocompleteProvider'),
|
|
|
|
);
|
2021-05-01 17:29:07 +00:00
|
|
|
autocompleteURL = info.url;
|
|
|
|
autocompleteQuery = info.query;
|
|
|
|
autocompleteCallback = info.callback;
|
|
|
|
}
|
|
|
|
|
2021-01-19 10:56:51 +00:00
|
|
|
this.setState({
|
2021-08-27 18:42:40 +00:00
|
|
|
url,
|
|
|
|
query,
|
|
|
|
autocompleteURL,
|
|
|
|
autocompleteQuery,
|
|
|
|
autocompleteCallback,
|
|
|
|
microphone,
|
2022-04-08 13:48:36 +00:00
|
|
|
currentSearch: info ? info.name : 'Custom',
|
2021-08-25 12:55:20 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleDropdown() {
|
2021-08-28 15:59:09 +00:00
|
|
|
if (this.state.searchDropdown === 'hidden') {
|
2021-08-25 12:55:20 +00:00
|
|
|
this.setState({
|
2022-04-08 13:48:36 +00:00
|
|
|
searchDropdown: 'visible',
|
2021-08-25 12:55:20 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.setState({
|
2022-04-08 13:48:36 +00:00
|
|
|
searchDropdown: 'hidden',
|
2021-08-25 12:55:20 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-26 18:48:20 +00:00
|
|
|
setSearch(name, custom) {
|
2021-08-25 12:55:20 +00:00
|
|
|
let url;
|
|
|
|
let query = 'q';
|
|
|
|
const info = searchEngines.find((i) => i.name === name);
|
|
|
|
|
|
|
|
if (info !== undefined) {
|
|
|
|
url = info.url;
|
|
|
|
if (info.query) {
|
|
|
|
query = info.query;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-26 18:48:20 +00:00
|
|
|
if (custom) {
|
|
|
|
url = localStorage.getItem('customSearchEngine');
|
|
|
|
}
|
|
|
|
|
2021-08-25 12:55:20 +00:00
|
|
|
this.setState({
|
2021-08-27 18:42:40 +00:00
|
|
|
url,
|
|
|
|
query,
|
2021-08-25 12:55:20 +00:00
|
|
|
currentSearch: name,
|
2022-04-08 13:48:36 +00:00
|
|
|
searchDropdown: 'hidden',
|
2021-01-19 10:56:51 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-12 17:55:02 +00:00
|
|
|
componentDidMount() {
|
|
|
|
EventBus.on('refresh', (data) => {
|
|
|
|
if (data === 'search') {
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
});
|
2022-04-08 13:48:36 +00:00
|
|
|
|
2021-04-12 17:55:02 +00:00
|
|
|
this.init();
|
2022-04-08 13:48:36 +00:00
|
|
|
|
|
|
|
if (localStorage.getItem('searchFocus') === 'true') {
|
|
|
|
const element = document.getElementById('searchtext');
|
|
|
|
if (element) {
|
|
|
|
element.focus();
|
|
|
|
}
|
|
|
|
}
|
2021-04-12 17:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2021-08-20 21:06:40 +00:00
|
|
|
EventBus.off('refresh');
|
2021-04-12 17:55:02 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 10:56:51 +00:00
|
|
|
render() {
|
2022-04-08 13:48:36 +00:00
|
|
|
const customText = variables.language
|
|
|
|
.getMessage(variables.languagecode, 'modals.main.settings.sections.search.custom')
|
|
|
|
.split(' ')[0];
|
2021-08-26 18:48:20 +00:00
|
|
|
|
2020-09-16 10:43:58 +00:00
|
|
|
return (
|
2022-04-08 13:48:36 +00:00
|
|
|
<div className="searchComponents">
|
|
|
|
<div className="searchMain">
|
|
|
|
<div className={this.state.classList}>
|
|
|
|
{localStorage.getItem('searchDropdown') === 'true' ? (
|
|
|
|
<Tooltip
|
|
|
|
title={variables.language.getMessage(variables.languagecode, 'widgets.search')}
|
|
|
|
>
|
|
|
|
<button>
|
|
|
|
<MdSettings onClick={() => this.toggleDropdown()} />
|
|
|
|
</button>
|
|
|
|
</Tooltip>
|
|
|
|
) : (
|
|
|
|
''
|
|
|
|
)}
|
|
|
|
<Tooltip
|
|
|
|
title={variables.language.getMessage(variables.languagecode, 'widgets.search')}
|
|
|
|
>
|
|
|
|
{this.state.microphone}
|
|
|
|
</Tooltip>
|
|
|
|
</div>
|
|
|
|
<form onSubmit={this.searchButton} className="searchBar">
|
|
|
|
<div className={this.state.classList}>
|
|
|
|
<Tooltip
|
|
|
|
title={variables.language.getMessage(variables.languagecode, 'widgets.search')}
|
|
|
|
>
|
|
|
|
<button onClick={this.searchButton}>
|
|
|
|
<MdSearch />
|
|
|
|
</button>
|
|
|
|
</Tooltip>
|
|
|
|
</div>
|
|
|
|
<AutocompleteInput
|
|
|
|
placeholder={variables.language.getMessage(variables.languagecode, 'widgets.search')}
|
|
|
|
id="searchtext"
|
|
|
|
suggestions={this.state.suggestions}
|
|
|
|
onChange={(e) => this.getSuggestions(e)}
|
|
|
|
onClick={this.searchButton}
|
|
|
|
/>
|
|
|
|
{/*variables.keybinds.focusSearch && variables.keybinds.focusSearch !== '' ? <Hotkeys keyName={variables.keybinds.focusSearch} onKeyDown={() => document.getElementById('searchtext').focus()}/> : null*/}
|
|
|
|
</form>
|
|
|
|
</div>
|
2021-11-11 20:43:33 +00:00
|
|
|
<div>
|
2022-04-08 13:48:36 +00:00
|
|
|
{localStorage.getItem('searchDropdown') === 'true' ? (
|
|
|
|
<div className="searchDropdown" style={{ visibility: this.state.searchDropdown }}>
|
|
|
|
{searchEngines.map(({ name }) => {
|
|
|
|
if (name === this.state.currentSearch) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<span className="searchDropdownList" onClick={() => this.setSearch(name)}>
|
|
|
|
{name}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
{this.state.currentSearch !== customText ? (
|
|
|
|
<span
|
|
|
|
className="searchDropdownList"
|
|
|
|
onClick={() => this.setSearch(customText, 'custom')}
|
|
|
|
>
|
|
|
|
{customText}
|
|
|
|
</span>
|
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
) : null}
|
2021-11-11 20:43:33 +00:00
|
|
|
</div>
|
2022-04-08 13:48:36 +00:00
|
|
|
</div>
|
2020-09-16 10:43:58 +00:00
|
|
|
);
|
2020-08-26 13:32:52 +00:00
|
|
|
}
|
2020-10-15 14:23:01 +00:00
|
|
|
}
|