2022-05-31 01:05:00 +02:00
|
|
|
/**
|
|
|
|
* EGroupware eTemplate2 - SearchMixin
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package api
|
|
|
|
* @link https://www.egroupware.org
|
|
|
|
* @author Nathan Gray
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
import {css, dedupeMixin, html, LitElement, render, SlotMixin} from "@lion/core";
|
|
|
|
|
|
|
|
|
|
|
|
// Export the Interface for TypeScript
|
|
|
|
type Constructor<T = {}> = new (...args : any[]) => T;
|
|
|
|
|
|
|
|
export declare class SearchMixinInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Enable searching on options
|
|
|
|
*/
|
|
|
|
search : boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get [additional] options from the server when you search, instead of just searching existing options
|
|
|
|
*/
|
|
|
|
searchUrl : string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allow adding new options that are not in the search results
|
|
|
|
*/
|
|
|
|
allowFreeEntries : boolean;
|
|
|
|
|
2022-05-31 21:40:31 +02:00
|
|
|
/**
|
|
|
|
* Additional search options passed to the search functions
|
|
|
|
*
|
|
|
|
* @type {object}
|
|
|
|
*/
|
|
|
|
searchOptions : object;
|
2022-05-31 01:05:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Start the search process
|
|
|
|
*/
|
|
|
|
startSearch() : void
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search local options
|
|
|
|
*/
|
2022-05-31 21:40:31 +02:00
|
|
|
localSearch(search : string, options : object) : Promise<void>
|
2022-05-31 01:05:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Search remote options.
|
|
|
|
* If searchUrl is not set, it will return very quickly with no results
|
|
|
|
*/
|
2022-05-31 21:40:31 +02:00
|
|
|
remoteSearch(search : string, options : object) : Promise<void>
|
2022-05-31 01:05:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check a [local] item to see if it matches
|
|
|
|
*/
|
2022-05-31 21:40:31 +02:00
|
|
|
searchMatch(search : string, options : object, item : LitElement) : boolean
|
2022-05-31 01:05:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for things that do search type behaviour
|
|
|
|
* Separated to keep things a little simpler.
|
|
|
|
*
|
|
|
|
* Currently I assume we're extending an Et2Select, so changes may need to be made for better abstraction
|
|
|
|
*/
|
|
|
|
export const Et2WithSearchMixin = dedupeMixin((superclass) =>
|
|
|
|
{
|
|
|
|
class Et2WidgetWithSearch extends SlotMixin(superclass)
|
|
|
|
{
|
|
|
|
static get properties()
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
...super.properties,
|
|
|
|
search: {type: Boolean, reflect: true},
|
|
|
|
|
|
|
|
searchUrl: {type: String},
|
|
|
|
|
2022-05-31 21:40:31 +02:00
|
|
|
allowFreeEntries: {type: Boolean},
|
|
|
|
|
|
|
|
searchOptions: {type: Object}
|
2022-05-31 01:05:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get slots()
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
...super.slots,
|
|
|
|
suffix: () =>
|
|
|
|
{
|
|
|
|
const input = document.createElement("sl-icon");
|
|
|
|
input.name = "search";
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static get styles()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
// @ts-ignore
|
|
|
|
...(super.styles ? (Symbol.iterator in Object(super.styles) ? super.styles : [super.styles]) : []),
|
|
|
|
css`
|
|
|
|
::slotted([slot="suffix"]) {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
:host([search]) ::slotted([slot="suffix"]) {
|
|
|
|
display: initial;
|
|
|
|
}
|
|
|
|
::slotted([name="search_input"]:focus ){
|
|
|
|
width: 100%;
|
|
|
|
}
|
|
|
|
.select__prefix ::slotted(.search_input) {
|
|
|
|
display: none;
|
|
|
|
margin-left: 0px;
|
|
|
|
width: 100%;
|
|
|
|
}
|
|
|
|
::slotted(.search_input.active) {
|
2022-05-31 21:40:31 +02:00
|
|
|
display: flex;
|
2022-05-31 01:05:00 +02:00
|
|
|
}
|
|
|
|
::slotted(.no-match) {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
`
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
private _searchTimeout : number;
|
2022-05-31 21:40:31 +02:00
|
|
|
protected static SEARCH_TIMEOUT = 500;
|
2022-05-31 01:05:00 +02:00
|
|
|
protected static MIN_CHARS = 2;
|
|
|
|
|
|
|
|
constructor(...args : any[])
|
|
|
|
{
|
|
|
|
super(...args);
|
|
|
|
|
2022-05-31 21:40:31 +02:00
|
|
|
this.search = false;
|
|
|
|
this.searchUrl = "";
|
|
|
|
this.searchOptions = {};
|
|
|
|
|
2022-05-31 01:05:00 +02:00
|
|
|
this._handleSearchButtonClick = this._handleSearchButtonClick.bind(this);
|
|
|
|
this._handleSearchAbort = this._handleSearchAbort.bind(this);
|
|
|
|
this._handleSearchKeyDown = this._handleSearchKeyDown.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
connectedCallback()
|
|
|
|
{
|
|
|
|
super.connectedCallback();
|
|
|
|
|
|
|
|
// Missing any of the required attributes? Don't change anything.
|
|
|
|
if(!this.search && !this.searchUrl && !this.allowFreeEntries)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._addNodes();
|
|
|
|
this._bindListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnectedCallback()
|
|
|
|
{
|
|
|
|
super.disconnectedCallback();
|
|
|
|
this._unbindListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add the nodes we need to search
|
|
|
|
*
|
|
|
|
* @protected
|
|
|
|
*
|
|
|
|
* NB: Not sure which is the best way yet, SlotMixin or using render()
|
|
|
|
*/
|
|
|
|
protected _addNodes()
|
|
|
|
{
|
|
|
|
const div = document.createElement("div");
|
|
|
|
div.classList.add("search_input");
|
|
|
|
div.slot = "prefix";
|
|
|
|
render(this._searchInputTemplate(), div);
|
|
|
|
this.appendChild(div);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected _searchInputTemplate()
|
|
|
|
{
|
|
|
|
return html`
|
|
|
|
<input type="text" @keydown=${this._handleSearchKeyDown}/>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-31 21:40:31 +02:00
|
|
|
/**
|
|
|
|
* Do we have the needed properties set, so we can actually do searching
|
|
|
|
*
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
public get searchEnabled() : boolean
|
|
|
|
{
|
|
|
|
return this.search || this.searchUrl.length > 0;
|
|
|
|
}
|
|
|
|
|
2022-05-31 01:05:00 +02:00
|
|
|
protected get _searchButtonNode()
|
|
|
|
{
|
|
|
|
return this.querySelector("sl-icon[slot='suffix']");
|
|
|
|
}
|
|
|
|
|
|
|
|
protected get _searchInputNode()
|
|
|
|
{
|
|
|
|
return this.querySelector(".search_input input");
|
|
|
|
}
|
|
|
|
|
|
|
|
protected get _activeControls()
|
|
|
|
{
|
|
|
|
return this.querySelector(".search_input");
|
|
|
|
}
|
|
|
|
|
|
|
|
protected get menuItems()
|
|
|
|
{
|
|
|
|
return this.querySelectorAll("sl-menu-item");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Only local options, excludes server options
|
|
|
|
*
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected get localItems()
|
|
|
|
{
|
|
|
|
return this.querySelectorAll("sl-menu-item:not(.remote)");
|
|
|
|
}
|
|
|
|
|
|
|
|
protected get remoteItems()
|
|
|
|
{
|
|
|
|
return this.querySelectorAll("sl-menu-item.remote");
|
|
|
|
}
|
|
|
|
|
|
|
|
getItems()
|
|
|
|
{
|
|
|
|
return [...this.querySelectorAll("sl-menu-item:not(.no-match)")];
|
|
|
|
}
|
|
|
|
|
|
|
|
protected _bindListeners()
|
|
|
|
{
|
|
|
|
this.addEventListener("sl-blur", this._handleSearchAbort);
|
|
|
|
this._searchButtonNode.addEventListener("click", this._handleSearchButtonClick);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected _unbindListeners()
|
|
|
|
{
|
|
|
|
this.removeEventListener("sl-blur", this._handleSearchAbort);
|
|
|
|
this._searchButtonNode.removeEventListener("click", this._handleSearchButtonClick);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleMenuShow()
|
|
|
|
{
|
|
|
|
super.handleMenuShow();
|
|
|
|
|
2022-05-31 21:40:31 +02:00
|
|
|
if(this.searchEnabled)
|
|
|
|
{
|
|
|
|
this._activeControls?.classList.add("active");
|
|
|
|
this._searchInputNode.focus();
|
|
|
|
this._searchInputNode.select();
|
|
|
|
}
|
2022-05-31 01:05:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
handleMenuHide()
|
|
|
|
{
|
|
|
|
super.handleMenuHide();
|
2022-05-31 21:40:31 +02:00
|
|
|
if(this.searchEnabled)
|
|
|
|
{
|
|
|
|
this._activeControls?.classList.remove("active");
|
|
|
|
}
|
2022-05-31 01:05:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle keypresses inside the search input
|
|
|
|
* @param {KeyboardEvent} event
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected _handleSearchKeyDown(event : KeyboardEvent)
|
|
|
|
{
|
2022-05-31 21:40:31 +02:00
|
|
|
this._activeControls?.classList.add("active");
|
2022-05-31 01:05:00 +02:00
|
|
|
this.dropdown.show();
|
|
|
|
|
|
|
|
// Pass off some keys to select
|
|
|
|
if(['ArrowDown', 'ArrowUp', 'Home', 'End'].includes(event.key))
|
|
|
|
{
|
|
|
|
return this.handleKeyDown(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't allow event to bubble or it will interact with select
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
if(event.key === "Enter")
|
|
|
|
{
|
|
|
|
// If there's only one option, select it
|
|
|
|
if(this.getItems().length === 1)
|
|
|
|
{
|
|
|
|
this.getItems()[0].click();
|
2022-06-01 16:05:34 +02:00
|
|
|
// not jQuery this.hide();
|
2022-05-31 01:05:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
this.startSearch();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the search automatically if they have enough letters
|
|
|
|
clearTimeout(this._searchTimeout);
|
|
|
|
if(this._searchInputNode.value.length >= Et2WidgetWithSearch.MIN_CHARS)
|
|
|
|
{
|
2022-05-31 21:40:31 +02:00
|
|
|
this._searchTimeout = window.setTimeout(() => {this.startSearch()}, Et2WidgetWithSearch.SEARCH_TIMEOUT);
|
2022-05-31 01:05:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start searching
|
|
|
|
*
|
|
|
|
* If we have local options, we'll search & display any matches.
|
|
|
|
* If serverUrl is set, we'll ask the server for results as well.
|
|
|
|
*/
|
|
|
|
public startSearch()
|
|
|
|
{
|
|
|
|
// Show a spinner instead of search button
|
|
|
|
this._searchButtonNode.style.display = "hidden";
|
|
|
|
let spinner = document.createElement("sl-spinner");
|
|
|
|
spinner.slot = this._searchButtonNode.slot;
|
|
|
|
this.appendChild(spinner);
|
|
|
|
|
|
|
|
// Start the searches
|
|
|
|
Promise.all([
|
2022-05-31 21:40:31 +02:00
|
|
|
this.localSearch(this._searchInputNode.value, this.searchOptions),
|
|
|
|
this.remoteSearch(this._searchInputNode.value, this.searchOptions)
|
2022-05-31 01:05:00 +02:00
|
|
|
]).then(() =>
|
|
|
|
{
|
|
|
|
spinner.remove();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter the local options
|
|
|
|
*
|
|
|
|
* @param {string} search
|
|
|
|
* @protected
|
|
|
|
*/
|
2022-05-31 21:40:31 +02:00
|
|
|
protected localSearch(search : string, options : object) : Promise<void>
|
2022-05-31 01:05:00 +02:00
|
|
|
{
|
|
|
|
return new Promise((resolve) =>
|
|
|
|
{
|
|
|
|
this.localItems.forEach((item) =>
|
|
|
|
{
|
|
|
|
let match = this.searchMatch(search, item);
|
|
|
|
item.classList.toggle("match", match);
|
|
|
|
// set disabled so arrow keys step over. Might be a better way to handle that
|
|
|
|
item.disabled = !match;
|
|
|
|
item.classList.toggle("no-match", !match);
|
|
|
|
})
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ask for remote options and add them in unconditionally
|
|
|
|
* @param {string} search
|
|
|
|
* @protected
|
|
|
|
*/
|
2022-05-31 21:40:31 +02:00
|
|
|
protected remoteSearch(search : string, options : object)
|
2022-05-31 01:05:00 +02:00
|
|
|
{
|
|
|
|
// Remove existing remote items
|
|
|
|
this.remoteItems.forEach(i => i.remove());
|
|
|
|
|
|
|
|
if(!this.searchUrl)
|
|
|
|
{
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fire off the query
|
2022-05-31 21:40:31 +02:00
|
|
|
let promise = this.remoteQuery(search, options);
|
2022-05-31 01:05:00 +02:00
|
|
|
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
2022-05-31 21:40:31 +02:00
|
|
|
protected remoteQuery(search : string, options : object)
|
2022-05-31 01:05:00 +02:00
|
|
|
{
|
2022-06-01 16:05:34 +02:00
|
|
|
return this.egw().request(this.searchUrl, [search]).sendRequest().then((result) =>
|
2022-05-31 01:05:00 +02:00
|
|
|
{
|
|
|
|
this.processRemoteResults(result);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add in remote results
|
|
|
|
* @param results
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected processRemoteResults(results)
|
|
|
|
{
|
2022-06-01 16:05:34 +02:00
|
|
|
this.select_options = results;
|
2022-05-31 01:05:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if one of our [local] items matches the search
|
|
|
|
*
|
|
|
|
* @param search
|
|
|
|
* @param item
|
|
|
|
* @returns {boolean}
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected searchMatch(search, item) : boolean
|
|
|
|
{
|
|
|
|
if(!item || !item.value)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(item.textContent?.toLowerCase().includes(search.toLowerCase()))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if(typeof item.value == "string")
|
|
|
|
{
|
|
|
|
return item.value.includes(search.toLowerCase());
|
|
|
|
}
|
|
|
|
return item.value == search;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected _handleSearchButtonClick(e)
|
|
|
|
{
|
|
|
|
this.handleMenuShow();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected _handleSearchAbort(e)
|
|
|
|
{
|
|
|
|
this._activeControls.classList.remove("active");
|
|
|
|
this._searchInputNode.value = "";
|
|
|
|
|
|
|
|
// Reset options. It might be faster to re-create instead.
|
|
|
|
this.menuItems.forEach((item) =>
|
|
|
|
{
|
|
|
|
item.disabled = false;
|
|
|
|
item.classList.remove("match");
|
|
|
|
item.classList.remove("no-match");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Et2WidgetWithSearch as Constructor<SearchMixinInterface> & T & LitElement;
|
2022-06-01 16:05:34 +02:00
|
|
|
});
|