2024-02-27 00:08:21 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2024 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { Controller } from "@hotwired/stimulus";
|
|
|
|
import { autocomplete } from '@algolia/autocomplete-js';
|
2024-02-28 21:38:11 +01:00
|
|
|
//import "@algolia/autocomplete-theme-classic/dist/theme.css";
|
|
|
|
import "../../css/components/autocomplete_bootstrap_theme.css";
|
2024-02-27 00:39:38 +01:00
|
|
|
import { createLocalStorageRecentSearchesPlugin } from '@algolia/autocomplete-plugin-recent-searches';
|
2024-02-27 00:08:21 +01:00
|
|
|
import {marked} from "marked";
|
|
|
|
|
2024-02-29 20:39:27 +01:00
|
|
|
import {
|
|
|
|
trans,
|
|
|
|
SEARCH_PLACEHOLDER,
|
2024-02-29 21:53:28 +01:00
|
|
|
SEARCH_SUBMIT,
|
|
|
|
STATISTICS_PARTS
|
2024-02-29 20:39:27 +01:00
|
|
|
} from '../../translator';
|
|
|
|
|
2024-02-29 22:50:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This controller is responsible for the search fields in the navbar and the homepage.
|
|
|
|
* It uses the Algolia Autocomplete library to provide a fast and responsive search.
|
|
|
|
*/
|
2024-02-27 00:08:21 +01:00
|
|
|
export default class extends Controller {
|
|
|
|
|
2024-02-27 00:28:17 +01:00
|
|
|
static targets = ["input"];
|
|
|
|
|
2024-02-27 00:08:21 +01:00
|
|
|
_autocomplete;
|
|
|
|
|
2024-02-29 23:00:21 +01:00
|
|
|
// Highlight the search query in the results
|
2024-02-29 22:20:30 +01:00
|
|
|
_highlight = (text, query) => {
|
|
|
|
if (!text) return text;
|
|
|
|
if (!query) return text;
|
|
|
|
|
|
|
|
const HIGHLIGHT_PRE_TAG = '__aa-highlight__'
|
|
|
|
const HIGHLIGHT_POST_TAG = '__/aa-highlight__'
|
|
|
|
|
|
|
|
const escaped = query.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
|
|
|
|
const regex = new RegExp(escaped, 'gi');
|
|
|
|
|
|
|
|
return text.replace(regex, (match) => `${HIGHLIGHT_PRE_TAG}${match}${HIGHLIGHT_POST_TAG}`);
|
|
|
|
}
|
|
|
|
|
2024-02-28 22:00:12 +01:00
|
|
|
initialize() {
|
2024-02-27 00:08:21 +01:00
|
|
|
// The endpoint for searching parts
|
|
|
|
const base_url = this.element.dataset.autocomplete;
|
|
|
|
// The URL template for the part detail pages
|
|
|
|
const part_detail_uri_template = this.element.dataset.detailUrl;
|
|
|
|
|
2024-02-29 21:53:28 +01:00
|
|
|
//The URL of the placeholder picture
|
|
|
|
const placeholder_image = this.element.dataset.placeholderImage;
|
|
|
|
|
2024-02-29 22:46:19 +01:00
|
|
|
//If the element is in navbar mode, or not
|
|
|
|
const navbar_mode = this.element.dataset.navbarMode === "true";
|
|
|
|
|
2024-02-27 00:28:17 +01:00
|
|
|
const that = this;
|
|
|
|
|
2024-02-27 00:39:38 +01:00
|
|
|
const recentSearchesPlugin = createLocalStorageRecentSearchesPlugin({
|
|
|
|
key: 'RECENT_SEARCH',
|
|
|
|
limit: 5,
|
|
|
|
});
|
|
|
|
|
2024-02-27 00:08:21 +01:00
|
|
|
this._autocomplete = autocomplete({
|
|
|
|
container: this.element,
|
2024-02-29 22:46:19 +01:00
|
|
|
//Place the panel in the navbar, if the element is in navbar mode
|
2024-02-29 23:00:21 +01:00
|
|
|
panelContainer: navbar_mode ? document.getElementById("navbar-search-form") : document.body,
|
2024-02-29 22:46:19 +01:00
|
|
|
panelPlacement: this.element.dataset.panelPlacement,
|
2024-02-27 00:39:38 +01:00
|
|
|
plugins: [recentSearchesPlugin],
|
|
|
|
openOnFocus: true,
|
2024-02-29 20:39:27 +01:00
|
|
|
placeholder: trans(SEARCH_PLACEHOLDER),
|
|
|
|
translations: {
|
|
|
|
submitButtonTitle: trans(SEARCH_SUBMIT)
|
|
|
|
},
|
2024-02-28 22:06:54 +01:00
|
|
|
|
|
|
|
// Use a navigator compatible with turbo:
|
|
|
|
navigator: {
|
|
|
|
navigate({ itemUrl }) {
|
|
|
|
window.Turbo.visit(itemUrl, { action: "advance" });
|
|
|
|
},
|
|
|
|
navigateNewTab({ itemUrl }) {
|
|
|
|
const windowReference = window.open(itemUrl, '_blank', 'noopener');
|
|
|
|
|
|
|
|
if (windowReference) {
|
|
|
|
windowReference.focus();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
navigateNewWindow({ itemUrl }) {
|
|
|
|
window.open(itemUrl, '_blank', 'noopener');
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// If the form is submitted, forward the term to the form
|
2024-02-27 00:28:17 +01:00
|
|
|
onSubmit({state, event, ...setters}) {
|
|
|
|
//Put the current text into each target input field
|
|
|
|
const input = that.inputTarget;
|
|
|
|
|
|
|
|
if (!input) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-29 22:48:07 +01:00
|
|
|
//Do not submit the form, if the input is empty
|
|
|
|
if (state.query === "") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-27 00:28:17 +01:00
|
|
|
input.value = state.query;
|
|
|
|
input.form.requestSubmit();
|
|
|
|
},
|
2024-02-28 22:06:54 +01:00
|
|
|
|
|
|
|
|
2024-02-27 00:08:21 +01:00
|
|
|
getSources({ query }) {
|
|
|
|
return [
|
2024-02-29 22:20:30 +01:00
|
|
|
// The parts source
|
2024-02-27 00:08:21 +01:00
|
|
|
{
|
|
|
|
sourceId: 'parts',
|
|
|
|
getItems() {
|
|
|
|
const url = base_url.replace('__QUERY__', encodeURIComponent(query));
|
|
|
|
|
2024-02-29 22:20:30 +01:00
|
|
|
const data = fetch(url)
|
|
|
|
.then((response) => response.json())
|
|
|
|
;
|
|
|
|
|
|
|
|
//Iterate over all fields besides the id and highlight them
|
|
|
|
const fields = ["name", "description", "category", "footprint"];
|
|
|
|
|
|
|
|
data.then((items) => {
|
|
|
|
items.forEach((item) => {
|
|
|
|
for (const field of fields) {
|
|
|
|
item[field] = that._highlight(item[field], query);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return data;
|
2024-02-27 00:08:21 +01:00
|
|
|
},
|
2024-02-28 22:06:54 +01:00
|
|
|
getItemUrl({ item }) {
|
|
|
|
return part_detail_uri_template.replace('__ID__', item.id);
|
|
|
|
},
|
2024-02-27 00:08:21 +01:00
|
|
|
templates: {
|
2024-02-27 23:02:05 +01:00
|
|
|
header({ html }) {
|
2024-02-29 21:53:28 +01:00
|
|
|
return html`<span class="aa-SourceHeaderTitle">${trans(STATISTICS_PARTS)}</span>
|
2024-02-27 23:02:05 +01:00
|
|
|
<div class="aa-SourceHeaderLine" />`;
|
|
|
|
},
|
2024-02-27 00:08:21 +01:00
|
|
|
item({item, components, html}) {
|
|
|
|
const details_url = part_detail_uri_template.replace('__ID__', item.id);
|
|
|
|
|
|
|
|
return html`
|
|
|
|
<a class="aa-ItemLink" href="${details_url}">
|
|
|
|
<div class="aa-ItemContent">
|
|
|
|
<div class="aa-ItemIcon aa-ItemIcon--picture aa-ItemIcon--alignTop">
|
2024-02-29 21:53:28 +01:00
|
|
|
<img src="${item.image !== "" ? item.image : placeholder_image}" alt="${item.name}" width="30" height="30"/>
|
2024-02-27 00:08:21 +01:00
|
|
|
</div>
|
|
|
|
<div class="aa-ItemContentBody">
|
|
|
|
<div class="aa-ItemContentTitle">
|
2024-02-29 21:53:28 +01:00
|
|
|
<b>
|
|
|
|
${components.Highlight({hit: item, attribute: 'name'})}
|
|
|
|
</b>
|
2024-02-27 00:08:21 +01:00
|
|
|
</div>
|
|
|
|
<div class="aa-ItemContentDescription">
|
2024-02-29 22:20:30 +01:00
|
|
|
${components.Highlight({hit: item, attribute: 'description'})}
|
|
|
|
${item.category ? html`<p class="m-0"><span class="fa-solid fa-tags fa-fw"></span>${components.Highlight({hit: item, attribute: 'category'})}</p>` : ""}
|
|
|
|
${item.footprint ? html`<p class="m-0"><span class="fa-solid fa-microchip fa-fw"></span>${components.Highlight({hit: item, attribute: 'footprint'})}</p>` : ""}
|
2024-02-27 00:08:21 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</a>
|
|
|
|
`;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
|
|
|
},
|
|
|
|
});
|
2024-10-13 23:19:03 +02:00
|
|
|
|
|
|
|
//Try to find the input field and register a defocus handler. This is necessarry, as by default the autocomplete
|
|
|
|
//lib has problems when multiple inputs are present on the page. (see https://github.com/algolia/autocomplete/issues/1216)
|
|
|
|
const inputs = this.element.getElementsByClassName('aa-Input');
|
|
|
|
for (const input of inputs) {
|
|
|
|
input.addEventListener('blur', () => {
|
|
|
|
this._autocomplete.setIsOpen(false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-02-27 00:08:21 +01:00
|
|
|
}
|
|
|
|
}
|