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
|
|
|
|
*/
|
|
|
|
|
2023-09-15 16:09:12 +02:00
|
|
|
import {css, CSSResultGroup, html, LitElement, nothing, TemplateResult} from "lit";
|
2022-06-01 17:23:07 +02:00
|
|
|
import {cleanSelectOptions, SelectOption} from "./FindSelectOptions";
|
2022-06-10 18:11:34 +02:00
|
|
|
import {Et2Tag} from "./Tag/Et2Tag";
|
2023-07-22 00:39:47 +02:00
|
|
|
import {StaticOptions} from "./StaticOptions";
|
2023-09-13 19:55:33 +02:00
|
|
|
import {dedupeMixin} from "@open-wc/dedupe-mixin";
|
2023-09-14 22:30:20 +02:00
|
|
|
import {SlOption} from "@shoelace-style/shoelace";
|
2023-09-29 21:43:12 +02:00
|
|
|
import {Et2Textbox} from "../Et2Textbox/Et2Textbox";
|
2023-10-06 19:37:11 +02:00
|
|
|
import {until} from "lit/directives/until.js";
|
2023-12-14 18:00:36 +01:00
|
|
|
import {waitForEvent} from "../Et2Widget/event";
|
2024-05-07 22:46:44 +02:00
|
|
|
import {Validator} from "../Validators/Validator";
|
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
|
|
|
|
*/
|
2024-01-16 16:25:39 +01:00
|
|
|
localSearch(search : string, options : object) : Promise<any[]>
|
2022-05-31 01:05:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Search remote options.
|
|
|
|
* If searchUrl is not set, it will return very quickly with no results
|
|
|
|
*/
|
2024-01-16 16:25:39 +01:00
|
|
|
remoteSearch(search : string, options : object) : Promise<any[]>
|
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
|
2023-09-13 19:55:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Additional customisation location, where we stick the search elements
|
|
|
|
*
|
|
|
|
* @type {TemplateResult}
|
|
|
|
*/
|
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
|
|
|
|
*/
|
2023-09-13 19:55:33 +02:00
|
|
|
export const Et2WithSearchMixin = dedupeMixin(<T extends Constructor<LitElement>>(superclass : T) =>
|
2022-05-31 01:05:00 +02:00
|
|
|
{
|
2023-09-13 19:55:33 +02:00
|
|
|
class Et2WidgetWithSearch extends superclass
|
2022-05-31 01:05:00 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
static get styles() : CSSResultGroup
|
2022-05-31 01:05:00 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
// @ts-ignore
|
|
|
|
...(super.styles ? (Symbol.iterator in Object(super.styles) ? super.styles : [super.styles]) : []),
|
|
|
|
css`
|
2023-09-13 19:55:33 +02:00
|
|
|
|
2022-08-31 21:27:10 +02:00
|
|
|
/* Full width search textbox covers loading spinner, lift it up */
|
|
|
|
::slotted(sl-spinner) {
|
|
|
|
z-index: 2;
|
|
|
|
}
|
2023-09-13 19:55:33 +02:00
|
|
|
|
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;
|
|
|
|
}
|
2023-09-13 19:55:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
:host([search]) sl-select[open]::part(prefix), :host([allowfreeentries]) sl-select[open]::part(prefix) {
|
|
|
|
order: 9;
|
2022-06-02 23:44:13 +02:00
|
|
|
flex: 2 1 auto;
|
2023-09-13 19:55:33 +02:00
|
|
|
flex-wrap: wrap;
|
2022-06-02 23:44:13 +02:00
|
|
|
width: 100%;
|
|
|
|
}
|
2023-09-13 19:55:33 +02:00
|
|
|
|
|
|
|
:host([search]) sl-select[open]::part(display-input), :host([allowfreeentries]) sl-select[open]::part(display-input) {
|
2022-06-22 22:28:37 +02:00
|
|
|
display: none;
|
|
|
|
}
|
2023-05-10 21:43:46 +02:00
|
|
|
|
2023-10-12 19:25:23 +02:00
|
|
|
:host([search]) sl-select[open]::part(expand-icon) {
|
2023-09-13 19:55:33 +02:00
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
|
2023-11-30 18:49:49 +01:00
|
|
|
sl-select[open][multiple]::part(tags) {
|
2023-09-13 19:55:33 +02:00
|
|
|
flex-basis: 100%;
|
|
|
|
}
|
|
|
|
|
2023-11-30 18:49:49 +01:00
|
|
|
sl-select[open][multiple]::part(combobox) {
|
2023-09-13 19:55:33 +02:00
|
|
|
flex-flow: wrap;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-10 21:43:46 +02:00
|
|
|
/* Search textbox general styling, starts hidden */
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
.search_input {
|
2022-05-31 01:05:00 +02:00
|
|
|
display: none;
|
2023-09-13 19:55:33 +02:00
|
|
|
/* See also etemplate2.css, searchbox border turned off in there */
|
|
|
|
border: none;
|
2022-06-14 01:21:35 +02:00
|
|
|
flex: 1 1 auto;
|
2023-09-13 19:55:33 +02:00
|
|
|
order: 2;
|
2022-05-31 01:05:00 +02:00
|
|
|
margin-left: 0px;
|
2022-06-14 01:21:35 +02:00
|
|
|
height: var(--sl-input-height-medium);
|
2023-09-13 19:55:33 +02:00
|
|
|
width: 100%;
|
2024-07-12 00:46:22 +02:00
|
|
|
background-color: var(--sl-color-neutral-0);
|
2023-05-10 21:43:46 +02:00
|
|
|
z-index: var(--sl-z-index-dropdown);
|
|
|
|
}
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
:host([search]) et2-textbox::part(base) {
|
|
|
|
border: none;
|
|
|
|
box-shadow: none;
|
|
|
|
}
|
|
|
|
|
2023-05-10 21:43:46 +02:00
|
|
|
/* Search UI active - show textbox & stuff */
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
.search_input.active,
|
2023-05-10 21:43:46 +02:00
|
|
|
.search_input.editing {
|
2022-05-31 21:40:31 +02:00
|
|
|
display: flex;
|
2023-05-10 21:43:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If multiple and no value, overlap search onto widget instead of below */
|
|
|
|
|
|
|
|
:host([multiple]) .search_input.active.novalue {
|
2022-09-01 19:53:19 +02:00
|
|
|
top: 0px;
|
2023-05-10 21:43:46 +02:00
|
|
|
}
|
2022-06-10 18:11:34 +02:00
|
|
|
|
|
|
|
/* Hide options that do not match current search text */
|
2023-09-13 19:55:33 +02:00
|
|
|
|
2023-11-23 00:03:01 +01:00
|
|
|
:host([search]) sl-option.no-match {
|
2022-05-31 01:05:00 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
/* 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
|
|
|
|
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[]>[];
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
// Hold current search results, selected or otherwise
|
|
|
|
private _remote_options = <SelectOption[]>[];
|
|
|
|
|
|
|
|
private _total_result_count = 0;
|
|
|
|
|
2024-01-08 19:33:53 +01:00
|
|
|
protected _searchPromise = null;
|
|
|
|
|
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.
|
2023-09-13 19:55:33 +02:00
|
|
|
this.maxOptionsVisible = -1;
|
2022-06-14 01:21:35 +02:00
|
|
|
|
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 = [];
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
this.handleOptionClick = this.handleOptionClick.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);
|
2023-09-13 19:55:33 +02:00
|
|
|
this._handleMenuHide = this._handleMenuHide.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);
|
2023-09-13 19:55:33 +02:00
|
|
|
this._handleSearchClear = this._handleSearchClear.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);
|
2023-09-13 19:55:33 +02:00
|
|
|
this._handleSearchMouseDown = this._handleSearchMouseDown.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._bindListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnectedCallback()
|
|
|
|
{
|
|
|
|
super.disconnectedCallback();
|
|
|
|
this._unbindListeners();
|
|
|
|
}
|
|
|
|
|
2023-09-29 21:43:12 +02:00
|
|
|
async getUpdateComplete()
|
|
|
|
{
|
|
|
|
const result = super.getUpdateComplete();
|
|
|
|
if(this._searchInputNode)
|
|
|
|
{
|
|
|
|
await this._searchInputNode.updateComplete;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2023-07-27 21:19:43 +02:00
|
|
|
// Add missing options if search or free entries enabled
|
2023-07-26 16:31:19 +02:00
|
|
|
if(changedProperties.has("value") && this.value)
|
|
|
|
{
|
|
|
|
// Overridden to add options if allowFreeEntries=true
|
2023-07-27 18:04:14 +02:00
|
|
|
if(this.allowFreeEntries && typeof this.value == "string" && !this.select_options.find(o => o.value == this.value &&
|
|
|
|
(!o.class || o.class && !o.class.includes('remote'))))
|
2023-07-26 16:31:19 +02:00
|
|
|
{
|
|
|
|
this.createFreeEntry(this.value);
|
|
|
|
}
|
|
|
|
else if(this.allowFreeEntries && this.multiple)
|
|
|
|
{
|
2023-09-13 19:55:33 +02:00
|
|
|
this.getValueAsArray().forEach((e) =>
|
2023-07-26 16:31:19 +02:00
|
|
|
{
|
|
|
|
if(!this.select_options.find(o => o.value == e))
|
|
|
|
{
|
|
|
|
this.createFreeEntry(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if(this.searchEnabled)
|
|
|
|
{
|
|
|
|
// Check to see if value is for an option we do not have
|
|
|
|
for(const newValueElement of this.getValueAsArray())
|
|
|
|
{
|
2023-12-05 19:51:24 +01:00
|
|
|
if(this.optionSearch(newValueElement))
|
2023-07-26 16:31:19 +02:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._missingOption(newValueElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-19 19:07:16 +02:00
|
|
|
}
|
|
|
|
|
2023-07-27 21:19:43 +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"))
|
|
|
|
{
|
2023-10-12 19:25:23 +02:00
|
|
|
this._unbindListeners();
|
2023-07-27 21:19:43 +02:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2023-10-12 19:25:23 +02:00
|
|
|
// Listeners may have been skipped from connectedCallback()
|
|
|
|
this._bindListeners();
|
2023-07-27 21:19:43 +02:00
|
|
|
}
|
|
|
|
// Update any tags if edit mode changes
|
|
|
|
if(changedProperties.has("editModeEnabled") || changedProperties.has("readonly"))
|
|
|
|
{
|
|
|
|
// 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-10-12 19:25:23 +02:00
|
|
|
|
|
|
|
if(this.readonly)
|
|
|
|
{
|
|
|
|
this._unbindListeners();
|
|
|
|
}
|
2023-07-27 21:19:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-19 00:09:43 +02:00
|
|
|
protected _extraTemplate() : TemplateResult | typeof nothing
|
2022-05-31 01:05:00 +02:00
|
|
|
{
|
2023-09-13 19:55:33 +02:00
|
|
|
if(!this.searchEnabled && !this.editModeEnabled && !this.allowFreeEntries || this.readonly)
|
2022-06-14 01:21:35 +02:00
|
|
|
{
|
2023-09-13 19:55:33 +02:00
|
|
|
return nothing;
|
2022-06-14 01:21:35 +02:00
|
|
|
}
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
return html`
|
|
|
|
${this._searchInputTemplate()}
|
2023-10-06 19:37:11 +02:00
|
|
|
${until(this._moreResultsTemplate(), nothing)}
|
2023-09-15 16:09:12 +02:00
|
|
|
${this._noResultsTemplate()}
|
2023-09-13 19:55:33 +02:00
|
|
|
`;
|
2022-05-31 01:05:00 +02:00
|
|
|
}
|
|
|
|
|
2023-10-06 19:37:11 +02:00
|
|
|
protected async _moreResultsTemplate()
|
2023-01-24 01:33:22 +01:00
|
|
|
{
|
2024-01-08 19:59:24 +01:00
|
|
|
if(this._total_result_count <= 0 || !this.select || !this._searchPromise)
|
2023-09-13 19:55:33 +02:00
|
|
|
{
|
|
|
|
return nothing;
|
|
|
|
}
|
2024-01-08 19:59:24 +01:00
|
|
|
return this._searchPromise.then(() =>
|
|
|
|
{
|
|
|
|
const moreCount = this._total_result_count - this.select?.querySelectorAll("sl-option.match").length;
|
|
|
|
const more = this.egw().lang("%1 more...", moreCount);
|
2023-01-24 01:33:22 +01:00
|
|
|
|
2024-01-08 19:59:24 +01:00
|
|
|
return html`<span class="more">${more}</span>`;
|
|
|
|
});
|
2023-01-24 01:33:22 +01:00
|
|
|
}
|
|
|
|
|
2022-05-31 01:05:00 +02:00
|
|
|
protected _searchInputTemplate()
|
|
|
|
{
|
2023-09-13 19:55:33 +02:00
|
|
|
let edit = nothing;
|
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-05-31 01:05:00 +02:00
|
|
|
return html`
|
2023-09-13 19:55:33 +02:00
|
|
|
<div class="search_input" slot="prefix">
|
|
|
|
<et2-textbox id="search" type="text" part="input"
|
|
|
|
exportparts="base:search__base"
|
|
|
|
clearable
|
2023-02-13 22:57:18 +01:00
|
|
|
autocomplete="off"
|
2023-11-22 19:52:49 +01:00
|
|
|
tabindex="-1"
|
2023-02-08 00:24:06 +01:00
|
|
|
placeholder="${this.egw().lang("search")}"
|
2023-09-13 19:55:33 +02:00
|
|
|
style="flex: 1 1 auto;"
|
2024-04-17 17:04:37 +02:00
|
|
|
@mousedown=${this._handleSearchMouseDown}
|
|
|
|
@keydown=${this._handleSearchKeyDown}
|
|
|
|
@blur=${this._handleSearchBlur}
|
2023-09-13 19:55:33 +02:00
|
|
|
@sl-clear=${this._handleSearchClear}
|
2023-09-28 19:00:30 +02:00
|
|
|
@sl-change=${this._handleSearchChange}
|
2023-02-08 00:24:06 +01:00
|
|
|
></et2-textbox>
|
2022-06-14 01:21:35 +02:00
|
|
|
${edit}
|
2023-09-13 19:55:33 +02:00
|
|
|
</div>
|
2022-05-31 01:05:00 +02:00
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2022-09-16 19:17:06 +02:00
|
|
|
protected _noResultsTemplate()
|
|
|
|
{
|
2023-09-25 18:16:32 +02:00
|
|
|
if(this._total_result_count !== 0 || !this._searchInputNode?.value)
|
2023-09-15 16:09:12 +02:00
|
|
|
{
|
|
|
|
return nothing;
|
|
|
|
}
|
|
|
|
|
2024-01-08 19:33:53 +01:00
|
|
|
const noSuggestions = html`
|
2022-09-16 19:17:06 +02:00
|
|
|
<div class="no-results">${this.egw().lang("no suggestions")}</div>`;
|
2024-01-08 19:33:53 +01:00
|
|
|
|
|
|
|
if(!this._searchPromise)
|
|
|
|
{
|
|
|
|
return noSuggestions;
|
|
|
|
}
|
|
|
|
|
|
|
|
let noResults = this._searchPromise.then(() =>
|
|
|
|
{
|
|
|
|
return this._total_result_count == 0 ?
|
|
|
|
noSuggestions :
|
|
|
|
nothing;
|
|
|
|
});
|
|
|
|
|
|
|
|
return html`${until(
|
|
|
|
noResults,
|
|
|
|
html`
|
|
|
|
<sl-spinner></sl-spinner>`
|
|
|
|
)}`;
|
2022-09-16 19:17:06 +02:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2023-09-29 21:43:12 +02:00
|
|
|
protected get _searchInputNode() : Et2Textbox
|
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
|
|
|
}
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
protected get optionTag()
|
|
|
|
{
|
|
|
|
return 'sl-option';
|
|
|
|
}
|
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
|
|
|
{
|
2023-09-13 19:55:33 +02:00
|
|
|
return this.select.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
|
|
|
{
|
2023-09-13 19:55:33 +02:00
|
|
|
return this.select?.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
|
|
|
|
{
|
2023-09-13 19:55:33 +02:00
|
|
|
return this.select?.querySelectorAll(this.optionTag + ".freeEntry") ?? [];
|
2022-09-22 19:15:25 +02:00
|
|
|
}
|
|
|
|
|
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
|
2023-08-28 21:50:09 +02:00
|
|
|
options = options.concat(this.__select_options ?? []);
|
2022-09-22 19:15:25 +02:00
|
|
|
|
|
|
|
// Any kept remote options
|
2023-08-28 21:50:09 +02:00
|
|
|
options = options.concat(this._selected_remote ?? []);
|
2022-09-22 19:15:25 +02:00
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
// Current search results
|
|
|
|
options = options.concat(this._remote_options ?? []);
|
|
|
|
|
2023-02-10 22:12:33 +01:00
|
|
|
if(this.allowFreeEntries)
|
|
|
|
{
|
2023-09-14 22:30:20 +02:00
|
|
|
this.freeEntries.forEach((item : SlOption) =>
|
2023-02-10 22:12:33 +01:00
|
|
|
{
|
2023-09-14 22:30:20 +02:00
|
|
|
if(!options.some(i => i.value == item.value.replaceAll("___", " ")))
|
2023-02-10 22:12:33 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2023-09-13 19:55:33 +02:00
|
|
|
|
2023-08-03 22:05:10 +02:00
|
|
|
// If widget is currently open, we may need to re-calculate search / dropdown positioning
|
|
|
|
if(this.isOpen)
|
|
|
|
{
|
2023-09-13 19:55:33 +02:00
|
|
|
this._handleMenuShow();
|
2023-08-03 22:05:10 +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[]) =>
|
|
|
|
{
|
2023-11-30 22:00:16 +01:00
|
|
|
// Re-set / update value since SlSelect probably removed it by now due to missing option
|
|
|
|
if(typeof this.select != "undefined")
|
2023-07-25 17:38:11 +02:00
|
|
|
{
|
2023-11-30 22:00:16 +01:00
|
|
|
this.select.value = this.shoelaceValue ?? this.value;
|
|
|
|
this.select.requestUpdate("value");
|
2023-07-25 17:38:11 +02:00
|
|
|
}
|
2023-11-30 22:00:16 +01:00
|
|
|
this.requestUpdate("value");
|
2023-07-25 17:38:11 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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
|
2023-09-13 19:55:33 +02:00
|
|
|
if(Object.values(this.getAllOptions()).filter((option) => valueArray.find(val => val == option.value)).length === 0)
|
2022-08-31 21:27:10 +02:00
|
|
|
{
|
|
|
|
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);
|
2023-09-13 19:55:33 +02:00
|
|
|
this.addEventListener("sl-show", this._handleMenuShow);
|
2022-09-20 17:43:25 +02:00
|
|
|
this.addEventListener("sl-after-show", this._handleAfterShow);
|
2023-09-13 19:55:33 +02:00
|
|
|
this.addEventListener("sl-hide", this._handleMenuHide);
|
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
|
|
|
|
2023-09-13 19:55:33 +02: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);
|
2023-09-13 19:55:33 +02:00
|
|
|
this.removeEventListener("sl-show", this._handleMenuShow);
|
2022-09-20 17:43:25 +02:00
|
|
|
this.removeEventListener("sl-after-show", this._handleAfterShow);
|
2023-09-13 19:55:33 +02:00
|
|
|
this.removeEventListener("sl-hide", this._handleMenuHide);
|
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
|
|
|
}
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
_handleMenuShow()
|
2022-05-31 01:05:00 +02:00
|
|
|
{
|
2022-06-24 22:27:49 +02:00
|
|
|
if(this.readonly)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2023-09-19 23:20:36 +02:00
|
|
|
this.setAttribute("open", "");
|
|
|
|
|
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
|
2023-09-13 19:55:33 +02:00
|
|
|
//this.dropdown.setAttribute("distance", 0);
|
2022-05-31 01:05:00 +02:00
|
|
|
|
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");
|
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
|
|
|
/**
|
2023-11-28 18:32:15 +01:00
|
|
|
* Focus the search input after showing the dropdown so user can just type.
|
|
|
|
*
|
|
|
|
* Timeout is needed for some systems to properly focus
|
2022-09-20 17:43:25 +02:00
|
|
|
*/
|
|
|
|
_handleAfterShow()
|
|
|
|
{
|
2023-09-13 19:55:33 +02:00
|
|
|
if(this.searchEnabled || this.allowFreeEntries)
|
|
|
|
{
|
2023-11-28 18:16:43 +01:00
|
|
|
window.setTimeout(() =>
|
2023-11-27 22:54:55 +01:00
|
|
|
{
|
|
|
|
this._searchInputNode.focus();
|
|
|
|
this._searchInputNode.select();
|
2023-11-28 18:16:43 +01:00
|
|
|
}, 100);
|
2023-09-13 19:55:33 +02:00
|
|
|
}
|
2022-09-20 17:43:25 +02:00
|
|
|
}
|
|
|
|
|
2022-08-17 00:46:20 +02:00
|
|
|
focus()
|
|
|
|
{
|
2023-09-20 22:24:01 +02:00
|
|
|
this.show().then(() =>
|
|
|
|
{
|
|
|
|
this._searchInputNode?.focus();
|
|
|
|
});
|
2022-08-17 00:46:20 +02:00
|
|
|
}
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
_handleMenuHide()
|
2022-05-31 01:05:00 +02:00
|
|
|
{
|
2022-06-24 22:27:49 +02:00
|
|
|
if(this.readonly)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2023-09-19 23:20:36 +02:00
|
|
|
this.removeAttribute("open");
|
|
|
|
|
2023-12-14 18:00:36 +01:00
|
|
|
waitForEvent(this, "sl-after-hide").then(() => this.clearSearch());
|
2022-09-22 01:13:29 +02:00
|
|
|
|
|
|
|
// Reset display
|
|
|
|
if(this._searchInputNode)
|
|
|
|
{
|
|
|
|
this._searchInputNode.style.display = "";
|
|
|
|
}
|
|
|
|
if(this._editInputNode)
|
|
|
|
{
|
|
|
|
this._editInputNode.style.display = "";
|
|
|
|
}
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
this._activeControls?.classList.remove("active");
|
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;
|
|
|
|
}
|
2023-09-27 19:15:13 +02:00
|
|
|
|
|
|
|
// Find and keep any selected remote entries
|
|
|
|
// Doing it here catches keypress changes too
|
2023-10-02 21:11:48 +02:00
|
|
|
this._keepSelectedRemote();
|
2022-08-31 21:27:10 +02:00
|
|
|
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-10-02 21:11:48 +02:00
|
|
|
this.hide();
|
2023-01-24 01:33:22 +01:00
|
|
|
this.updateComplete.then(() =>
|
|
|
|
{
|
|
|
|
tag.startEdit(event);
|
|
|
|
});
|
2022-06-10 18:11:34 +02:00
|
|
|
}
|
|
|
|
|
2023-10-02 21:11:48 +02:00
|
|
|
_keepSelectedRemote()
|
|
|
|
{
|
|
|
|
this.select.querySelectorAll("[aria-selected=true].remote").forEach((node) =>
|
|
|
|
{
|
|
|
|
const value = node.value.replaceAll("___", " ");
|
|
|
|
if(!node.selected || this._selected_remote.some(o => o.value == value))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2023-11-02 23:58:38 +01:00
|
|
|
const filter = (options) =>
|
2023-10-02 21:11:48 +02:00
|
|
|
{
|
2023-11-02 23:58:38 +01:00
|
|
|
for(let i = options.length - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
if(Array.isArray(options[i].value))
|
|
|
|
{
|
|
|
|
filter(options[i].value);
|
|
|
|
}
|
|
|
|
else if(options[i].value == value)
|
|
|
|
{
|
|
|
|
this._selected_remote.push(options[i]);
|
|
|
|
options.splice(i, 1);
|
|
|
|
}
|
|
|
|
}
|
2023-10-02 21:11:48 +02:00
|
|
|
}
|
2023-11-02 23:58:38 +01:00
|
|
|
filter(this._remote_options)
|
2023-10-02 21:11:48 +02:00
|
|
|
});
|
|
|
|
}
|
2022-06-14 01:21:35 +02:00
|
|
|
/**
|
|
|
|
* An option was selected
|
|
|
|
*/
|
2023-09-13 19:55:33 +02:00
|
|
|
handleOptionClick(event)
|
2022-06-14 01:21:35 +02:00
|
|
|
{
|
2023-09-27 19:15:13 +02:00
|
|
|
// Only interested in option clicks, but handler is bound higher
|
|
|
|
if(event.target.tagName !== "SL-OPTION")
|
2022-06-16 19:02:16 +02:00
|
|
|
{
|
2023-09-27 19:15:13 +02:00
|
|
|
return;
|
2022-06-16 19:02:16 +02:00
|
|
|
}
|
2023-09-27 19:15:13 +02:00
|
|
|
|
|
|
|
if(typeof super.handleOptionClick == "function")
|
2023-09-13 19:55:33 +02:00
|
|
|
super.handleOptionClick(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-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
|
2023-09-13 19:55:33 +02:00
|
|
|
this._selected_remote = this._selected_remote.filter((option) => this.value.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
|
|
|
}
|
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)
|
|
|
|
{
|
2024-04-17 17:04:37 +02:00
|
|
|
event.stopPropagation();
|
2022-10-17 17:56:58 +02:00
|
|
|
clearTimeout(this._searchTimeout);
|
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
|
|
|
|
|
|
|
// 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
|
2023-09-13 19:55:33 +02:00
|
|
|
// TODO
|
|
|
|
return;
|
2022-05-31 01:05:00 +02:00
|
|
|
}
|
2023-11-22 19:52:49 +01:00
|
|
|
else if(event.key == "Tab" && !this._searchInputNode.value)
|
|
|
|
{
|
|
|
|
// Mess with tabindexes to allow focus to easily go to next control
|
|
|
|
const input = this.select.shadowRoot.querySelector('[tabindex="0"]');
|
|
|
|
input.setAttribute("tabindex", "-1");
|
|
|
|
this.updateComplete.then(() =>
|
|
|
|
{
|
|
|
|
// Set it back so we can get focus again later
|
|
|
|
input.setAttribute("tabindex", "0");
|
|
|
|
})
|
|
|
|
// Allow to propagate
|
|
|
|
return;
|
|
|
|
}
|
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 = "";
|
2023-09-13 19:55:33 +02:00
|
|
|
this.updateComplete.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)
|
|
|
|
{
|
2023-09-13 19:55:33 +02:00
|
|
|
// await this.show();
|
2022-09-22 01:13:29 +02:00
|
|
|
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);
|
2023-09-21 16:18:09 +02:00
|
|
|
this.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
/**
|
|
|
|
* Combobox listens for mousedown, which interferes with search clear button.
|
|
|
|
* Here we block it from bubbling
|
|
|
|
* @param {MouseEvent} event
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected _handleSearchMouseDown(event : MouseEvent)
|
|
|
|
{
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2023-10-03 00:06:25 +02:00
|
|
|
this.setAttribute("searching", "");
|
|
|
|
|
2022-09-15 16:30:50 +02:00
|
|
|
// Hide clear button
|
2023-09-29 21:43:12 +02:00
|
|
|
let clear_button = <HTMLElement>this._searchInputNode?.shadowRoot?.querySelector(".input__clear");
|
2022-09-15 16:30:50 +02:00
|
|
|
if(clear_button)
|
|
|
|
{
|
|
|
|
clear_button.style.display = "none";
|
|
|
|
}
|
|
|
|
|
2022-09-16 18:20:47 +02:00
|
|
|
// Clear previous results
|
2023-09-13 19:55:33 +02:00
|
|
|
this._total_result_count = 0;
|
2022-09-16 18:20:47 +02:00
|
|
|
this._clearResults();
|
|
|
|
|
2022-05-31 01:05:00 +02:00
|
|
|
// Start the searches
|
2024-01-08 19:33:53 +01:00
|
|
|
this._searchPromise = Promise.all([
|
2022-05-31 21:40:31 +02:00
|
|
|
this.localSearch(this._searchInputNode.value, this.searchOptions),
|
|
|
|
this.remoteSearch(this._searchInputNode.value, this.searchOptions)
|
2023-09-29 21:43:12 +02:00
|
|
|
]).then(async() =>
|
2022-05-31 01:05:00 +02:00
|
|
|
{
|
2023-10-03 00:06:25 +02:00
|
|
|
this.removeAttribute("searching");
|
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 = "";
|
|
|
|
}
|
2023-09-29 21:43:12 +02:00
|
|
|
await this.updateComplete;
|
2024-01-08 19:33:53 +01:00
|
|
|
|
|
|
|
this._searchPromise = null;
|
2022-05-31 01:05:00 +02:00
|
|
|
});
|
2024-01-08 19:33:53 +01:00
|
|
|
|
|
|
|
this.requestUpdate();
|
|
|
|
|
|
|
|
return this._searchPromise;
|
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
|
|
|
|
2023-10-02 21:11:48 +02:00
|
|
|
this._keepSelectedRemote();
|
2023-10-03 00:06:25 +02:00
|
|
|
|
2023-09-27 19:15:13 +02:00
|
|
|
this._remote_options = [];
|
2023-09-19 16:46:41 +02:00
|
|
|
|
2023-10-06 19:37:11 +02:00
|
|
|
this._total_result_count = 0;
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
// Not searching anymore, clear flag
|
2023-11-01 20:26:23 +01:00
|
|
|
const clear_flag = (option) =>
|
|
|
|
{
|
|
|
|
if(Array.isArray(option.value))
|
|
|
|
{
|
2023-11-02 23:58:38 +01:00
|
|
|
option.value.map(clear_flag)
|
2023-11-01 20:26:23 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
option.isMatch = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.select_options.map(clear_flag);
|
2023-09-13 19:55:33 +02:00
|
|
|
this.requestUpdate("select_options");
|
2023-11-23 00:03:01 +01:00
|
|
|
|
|
|
|
// Rendering options using repeat() means we need to explicitly update the nodes since they
|
|
|
|
// don't always get re-rendered
|
|
|
|
for(const option of this.select.querySelectorAll(".no-match"))
|
|
|
|
{
|
|
|
|
option.classList.remove("no-match", "match");
|
|
|
|
}
|
2022-09-07 16:40:46 +02:00
|
|
|
}
|
|
|
|
|
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) =>
|
|
|
|
{
|
2023-09-13 19:55:33 +02:00
|
|
|
this.select_options.forEach((option) =>
|
2022-05-31 01:05:00 +02:00
|
|
|
{
|
2023-09-13 19:55:33 +02:00
|
|
|
option.isMatch = this.searchMatch(search, option);
|
2022-05-31 01:05:00 +02:00
|
|
|
})
|
2023-09-13 19:55:33 +02:00
|
|
|
this.requestUpdate("select_options");
|
2022-05-31 01:05:00 +02:00
|
|
|
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;
|
2024-03-20 23:50:52 +01:00
|
|
|
let resultLimit = Math.max(parseInt(this.egw().preference('maxmatchs', 'common')), 100);
|
2023-07-22 00:39:47 +02:00
|
|
|
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)
|
|
|
|
});
|
2023-07-31 18:10:16 +02:00
|
|
|
// Limit results
|
2023-10-06 19:37:11 +02:00
|
|
|
this._total_result_count += filtered.length;
|
2024-03-20 23:50:52 +01:00
|
|
|
if(filtered.length > resultLimit)
|
2023-07-31 18:10:16 +02:00
|
|
|
{
|
2024-03-20 23:50:52 +01:00
|
|
|
filtered.splice(resultLimit);
|
2023-07-31 18:10:16 +02:00
|
|
|
}
|
2023-07-22 00:39:47 +02:00
|
|
|
// Add the matches
|
2023-11-02 23:58:38 +01:00
|
|
|
this._total_result_count -= this.processRemoteResults(filtered);
|
2023-07-22 00:39:47 +02:00
|
|
|
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 = {
|
2024-03-20 23:50:52 +01:00
|
|
|
num_rows: parseInt(this.egw().preference('maxmatchs', 'common')) ?? 100,
|
2022-10-04 23:49:07 +02:00
|
|
|
...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-11-16 23:05:38 +01:00
|
|
|
return this._processResultCount(results);
|
2022-05-31 01:05:00 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-11-16 23:05:38 +01:00
|
|
|
/**
|
|
|
|
* Update total result count, checking results for a total attribute, then further processing the results
|
|
|
|
* into select options
|
|
|
|
*
|
|
|
|
* @param results
|
|
|
|
* @returns {SelectOption[]}
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected _processResultCount(results)
|
|
|
|
{
|
|
|
|
// If results have a total included, pull it out.
|
|
|
|
// It will cause errors if left in the results
|
|
|
|
if(typeof results.total !== "undefined")
|
|
|
|
{
|
|
|
|
this._total_result_count += results.total;
|
|
|
|
delete results.total;
|
|
|
|
// Make it an array, since it was probably an object, and cleanSelectOptions() treats objects differently
|
|
|
|
results = Object.values(results);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._total_result_count += results.length;
|
|
|
|
}
|
|
|
|
let entries = cleanSelectOptions(results);
|
|
|
|
let entryCount = entries.length;
|
|
|
|
this._total_result_count -= this.processRemoteResults(entries);
|
|
|
|
|
|
|
|
return entries;
|
|
|
|
}
|
|
|
|
|
2022-05-31 01:05:00 +02:00
|
|
|
/**
|
|
|
|
* Add in remote results
|
2023-11-02 23:58:38 +01:00
|
|
|
*
|
|
|
|
* Any results that already exist will be removed to avoid duplicates
|
|
|
|
*
|
2022-05-31 01:05:00 +02:00
|
|
|
* @param results
|
2023-11-02 23:58:38 +01:00
|
|
|
* @return Duplicate count
|
2022-05-31 01:05:00 +02:00
|
|
|
* @protected
|
|
|
|
*/
|
2023-09-13 19:55:33 +02:00
|
|
|
protected processRemoteResults(entries)
|
2022-05-31 01:05:00 +02:00
|
|
|
{
|
2023-07-27 12:45:40 +02:00
|
|
|
if(!entries?.length)
|
2022-09-16 18:20:47 +02:00
|
|
|
{
|
2023-11-02 23:58:38 +01:00
|
|
|
return 0;
|
2022-09-16 18:20:47 +02:00
|
|
|
}
|
2023-11-02 23:58:38 +01:00
|
|
|
let duplicateCount = 0;
|
|
|
|
|
2023-11-01 20:26:23 +01:00
|
|
|
const process = (entries) =>
|
2022-06-01 17:23:07 +02:00
|
|
|
{
|
2023-11-01 20:26:23 +01:00
|
|
|
// Add a "remote" class so we can tell these apart from any local results
|
|
|
|
for(let i = entries.length - 1; i >= 0; i--)
|
2023-09-13 19:55:33 +02:00
|
|
|
{
|
2023-11-01 20:26:23 +01:00
|
|
|
const entry = entries[i];
|
|
|
|
entry.class = (entry.class || "") + " remote";
|
|
|
|
|
|
|
|
// Handle option groups
|
|
|
|
if(Array.isArray(entry.value))
|
|
|
|
{
|
|
|
|
process(entry.value);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Server says it's a match
|
|
|
|
entry.isMatch = true;
|
|
|
|
|
|
|
|
// Avoid duplicates with existing options
|
|
|
|
if(this.select_options.some(o => o.value == entry.value))
|
|
|
|
{
|
2023-11-02 23:58:38 +01:00
|
|
|
duplicateCount++
|
2023-11-01 20:26:23 +01:00
|
|
|
entries.splice(i, 1);
|
|
|
|
}
|
2023-09-13 19:55:33 +02:00
|
|
|
}
|
2023-09-27 19:15:13 +02:00
|
|
|
}
|
2023-11-01 20:26:23 +01:00
|
|
|
process(entries);
|
2022-09-15 16:30:50 +02:00
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
this._remote_options = entries;
|
|
|
|
this.requestUpdate("select_options");
|
2023-11-02 23:58:38 +01:00
|
|
|
|
|
|
|
return duplicateCount;
|
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
|
|
|
|
*/
|
2023-12-01 17:32:47 +01:00
|
|
|
protected searchMatch(search : string, option : SelectOption) : boolean
|
2022-05-31 01:05:00 +02:00
|
|
|
{
|
2023-09-13 19:55:33 +02:00
|
|
|
if(!option || !option.value)
|
2022-05-31 01:05:00 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2023-09-13 19:55:33 +02:00
|
|
|
if(option.label?.toLowerCase().includes(search.toLowerCase()))
|
2022-05-31 01:05:00 +02:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2023-09-13 19:55:33 +02:00
|
|
|
if(typeof option.value == "string")
|
2022-05-31 01:05:00 +02:00
|
|
|
{
|
2023-09-13 19:55:33 +02:00
|
|
|
return option.value.includes(search.toLowerCase());
|
2022-05-31 01:05:00 +02:00
|
|
|
}
|
2023-09-13 19:55:33 +02:00
|
|
|
return option.value == search;
|
2022-05-31 01:05:00 +02:00
|
|
|
}
|
|
|
|
|
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
|
2023-09-25 18:16:32 +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(),
|
2023-11-23 00:03:01 +01:00
|
|
|
class: "freeEntry",
|
|
|
|
isMatch: false
|
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
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
// Make sure not to double-add, but wait until the option is there
|
2023-09-20 22:24:01 +02:00
|
|
|
if(this.multiple && this.getValueAsArray().indexOf(text) == -1)
|
2022-06-10 18:11:34 +02:00
|
|
|
{
|
2023-09-20 22:24:01 +02:00
|
|
|
let value = this.getValueAsArray();
|
|
|
|
value.push(text);
|
|
|
|
this.value = value;
|
|
|
|
}
|
|
|
|
else if(!this.multiple && this.value !== text)
|
|
|
|
{
|
|
|
|
this.value = text;
|
|
|
|
}
|
2023-09-25 18:16:32 +02:00
|
|
|
this.dispatchEvent(new Event("change", {bubbles: true}));
|
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.__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
|
|
|
{
|
2023-09-13 19:55:33 +02:00
|
|
|
if(abort.relatedTarget?.localName == this.optionTag)
|
2022-08-04 23:50:52 +02:00
|
|
|
{
|
|
|
|
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-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-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;
|
|
|
|
}
|
2023-09-13 19:55:33 +02:00
|
|
|
|
|
|
|
protected _handleSearchClear(e)
|
|
|
|
{
|
|
|
|
e.stopImmediatePropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
this.clearSearch();
|
|
|
|
}
|
2022-05-31 01:05:00 +02:00
|
|
|
}
|
|
|
|
|
2022-06-10 18:11:34 +02:00
|
|
|
return Et2WidgetWithSearch as unknown as Constructor<SearchMixinInterface> & T;
|
2023-09-13 19:55:33 +02:00
|
|
|
});
|