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
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2022-09-15 16:30:50 +02:00
|
|
|
import {css, html, LitElement, render, SlotMixin} from "@lion/core";
|
2022-06-01 17:23:07 +02:00
|
|
|
import {cleanSelectOptions, SelectOption} from "./FindSelectOptions";
|
2022-06-10 18:11:34 +02:00
|
|
|
import {Validator} from "@lion/form-core";
|
|
|
|
import {Et2Tag} from "./Tag/Et2Tag";
|
2022-12-05 21:56:31 +01:00
|
|
|
import {SlMenuItem} from "@shoelace-style/shoelace";
|
2023-01-24 01:33:22 +01:00
|
|
|
import {waitForEvent} from "@shoelace-style/shoelace/dist/internal/event";
|
2023-07-22 00:39:47 +02:00
|
|
|
import {StaticOptions} from "./StaticOptions";
|
2022-05-31 01:05:00 +02:00
|
|
|
|
2022-06-10 18:11:34 +02:00
|
|
|
// Otherwise import gets stripped
|
|
|
|
let keep_import : Et2Tag;
|
2022-05-31 01:05:00 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
|
*/
|
2022-06-10 18:11:34 +02:00
|
|
|
export const Et2WithSearchMixin = <T extends Constructor<LitElement>>(superclass : T) =>
|
2022-05-31 01:05:00 +02:00
|
|
|
{
|
|
|
|
class Et2WidgetWithSearch extends SlotMixin(superclass)
|
|
|
|
{
|
|
|
|
static get properties()
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
...super.properties,
|
|
|
|
search: {type: Boolean, reflect: true},
|
|
|
|
|
|
|
|
searchUrl: {type: String},
|
|
|
|
|
2022-06-10 18:11:34 +02:00
|
|
|
/**
|
|
|
|
* Allow custom entries that are not in the options
|
|
|
|
*/
|
|
|
|
allowFreeEntries: {type: Boolean, reflect: true},
|
2022-05-31 21:40:31 +02:00
|
|
|
|
2022-06-22 22:28:37 +02:00
|
|
|
/**
|
|
|
|
* Additional search parameters that are passed to the server
|
|
|
|
* when we query searchUrl
|
|
|
|
*/
|
2022-06-10 18:11:34 +02:00
|
|
|
searchOptions: {type: Object},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allow editing tags by clicking on them.
|
|
|
|
* allowFreeEntries must be true
|
|
|
|
*/
|
|
|
|
editModeEnabled: {type: Boolean}
|
2022-05-31 01:05:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static get styles()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
// @ts-ignore
|
|
|
|
...(super.styles ? (Symbol.iterator in Object(super.styles) ? super.styles : [super.styles]) : []),
|
|
|
|
css`
|
2022-07-22 18:43:30 +02:00
|
|
|
/* Move the widget border
|
2022-06-14 01:21:35 +02:00
|
|
|
.form-control-input {
|
|
|
|
border: solid var(--sl-input-border-width) var(--sl-input-border-color);
|
|
|
|
border-radius: var(--sl-input-border-radius-medium);
|
|
|
|
}
|
2022-07-22 18:43:30 +02:00
|
|
|
.form-control-input:hover {
|
|
|
|
background-color: var(--sl-input-background-color-hover);
|
|
|
|
border-color: var(--sl-input-border-color-hover);
|
|
|
|
color: var(--sl-input-color-hover);
|
|
|
|
}
|
2022-06-14 01:21:35 +02:00
|
|
|
.select--standard .select__control {
|
|
|
|
border-style: none;
|
|
|
|
}
|
|
|
|
/* Move focus highlight */
|
|
|
|
.form-control-input:focus-within {
|
|
|
|
box-shadow: var(--sl-focus-ring);
|
|
|
|
}
|
|
|
|
.select--standard.select--focused:not(.select--disabled) .select__control {
|
|
|
|
box-shadow: initial;
|
|
|
|
}
|
2022-06-16 19:02:16 +02:00
|
|
|
/* Show / hide SlSelect icons - dropdown arrow, etc but not loading spinner */
|
|
|
|
:host([allowFreeEntries]) ::slotted(sl-icon[slot="suffix"]) {
|
2022-06-10 18:11:34 +02:00
|
|
|
display: none;
|
|
|
|
}
|
2022-06-13 17:58:46 +02:00
|
|
|
/* Make search textbox take full width */
|
2022-06-14 01:21:35 +02:00
|
|
|
::slotted(.search_input), ::slotted(.search_input) input, .search_input, .search_input input {
|
|
|
|
width: 100%;
|
|
|
|
}
|
|
|
|
.search_input input {
|
|
|
|
flex: 1 1 auto;
|
2022-05-31 01:05:00 +02:00
|
|
|
width: 100%;
|
|
|
|
}
|
2022-08-31 21:27:10 +02:00
|
|
|
/* Full width search textbox covers loading spinner, lift it up */
|
|
|
|
::slotted(sl-spinner) {
|
|
|
|
z-index: 2;
|
|
|
|
}
|
2022-06-14 17:09:11 +02:00
|
|
|
/* Don't show the current value while searching for single, we want the space
|
|
|
|
This lets the current value shrink to nothing so the input can expand
|
|
|
|
*/
|
|
|
|
.select__label {
|
|
|
|
flex: 1 15 auto;
|
|
|
|
}
|
2022-06-14 01:21:35 +02:00
|
|
|
/* Show edit textbox only when editing */
|
|
|
|
.search_input #edit {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
.search_input.editing #search {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
.search_input.editing #edit {
|
|
|
|
display: initial;
|
|
|
|
}
|
|
|
|
:host([search]:not([multiple])) .select--open .select__prefix {
|
2022-06-02 23:44:13 +02:00
|
|
|
flex: 2 1 auto;
|
|
|
|
width: 100%;
|
|
|
|
}
|
2022-07-20 23:03:15 +02:00
|
|
|
:host([search]:not([multiple])) .select--open .select__label {
|
|
|
|
margin: 0px;
|
|
|
|
}
|
2022-06-22 22:28:37 +02:00
|
|
|
:host([allowfreeentries]:not([multiple])) .select--standard.select--open:not(.select--disabled) .select__control .select__prefix {
|
|
|
|
flex: 1 1 auto;
|
|
|
|
}
|
|
|
|
:host([allowfreeentries]:not([multiple])) .select--standard.select--open:not(.select--disabled) .select__control .select__label {
|
|
|
|
display: none;
|
|
|
|
}
|
2022-06-10 18:11:34 +02:00
|
|
|
|
|
|
|
/* Search textbox general styling, starts hidden */
|
2022-06-14 01:21:35 +02:00
|
|
|
.select__prefix ::slotted(.search_input),.search_input {
|
2022-05-31 01:05:00 +02:00
|
|
|
display: none;
|
2022-06-14 01:21:35 +02:00
|
|
|
flex: 1 1 auto;
|
2022-05-31 01:05:00 +02:00
|
|
|
margin-left: 0px;
|
|
|
|
width: 100%;
|
2022-06-14 01:21:35 +02:00
|
|
|
height: var(--sl-input-height-medium);
|
2022-08-04 23:50:52 +02:00
|
|
|
position: absolute;
|
2022-06-14 01:21:35 +02:00
|
|
|
background-color: white;
|
2022-08-04 23:50:52 +02:00
|
|
|
z-index: 1;
|
2022-05-31 01:05:00 +02:00
|
|
|
}
|
2022-06-10 18:11:34 +02:00
|
|
|
/* Search UI active - show textbox & stuff */
|
2022-06-14 01:21:35 +02:00
|
|
|
::slotted(.search_input.active),.search_input.active,
|
|
|
|
.search_input.editing{
|
2022-05-31 21:40:31 +02:00
|
|
|
display: flex;
|
2022-05-31 01:05:00 +02:00
|
|
|
}
|
2022-09-01 19:53:19 +02:00
|
|
|
/* If multiple and no value, overlap search onto widget instead of below */
|
|
|
|
:host([multiple]) .search_input.active.novalue {
|
|
|
|
top: 0px;
|
|
|
|
}
|
2022-06-10 18:11:34 +02:00
|
|
|
|
|
|
|
/* Hide options that do not match current search text */
|
2022-05-31 01:05:00 +02:00
|
|
|
::slotted(.no-match) {
|
|
|
|
display: none;
|
|
|
|
}
|
2022-06-10 22:11:57 +02:00
|
|
|
/* Different cursor for editable tags */
|
2022-06-17 22:06:12 +02:00
|
|
|
:host([allowfreeentries]):not([readonly]) .search_tag::part(base) {
|
2022-06-10 22:11:57 +02:00
|
|
|
cursor: text;
|
|
|
|
}
|
2022-06-17 22:06:12 +02:00
|
|
|
|
|
|
|
/** Readonly **/
|
|
|
|
/* No border */
|
|
|
|
:host([readonly]) .form-control-input {
|
|
|
|
border: none;
|
|
|
|
}
|
|
|
|
/* disable focus border */
|
|
|
|
:host([readonly]) .form-control-input:focus-within {
|
|
|
|
box-shadow: none;
|
|
|
|
}
|
|
|
|
/* no menu */
|
|
|
|
:host([readonly]) sl-menu {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
/* normal cursor */
|
|
|
|
:host([readonly]) .select__control {
|
|
|
|
cursor: initial;
|
|
|
|
}
|
2022-05-31 01:05:00 +02:00
|
|
|
`
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2022-06-10 18:11:34 +02:00
|
|
|
// Borrowed from Lion ValidatorMixin, but we don't want the whole thing
|
|
|
|
protected defaultValidators : Validator[];
|
|
|
|
protected validators : Validator[];
|
|
|
|
|
2022-05-31 01:05:00 +02:00
|
|
|
private _searchTimeout : number;
|
2022-10-04 23:49:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* When user is typing, we wait this long for them to be finished before we start the search
|
|
|
|
* @type {number}
|
|
|
|
* @protected
|
|
|
|
*/
|
2022-05-31 21:40:31 +02:00
|
|
|
protected static SEARCH_TIMEOUT = 500;
|
2022-10-04 23:49:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* We need at least this many characters before we start the search
|
|
|
|
*
|
|
|
|
* @type {number}
|
|
|
|
* @protected
|
|
|
|
*/
|
2022-05-31 01:05:00 +02:00
|
|
|
protected static MIN_CHARS = 2;
|
2022-10-04 23:49:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Limit server searches to 100 results, matches Link::DEFAULT_NUM_ROWS
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
static RESULT_LIMIT : number = 100;
|
|
|
|
|
|
|
|
|
2022-06-16 19:02:16 +02:00
|
|
|
// Hold the original option data from earlier search results, since we discard on subsequent search
|
|
|
|
private _selected_remote = <SelectOption[]>[];
|
|
|
|
|
2022-06-14 01:21:35 +02:00
|
|
|
/**
|
|
|
|
* These characters will end a free tag
|
|
|
|
* @type {string[]}
|
|
|
|
*/
|
|
|
|
static TAG_BREAK : string[] = ["Tab", "Enter", ","];
|
2022-05-31 01:05:00 +02:00
|
|
|
|
|
|
|
constructor(...args : any[])
|
|
|
|
{
|
|
|
|
super(...args);
|
|
|
|
|
2022-05-31 21:40:31 +02:00
|
|
|
this.search = false;
|
|
|
|
this.searchUrl = "";
|
2022-08-08 17:27:21 +02:00
|
|
|
this.searchOptions = {app: "addressbook"};
|
2022-05-31 21:40:31 +02:00
|
|
|
|
2022-06-10 18:11:34 +02:00
|
|
|
this.allowFreeEntries = false;
|
|
|
|
this.editModeEnabled = false;
|
|
|
|
|
2022-06-14 01:21:35 +02:00
|
|
|
// Hiding the selected options from the dropdown means we can't un-select the tags
|
|
|
|
// hidden by the max limit. Prefer no limit.
|
|
|
|
this.maxTagsVisible = -1;
|
|
|
|
|
2022-06-10 18:11:34 +02:00
|
|
|
this.validators = [];
|
|
|
|
/**
|
|
|
|
* Used by Subclassers to add default Validators.
|
|
|
|
* A email input for instance, always needs the isEmail validator.
|
|
|
|
* @example
|
|
|
|
* ```js
|
|
|
|
* this.defaultValidators.push(new IsDate());
|
|
|
|
* ```
|
|
|
|
* @type {Validator[]}
|
|
|
|
*/
|
|
|
|
this.defaultValidators = [];
|
|
|
|
|
2022-09-15 16:30:50 +02:00
|
|
|
this.handleMenuSelect = this.handleMenuSelect.bind(this);
|
2022-06-23 18:36:54 +02:00
|
|
|
this._handleChange = this._handleChange.bind(this);
|
2023-01-24 01:33:22 +01:00
|
|
|
this.handleTagEdit = this.handleTagEdit.bind(this);
|
2022-09-20 17:43:25 +02:00
|
|
|
this._handleAfterShow = this._handleAfterShow.bind(this);
|
2022-09-13 17:06:59 +02:00
|
|
|
this._handleSearchBlur = this._handleSearchBlur.bind(this);
|
2022-06-14 18:06:27 +02:00
|
|
|
this._handleClear = this._handleClear.bind(this);
|
2022-06-14 01:21:35 +02:00
|
|
|
this._handleDoubleClick = this._handleDoubleClick.bind(this);
|
2022-05-31 01:05:00 +02:00
|
|
|
this._handleSearchAbort = this._handleSearchAbort.bind(this);
|
2022-08-31 21:27:10 +02:00
|
|
|
this._handleSearchChange = this._handleSearchChange.bind(this);
|
2022-05-31 01:05:00 +02:00
|
|
|
this._handleSearchKeyDown = this._handleSearchKeyDown.bind(this);
|
2022-06-14 01:21:35 +02:00
|
|
|
this._handleEditKeyDown = this._handleEditKeyDown.bind(this);
|
2023-01-31 17:47:46 +01:00
|
|
|
this._handlePaste = this._handlePaste.bind(this);
|
2022-05-31 01:05:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
connectedCallback()
|
|
|
|
{
|
|
|
|
super.connectedCallback();
|
|
|
|
|
2022-06-02 23:44:13 +02:00
|
|
|
this.classList.toggle("search", this.searchEnabled);
|
|
|
|
|
2022-05-31 01:05:00 +02:00
|
|
|
// Missing any of the required attributes? Don't change anything.
|
2022-06-17 22:06:12 +02:00
|
|
|
// If readonly, skip it
|
2022-07-26 17:15:05 +02:00
|
|
|
if(!this.searchEnabled && !this.editModeEnabled && !this.allowFreeEntries || this.readonly)
|
2022-05-31 01:05:00 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._addNodes();
|
|
|
|
this._bindListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnectedCallback()
|
|
|
|
{
|
|
|
|
super.disconnectedCallback();
|
|
|
|
this._unbindListeners();
|
|
|
|
}
|
|
|
|
|
2022-06-10 22:11:57 +02:00
|
|
|
willUpdate(changedProperties)
|
|
|
|
{
|
|
|
|
super.willUpdate(changedProperties);
|
|
|
|
|
2022-08-19 19:07:16 +02:00
|
|
|
// Turn on search if there's more than 20 options
|
|
|
|
if(changedProperties.has("select_options") && this.select_options.length > 20)
|
|
|
|
{
|
|
|
|
this.search = true;
|
|
|
|
}
|
|
|
|
|
2022-06-10 22:11:57 +02:00
|
|
|
// If searchURL is set, turn on search
|
|
|
|
if(changedProperties.has("searchUrl") && this.searchUrl)
|
|
|
|
{
|
|
|
|
this.search = true;
|
2022-09-08 19:17:34 +02:00
|
|
|
// Decode URL, possibly again. If set in template, it can wind up double-encoded.
|
|
|
|
this.searchUrl = this.egw().decodePath(this.searchUrl);
|
2022-06-10 22:11:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-19 19:07:16 +02:00
|
|
|
update(changedProperties)
|
|
|
|
{
|
|
|
|
super.update(changedProperties);
|
|
|
|
|
|
|
|
// One of the key properties has changed, need to add the needed nodes
|
|
|
|
if(changedProperties.has("search") || changedProperties.has("editModeEnabled") || changedProperties.has("allowFreeEntries"))
|
|
|
|
{
|
|
|
|
// Missing any of the required attributes? Now we need to take it out.
|
|
|
|
if(!this.searchEnabled && !this.editModeEnabled && !this.allowFreeEntries || this.readonly)
|
|
|
|
{
|
|
|
|
this.querySelector(".search_input")?.remove();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normally this should be handled in render(), but we have to add our nodes in
|
|
|
|
this._addNodes();
|
|
|
|
}
|
2023-01-24 01:33:22 +01:00
|
|
|
// Update any tags if edit mode changes
|
2023-02-03 22:08:27 +01:00
|
|
|
if(changedProperties.has("editModeEnabled") || changedProperties.has("readonly"))
|
2023-01-24 01:33:22 +01:00
|
|
|
{
|
2023-02-03 22:08:27 +01:00
|
|
|
// Required because we explicitly create tags instead of doing it in render()
|
|
|
|
this.shadowRoot.querySelectorAll(".select__tags > *").forEach((tag : Et2Tag) =>
|
|
|
|
{
|
|
|
|
tag.editable = this.editModeEnabled && !this.readonly;
|
|
|
|
tag.removable = !this.readonly;
|
|
|
|
});
|
2023-01-24 01:33:22 +01:00
|
|
|
}
|
2022-08-19 19:07:16 +02:00
|
|
|
}
|
|
|
|
|
2022-05-31 01:05:00 +02:00
|
|
|
/**
|
2022-06-14 01:21:35 +02:00
|
|
|
* Add the nodes we need to search - adjust parent shadowDOM
|
2022-05-31 01:05:00 +02:00
|
|
|
*
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected _addNodes()
|
|
|
|
{
|
2022-09-22 01:13:29 +02:00
|
|
|
if(this._activeControls)
|
|
|
|
{
|
|
|
|
// Already there
|
2023-04-27 22:01:59 +02:00
|
|
|
return;
|
2022-09-22 01:13:29 +02:00
|
|
|
}
|
|
|
|
|
2022-05-31 01:05:00 +02:00
|
|
|
const div = document.createElement("div");
|
|
|
|
div.classList.add("search_input");
|
|
|
|
render(this._searchInputTemplate(), div);
|
2022-06-14 01:21:35 +02:00
|
|
|
if(!super.multiple)
|
|
|
|
{
|
|
|
|
div.slot = "prefix";
|
|
|
|
this.appendChild(div);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
super.updateComplete.then(() =>
|
|
|
|
{
|
|
|
|
let control = this.shadowRoot.querySelector(".form-control-input");
|
|
|
|
control.append(div);
|
|
|
|
});
|
2022-05-31 01:05:00 +02:00
|
|
|
}
|
|
|
|
|
2023-01-24 01:33:22 +01:00
|
|
|
/**
|
|
|
|
* Customise how tags are rendered.
|
|
|
|
* Override to add edit
|
|
|
|
*
|
|
|
|
* @param item
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected _createTagNode(item)
|
|
|
|
{
|
|
|
|
let tag = <Et2Tag>document.createElement(this.tagTag);
|
2023-02-03 22:08:27 +01:00
|
|
|
tag.editable = this.editModeEnabled && !this.readonly;
|
2023-01-24 01:33:22 +01:00
|
|
|
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
2022-05-31 01:05:00 +02:00
|
|
|
protected _searchInputTemplate()
|
|
|
|
{
|
2022-06-16 00:43:39 +02:00
|
|
|
let edit = null;
|
2022-06-14 01:21:35 +02:00
|
|
|
if(this.editModeEnabled)
|
|
|
|
{
|
2023-02-13 22:57:18 +01:00
|
|
|
edit = html`<input id="edit" type="text" part="input" autocomplete="off" style="width:100%"
|
2022-06-14 01:21:35 +02:00
|
|
|
@keydown=${this._handleEditKeyDown}
|
2022-09-22 01:13:29 +02:00
|
|
|
@click=${(e) => e.stopPropagation()}
|
2022-06-14 01:21:35 +02:00
|
|
|
@blur=${this.stopEdit.bind(this)}
|
|
|
|
/>`;
|
|
|
|
}
|
2022-06-02 23:44:13 +02:00
|
|
|
// I can't figure out how to get this full width via CSS
|
2022-05-31 01:05:00 +02:00
|
|
|
return html`
|
2023-02-08 00:24:06 +01:00
|
|
|
<et2-textbox id="search" type="text" part="input" clearable
|
2023-02-13 22:57:18 +01:00
|
|
|
autocomplete="off"
|
2023-02-08 00:24:06 +01:00
|
|
|
placeholder="${this.egw().lang("search")}"
|
2022-08-17 00:46:20 +02:00
|
|
|
style="width:100%"
|
|
|
|
@keydown=${this._handleSearchKeyDown}
|
2022-09-13 17:06:59 +02:00
|
|
|
@blur=${this._handleSearchBlur}
|
2023-02-08 00:24:06 +01:00
|
|
|
></et2-textbox>
|
2022-06-14 01:21:35 +02:00
|
|
|
${edit}
|
2022-05-31 01:05:00 +02:00
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2022-09-16 19:17:06 +02:00
|
|
|
protected _noResultsTemplate()
|
|
|
|
{
|
|
|
|
return html`
|
|
|
|
<div class="no-results">${this.egw().lang("no suggestions")}</div>`;
|
|
|
|
}
|
2022-05-31 01:05:00 +02:00
|
|
|
|
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
|
|
|
|
{
|
2022-06-17 22:06:12 +02:00
|
|
|
return !this.readonly && (this.search || this.searchUrl.length > 0);
|
2022-05-31 21:40:31 +02:00
|
|
|
}
|
|
|
|
|
2022-06-23 18:36:54 +02:00
|
|
|
protected get _searchInputNode() : HTMLInputElement
|
2022-05-31 01:05:00 +02:00
|
|
|
{
|
2022-08-31 21:27:10 +02:00
|
|
|
return this._activeControls?.querySelector("#search");
|
2022-06-14 01:21:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected get _editInputNode() : HTMLInputElement
|
|
|
|
{
|
2022-08-31 21:27:10 +02:00
|
|
|
return this._activeControls?.querySelector("input#edit");
|
2022-05-31 01:05:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected get _activeControls()
|
|
|
|
{
|
2022-09-13 10:17:59 +02:00
|
|
|
return this.shadowRoot?.querySelector(".search_input") ||
|
2022-06-22 22:28:37 +02:00
|
|
|
this.querySelector(".search_input");
|
2022-05-31 01:05:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Only local options, excludes server options
|
|
|
|
*
|
|
|
|
* @protected
|
|
|
|
*/
|
2022-06-16 19:02:16 +02:00
|
|
|
protected get localItems() : NodeList
|
2022-05-31 01:05:00 +02:00
|
|
|
{
|
2022-06-10 22:11:57 +02:00
|
|
|
return this.querySelectorAll(this.optionTag + ":not(.remote)");
|
2022-05-31 01:05:00 +02:00
|
|
|
}
|
|
|
|
|
2022-09-22 19:15:25 +02:00
|
|
|
/**
|
|
|
|
* Only remote options from search results
|
|
|
|
* @returns {NodeList}
|
|
|
|
* @protected
|
|
|
|
*/
|
2022-06-16 19:02:16 +02:00
|
|
|
protected get remoteItems() : NodeList
|
2022-05-31 01:05:00 +02:00
|
|
|
{
|
2022-06-10 22:11:57 +02:00
|
|
|
return this.querySelectorAll(this.optionTag + ".remote");
|
2022-05-31 01:05:00 +02:00
|
|
|
}
|
|
|
|
|
2022-09-22 19:15:25 +02:00
|
|
|
/**
|
|
|
|
* Only free entries
|
|
|
|
* @returns {NodeList}
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected get freeEntries() : NodeList
|
|
|
|
{
|
|
|
|
return this.querySelectorAll(this.optionTag + ".freeEntry");
|
|
|
|
}
|
|
|
|
|
2022-12-05 21:56:31 +01:00
|
|
|
get select_options() : SelectOption[]
|
2022-09-22 19:15:25 +02:00
|
|
|
{
|
|
|
|
let options = [];
|
|
|
|
|
|
|
|
// Any provided options
|
2022-12-05 21:56:31 +01:00
|
|
|
options = options.concat(this.__select_options);
|
2022-09-22 19:15:25 +02:00
|
|
|
|
|
|
|
// Any kept remote options
|
|
|
|
options = options.concat(this._selected_remote);
|
|
|
|
|
2023-02-10 22:12:33 +01:00
|
|
|
if(this.allowFreeEntries)
|
|
|
|
{
|
|
|
|
this.freeEntries.forEach((item : SlMenuItem) =>
|
|
|
|
{
|
|
|
|
if(!options.some(i => i.value == item.value))
|
|
|
|
{
|
|
|
|
options.push({value: item.value, label: item.textContent, class: item.classList.toString()});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-09-22 19:15:25 +02:00
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2022-12-05 21:56:31 +01:00
|
|
|
set select_options(options : SelectOption[])
|
|
|
|
{
|
2022-12-06 19:59:51 +01:00
|
|
|
super.select_options = options;
|
2022-12-08 22:28:05 +01:00
|
|
|
|
|
|
|
// Remove any selected remote, they're real options now
|
|
|
|
for(let remote_index = this._selected_remote.length - 1; remote_index >= 0; remote_index--)
|
|
|
|
{
|
|
|
|
let remote = this._selected_remote[remote_index];
|
|
|
|
if(options.findIndex(o => o.value == remote.value) != -1)
|
|
|
|
{
|
|
|
|
this._selected_remote.splice(remote_index, 1);
|
2023-04-28 19:27:26 +02:00
|
|
|
this.querySelector('[value="' + remote.value + '"]')?.classList.remove("remote");
|
2022-12-08 22:28:05 +01:00
|
|
|
}
|
|
|
|
}
|
2022-12-05 21:56:31 +01:00
|
|
|
}
|
|
|
|
|
2022-06-10 18:11:34 +02:00
|
|
|
get value()
|
|
|
|
{
|
|
|
|
return super.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
set value(new_value : string | string[])
|
|
|
|
{
|
|
|
|
super.value = new_value;
|
|
|
|
|
2022-08-08 17:27:21 +02:00
|
|
|
if(!new_value || !this.allowFreeEntries && !this.searchUrl)
|
2022-06-15 01:31:39 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-10 18:11:34 +02:00
|
|
|
// Overridden to add options if allowFreeEntries=true
|
2022-08-15 18:38:19 +02:00
|
|
|
if(this.allowFreeEntries && typeof this.value == "string" && !this._menuItems.find(o => o.value == this.value && !o.classList.contains('remote')))
|
2022-06-10 18:11:34 +02:00
|
|
|
{
|
2022-06-22 22:28:37 +02:00
|
|
|
this.createFreeEntry(this.value);
|
|
|
|
}
|
2022-09-15 16:49:36 +02:00
|
|
|
else if(this.allowFreeEntries && this.multiple)
|
|
|
|
{
|
|
|
|
this.value.forEach((e) =>
|
|
|
|
{
|
|
|
|
if(!this._menuItems.find(o => o.value == e))
|
|
|
|
{
|
|
|
|
this.createFreeEntry(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2023-07-22 00:39:47 +02:00
|
|
|
if(this.searchEnabled)
|
|
|
|
{
|
|
|
|
for(const newValueElement of this.getValueAsArray())
|
|
|
|
{
|
|
|
|
if(this.select_options.some(o => o.value == newValueElement))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-07-25 17:38:11 +02:00
|
|
|
this._missingOption(newValueElement);
|
2023-07-22 00:39:47 +02:00
|
|
|
}
|
|
|
|
}
|
2022-06-10 18:11:34 +02:00
|
|
|
}
|
|
|
|
|
2023-07-25 17:38:11 +02:00
|
|
|
/**
|
|
|
|
* Some [part of a] value is missing from the available options, but should be there, so find and add it.
|
|
|
|
*
|
|
|
|
* This is used when not all options are sent to the client (search, link list). Ideally we want to send
|
|
|
|
* the options for the current value, but sometimes this is not the best option so here we search or create
|
|
|
|
* the option as needed. These are not free entries, but need to match some list somewhere.
|
|
|
|
*
|
|
|
|
* @param {string} newValueElement
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected _missingOption(newValueElement : string)
|
|
|
|
{
|
|
|
|
// Given a value we need to search for - this will add in all matches, including the one needed
|
|
|
|
this.remoteSearch(newValueElement, this.searchOptions).then((result : SelectOption[]) =>
|
|
|
|
{
|
|
|
|
const option = <SelectOption>result.find(o => o.value == newValueElement);
|
|
|
|
if(option)
|
|
|
|
{
|
|
|
|
this._selected_remote.push(option);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-31 21:27:10 +02:00
|
|
|
protected fix_bad_value()
|
|
|
|
{
|
|
|
|
if(!this.allowFreeEntries && !this.searchEnabled)
|
|
|
|
{
|
|
|
|
// Let regular select deal with it
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const valueArray = Array.isArray(this.value) ? this.value : (!this.value ? [] : this.value.toString().split(','));
|
|
|
|
|
|
|
|
// Check any already found options
|
|
|
|
if(Object.values(this.menuItems).filter((option) => valueArray.find(val => val == option.value)).length === 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
// TODO? Should we check the server, or just be OK with it? Passing the "current" value in sel_options makes sure the value is there
|
|
|
|
}
|
|
|
|
|
2022-05-31 01:05:00 +02:00
|
|
|
protected _bindListeners()
|
|
|
|
{
|
2022-09-20 17:43:25 +02:00
|
|
|
this.addEventListener("sl-clear", this._handleClear);
|
|
|
|
this.addEventListener("sl-after-show", this._handleAfterShow);
|
2022-07-07 18:47:14 +02:00
|
|
|
|
|
|
|
// Need our own change to catch the change event from search input
|
|
|
|
this.addEventListener("change", this._handleChange);
|
2022-09-20 17:43:25 +02:00
|
|
|
|
2023-01-31 17:47:46 +01:00
|
|
|
if(this.allowFreeEntries)
|
|
|
|
{
|
|
|
|
this.addEventListener("paste", this._handlePaste);
|
|
|
|
}
|
|
|
|
|
2022-07-06 19:20:53 +02:00
|
|
|
this.updateComplete.then(() =>
|
2022-06-23 18:36:54 +02:00
|
|
|
{
|
2022-07-06 19:20:53 +02:00
|
|
|
// Search messes up event order. Since it throws its own bubbling change event,
|
|
|
|
// selecting an option fires 2 change events - 1 before the widget is finished adjusting, losing the value
|
|
|
|
// We catch all change events, then call this._oldChange only when value changes
|
|
|
|
this.removeEventListener("change", this._oldChange);
|
2022-08-31 21:27:10 +02:00
|
|
|
|
2022-10-14 23:12:08 +02:00
|
|
|
this._searchInputNode?.removeEventListener("change", this._searchInputNode.handleChange);
|
|
|
|
this._searchInputNode?.addEventListener("change", this._handleSearchChange);
|
2023-01-24 01:33:22 +01:00
|
|
|
|
|
|
|
this.dropdown.querySelector('.select__label').addEventListener("change", this.handleTagEdit);
|
2022-07-06 19:20:53 +02:00
|
|
|
});
|
2022-05-31 01:05:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected _unbindListeners()
|
|
|
|
{
|
2022-06-14 18:06:27 +02:00
|
|
|
this.removeEventListener("sl-select", this._handleSelect);
|
2022-09-20 17:43:25 +02:00
|
|
|
this.removeEventListener("sl-after-show", this._handleAfterShow);
|
2022-06-14 18:06:27 +02:00
|
|
|
this.removeEventListener("sl-clear", this._handleClear)
|
2022-06-23 18:36:54 +02:00
|
|
|
this.removeEventListener("change", this._handleChange);
|
2023-01-31 17:47:46 +01:00
|
|
|
this.removeEventListener("paste", this._handlePaste);
|
2022-08-31 21:27:10 +02:00
|
|
|
|
|
|
|
this._searchInputNode?.removeEventListener("change", this._handleSearchChange);
|
2022-05-31 01:05:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
handleMenuShow()
|
|
|
|
{
|
2022-06-24 22:27:49 +02:00
|
|
|
if(this.readonly)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2022-09-01 19:53:19 +02:00
|
|
|
// Move search (& menu) if there's no value
|
2022-09-07 16:40:46 +02:00
|
|
|
this._activeControls?.classList.toggle("novalue", this.multiple && this.value == '' || !this.multiple);
|
2022-09-01 19:53:19 +02:00
|
|
|
|
2022-09-20 18:07:36 +02:00
|
|
|
// Reset for parent calculations, will be adjusted after if needed
|
|
|
|
this.dropdown.setAttribute("distance", 0);
|
|
|
|
|
2022-05-31 01:05:00 +02:00
|
|
|
super.handleMenuShow();
|
|
|
|
|
2022-06-22 22:28:37 +02:00
|
|
|
if(this.searchEnabled || this.allowFreeEntries)
|
2022-05-31 21:40:31 +02:00
|
|
|
{
|
|
|
|
this._activeControls?.classList.add("active");
|
|
|
|
this._searchInputNode.focus();
|
|
|
|
this._searchInputNode.select();
|
2022-09-22 01:13:29 +02:00
|
|
|
// Hide edit explicitly since it's so hard via CSS
|
|
|
|
if(this._editInputNode)
|
|
|
|
{
|
|
|
|
this._editInputNode.style.display = "none";
|
|
|
|
}
|
2022-05-31 21:40:31 +02:00
|
|
|
}
|
2022-09-01 19:53:19 +02:00
|
|
|
|
2022-09-22 01:13:29 +02:00
|
|
|
if(this.editModeEnabled && this.allowFreeEntries && !this.multiple && this.value)
|
2022-06-22 22:28:37 +02:00
|
|
|
{
|
|
|
|
this.startEdit();
|
|
|
|
this._editInputNode.select();
|
2022-09-20 17:43:25 +02:00
|
|
|
// Hide search explicitly since it's so hard via CSS
|
2022-06-22 22:28:37 +02:00
|
|
|
this._searchInputNode.style.display = "none";
|
|
|
|
}
|
2022-05-31 01:05:00 +02:00
|
|
|
}
|
|
|
|
|
2022-09-20 17:43:25 +02:00
|
|
|
/**
|
|
|
|
* Reposition the dropdown to allow space for current value and search. If the dropdown was positioned above
|
|
|
|
* instead of below, we don't need the extra space - remove it.
|
|
|
|
*/
|
|
|
|
_handleAfterShow()
|
|
|
|
{
|
2022-09-20 18:07:36 +02:00
|
|
|
// Need to give positioner a chance to position.
|
|
|
|
// If we call it right away, it has not updated.
|
|
|
|
// I haven't found an event or Promise to hook on to
|
|
|
|
window.setTimeout(() =>
|
2022-09-20 17:43:25 +02:00
|
|
|
{
|
2023-01-24 21:58:38 +01:00
|
|
|
if(this.dropdown?.getAttribute("distance") && this.dropdown?.popup?.dataset.currentPlacement == "top")
|
2022-09-20 18:07:36 +02:00
|
|
|
{
|
|
|
|
this.dropdown.setAttribute("distance", 0);
|
|
|
|
this.dropdown.reposition();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.dropdown?.setAttribute("distance",
|
|
|
|
!this._activeControls || this._activeControls?.classList.contains("novalue") ?
|
2022-11-09 17:30:53 +01:00
|
|
|
parseInt(getComputedStyle(this.control).getPropertyValue("border-width")) :
|
2022-09-20 18:07:36 +02:00
|
|
|
// Make room for search below
|
|
|
|
parseInt(getComputedStyle(this._activeControls).getPropertyValue("--sl-input-height-medium"))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}, 100);
|
2022-09-20 17:43:25 +02:00
|
|
|
}
|
|
|
|
|
2022-08-17 00:46:20 +02:00
|
|
|
focus()
|
|
|
|
{
|
|
|
|
this.dropdown?.show().then(() =>
|
|
|
|
{
|
|
|
|
this._searchInputNode.focus();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-31 01:05:00 +02:00
|
|
|
handleMenuHide()
|
|
|
|
{
|
2022-06-24 22:27:49 +02:00
|
|
|
if(this.readonly)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2022-10-17 17:56:58 +02:00
|
|
|
clearTimeout(this._searchTimeout);
|
2022-05-31 01:05:00 +02:00
|
|
|
super.handleMenuHide();
|
2022-09-22 01:13:29 +02:00
|
|
|
|
|
|
|
// Reset display
|
|
|
|
if(this._searchInputNode)
|
|
|
|
{
|
|
|
|
this._searchInputNode.style.display = "";
|
|
|
|
}
|
|
|
|
if(this._editInputNode)
|
|
|
|
{
|
|
|
|
this._editInputNode.style.display = "";
|
|
|
|
}
|
|
|
|
|
2022-06-22 22:28:37 +02:00
|
|
|
if(this.searchEnabled || this.allowFreeEntries)
|
2022-05-31 21:40:31 +02:00
|
|
|
{
|
|
|
|
this._activeControls?.classList.remove("active");
|
2022-06-22 22:28:37 +02:00
|
|
|
this.shadowRoot.querySelector('.select__label').style.display = "";
|
2022-05-31 21:40:31 +02:00
|
|
|
}
|
2022-05-31 01:05:00 +02:00
|
|
|
}
|
|
|
|
|
2022-08-31 21:27:10 +02:00
|
|
|
_triggerChange(event)
|
|
|
|
{
|
|
|
|
// Don't want searchbox events to trigger change event
|
|
|
|
if(event.target == this._searchInputNode)
|
|
|
|
{
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-06-23 18:36:54 +02:00
|
|
|
_handleChange(event)
|
|
|
|
{
|
|
|
|
if(event.target == this._searchInputNode)
|
|
|
|
{
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._oldChange(event);
|
|
|
|
}
|
|
|
|
|
2022-06-14 01:21:35 +02:00
|
|
|
_handleDoubleClick(event : MouseEvent)
|
2022-06-10 18:11:34 +02:00
|
|
|
{
|
2022-06-14 01:21:35 +02:00
|
|
|
// No edit (shouldn't happen...)
|
|
|
|
if(!this.editModeEnabled)
|
2022-06-10 18:11:34 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the tag
|
2022-06-14 01:21:35 +02:00
|
|
|
const path = event.composedPath();
|
2022-06-10 18:11:34 +02:00
|
|
|
const tag = <Et2Tag>path.find((el) => el instanceof Et2Tag);
|
2023-01-24 01:33:22 +01:00
|
|
|
this.dropdown.hide();
|
|
|
|
this.updateComplete.then(() =>
|
|
|
|
{
|
|
|
|
tag.startEdit(event);
|
|
|
|
});
|
2022-06-10 18:11:34 +02:00
|
|
|
}
|
|
|
|
|
2022-06-14 01:21:35 +02:00
|
|
|
/**
|
|
|
|
* An option was selected
|
|
|
|
*/
|
2022-09-15 16:30:50 +02:00
|
|
|
handleMenuSelect(event)
|
2022-06-14 01:21:35 +02:00
|
|
|
{
|
2022-06-16 19:02:16 +02:00
|
|
|
// Need to keep the remote option - only if selected
|
2023-04-18 18:36:34 +02:00
|
|
|
if(event.detail.item.classList.contains("remote") && !this.select_options.find(o => o.value == event.detail.item.value))
|
2022-06-16 19:02:16 +02:00
|
|
|
{
|
|
|
|
this._selected_remote.push({...event.detail.item.option});
|
|
|
|
}
|
2022-09-15 16:30:50 +02:00
|
|
|
super.handleMenuSelect(event);
|
2022-06-16 19:02:16 +02:00
|
|
|
|
2022-09-15 16:30:50 +02:00
|
|
|
this.updateComplete.then(() =>
|
2022-06-14 01:21:35 +02:00
|
|
|
{
|
2022-09-15 16:30:50 +02:00
|
|
|
// If they just chose one from the list, re-focus the search
|
|
|
|
if(this.multiple && this.searchEnabled)
|
2022-09-15 12:56:12 +02:00
|
|
|
{
|
2022-09-15 16:30:50 +02:00
|
|
|
this._searchInputNode.focus();
|
|
|
|
this._searchInputNode.select();
|
2022-09-15 12:56:12 +02:00
|
|
|
|
2022-09-15 16:30:50 +02:00
|
|
|
// If we were overlapping, reset
|
|
|
|
if(this._activeControls.classList.contains("novalue"))
|
2022-09-15 12:56:12 +02:00
|
|
|
{
|
2022-09-15 16:30:50 +02:00
|
|
|
this.handleMenuShow();
|
2022-09-22 01:13:29 +02:00
|
|
|
this._handleAfterShow();
|
2022-09-15 16:30:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Scroll the new tag into view
|
|
|
|
if(event.detail && event.detail.item)
|
|
|
|
{
|
2022-12-08 22:47:02 +01:00
|
|
|
// Causes sidemenu (calendar) to scroll to top & get stuck
|
|
|
|
/*
|
2022-09-15 16:30:50 +02:00
|
|
|
this.updateComplete.then(() =>
|
|
|
|
{
|
2022-12-08 22:47:02 +01:00
|
|
|
this.shadowRoot.querySelector("et2-tag[value='" + event.detail.item.value.replace(/'/g, "\\\'") + "']")?.scrollIntoView({block: "nearest"});
|
2022-09-15 16:30:50 +02:00
|
|
|
});
|
2022-12-08 22:47:02 +01:00
|
|
|
*/
|
2022-09-15 16:30:50 +02:00
|
|
|
}
|
2022-09-14 23:46:59 +02:00
|
|
|
}
|
2022-09-15 16:30:50 +02:00
|
|
|
else if(!this.multiple && this.searchEnabled)
|
|
|
|
{
|
|
|
|
// Stop all the search stuff when they select an option
|
|
|
|
// this shows all non-matching options again
|
|
|
|
this._handleSearchAbort(event);
|
|
|
|
}
|
|
|
|
});
|
2022-06-14 01:21:35 +02:00
|
|
|
}
|
|
|
|
|
2022-06-02 19:52:27 +02:00
|
|
|
/**
|
|
|
|
* Value was cleared
|
|
|
|
*/
|
2022-08-25 23:51:15 +02:00
|
|
|
_handleClear(e)
|
2022-06-02 19:52:27 +02:00
|
|
|
{
|
2022-09-22 01:13:29 +02:00
|
|
|
// Only keep remote options that are still used
|
|
|
|
this._selected_remote = this._selected_remote.filter((option) => this.getValueAsArray().indexOf(option.value) !== -1);
|
2022-06-16 19:02:16 +02:00
|
|
|
|
2022-06-14 18:06:27 +02:00
|
|
|
if(!this.multiple && this.searchEnabled)
|
|
|
|
{
|
2022-08-25 23:51:15 +02:00
|
|
|
this._handleSearchAbort(e);
|
|
|
|
|
2022-06-14 18:06:27 +02:00
|
|
|
// Restore label styling
|
|
|
|
this.shadowRoot.querySelector("[part='display-label']").style.display = "";
|
|
|
|
|
|
|
|
// Start searching again
|
|
|
|
this.updateComplete.then(() => this.handleMenuShow())
|
|
|
|
}
|
2022-06-02 19:52:27 +02:00
|
|
|
}
|
|
|
|
|
2022-09-13 17:06:59 +02:00
|
|
|
/**
|
|
|
|
* Handle blur from search field
|
|
|
|
*
|
|
|
|
* Either the user changed fields, or selected an option. For selecting don't interfere, but for
|
|
|
|
* changing fields we need to make sure the menu is hidden.
|
|
|
|
*
|
|
|
|
* @param event
|
|
|
|
*/
|
|
|
|
async _handleSearchBlur(event : FocusEvent)
|
|
|
|
{
|
2022-10-17 17:56:58 +02:00
|
|
|
clearTimeout(this._searchTimeout);
|
2022-12-05 21:56:31 +01:00
|
|
|
if(event.relatedTarget && event.relatedTarget instanceof SlMenuItem)
|
2022-09-13 17:06:59 +02:00
|
|
|
{
|
2022-12-05 21:56:31 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try any value they had in progress
|
|
|
|
if(this._searchInputNode.value && this.allowFreeEntries)
|
|
|
|
{
|
|
|
|
this.createFreeEntry(this._searchInputNode.value);
|
|
|
|
}
|
|
|
|
this.clearSearch();
|
2022-09-13 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
2022-05-31 01:05:00 +02:00
|
|
|
/**
|
|
|
|
* Handle keypresses inside the search input
|
|
|
|
* @param {KeyboardEvent} event
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected _handleSearchKeyDown(event : KeyboardEvent)
|
|
|
|
{
|
2022-06-16 19:02:16 +02:00
|
|
|
clearTimeout(this._searchTimeout);
|
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))
|
|
|
|
{
|
2023-03-24 00:21:32 +01:00
|
|
|
|
|
|
|
// Strip out hidden non-matching selected & disabled items so key navigation works
|
|
|
|
this.menuItems = this.menuItems.filter(i => !i.disabled);
|
|
|
|
return super.handleKeyDown(event);
|
2022-05-31 01:05:00 +02:00
|
|
|
}
|
2023-02-08 00:24:06 +01:00
|
|
|
event.stopPropagation();
|
2022-05-31 01:05:00 +02:00
|
|
|
|
|
|
|
// Don't allow event to bubble or it will interact with select
|
|
|
|
event.stopImmediatePropagation();
|
2022-06-14 01:21:35 +02:00
|
|
|
if(Et2WidgetWithSearch.TAG_BREAK.indexOf(event.key) !== -1 && this.allowFreeEntries && this.createFreeEntry(this._searchInputNode.value))
|
2022-05-31 01:05:00 +02:00
|
|
|
{
|
|
|
|
event.preventDefault();
|
2022-06-13 17:58:46 +02:00
|
|
|
this._searchInputNode.value = "";
|
2022-09-22 01:13:29 +02:00
|
|
|
this.dropdown.hide().then(async() =>
|
2022-06-10 18:11:34 +02:00
|
|
|
{
|
2022-09-22 01:13:29 +02:00
|
|
|
// update sizing / position before getting ready for another one
|
|
|
|
if(this.multiple)
|
|
|
|
{
|
|
|
|
await this.dropdown.show();
|
|
|
|
this._searchInputNode.focus();
|
|
|
|
}
|
|
|
|
});
|
2022-06-13 17:58:46 +02:00
|
|
|
}
|
|
|
|
else if(event.key == "Enter")
|
|
|
|
{
|
|
|
|
event.preventDefault();
|
|
|
|
this.startSearch();
|
2022-06-16 19:02:16 +02:00
|
|
|
return;
|
2022-05-31 01:05:00 +02:00
|
|
|
}
|
2022-06-14 01:21:35 +02:00
|
|
|
else if(event.key == "Escape")
|
|
|
|
{
|
|
|
|
this._handleSearchAbort(event);
|
2022-09-23 18:40:16 +02:00
|
|
|
this.dropdown.hide();
|
2022-06-16 19:02:16 +02:00
|
|
|
return;
|
2022-06-14 01:21:35 +02:00
|
|
|
}
|
2022-05-31 01:05:00 +02:00
|
|
|
|
|
|
|
// Start the search automatically if they have enough letters
|
2023-02-08 00:24:06 +01:00
|
|
|
// -1 because we're in keyDown handler, and value is from _before_ this key was pressed
|
|
|
|
if(this._searchInputNode.value.length >= Et2WidgetWithSearch.MIN_CHARS - 1)
|
2022-05-31 01:05:00 +02:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 01:21:35 +02:00
|
|
|
protected _handleEditKeyDown(event : KeyboardEvent)
|
|
|
|
{
|
2022-06-22 22:28:37 +02:00
|
|
|
// Stop propagation, or parent key handler will add again
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
|
2022-08-04 23:50:52 +02:00
|
|
|
if(Et2WidgetWithSearch.TAG_BREAK.indexOf(event.key) !== -1 && this.allowFreeEntries)
|
2022-06-14 01:21:35 +02:00
|
|
|
{
|
2022-06-22 22:28:37 +02:00
|
|
|
// Prevent default, since that would try to submit
|
2022-06-14 01:21:35 +02:00
|
|
|
event.preventDefault();
|
|
|
|
this.stopEdit();
|
|
|
|
}
|
|
|
|
// Abort edit, put original value back
|
|
|
|
else if(event.key == "Escape")
|
|
|
|
{
|
|
|
|
this.stopEdit(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-31 17:47:46 +01:00
|
|
|
/**
|
|
|
|
* Sometimes users paste multiple comma separated values at once. Split them then handle normally.
|
|
|
|
*
|
|
|
|
* @param {ClipboardEvent} event
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected _handlePaste(event : ClipboardEvent)
|
|
|
|
{
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
let paste = event.clipboardData.getData('text');
|
|
|
|
if(!paste)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const selection = window.getSelection();
|
|
|
|
if(selection.rangeCount)
|
|
|
|
{
|
|
|
|
selection.deleteFromDocument();
|
|
|
|
}
|
|
|
|
let values = paste.split(/,\t/);
|
|
|
|
|
|
|
|
values.forEach(v =>
|
|
|
|
{
|
|
|
|
this.createFreeEntry(v.trim());
|
|
|
|
});
|
|
|
|
this.dropdown.hide();
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2022-09-16 18:20:47 +02:00
|
|
|
public async startSearch()
|
2022-05-31 01:05:00 +02:00
|
|
|
{
|
2022-06-16 19:02:16 +02:00
|
|
|
// Stop timeout timer
|
|
|
|
clearTimeout(this._searchTimeout);
|
|
|
|
|
2022-07-14 22:17:50 +02:00
|
|
|
// Show a spinner
|
2022-05-31 01:05:00 +02:00
|
|
|
let spinner = document.createElement("sl-spinner");
|
2022-07-14 22:17:50 +02:00
|
|
|
spinner.slot = "suffix";
|
2022-05-31 01:05:00 +02:00
|
|
|
this.appendChild(spinner);
|
|
|
|
|
2022-09-15 16:30:50 +02:00
|
|
|
// Hide clear button
|
|
|
|
let clear_button = <HTMLElement>this._searchInputNode.shadowRoot.querySelector(".input__clear")
|
|
|
|
if(clear_button)
|
|
|
|
{
|
|
|
|
clear_button.style.display = "none";
|
|
|
|
}
|
|
|
|
|
2022-09-16 18:20:47 +02:00
|
|
|
// Clear previous results
|
|
|
|
this._clearResults();
|
|
|
|
await this.updateComplete;
|
|
|
|
|
2022-05-31 01:05:00 +02:00
|
|
|
// Start the searches
|
2022-09-16 18:20:47 +02:00
|
|
|
return 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(() =>
|
|
|
|
{
|
2022-09-16 19:17:06 +02:00
|
|
|
// Show no results indicator
|
|
|
|
if(this.menuItems.filter(e => !e.classList.contains("no-match")).length == 0)
|
|
|
|
{
|
|
|
|
let target = this._optionTargetNode || this;
|
|
|
|
let temp = document.createElement("div");
|
|
|
|
render(this._noResultsTemplate(), temp);
|
|
|
|
target.append(temp.children[0]);
|
|
|
|
}
|
2022-09-16 18:20:47 +02:00
|
|
|
|
|
|
|
// Remove spinner
|
2022-05-31 01:05:00 +02:00
|
|
|
spinner.remove();
|
2022-09-16 18:20:47 +02:00
|
|
|
|
2022-09-15 16:30:50 +02:00
|
|
|
// Restore clear button
|
|
|
|
if(clear_button)
|
|
|
|
{
|
|
|
|
clear_button.style.display = "";
|
|
|
|
}
|
2022-09-16 18:20:47 +02:00
|
|
|
}).then(() =>
|
|
|
|
{
|
2022-09-20 17:43:25 +02:00
|
|
|
// Not sure why this stays hidden if there's no results, but it sticks and hides all results afterward
|
2022-09-16 18:20:47 +02:00
|
|
|
this.dropdown.shadowRoot.querySelector(".dropdown__panel").removeAttribute("hidden");
|
2022-09-20 17:43:25 +02:00
|
|
|
|
2022-09-20 18:07:36 +02:00
|
|
|
// Call our resize stuff explicitly
|
|
|
|
this._handleAfterShow();
|
2022-05-31 01:05:00 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-09-07 16:40:46 +02:00
|
|
|
/**
|
|
|
|
* Clear search term and any search results
|
|
|
|
*
|
|
|
|
* Local options are not removed, but remote options are
|
|
|
|
*/
|
|
|
|
public clearSearch()
|
|
|
|
{
|
|
|
|
// Stop timeout timer
|
|
|
|
clearTimeout(this._searchTimeout);
|
|
|
|
|
2022-09-16 18:20:47 +02:00
|
|
|
this._clearResults();
|
|
|
|
|
|
|
|
// Clear search term
|
|
|
|
if(this._searchInputNode)
|
|
|
|
{
|
|
|
|
this._searchInputNode.value = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected _clearResults()
|
|
|
|
{
|
2022-09-07 16:40:46 +02:00
|
|
|
let target = this._optionTargetNode || this;
|
2022-09-16 19:17:06 +02:00
|
|
|
|
|
|
|
// Remove "no suggestions"
|
|
|
|
target.querySelector(".no-results")?.remove();
|
|
|
|
|
2022-09-22 19:15:25 +02:00
|
|
|
// Remove any previously selected remote options that aren't used anymore
|
|
|
|
this._selected_remote = this._selected_remote.filter((option) =>
|
|
|
|
{
|
2022-12-09 21:14:57 +01:00
|
|
|
return this.multiple ? this.value.indexOf(option.value) != -1 : this.value == option.value;
|
2022-09-22 19:15:25 +02:00
|
|
|
});
|
2022-09-16 19:17:06 +02:00
|
|
|
// Remove remote options that aren't used
|
2022-09-12 18:19:38 +02:00
|
|
|
let keepers = this._selected_remote.reduce((prev, current) =>
|
|
|
|
{
|
2022-12-09 21:14:57 +01:00
|
|
|
return prev + ":not([value='" + ('' + current.value).replace(/'/g, "\\\'") + "'])";
|
2022-09-12 18:19:38 +02:00
|
|
|
}, "");
|
|
|
|
target.querySelectorAll(".remote" + keepers).forEach(o => o.remove());
|
2022-09-15 16:30:50 +02:00
|
|
|
target.childNodes.forEach((n) =>
|
|
|
|
{
|
|
|
|
if(n.nodeType == Node.COMMENT_NODE)
|
|
|
|
{
|
|
|
|
n.remove();
|
|
|
|
}
|
|
|
|
})
|
2022-09-07 16:40:46 +02:00
|
|
|
|
|
|
|
// Reset remaining options. It might be faster to re-create instead.
|
|
|
|
this._menuItems.forEach((item) =>
|
|
|
|
{
|
2023-02-22 17:51:57 +01:00
|
|
|
item.disabled = item.option?.disabled || false;
|
2022-09-07 16:40:46 +02:00
|
|
|
item.classList.remove("match");
|
|
|
|
item.classList.remove("no-match");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-31 01:05:00 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2023-07-24 16:51:27 +02:00
|
|
|
protected remoteSearch(search : string, options : object) : Promise<SelectOption[]>
|
2022-05-31 01:05:00 +02:00
|
|
|
{
|
|
|
|
if(!this.searchUrl)
|
|
|
|
{
|
2023-07-24 16:51:27 +02:00
|
|
|
return Promise.resolve([]);
|
2022-05-31 01:05:00 +02:00
|
|
|
}
|
|
|
|
|
2023-07-22 00:39:47 +02:00
|
|
|
// Check our URL: JSON file or URL?
|
|
|
|
if(this.searchUrl.includes(".json"))
|
|
|
|
{
|
|
|
|
// Get the file, search it
|
|
|
|
return this.jsonQuery(search, options);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Fire off the query to the server
|
2023-07-24 16:51:27 +02:00
|
|
|
return this.remoteQuery(search, options);
|
2023-07-22 00:39:47 +02:00
|
|
|
}
|
2022-05-31 01:05:00 +02:00
|
|
|
}
|
|
|
|
|
2023-07-22 00:39:47 +02:00
|
|
|
/**
|
|
|
|
* Search through a JSON file in the browser
|
|
|
|
*
|
|
|
|
* @param {string} search
|
|
|
|
* @param {object} options
|
|
|
|
* @protected
|
|
|
|
*/
|
2023-07-24 16:51:27 +02:00
|
|
|
protected jsonQuery(search : string, options : object) : Promise<SelectOption[]>
|
2023-07-22 00:39:47 +02:00
|
|
|
{
|
|
|
|
// Get the file
|
|
|
|
const controller = new AbortController();
|
|
|
|
const signal = controller.signal;
|
|
|
|
let response_ok = false;
|
|
|
|
return StaticOptions.cached_from_file(this, this.searchUrl)
|
|
|
|
.then(options =>
|
|
|
|
{
|
|
|
|
// Filter the options
|
|
|
|
const lower_search = search.toLowerCase();
|
|
|
|
const filtered = options.filter(option =>
|
|
|
|
{
|
|
|
|
return option.label.toLowerCase().includes(lower_search) || option.value.includes(search)
|
|
|
|
});
|
|
|
|
// Add the matches
|
|
|
|
this.processRemoteResults(filtered);
|
|
|
|
return filtered;
|
|
|
|
})
|
|
|
|
.catch((_err) =>
|
|
|
|
{
|
|
|
|
this.egw().message(_err.statusText || this.searchUrl, "error");
|
|
|
|
return [];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-10 18:11:34 +02:00
|
|
|
/**
|
|
|
|
* Actually query the server.
|
|
|
|
*
|
2022-08-08 17:27:21 +02:00
|
|
|
* This can be overridden to change request parameters or eg. send them as POST parameters.
|
|
|
|
*
|
2022-08-11 11:23:54 +02:00
|
|
|
* Default implementation here sends search string and options:
|
|
|
|
* - as two parameters to the AJAX function
|
|
|
|
* - and (additional) as GET parameters plus search string as "query"
|
|
|
|
*
|
|
|
|
* This is done to support as well the old taglist callbacks, as the regular select ones!
|
2022-06-10 18:11:34 +02:00
|
|
|
*
|
|
|
|
* @param {string} search
|
|
|
|
* @param {object} options
|
|
|
|
* @returns {any}
|
|
|
|
* @protected
|
|
|
|
*/
|
2023-07-24 16:51:27 +02:00
|
|
|
protected remoteQuery(search : string, options : object) : Promise<SelectOption[]>
|
2022-05-31 01:05:00 +02:00
|
|
|
{
|
2022-10-04 23:49:07 +02:00
|
|
|
// Include a limit, even if options don't, to avoid massive lists breaking the UI
|
|
|
|
let sendOptions = {
|
|
|
|
num_rows: Et2WidgetWithSearch.RESULT_LIMIT,
|
|
|
|
...options
|
|
|
|
}
|
2022-09-08 19:17:34 +02:00
|
|
|
return this.egw().request(this.egw().link(this.egw().ajaxUrl(this.egw().decodePath(this.searchUrl)),
|
2023-07-24 16:51:27 +02:00
|
|
|
{query: search, ...sendOptions}), [search, sendOptions]).then((results) =>
|
2022-05-31 01:05:00 +02:00
|
|
|
{
|
2023-07-24 16:51:27 +02:00
|
|
|
// If results have a total included, pull it out.
|
|
|
|
// It will cause errors if left in the results
|
|
|
|
let total = null;
|
|
|
|
if(typeof results.total !== "undefined")
|
|
|
|
{
|
|
|
|
total = results.total;
|
|
|
|
delete results.total;
|
|
|
|
}
|
|
|
|
let entries = cleanSelectOptions(results);
|
|
|
|
this.processRemoteResults(entries, total);
|
|
|
|
return entries;
|
2022-05-31 01:05:00 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add in remote results
|
|
|
|
* @param results
|
2023-07-24 16:51:27 +02:00
|
|
|
* @param totalResults If there are more results than were returned, total number of matches
|
2022-05-31 01:05:00 +02:00
|
|
|
* @protected
|
|
|
|
*/
|
2023-07-24 16:51:27 +02:00
|
|
|
protected processRemoteResults(entries, totalResults = 0)
|
2022-05-31 01:05:00 +02:00
|
|
|
{
|
2022-10-04 23:49:07 +02:00
|
|
|
let resultCount = entries.length;
|
2022-06-01 17:23:07 +02:00
|
|
|
|
2022-09-16 18:20:47 +02:00
|
|
|
if(entries.length == 0)
|
|
|
|
{
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2022-06-01 17:23:07 +02:00
|
|
|
// Add a "remote" class so we can tell these apart from any local results
|
2022-06-01 17:41:53 +02:00
|
|
|
entries.forEach((entry) => entry.class = (entry.class || "") + " remote");
|
2022-06-01 17:23:07 +02:00
|
|
|
|
|
|
|
let target = this._optionTargetNode || this;
|
|
|
|
if(target)
|
|
|
|
{
|
2022-09-22 19:15:25 +02:00
|
|
|
// Add in remote options, avoiding duplicates
|
|
|
|
this.select_options.filter(function(item)
|
2022-06-15 01:31:39 +02:00
|
|
|
{
|
|
|
|
let i = entries.findIndex(x => (x.value == item.value));
|
|
|
|
if(i <= -1)
|
|
|
|
{
|
|
|
|
entries.push(item);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
2022-06-02 19:52:27 +02:00
|
|
|
|
2022-09-15 16:30:50 +02:00
|
|
|
let options = html`${entries.map(this._optionTemplate.bind(this))}`;
|
|
|
|
|
|
|
|
/**
|
2022-09-22 19:15:25 +02:00
|
|
|
* Add in new options.
|
|
|
|
* Rendering directly into target will remove existing options, which we don't need to do
|
2022-09-15 16:30:50 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
let temp_target = document.createElement("div");
|
|
|
|
|
|
|
|
render(options, temp_target);
|
|
|
|
return Promise.all(([...temp_target.querySelectorAll(":scope > *")].map(item => item.render)))
|
|
|
|
.then(() =>
|
|
|
|
{
|
|
|
|
temp_target.querySelectorAll(":scope > *").forEach((item) =>
|
|
|
|
{
|
|
|
|
// Avoid duplicate error
|
2022-10-04 23:49:07 +02:00
|
|
|
if(!target.querySelector("[value='" + ('' + item.value).replace(/'/g, "\\\'") + "']"))
|
2022-09-15 16:30:50 +02:00
|
|
|
{
|
|
|
|
target.appendChild(item);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
this.handleMenuSlotChange();
|
2022-10-04 23:49:07 +02:00
|
|
|
})
|
|
|
|
.then(() =>
|
|
|
|
{
|
2023-07-24 16:51:27 +02:00
|
|
|
if(totalResults && totalResults > resultCount)
|
2022-10-04 23:49:07 +02:00
|
|
|
{
|
|
|
|
// More results available that were not sent
|
|
|
|
let count = document.createElement("span")
|
|
|
|
count.classList.add("remote");
|
2023-07-24 16:51:27 +02:00
|
|
|
count.textContent = this.egw().lang("%1 more...", totalResults - resultCount);
|
2022-10-04 23:49:07 +02:00
|
|
|
target.appendChild(count);
|
|
|
|
}
|
2022-09-15 16:30:50 +02:00
|
|
|
});
|
2022-06-01 17:23:07 +02:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2022-06-10 18:11:34 +02:00
|
|
|
/**
|
|
|
|
* Create an entry that is not in the options and add it to the value
|
|
|
|
*
|
|
|
|
* @param {string} text Used as both value and label
|
|
|
|
*/
|
|
|
|
public createFreeEntry(text : string) : boolean
|
|
|
|
{
|
2022-09-22 23:17:56 +02:00
|
|
|
if(!text || !this.validateFreeEntry(text))
|
2022-06-10 18:11:34 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Make sure not to double-add
|
2022-09-27 21:54:54 +02:00
|
|
|
if(!this.querySelector("[value='" + text.replace(/'/g, "\\\'") + "']") && !this.__select_options.find(o => o.value == text))
|
2022-06-10 18:11:34 +02:00
|
|
|
{
|
2022-07-26 17:15:05 +02:00
|
|
|
this.__select_options.push(<SelectOption>{
|
2023-01-24 01:33:22 +01:00
|
|
|
value: text.trim(),
|
|
|
|
label: text.trim(),
|
2022-09-22 19:15:25 +02:00
|
|
|
class: "freeEntry"
|
2022-06-10 18:11:34 +02:00
|
|
|
});
|
2022-06-14 01:21:35 +02:00
|
|
|
this.requestUpdate('select_options');
|
2022-06-10 18:11:34 +02:00
|
|
|
}
|
2022-06-14 01:21:35 +02:00
|
|
|
|
2022-06-10 18:11:34 +02:00
|
|
|
// Make sure not to double-add
|
|
|
|
if(this.multiple && this.value.indexOf(text) == -1)
|
|
|
|
{
|
|
|
|
this.value.push(text);
|
|
|
|
}
|
2022-06-22 22:28:37 +02:00
|
|
|
else if(!this.multiple && this.value !== text)
|
2022-06-10 18:11:34 +02:00
|
|
|
{
|
|
|
|
this.value = text;
|
|
|
|
}
|
2022-09-22 23:17:56 +02:00
|
|
|
this.requestUpdate("value");
|
2022-06-14 01:21:35 +02:00
|
|
|
|
2022-09-12 20:47:45 +02:00
|
|
|
// If we were overlapping edit inputbox with the value display, reset
|
2022-09-13 09:15:55 +02:00
|
|
|
if(!this.readonly && this._activeControls?.classList.contains("novalue"))
|
2022-09-12 20:47:45 +02:00
|
|
|
{
|
2022-09-22 01:13:29 +02:00
|
|
|
this._searchInputNode.style.display = "";
|
2022-09-12 20:47:45 +02:00
|
|
|
}
|
2022-06-10 18:11:34 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a free entry value is acceptable.
|
|
|
|
* We use validators directly using the proposed value
|
|
|
|
*
|
|
|
|
* @param text
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
public validateFreeEntry(text) : boolean
|
|
|
|
{
|
|
|
|
let validators = [...this.validators, ...this.defaultValidators];
|
|
|
|
let result = validators.filter(v =>
|
|
|
|
v.execute(text, v.param, {node: this}),
|
|
|
|
);
|
2022-09-01 16:29:22 +02:00
|
|
|
return validators.length > 0 && result.length == 0 || validators.length == 0;
|
2022-06-10 18:11:34 +02:00
|
|
|
}
|
|
|
|
|
2023-01-24 01:33:22 +01:00
|
|
|
public handleTagEdit(event)
|
|
|
|
{
|
|
|
|
let value = event.target.value;
|
|
|
|
let original = event.target.dataset.original_value;
|
|
|
|
|
|
|
|
if(!value || !this.allowFreeEntries || !this.validateFreeEntry(value))
|
|
|
|
{
|
|
|
|
// Not a good value, reset it.
|
|
|
|
event.target.variant = "danger"
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
event.target.variant = "success";
|
|
|
|
|
|
|
|
// Add to internal list
|
|
|
|
this.createFreeEntry(value);
|
|
|
|
|
|
|
|
// Remove original from value & DOM
|
|
|
|
if(value != original)
|
|
|
|
{
|
|
|
|
if(this.multiple)
|
|
|
|
{
|
|
|
|
this.value = this.value.filter(v => v !== original);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.value = value;
|
|
|
|
}
|
|
|
|
this.querySelector("[value='" + original.replace(/'/g, "\\\'") + "']")?.remove();
|
|
|
|
this.__select_options = this.__select_options.filter(v => v.value !== original);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 01:21:35 +02:00
|
|
|
/**
|
2023-01-24 01:33:22 +01:00
|
|
|
* Start editing the current value if multiple=false
|
2022-06-14 01:21:35 +02:00
|
|
|
*
|
|
|
|
* @param {Et2Tag} tag
|
|
|
|
*/
|
2022-06-22 22:28:37 +02:00
|
|
|
public startEdit(tag? : Et2Tag)
|
2022-06-10 18:11:34 +02:00
|
|
|
{
|
2022-06-22 22:28:37 +02:00
|
|
|
const tag_value = tag ? tag.value : this.value;
|
2022-06-14 01:21:35 +02:00
|
|
|
|
|
|
|
// hide the menu
|
2023-01-24 01:33:22 +01:00
|
|
|
this.dropdown.hide()
|
2022-06-10 18:11:34 +02:00
|
|
|
|
2023-01-24 01:33:22 +01:00
|
|
|
waitForEvent(this, "sl-after-hide").then(() =>
|
2022-06-22 22:28:37 +02:00
|
|
|
{
|
2023-01-24 01:33:22 +01:00
|
|
|
// Turn on edit UI
|
|
|
|
this._activeControls.classList.add("editing", "active");
|
|
|
|
|
|
|
|
// Pre-set value to tag value
|
|
|
|
this._editInputNode.style.display = "";
|
|
|
|
this._editInputNode.value = tag_value
|
|
|
|
this._editInputNode.focus();
|
2022-06-14 01:21:35 +02:00
|
|
|
|
2023-01-24 01:33:22 +01:00
|
|
|
// If they abort the edit, they'll want the original back.
|
|
|
|
this._editInputNode.dataset.initial = tag_value;
|
|
|
|
})
|
2022-06-14 01:21:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected stopEdit(abort = false)
|
|
|
|
{
|
2022-06-22 22:28:37 +02:00
|
|
|
// type to select will focus matching entries, but we don't want to stop the edit yet
|
2022-08-04 23:50:52 +02:00
|
|
|
if(typeof abort == "object" && abort.type == "blur")
|
2022-06-22 22:28:37 +02:00
|
|
|
{
|
2022-08-04 23:50:52 +02:00
|
|
|
if(abort.relatedTarget?.localName == "sl-menu-item")
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Edit lost focus, accept changes
|
|
|
|
abort = false;
|
2022-06-22 22:28:37 +02:00
|
|
|
}
|
|
|
|
|
2022-09-22 01:13:29 +02:00
|
|
|
const original = this._editInputNode.dataset.initial;
|
2022-06-14 01:21:35 +02:00
|
|
|
delete this._editInputNode.dataset.initial;
|
|
|
|
|
2022-09-22 01:13:29 +02:00
|
|
|
let value = abort ? original : this._editInputNode.value;
|
|
|
|
this._editInputNode.value = "";
|
|
|
|
|
|
|
|
if(value && value != original)
|
|
|
|
{
|
|
|
|
this.createFreeEntry(value);
|
|
|
|
|
|
|
|
this.updateComplete.then(() =>
|
|
|
|
{
|
2022-09-27 21:54:54 +02:00
|
|
|
const item = this.querySelector("[value='" + value.replace(/'/g, "\\\'") + "']");
|
2022-09-22 01:13:29 +02:00
|
|
|
item.dispatchEvent(new CustomEvent("sl-select", {detail: {item}}));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove original from value & DOM
|
|
|
|
if(value != original)
|
|
|
|
{
|
|
|
|
if(this.multiple)
|
|
|
|
{
|
|
|
|
this.value = this.value.filter(v => v !== original);
|
2022-09-27 21:54:54 +02:00
|
|
|
this.querySelector("[value='" + original.replace(/'/g, "\\\'") + "']")?.remove();
|
2022-09-22 01:13:29 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.value = value;
|
|
|
|
}
|
|
|
|
this.select_options = this.select_options.filter(v => v.value !== original);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-06-14 01:21:35 +02:00
|
|
|
this._activeControls.classList.remove("editing", "active");
|
2022-06-22 22:28:37 +02:00
|
|
|
if(!this.multiple)
|
|
|
|
{
|
2022-09-22 01:13:29 +02:00
|
|
|
this.updateComplete.then(async() =>
|
|
|
|
{
|
|
|
|
// Don't know why, but this doesn't always work leaving the value hidden by prefix
|
|
|
|
await this.dropdown.hide();
|
|
|
|
this.dropdown.classList.remove("select--open");
|
|
|
|
this.dropdown.panel.setAttribute("hidden", "");
|
|
|
|
});
|
2022-06-22 22:28:37 +02:00
|
|
|
}
|
2022-12-01 22:54:34 +01:00
|
|
|
this.syncItemsFromValue();
|
2022-06-10 18:11:34 +02:00
|
|
|
}
|
|
|
|
|
2022-05-31 01:05:00 +02:00
|
|
|
protected _handleSearchAbort(e)
|
|
|
|
{
|
|
|
|
this._activeControls.classList.remove("active");
|
2022-09-07 16:40:46 +02:00
|
|
|
this.clearSearch();
|
2022-06-02 23:44:13 +02:00
|
|
|
this.syncItemsFromValue();
|
2022-05-31 01:05:00 +02:00
|
|
|
}
|
2022-08-31 21:27:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* et2-searchbox (SlInput) sends out an event on change.
|
|
|
|
* We don't care, and if we let it bubble it'll get in the way.
|
|
|
|
* @param e
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected _handleSearchChange(e)
|
|
|
|
{
|
|
|
|
e.stopImmediatePropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
}
|
2022-05-31 01:05:00 +02:00
|
|
|
}
|
|
|
|
|
2022-06-10 18:11:34 +02:00
|
|
|
return Et2WidgetWithSearch as unknown as Constructor<SearchMixinInterface> & T;
|
|
|
|
}
|