2022-01-04 23:38:10 +01:00
|
|
|
/**
|
2022-03-01 12:35:33 +01:00
|
|
|
* EGroupware eTemplate2 - Select WebComponent
|
2022-01-04 23:38:10 +01:00
|
|
|
*
|
|
|
|
* @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-13 19:55:33 +02:00
|
|
|
import {css, LitElement, nothing, PropertyValues, TemplateResult} from "lit";
|
|
|
|
import {html, literal, StaticValue} from "lit/static-html.js";
|
2023-08-18 16:47:37 +02:00
|
|
|
import {Et2WidgetWithSelectMixin} from "./Et2WidgetWithSelectMixin";
|
2023-09-13 19:55:33 +02:00
|
|
|
import {SelectOption} from "./FindSelectOptions";
|
2022-05-27 22:12:31 +02:00
|
|
|
import shoelace from "../Styles/shoelace";
|
2022-07-12 01:27:17 +02:00
|
|
|
import {RowLimitedMixin} from "../Layout/RowLimitedMixin";
|
2023-09-13 19:55:33 +02:00
|
|
|
import {Et2WithSearchMixin} from "./SearchMixin";
|
|
|
|
import {property} from "lit/decorators/property.js";
|
2023-09-19 00:09:43 +02:00
|
|
|
import {SlChangeEvent, SlOption, SlSelect} from "@shoelace-style/shoelace";
|
2023-09-13 19:55:33 +02:00
|
|
|
import {repeat} from "lit/directives/repeat.js";
|
2023-09-29 21:43:12 +02:00
|
|
|
import {classMap} from "lit/directives/class-map.js";
|
2022-01-04 23:38:10 +01:00
|
|
|
|
2022-03-01 13:31:13 +01:00
|
|
|
// export Et2WidgetWithSelect which is used as type in other modules
|
2023-09-13 19:55:33 +02:00
|
|
|
export class Et2WidgetWithSelect extends RowLimitedMixin(Et2WidgetWithSelectMixin(LitElement))
|
2022-05-27 22:12:31 +02:00
|
|
|
{
|
2023-09-13 19:55:33 +02:00
|
|
|
// Gets an array of all <sl-option> elements
|
|
|
|
protected getAllOptions()
|
|
|
|
{
|
|
|
|
// @ts-ignore
|
|
|
|
return [...this.querySelectorAll<Et2Option>('sl-option')];
|
|
|
|
}
|
2022-05-27 22:12:31 +02:00
|
|
|
};
|
2022-03-01 13:31:13 +01:00
|
|
|
|
2022-06-24 20:10:10 +02:00
|
|
|
/**
|
|
|
|
* Select widget
|
|
|
|
*
|
|
|
|
* At its most basic, you can select one option from a list provided. The list can be passed from the server in
|
|
|
|
* the sel_options array or options can be added as children in the template. Some extending classes provide specific
|
|
|
|
* options, such as Et2SelectPercent or Et2SelectCountry. All provided options will be mixed together and used.
|
|
|
|
*
|
|
|
|
* To allow selecting more than one option, use the attribute multiple="true". This will take & return an array
|
|
|
|
* as value instead of just a string.
|
|
|
|
*
|
|
|
|
* SearchMixin adds additional abilities to ALL select boxes
|
|
|
|
* @see Et2WithSearchMixin
|
|
|
|
*
|
|
|
|
* Override for extending widgets:
|
|
|
|
* # Custom display of selected value
|
|
|
|
* When selecting a single value (!multiple) you can override doLabelChange() to customise the displayed label
|
|
|
|
* @see Et2SelectCategory, which adds in the category icon
|
|
|
|
*
|
|
|
|
* # Custom option rows
|
|
|
|
* Options can have 'class' and 'icon' properties that will be used for the option
|
|
|
|
* The easiest way for further customisation to use CSS in an external file (like etemplate2.css) and ::part().
|
|
|
|
* @see Et2SelectCountry which displays flags via CSS instead of using SelectOption.icon
|
|
|
|
*
|
|
|
|
* # Custom tags
|
|
|
|
* When multiple is set, instead of a single value each selected value is shown in a tag. While it's possible to
|
|
|
|
* use CSS to some degree, we can also use a custom tag class that extends Et2Tag.
|
|
|
|
* 1. Create the extending class
|
|
|
|
* 2. Make sure it's loaded (add to etemplate2.ts)
|
|
|
|
* 3. In your extending Et2Select, override get tagTag() to return the custom tag name
|
|
|
|
*
|
|
|
|
*/
|
2022-06-10 18:25:31 +02:00
|
|
|
// @ts-ignore SlSelect styles is a single CSSResult, not an array, so TS complains
|
2022-06-30 17:39:22 +02:00
|
|
|
export class Et2Select extends Et2WithSearchMixin(Et2WidgetWithSelect)
|
2022-01-04 23:38:10 +01:00
|
|
|
{
|
2022-01-13 23:28:52 +01:00
|
|
|
static get styles()
|
|
|
|
{
|
|
|
|
return [
|
2022-06-10 18:25:31 +02:00
|
|
|
// Parent (SlSelect) returns a single cssResult, not an array
|
2022-05-27 22:12:31 +02:00
|
|
|
shoelace,
|
2022-08-04 23:50:52 +02:00
|
|
|
super.styles,
|
2022-01-13 23:28:52 +01:00
|
|
|
css`
|
2023-09-13 19:55:33 +02:00
|
|
|
:host {
|
2022-04-22 21:07:22 +02:00
|
|
|
display: block;
|
2022-07-07 21:18:42 +02:00
|
|
|
flex: 1 0 auto;
|
2022-06-10 22:11:57 +02:00
|
|
|
--icon-width: 20px;
|
2023-09-13 19:55:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
::slotted(img), img {
|
2022-06-10 22:11:57 +02:00
|
|
|
vertical-align: middle;
|
|
|
|
}
|
|
|
|
|
2022-06-16 00:43:39 +02:00
|
|
|
/* Get rid of padding before/after options */
|
|
|
|
sl-menu::part(base) {
|
|
|
|
padding: 0px;
|
|
|
|
}
|
2022-07-26 17:12:52 +02:00
|
|
|
/* No horizontal scrollbar, even if options are long */
|
|
|
|
.dropdown__panel {
|
|
|
|
overflow-x: clip;
|
2023-04-18 18:36:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Ellipsis when too small */
|
2023-04-19 18:25:49 +02:00
|
|
|
.select__tags {
|
2023-04-18 18:36:34 +02:00
|
|
|
max-width: 100%;
|
|
|
|
}
|
|
|
|
.select__label {
|
2022-08-23 19:07:02 +02:00
|
|
|
display: block;
|
2023-04-18 18:36:34 +02:00
|
|
|
text-overflow: ellipsis;
|
|
|
|
/* This is usually not used due to flex, but is the basis for ellipsis calculation */
|
|
|
|
width: 10ex;
|
|
|
|
}
|
2022-01-04 23:38:10 +01:00
|
|
|
|
2023-02-03 22:08:27 +01:00
|
|
|
/** multiple=true uses tags for each value **/
|
|
|
|
/* styling for icon inside tag (not option) */
|
|
|
|
|
|
|
|
.tag_image {
|
2022-06-16 00:43:39 +02:00
|
|
|
margin-right: var(--sl-spacing-x-small);
|
2023-02-03 22:08:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Maximum height + scrollbar on tags (+ other styling) */
|
|
|
|
|
|
|
|
.select__tags {
|
2022-06-24 22:12:04 +02:00
|
|
|
margin-left: 0px;
|
2022-07-12 01:27:17 +02:00
|
|
|
max-height: initial;
|
2022-06-16 00:43:39 +02:00
|
|
|
overflow-y: auto;
|
|
|
|
gap: 0.1rem 0.5rem;
|
2023-02-03 22:08:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
.select--medium .select__tags {
|
2022-10-13 17:33:04 +02:00
|
|
|
padding-top: 2px;
|
|
|
|
padding-bottom: 2px;
|
2023-02-03 22:08:27 +01:00
|
|
|
}
|
|
|
|
|
2023-02-23 00:23:56 +01:00
|
|
|
:host([rows]) .select__control > .select__label > .select__tags {
|
2023-02-16 19:58:56 +01:00
|
|
|
max-height: calc(var(--rows, 5) * 29px);
|
|
|
|
}
|
|
|
|
|
|
|
|
:host([rows='1']) .select__tags {
|
2023-02-06 22:48:55 +01:00
|
|
|
overflow: hidden;
|
2023-02-03 22:08:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Keep overflow tag right-aligned. It's the only sl-tag. */
|
|
|
|
|
|
|
|
.select__tags sl-tag {
|
2022-06-16 00:43:39 +02:00
|
|
|
margin-left: auto;
|
2023-02-03 22:08:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
select:hover {
|
2022-01-13 23:28:52 +01:00
|
|
|
box-shadow: 1px 1px 1px rgb(0 0 0 / 60%);
|
2023-02-03 22:08:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Hide dropdown trigger when multiple & readonly */
|
|
|
|
|
2023-09-14 22:30:20 +02:00
|
|
|
:host([readonly][multiple])::part(expand-icon) {
|
2023-02-03 22:08:27 +01:00
|
|
|
display: none;
|
|
|
|
}
|
2023-02-06 22:48:55 +01:00
|
|
|
|
2023-02-15 21:22:27 +01:00
|
|
|
/* Style for tag count if rows=1 */
|
|
|
|
|
2023-09-14 22:30:20 +02:00
|
|
|
:host([readonly][multiple][rows])::part(tags) {
|
2023-02-15 21:22:27 +01:00
|
|
|
position: absolute;
|
|
|
|
right: 0px;
|
|
|
|
top: 1px;
|
|
|
|
box-shadow: rgb(0 0 0/50%) -1.5ex 0px 1ex -1ex, rgb(0 0 0 / 0%) 0px 0px 0px 0px;
|
|
|
|
}
|
|
|
|
|
|
|
|
:host([readonly][multiple][rows]) .select__tags sl-tag::part(base) {
|
|
|
|
background-color: var(--sl-input-background-color);
|
|
|
|
border-top-left-radius: 0;
|
|
|
|
border-bottom-left-radius: 0;
|
|
|
|
font-weight: bold;
|
|
|
|
min-width: 3em;
|
|
|
|
justify-content: center;
|
|
|
|
}
|
|
|
|
|
2023-02-06 22:48:55 +01:00
|
|
|
/* Show all rows on hover if rows=1 */
|
|
|
|
|
|
|
|
:host([readonly][multiple][rows]):hover .select__tags {
|
|
|
|
width: -webkit-fill-available;
|
|
|
|
width: -moz-fill-available;
|
|
|
|
width: fill-available;
|
|
|
|
}
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
::part(listbox) {
|
2023-02-06 22:48:55 +01:00
|
|
|
z-index: 1;
|
|
|
|
background: var(--sl-input-background-color);
|
|
|
|
padding: var(--sl-input-spacing-small);
|
2023-02-07 20:04:05 +01:00
|
|
|
padding-left: 2px;
|
|
|
|
|
|
|
|
box-shadow: var(--sl-shadow-large);
|
|
|
|
min-width: fit-content;
|
|
|
|
border-radius: var(--sl-border-radius-small);
|
|
|
|
border: 1px solid var(--sl-color-neutral-200);
|
|
|
|
max-height: 15em;
|
|
|
|
overflow-y: auto;
|
|
|
|
}
|
|
|
|
|
|
|
|
::part(display-label) {
|
|
|
|
margin: 0;
|
2023-02-06 22:48:55 +01:00
|
|
|
}
|
2023-09-13 19:55:33 +02:00
|
|
|
|
|
|
|
:host::part(display-label) {
|
|
|
|
max-height: 8em;
|
2023-03-31 14:07:30 +02:00
|
|
|
overflow-y: auto;
|
2023-09-13 19:55:33 +02:00
|
|
|
}
|
2023-09-19 14:03:26 +02:00
|
|
|
:host([readonly])::part(combobox) {
|
|
|
|
background: none;
|
|
|
|
opacity: 1;
|
|
|
|
border: none;
|
|
|
|
}
|
2023-02-03 22:08:27 +01:00
|
|
|
`
|
2022-01-13 23:28:52 +01:00
|
|
|
];
|
|
|
|
}
|
2022-01-04 23:38:10 +01:00
|
|
|
|
2022-05-27 23:39:51 +02:00
|
|
|
static get properties()
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
...super.properties,
|
|
|
|
/**
|
|
|
|
* Toggle between single and multiple selection
|
|
|
|
*/
|
|
|
|
multiple: {
|
|
|
|
type: Boolean,
|
|
|
|
reflect: true,
|
2023-02-08 17:10:52 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Click handler for individual tags instead of the select as a whole.
|
|
|
|
* Only used if multiple=true so we have tags
|
|
|
|
*/
|
|
|
|
onTagClick: {
|
|
|
|
type: Function,
|
2022-05-27 23:39:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
|
|
|
|
/** Placeholder text to show as a hint when the select is empty. */
|
|
|
|
@property() placeholder = '';
|
|
|
|
/** Allows more than one option to be selected. */
|
|
|
|
@property({type: Boolean, reflect: true}) multiple = false;
|
|
|
|
/** Disables the select control. */
|
|
|
|
@property({type: Boolean, reflect: true}) disabled = false;
|
|
|
|
|
|
|
|
/** Adds a clear button when the select is not empty. */
|
|
|
|
@property({type: Boolean}) clearable = false;
|
|
|
|
|
|
|
|
/** The select's label. If you need to display HTML, use the `label` slot instead. */
|
|
|
|
@property() label = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The preferred placement of the select's menu. Note that the actual placement may vary as needed to keep the listbox
|
|
|
|
* inside of the viewport.
|
|
|
|
*/
|
|
|
|
@property({reflect: true}) placement : 'top' | 'bottom' = 'bottom';
|
|
|
|
|
|
|
|
/** The select's help text. If you need to display HTML, use the `help-text` slot instead. */
|
|
|
|
@property({attribute: 'help-text'}) helpText = '';
|
|
|
|
|
|
|
|
/** The select's required attribute. */
|
|
|
|
@property({type: Boolean, reflect: true}) required = false;
|
|
|
|
|
|
|
|
|
|
|
|
private __value : string | string[] = "";
|
|
|
|
|
|
|
|
constructor()
|
|
|
|
{
|
|
|
|
super();
|
|
|
|
this.hoist = true;
|
|
|
|
|
|
|
|
this._tagTemplate = this._tagTemplate.bind(this);
|
|
|
|
}
|
2022-07-26 15:46:42 +02:00
|
|
|
/**
|
|
|
|
* List of properties that get translated
|
|
|
|
*
|
|
|
|
* @returns object
|
|
|
|
*/
|
|
|
|
static get translate()
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
...super.translate,
|
|
|
|
emptyLabel: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-13 23:28:52 +01:00
|
|
|
connectedCallback()
|
|
|
|
{
|
|
|
|
super.connectedCallback();
|
2022-06-23 23:55:42 +02:00
|
|
|
this.updateComplete.then(() =>
|
2022-05-27 23:39:51 +02:00
|
|
|
{
|
2022-06-07 00:41:29 +02:00
|
|
|
this.addEventListener("sl-change", this._triggerChange);
|
2023-09-13 19:55:33 +02:00
|
|
|
// Fixes missing empty label
|
|
|
|
this.select?.requestUpdate("value");
|
|
|
|
// Fixes incorrect opening position
|
2023-09-28 19:00:30 +02:00
|
|
|
this.select?.popup?.handleAnchorChange();
|
2022-05-27 23:39:51 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnectedCallback()
|
|
|
|
{
|
|
|
|
super.disconnectedCallback();
|
|
|
|
|
2022-06-08 22:01:00 +02:00
|
|
|
this.removeEventListener("sl-change", this._triggerChange);
|
2022-01-13 23:28:52 +01:00
|
|
|
}
|
|
|
|
|
2022-05-27 23:39:51 +02:00
|
|
|
_triggerChange(e)
|
|
|
|
{
|
2023-09-25 17:37:48 +02:00
|
|
|
if(super._triggerChange(e))
|
2022-08-31 21:27:10 +02:00
|
|
|
{
|
2023-04-19 18:03:54 +02:00
|
|
|
this.dispatchEvent(new Event("change", {bubbles: true}));
|
2022-08-31 21:27:10 +02:00
|
|
|
}
|
2022-05-27 23:39:51 +02:00
|
|
|
}
|
|
|
|
|
2023-09-14 19:48:30 +02:00
|
|
|
/**
|
|
|
|
* Handle the case where there is no value set, or the value provided is not an option.
|
|
|
|
* If this happens, we choose the first option or empty label.
|
|
|
|
*
|
|
|
|
* Careful when this is called. We change the value here, so an infinite loop is possible if the widget has
|
|
|
|
* onchange.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
protected fix_bad_value()
|
|
|
|
{
|
|
|
|
// Stop if there are no options
|
|
|
|
if(!Array.isArray(this.select_options) || this.select_options.length == 0)
|
|
|
|
{
|
|
|
|
// Nothing to do here
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// emptyLabel is fine
|
|
|
|
if(this.value === "" && this.emptyLabel)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let valueArray = this.getValueAsArray();
|
|
|
|
|
|
|
|
// Check for value using missing options (deleted or otherwise not allowed)
|
|
|
|
let filtered = this.filterOutMissingOptions(valueArray);
|
|
|
|
if(filtered.length != valueArray.length)
|
|
|
|
{
|
|
|
|
this.value = filtered;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Multiple is allowed to be empty, and if we don't have an emptyLabel or options nothing to do
|
|
|
|
if(this.multiple || (!this.emptyLabel && this.select_options.length === 0))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if parent (search / free entry) is OK with it
|
|
|
|
if(super.fix_bad_value())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// If somebody gave '' as a select_option, let it be
|
|
|
|
if(this.value === '' && this.select_options.filter((option) => this.value === option.value).length == 1)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// If no value is set, choose the first option
|
|
|
|
// Only do this on once during initial setup, or it can be impossible to clear the value
|
|
|
|
|
|
|
|
// value not in options --> use emptyLabel, if exists, or first option otherwise
|
|
|
|
if(this.select_options.filter((option) => valueArray.find(val => val == option.value) ||
|
|
|
|
Array.isArray(option.value) && option.value.filter(o => valueArray.find(val => val == o.value))).length === 0)
|
|
|
|
{
|
|
|
|
let oldValue = this.value;
|
|
|
|
this.value = this.emptyLabel ? "" : "" + this.select_options[0]?.value;
|
|
|
|
// ""+ to cast value of 0 to "0", to not replace with ""
|
|
|
|
this.requestUpdate("value", oldValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
@property()
|
|
|
|
get value()
|
2022-03-03 18:38:56 +01:00
|
|
|
{
|
2023-09-19 21:01:49 +02:00
|
|
|
// Handle a bunch of non-values, if it's multiple we want an array
|
2023-09-20 16:20:22 +02:00
|
|
|
if(this.multiple && (this.__value == "null" || this.__value == null || typeof this.__value == "undefined" ||
|
|
|
|
!this.emptyLabel && this.__value == "" && !this.select_options.find(o => o.value == "")))
|
2023-09-19 21:01:49 +02:00
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
2023-09-20 16:20:22 +02:00
|
|
|
if(!this.multiple && !this.emptyLabel && this.__value == "" && !this.select_options.find(o => o.value == ""))
|
2023-09-19 21:01:49 +02:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2023-09-13 19:55:33 +02:00
|
|
|
return this.multiple ?
|
|
|
|
this.__value ?? [] :
|
|
|
|
this.__value ?? "";
|
2022-03-03 18:38:56 +01:00
|
|
|
}
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
// @ts-ignore
|
|
|
|
set value(val : string | string[] | number | number[])
|
2022-06-23 23:55:42 +02:00
|
|
|
{
|
2023-09-13 19:55:33 +02:00
|
|
|
if(typeof val === "undefined" || val == null)
|
2022-07-04 23:08:00 +02:00
|
|
|
{
|
2023-09-13 19:55:33 +02:00
|
|
|
val = "";
|
2022-07-04 23:08:00 +02:00
|
|
|
}
|
2023-04-03 22:55:04 +02:00
|
|
|
if(typeof val === 'string' && val.indexOf(',') !== -1 && this.multiple)
|
2022-07-19 19:47:03 +02:00
|
|
|
{
|
|
|
|
val = val.split(',');
|
|
|
|
}
|
|
|
|
if(typeof val === 'number')
|
2022-06-10 13:07:27 +02:00
|
|
|
{
|
|
|
|
val = val.toString();
|
|
|
|
}
|
2023-09-13 19:55:33 +02:00
|
|
|
const oldValue = this.value;
|
2022-07-19 19:47:03 +02:00
|
|
|
if(Array.isArray(val))
|
2022-06-18 15:06:50 +02:00
|
|
|
{
|
2023-02-13 22:00:16 +01:00
|
|
|
// Make sure value has no duplicates, and values are strings
|
2023-09-13 19:55:33 +02:00
|
|
|
this.__value = <string[]>[...new Set(val.map(v => (typeof v === 'number' ? v.toString() : v || '')))];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.__value = val;
|
|
|
|
}
|
|
|
|
if(this.multiple && typeof this.__value == "string")
|
|
|
|
{
|
|
|
|
this.__value = this.__value != "" ? [this.__value] : [];
|
2022-06-18 15:06:50 +02:00
|
|
|
}
|
2023-09-13 19:55:33 +02:00
|
|
|
if(this.select)
|
|
|
|
{
|
|
|
|
this.select.value = this.__value;
|
|
|
|
}
|
|
|
|
this.requestUpdate("value", oldValue);
|
2022-06-02 16:12:38 +02:00
|
|
|
}
|
|
|
|
|
2023-05-05 19:08:47 +02:00
|
|
|
/**
|
|
|
|
* Check a value for missing options and remove them.
|
|
|
|
*
|
|
|
|
* We'll warn about it in the helpText, and if they save the change will be made.
|
|
|
|
* This is to avoid the server-side validation error, which the user can't do much about.
|
|
|
|
*
|
|
|
|
* @param {string[]} value
|
|
|
|
* @returns {string[]}
|
|
|
|
*/
|
|
|
|
filterOutMissingOptions(value : string[]) : string[]
|
|
|
|
{
|
|
|
|
if(!this.readonly && value && value.length > 0 && !this.allowFreeEntries && this.select_options.length > 0)
|
|
|
|
{
|
2023-05-16 19:02:20 +02:00
|
|
|
function filterBySelectOptions(arrayToFilter, options : SelectOption[])
|
|
|
|
{
|
|
|
|
const filteredArray = arrayToFilter.filter(item =>
|
|
|
|
{
|
|
|
|
// Check if item is found in options
|
|
|
|
return !options.some(option =>
|
|
|
|
{
|
|
|
|
if(typeof option.value === 'string')
|
|
|
|
{
|
|
|
|
// Regular option
|
|
|
|
return option.value === item;
|
|
|
|
}
|
|
|
|
else if(Array.isArray(option.value))
|
|
|
|
{
|
|
|
|
// Recursively check if item is found in nested array (option groups)
|
|
|
|
return filterBySelectOptions([item], option.value).length > 0;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return filteredArray;
|
|
|
|
}
|
|
|
|
|
2023-09-14 19:48:30 +02:00
|
|
|
// Empty is allowed, if there's an emptyLabel
|
|
|
|
if(value.toString() == "" && this.emptyLabel)
|
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2023-05-16 19:02:20 +02:00
|
|
|
const missing = filterBySelectOptions(value, this.select_options);
|
2023-05-05 19:08:47 +02:00
|
|
|
if(missing.length > 0)
|
|
|
|
{
|
2023-09-14 19:48:30 +02:00
|
|
|
debugger;
|
2023-09-19 21:01:49 +02:00
|
|
|
console.warn("Invalid option '" + missing.join(", ") + "' removed from " + this.id, this);
|
2023-05-05 19:08:47 +02:00
|
|
|
value = value.filter(item => missing.indexOf(item) == -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2022-07-04 23:08:00 +02:00
|
|
|
/**
|
2023-09-19 21:01:49 +02:00
|
|
|
* Additional customisations from the XET node
|
2022-07-04 23:08:00 +02:00
|
|
|
*
|
2023-09-19 21:01:49 +02:00
|
|
|
* @param {Element} _node
|
2022-07-04 23:08:00 +02:00
|
|
|
*/
|
|
|
|
loadFromXML(_node : Element)
|
|
|
|
{
|
|
|
|
super.loadFromXML(_node);
|
2022-10-03 17:12:00 +02:00
|
|
|
|
|
|
|
// Wait for update to be complete before we check for bad value so extending selects can have a chance
|
|
|
|
this.updateComplete.then(() => this.fix_bad_value());
|
2022-07-04 23:08:00 +02:00
|
|
|
}
|
|
|
|
|
2022-02-18 20:02:27 +01:00
|
|
|
/** @param {import('@lion/core').PropertyValues } changedProperties */
|
2022-06-23 23:55:42 +02:00
|
|
|
willUpdate(changedProperties : PropertyValues)
|
2022-01-13 23:28:52 +01:00
|
|
|
{
|
2022-06-23 23:55:42 +02:00
|
|
|
super.willUpdate(changedProperties);
|
2022-03-03 18:38:56 +01:00
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
if(changedProperties.has("multiple"))
|
|
|
|
{
|
|
|
|
this.value = this.__value;
|
|
|
|
}
|
|
|
|
if(changedProperties.has("select_options") || changedProperties.has("value") || changedProperties.has("emptyLabel"))
|
2022-01-04 23:38:10 +01:00
|
|
|
{
|
2022-10-03 17:12:00 +02:00
|
|
|
this.updateComplete.then(() => this.fix_bad_value());
|
2022-08-04 00:04:08 +02:00
|
|
|
}
|
|
|
|
if(changedProperties.has("select_options") && changedProperties.has("value"))
|
|
|
|
{
|
2022-01-04 23:38:10 +01:00
|
|
|
}
|
2022-01-13 23:28:52 +01:00
|
|
|
}
|
|
|
|
|
2022-06-16 00:43:39 +02:00
|
|
|
/**
|
|
|
|
* Tag used for rendering tags when multiple=true
|
|
|
|
* Used for creating, finding & filtering options.
|
|
|
|
* @see createTagNode()
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2023-09-13 19:55:33 +02:00
|
|
|
public get tagTag() : StaticValue
|
|
|
|
{
|
|
|
|
return literal`et2-tag`;
|
|
|
|
}
|
|
|
|
|
2023-05-09 22:04:02 +02:00
|
|
|
blur()
|
|
|
|
{
|
|
|
|
if(typeof super.blur == "function")
|
|
|
|
{
|
|
|
|
super.blur();
|
|
|
|
}
|
2023-09-14 22:33:16 +02:00
|
|
|
this.hide();
|
2023-05-09 22:04:02 +02:00
|
|
|
}
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
/* Parent should be fine now?
|
2023-02-07 18:04:11 +01:00
|
|
|
private handleTagRemove(event : CustomEvent, option)
|
|
|
|
{
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
|
|
if(!this.disabled)
|
|
|
|
{
|
|
|
|
option.selected = false;
|
|
|
|
let index = this.value.indexOf(option.value);
|
|
|
|
if(index > -1)
|
|
|
|
{
|
|
|
|
this.value.splice(index, 1);
|
|
|
|
}
|
|
|
|
this.dispatchEvent(new CustomEvent('sl-input'));
|
|
|
|
this.dispatchEvent(new CustomEvent('sl-change'));
|
2023-05-18 18:48:33 +02:00
|
|
|
this.validate();
|
2023-02-07 18:04:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
*/
|
|
|
|
|
2023-03-23 22:23:32 +01:00
|
|
|
/**
|
2023-04-17 17:28:27 +02:00
|
|
|
* Apply the user preference to close the dropdown if an option is clicked, even if multiple=true.
|
|
|
|
* The default (from SlSelect) leaves the dropdown open for multiple=true
|
2023-03-23 22:23:32 +01:00
|
|
|
*
|
|
|
|
* @param {MouseEvent} event
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
private handleOptionClick(event : MouseEvent)
|
|
|
|
{
|
2023-09-13 19:55:33 +02:00
|
|
|
super.handleOptionClick(event);
|
|
|
|
|
2023-09-27 19:15:13 +02:00
|
|
|
// Only interested in option clicks, but handler is bound higher
|
|
|
|
if(event.target.tagName !== "SL-OPTION")
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-17 17:28:27 +02:00
|
|
|
if(this._close_on_select)
|
2023-03-23 22:23:32 +01:00
|
|
|
{
|
2023-09-13 19:55:33 +02:00
|
|
|
this.hide();
|
2023-04-17 17:28:27 +02:00
|
|
|
}
|
2023-03-23 22:23:32 +01:00
|
|
|
}
|
|
|
|
|
2023-05-09 22:04:02 +02:00
|
|
|
private et2HandleBlur(event : Event)
|
|
|
|
{
|
|
|
|
if(typeof super.et2HandleBlur === "function")
|
|
|
|
{
|
|
|
|
super.et2HandleBlur(event);
|
|
|
|
}
|
|
|
|
this.dropdown?.hide();
|
|
|
|
}
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
|
|
|
|
protected handleValueChange(e : SlChangeEvent)
|
|
|
|
{
|
2023-09-28 19:00:30 +02:00
|
|
|
// Only interested when selected value changes, not any nested inputs
|
|
|
|
if(e.target !== this.select)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
const old_value = this.__value;
|
2023-09-20 00:04:19 +02:00
|
|
|
this.__value = Array.isArray(this.select.value) ?
|
|
|
|
this.select.value.map(e => e.replaceAll("___", " ")) :
|
|
|
|
this.select.value.replaceAll("___", " ");
|
2023-09-13 19:55:33 +02:00
|
|
|
this.requestUpdate("value", old_value);
|
|
|
|
}
|
|
|
|
|
2023-03-23 23:02:09 +01:00
|
|
|
/**
|
|
|
|
* Always close the dropdown if an option is clicked, even if multiple=true. This differs from SlSelect,
|
|
|
|
* which leaves the dropdown open for multiple=true
|
|
|
|
*
|
|
|
|
* @param {KeyboardEvent} event
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
private handleKeyDown(event : KeyboardEvent)
|
|
|
|
{
|
|
|
|
if(event.key === 'Enter' || (event.key === ' ' && this.typeToSelectString === ''))
|
|
|
|
{
|
|
|
|
this.dropdown.hide().then(() =>
|
|
|
|
{
|
|
|
|
if(typeof this.handleMenuHide == "function")
|
|
|
|
{
|
|
|
|
// Make sure search gets hidden
|
|
|
|
this.handleMenuHide();
|
|
|
|
}
|
|
|
|
});
|
2023-03-24 00:21:32 +01:00
|
|
|
event.stopPropagation();
|
2023-03-23 23:02:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
/** Shows the listbox. */
|
|
|
|
async show()
|
2022-06-29 19:49:24 +02:00
|
|
|
{
|
2023-09-13 19:55:33 +02:00
|
|
|
return this.select.show();
|
2022-06-29 19:49:24 +02:00
|
|
|
}
|
2022-01-13 23:28:52 +01:00
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
/** Hides the listbox. */
|
|
|
|
async hide()
|
2022-01-17 23:12:46 +01:00
|
|
|
{
|
2023-09-13 19:55:33 +02:00
|
|
|
this.select.hide();
|
2023-08-28 21:50:09 +02:00
|
|
|
}
|
2022-03-16 21:36:23 +01:00
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
get open()
|
2023-08-28 21:50:09 +02:00
|
|
|
{
|
2023-09-13 19:55:33 +02:00
|
|
|
return this.select?.open ?? false;
|
2022-01-17 23:12:46 +01:00
|
|
|
}
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
protected _renderOptions()
|
|
|
|
{return Promise.resolve();}
|
2022-01-17 23:12:46 +01:00
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
protected get select() : SlSelect
|
2022-07-19 15:17:16 +02:00
|
|
|
{
|
2023-09-13 19:55:33 +02:00
|
|
|
return this.shadowRoot?.querySelector("sl-select");
|
2022-07-19 15:17:16 +02:00
|
|
|
}
|
|
|
|
|
2023-09-19 00:09:43 +02:00
|
|
|
/**
|
|
|
|
* Custom, dynamic styling
|
|
|
|
*
|
|
|
|
* Put as much as you can in static styles for performance reasons
|
|
|
|
* Override this for custom dynamic styles
|
|
|
|
*
|
|
|
|
* @returns {TemplateResult}
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected _styleTemplate() : TemplateResult
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used for the "no value" option for single select
|
|
|
|
* Placeholder is used for multi-select with no value
|
|
|
|
*
|
|
|
|
* @returns {TemplateResult}
|
|
|
|
*/
|
|
|
|
_emptyLabelTemplate() : TemplateResult
|
|
|
|
{
|
|
|
|
if(!this.emptyLabel || this.multiple)
|
|
|
|
{
|
|
|
|
return html``;
|
|
|
|
}
|
|
|
|
return html`
|
2023-09-19 21:53:15 +02:00
|
|
|
<sl-option
|
2023-09-19 23:20:36 +02:00
|
|
|
part="emptyLabel option"
|
2023-09-19 21:53:15 +02:00
|
|
|
value=""
|
|
|
|
.selected=${this.getValueAsArray().some(v => v == "")}
|
2023-09-19 00:09:43 +02:00
|
|
|
>
|
|
|
|
${this.emptyLabel}
|
|
|
|
</sl-option>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterate over all the options
|
|
|
|
* @returns {TemplateResult}
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected _optionsTemplate() : TemplateResult
|
|
|
|
{
|
|
|
|
return html`${repeat(this.select_options
|
|
|
|
// Filter out empty values if we have empty label to avoid duplicates
|
|
|
|
.filter(o => this.emptyLabel ? o.value !== '' : o), this._groupTemplate.bind(this))
|
|
|
|
}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to render each option into the select
|
|
|
|
* Override for custom select options. Note that spaces are not allowed in option values,
|
|
|
|
* and sl-select _requires_ options to be <sl-option>
|
|
|
|
*
|
|
|
|
* @param {SelectOption} option
|
|
|
|
* @returns {TemplateResult}
|
|
|
|
*/
|
|
|
|
protected _optionTemplate(option : SelectOption) : TemplateResult
|
|
|
|
{
|
|
|
|
// Exclude non-matches when searching
|
2023-09-29 21:43:12 +02:00
|
|
|
// unless they're already selected, in which case removing them removes them from value
|
|
|
|
if(typeof option.isMatch == "boolean" && !option.isMatch && !this.getValueAsArray().includes(option.value))
|
2023-09-19 00:09:43 +02:00
|
|
|
{
|
|
|
|
return html``;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tag used must match this.optionTag, but you can't use the variable directly.
|
|
|
|
// Pass option along so SearchMixin can grab it if needed
|
|
|
|
const value = (<string>option.value).replaceAll(" ", "___");
|
|
|
|
return html`
|
|
|
|
<sl-option
|
|
|
|
part="option"
|
|
|
|
value="${value}"
|
|
|
|
title="${!option.title || this.noLang ? option.title : this.egw().lang(option.title)}"
|
2023-09-29 21:43:12 +02:00
|
|
|
class=${classMap({
|
|
|
|
"match": option.isMatch,
|
|
|
|
"no-match": !option.isMatch,
|
|
|
|
...Object.fromEntries((option.class || "").split(" ").map(k => [k, true]))
|
|
|
|
})}
|
|
|
|
.option=${option}
|
2023-09-19 00:09:43 +02:00
|
|
|
.selected=${this.getValueAsArray().some(v => v == value)}
|
|
|
|
?disabled=${option.disabled}
|
|
|
|
>
|
|
|
|
${this._iconTemplate(option)}
|
|
|
|
${this.noLang ? option.label : this.egw().lang(option.label)}
|
|
|
|
</sl-option>`;
|
|
|
|
}
|
|
|
|
|
2023-09-20 22:24:01 +02:00
|
|
|
/**
|
|
|
|
* Get the icon for the select option
|
|
|
|
*
|
|
|
|
* @param option
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected _iconTemplate(option)
|
|
|
|
{
|
|
|
|
if(!option.icon)
|
|
|
|
{
|
|
|
|
return html``;
|
|
|
|
}
|
|
|
|
|
|
|
|
return html`
|
|
|
|
<et2-image slot="prefix" part="icon" style="width: var(--icon-width)"
|
|
|
|
src="${option.icon}"></et2-image>`
|
|
|
|
}
|
|
|
|
|
2023-09-19 00:09:43 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom tag
|
|
|
|
*
|
|
|
|
* Override this to customise display when multiple=true.
|
|
|
|
* There is no restriction on the tag used, unlike _optionTemplate()
|
|
|
|
*
|
|
|
|
* @param {Et2Option} option
|
|
|
|
* @param {number} index
|
|
|
|
* @returns {TemplateResult}
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected _tagTemplate(option : SlOption, index : number) : TemplateResult
|
|
|
|
{
|
|
|
|
const readonly = (this.readonly || option && typeof (option.disabled) != "undefined" && option.disabled);
|
|
|
|
const isEditable = this.editModeEnabled && !readonly;
|
2023-09-20 22:24:01 +02:00
|
|
|
const image = this._iconTemplate(option);
|
2023-09-19 00:09:43 +02:00
|
|
|
const tagName = this.tagTag;
|
|
|
|
return html`
|
|
|
|
<${tagName}
|
|
|
|
part="tag"
|
|
|
|
exportparts="
|
|
|
|
base:tag__base,
|
|
|
|
content:tag__content,
|
|
|
|
remove-button:tag__remove-button,
|
|
|
|
remove-button__base:tag__remove-button__base,
|
|
|
|
icon:icon
|
|
|
|
"
|
|
|
|
class=${"search_tag " + option.classList.value}
|
|
|
|
?pill=${this.pill}
|
|
|
|
size=${this.size || "medium"}
|
|
|
|
?removable=${!readonly}
|
|
|
|
?readonly=${readonly}
|
|
|
|
?editable=${isEditable}
|
|
|
|
.value=${option.value.replaceAll("___", " ")}
|
2023-09-20 22:24:01 +02:00
|
|
|
@change=${this.handleTagEdit}
|
2023-09-19 00:09:43 +02:00
|
|
|
@dblclick=${this._handleDoubleClick}
|
|
|
|
@click=${typeof this.onTagClick == "function" ? (e) => this.onTagClick(e, e.target) : nothing}
|
|
|
|
>
|
|
|
|
${image ?? nothing}
|
|
|
|
${option.getTextLabel().trim()}
|
|
|
|
</${tagName}>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Additional customisation template
|
|
|
|
* Override if needed. Added after select options.
|
|
|
|
*
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected _extraTemplate() : TemplateResult | typeof nothing
|
|
|
|
{
|
|
|
|
return typeof super._extraTemplate == "function" ? super._extraTemplate() : nothing;
|
|
|
|
}
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
public render()
|
2022-07-19 15:17:16 +02:00
|
|
|
{
|
2023-09-13 19:55:33 +02:00
|
|
|
const value = Array.isArray(this.value) ?
|
|
|
|
this.value.map(v => { return v.replaceAll(" ", "___"); }) :
|
|
|
|
(typeof this.value == "string" ? this.value.replaceAll(" ", "___") : "");
|
2022-07-19 15:17:16 +02:00
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
let icon : TemplateResult | typeof nothing = nothing;
|
|
|
|
if(!this.multiple)
|
2022-01-17 23:12:46 +01:00
|
|
|
{
|
2023-09-13 19:55:33 +02:00
|
|
|
const icon_option = this.select_options.find(o => (o.value == value || Array.isArray(value) && value.includes(o.value)) && o.icon);
|
|
|
|
if(icon_option)
|
2022-01-17 23:12:46 +01:00
|
|
|
{
|
2023-09-13 19:55:33 +02:00
|
|
|
icon = this._iconTemplate(icon_option);
|
2022-01-17 23:12:46 +01:00
|
|
|
}
|
|
|
|
}
|
2023-09-13 19:55:33 +02:00
|
|
|
return html`
|
2023-09-19 00:09:43 +02:00
|
|
|
${this._styleTemplate()}
|
2023-09-13 19:55:33 +02:00
|
|
|
<sl-select
|
|
|
|
exportparts="prefix, tags, display-input, expand-icon, combobox, listbox, option"
|
|
|
|
label=${this.label}
|
2023-09-19 21:01:49 +02:00
|
|
|
placeholder=${this.placeholder || (this.multiple && this.emptyLabel ? this.emptyLabel : "")}
|
2023-09-13 19:55:33 +02:00
|
|
|
?multiple=${this.multiple}
|
2023-09-19 00:09:43 +02:00
|
|
|
?disabled=${this.disabled || this.readonly}
|
2023-09-13 19:55:33 +02:00
|
|
|
?clearable=${this.clearable}
|
|
|
|
?required=${this.required}
|
|
|
|
helpText=${this.helpText}
|
|
|
|
hoist
|
|
|
|
placement=${this.placement}
|
|
|
|
.getTag=${this._tagTemplate}
|
|
|
|
.maxOptionsVisible=${0}
|
|
|
|
.value=${value}
|
|
|
|
@sl-change=${this.handleValueChange}
|
2023-09-27 19:15:13 +02:00
|
|
|
@mouseup=${this.handleOptionClick}
|
2023-09-13 19:55:33 +02:00
|
|
|
>
|
|
|
|
${icon}
|
|
|
|
${this._emptyLabelTemplate()}
|
|
|
|
${this._optionsTemplate()}
|
|
|
|
${this._extraTemplate()}
|
|
|
|
<slot></slot>
|
|
|
|
</sl-select>
|
|
|
|
`;
|
2022-01-17 23:12:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
if(typeof customElements.get("et2-select") === "undefined")
|
2022-01-17 23:12:46 +01:00
|
|
|
{
|
2023-09-13 19:55:33 +02:00
|
|
|
customElements.define("et2-select", Et2Select);
|
2022-01-17 23:12:46 +01:00
|
|
|
}
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
declare global
|
2023-02-06 15:52:58 +01:00
|
|
|
{
|
2023-09-13 19:55:33 +02:00
|
|
|
interface HTMLElementTagNameMap
|
2023-02-06 15:52:58 +01:00
|
|
|
{
|
2023-09-13 19:55:33 +02:00
|
|
|
"et2-select" : Et2Select;
|
2023-02-06 15:52:58 +01:00
|
|
|
}
|
2023-09-13 19:55:33 +02:00
|
|
|
}
|