2022-03-01 12:35:33 +01:00
|
|
|
/**
|
|
|
|
* EGroupware eTemplate2 - WidgetWithSelectMixin
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package api
|
|
|
|
* @link https://www.egroupware.org
|
|
|
|
* @author Nathan Gray
|
|
|
|
*/
|
|
|
|
|
2022-06-10 18:25:31 +02:00
|
|
|
import {Et2InputWidget, Et2InputWidgetInterface} from "../Et2InputWidget/Et2InputWidget";
|
2024-03-15 21:13:56 +01:00
|
|
|
import {html, LitElement, PropertyValues, TemplateResult} from "lit";
|
2023-09-13 19:55:33 +02:00
|
|
|
import {property} from "lit/decorators/property.js";
|
2022-03-01 12:35:33 +01:00
|
|
|
import {et2_readAttrWithDefault} from "../et2_core_xml";
|
2022-06-01 17:23:07 +02:00
|
|
|
import {cleanSelectOptions, find_select_options, SelectOption} from "./FindSelectOptions";
|
2024-02-22 22:25:12 +01:00
|
|
|
|
|
|
|
import {SearchMixinInterface} from "../Et2Widget/SearchMixin";
|
2022-03-01 12:35:33 +01:00
|
|
|
|
|
|
|
/**
|
2024-02-21 00:20:00 +01:00
|
|
|
* @summary Base class for things that do selectbox type behaviour, to avoid putting too much or copying into read-only
|
2022-03-01 12:35:33 +01:00
|
|
|
* selectboxes, also for common handling of properties for more special selectboxes.
|
|
|
|
*
|
2023-09-13 19:55:33 +02:00
|
|
|
* As with most other widgets that extend Shoelace components, do not override render() without good reason.
|
2022-03-03 18:38:56 +01:00
|
|
|
* To extend this mixin, override:
|
|
|
|
* - _optionTargetNode(): Return the HTMLElement where the "options" go.
|
|
|
|
* - _optionTemplate(option:SelectOption): Renders the option. To use a special widget, use its tag in render.
|
|
|
|
* Select option:
|
|
|
|
* ```js
|
|
|
|
* return html`
|
|
|
|
* <option value="${option.value}" title="${option.title}" ?selected=${option.value == this.modelValue}>
|
|
|
|
* ${option.label}
|
|
|
|
* </option>`;
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* or pass it off to a different WebComponent:
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* _optionTemplate(option:SelectOption) : TemplateResult
|
|
|
|
* {
|
|
|
|
* return html`
|
|
|
|
* <special-option-tag .value=${option}></special-option-tag>`;
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Optionally, you can override:
|
|
|
|
* - _emptyLabelTemplate(): How to render the empty label
|
|
|
|
* You can specify something else, or return {} to do your own thing. This is a little more complicated. You should
|
|
|
|
* also override _inputGroupInputTemplate() to do what you normally would in render().
|
|
|
|
*
|
2022-03-01 12:35:33 +01:00
|
|
|
*/
|
2022-06-10 18:25:31 +02:00
|
|
|
// Export the Interface for TypeScript
|
|
|
|
type Constructor<T = {}> = new (...args : any[]) => T;
|
|
|
|
|
2023-08-18 16:47:37 +02:00
|
|
|
export const Et2WidgetWithSelectMixin = <T extends Constructor<LitElement>>(superclass : T) =>
|
2022-03-01 12:35:33 +01:00
|
|
|
{
|
2024-02-21 00:20:00 +01:00
|
|
|
/**
|
|
|
|
* @summary Mixin for widgets where you can select from a pre-defined list of options
|
|
|
|
*
|
|
|
|
* Sample text
|
|
|
|
*
|
|
|
|
*/
|
2022-03-01 12:35:33 +01:00
|
|
|
class Et2WidgetWithSelect extends Et2InputWidget(superclass)
|
|
|
|
{
|
2023-09-13 19:55:33 +02:00
|
|
|
/**
|
|
|
|
* The current value of the select, submitted as a name/value pair with form data. When `multiple` is enabled, the
|
|
|
|
* value attribute will be a space-delimited list of values based on the options selected, and the value property will
|
|
|
|
* be an array.
|
|
|
|
*
|
|
|
|
@property({
|
|
|
|
noAccessor: true,
|
|
|
|
converter: {
|
|
|
|
fromAttribute: (value : string) => value.split(',')
|
2022-03-01 12:35:33 +01:00
|
|
|
}
|
2023-09-13 19:55:33 +02:00
|
|
|
})
|
|
|
|
value : string | string[] = "";
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Textual label for first row, eg: 'All' or 'None'. It's value will be ''
|
|
|
|
*/
|
|
|
|
@property({type: String})
|
|
|
|
emptyLabel : String = "";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Limit size
|
|
|
|
*/
|
|
|
|
@property({type: Number, noAccessor: true, reflect: true})
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal list of possible select options
|
|
|
|
*
|
|
|
|
* This is where we keep options sent from the server. This is not always the complete list, as extending
|
|
|
|
* classes may have their own options to add in. For example, static options are kept separate, as are search
|
|
|
|
* results. The select_options getter should give the complete list.
|
|
|
|
*/
|
|
|
|
private __select_options : SelectOption[] = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When we create the select option elements, it takes a while.
|
|
|
|
* If we don't wait for them, it causes issues in SlSelect
|
|
|
|
*/
|
|
|
|
protected _optionRenderPromise : Promise<void> = Promise.resolve();
|
2022-03-01 12:35:33 +01:00
|
|
|
|
2022-06-08 22:57:26 +02:00
|
|
|
/**
|
|
|
|
* Options found in the XML when reading the template
|
|
|
|
* @type {SelectOption[]}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
private _xmlOptions : SelectOption[] = [];
|
|
|
|
|
2024-09-06 22:20:20 +02:00
|
|
|
/**
|
|
|
|
* List of properties that get translated
|
|
|
|
* @returns object
|
|
|
|
*/
|
|
|
|
static get translate()
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
...super.translate,
|
|
|
|
emptyLabel: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-06-10 18:25:31 +02:00
|
|
|
constructor(...args : any[])
|
2022-03-01 12:35:33 +01:00
|
|
|
{
|
2022-06-10 18:25:31 +02:00
|
|
|
super(...args);
|
2022-03-01 12:35:33 +01:00
|
|
|
|
2022-06-08 22:57:26 +02:00
|
|
|
this.__select_options = <SelectOption[]>[];
|
2022-03-01 12:35:33 +01:00
|
|
|
}
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
async getUpdateComplete() : Promise<boolean>
|
|
|
|
{
|
|
|
|
const result = await super.getUpdateComplete();
|
|
|
|
await this._optionRenderPromise;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-03-01 12:35:33 +01:00
|
|
|
updated(changedProperties : PropertyValues)
|
|
|
|
{
|
|
|
|
super.updated(changedProperties);
|
|
|
|
|
2022-11-07 17:56:26 +01:00
|
|
|
// If the ID changed (or was just set) and select_options wasn't, find the new select options
|
|
|
|
if(changedProperties.has("id") && !changedProperties.has("select_options"))
|
2022-03-01 12:35:33 +01:00
|
|
|
{
|
2022-06-08 22:57:26 +02:00
|
|
|
const options = find_select_options(this, {}, this._xmlOptions);
|
2022-11-07 17:56:26 +01:00
|
|
|
if(options.length)
|
|
|
|
{
|
|
|
|
this.select_options = options;
|
|
|
|
}
|
2022-03-01 12:35:33 +01:00
|
|
|
}
|
2022-03-03 18:38:56 +01:00
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public getValueAsArray()
|
|
|
|
{
|
|
|
|
if(Array.isArray(this.value))
|
|
|
|
{
|
|
|
|
return this.value;
|
|
|
|
}
|
2023-09-19 21:01:49 +02:00
|
|
|
if(this.value == "null" || this.value == null || typeof this.value == "undefined" || !this.emptyLabel && this.value == "")
|
2023-09-13 19:55:33 +02:00
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
return [this.value];
|
|
|
|
}
|
|
|
|
|
2023-12-05 19:51:24 +01:00
|
|
|
/**
|
|
|
|
* Search options for a given value, returning the first matching option
|
|
|
|
*
|
|
|
|
* @return SelectOption | null
|
|
|
|
*/
|
2024-02-16 16:59:11 +01:00
|
|
|
public optionSearch(value : string, options : SelectOption[] = null, searchKey : string = "value", childKey : string = "value") : SelectOption | null
|
2023-12-05 19:51:24 +01:00
|
|
|
{
|
2024-02-14 18:41:22 +01:00
|
|
|
let result = null;
|
2023-12-05 19:51:24 +01:00
|
|
|
let search = function(options, value)
|
|
|
|
{
|
|
|
|
return options.find((option) =>
|
|
|
|
{
|
2024-02-16 16:59:11 +01:00
|
|
|
if(!Array.isArray(option[searchKey]) && option[searchKey] == value)
|
2023-12-05 19:51:24 +01:00
|
|
|
{
|
2024-02-14 18:41:22 +01:00
|
|
|
result = option;
|
|
|
|
}
|
|
|
|
if(Array.isArray(option[childKey]))
|
|
|
|
{
|
|
|
|
return search(option[childKey], value);
|
2023-12-05 19:51:24 +01:00
|
|
|
}
|
2024-02-16 16:59:11 +01:00
|
|
|
return option[searchKey] == value;
|
2023-12-05 19:51:24 +01:00
|
|
|
});
|
|
|
|
}
|
2024-02-14 18:41:22 +01:00
|
|
|
search(options ?? this.select_options, value);
|
|
|
|
return result;
|
2023-12-05 19:51:24 +01:00
|
|
|
}
|
|
|
|
|
2022-03-01 12:35:33 +01:00
|
|
|
/**
|
|
|
|
* Set the select options
|
|
|
|
*
|
2022-03-16 21:36:23 +01:00
|
|
|
* @param new_options
|
2022-03-01 12:35:33 +01:00
|
|
|
*/
|
2022-05-13 17:01:44 +02:00
|
|
|
set select_options(new_options : SelectOption[])
|
2022-03-01 12:35:33 +01:00
|
|
|
{
|
2022-03-16 21:36:23 +01:00
|
|
|
const old_options = this.__select_options;
|
2022-06-01 17:23:07 +02:00
|
|
|
|
|
|
|
this.__select_options = cleanSelectOptions(new_options);
|
|
|
|
|
2022-03-16 21:36:23 +01:00
|
|
|
this.requestUpdate("select_options", old_options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set select options
|
|
|
|
*
|
|
|
|
* @deprecated assign to select_options
|
|
|
|
* @param new_options
|
|
|
|
*/
|
2022-05-13 17:01:44 +02:00
|
|
|
set_select_options(new_options : SelectOption[] | { [key : string] : string }[])
|
2022-03-16 21:36:23 +01:00
|
|
|
{
|
2022-05-13 17:01:44 +02:00
|
|
|
this.select_options = <SelectOption[]>new_options;
|
2022-03-01 12:35:33 +01:00
|
|
|
}
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
/**
|
|
|
|
* Select box options
|
|
|
|
*
|
|
|
|
* Will be found automatically based on ID and type, or can be set explicitly in the template using
|
|
|
|
* <option/> children, or using widget.select_options = SelectOption[]
|
|
|
|
*/
|
|
|
|
@property({type: Object})
|
2022-05-13 17:01:44 +02:00
|
|
|
get select_options() : SelectOption[]
|
2022-03-01 12:35:33 +01:00
|
|
|
{
|
2022-03-16 21:36:23 +01:00
|
|
|
return this.__select_options;
|
2022-03-01 12:35:33 +01:00
|
|
|
}
|
|
|
|
|
2022-03-03 18:38:56 +01:00
|
|
|
/**
|
|
|
|
* Get the node where we're putting the options
|
|
|
|
*
|
|
|
|
* If this were a normal selectbox, this would be just the <select> tag (this._inputNode) but in a more
|
|
|
|
* complicated widget, this could be anything.
|
|
|
|
*
|
|
|
|
* @overridable
|
|
|
|
* @returns {HTMLElement}
|
|
|
|
*/
|
|
|
|
get _optionTargetNode() : HTMLElement
|
|
|
|
{
|
|
|
|
return <HTMLElement><unknown>this;
|
|
|
|
}
|
|
|
|
|
2022-03-01 12:35:33 +01:00
|
|
|
/**
|
|
|
|
* Render the "empty label", used when the selectbox does not currently have a value
|
|
|
|
*
|
2022-03-03 18:38:56 +01:00
|
|
|
* @overridable
|
|
|
|
* @returns {TemplateResult}
|
2022-03-01 12:35:33 +01:00
|
|
|
*/
|
|
|
|
_emptyLabelTemplate() : TemplateResult
|
|
|
|
{
|
2022-07-26 15:46:42 +02:00
|
|
|
return html`${this.emptyLabel}`;
|
2022-03-01 12:35:33 +01:00
|
|
|
}
|
|
|
|
|
2022-03-03 18:38:56 +01:00
|
|
|
/**
|
|
|
|
* Render a single option
|
|
|
|
*
|
|
|
|
* Override this method to specify how to render each option.
|
|
|
|
* In a normal selectbox, this would be something like:
|
|
|
|
*```
|
|
|
|
* <option value="${option.value}" title="${option.title}" ?selected=${option.value == this.modelValue}>
|
|
|
|
* ${option.label}
|
|
|
|
* </option>`;
|
|
|
|
* ```
|
|
|
|
* but you can do whatever you need. To use a different WebComponent, just use its tag instead of "option".
|
|
|
|
* We should even be able to pass the whole SelectOption across
|
|
|
|
* ```
|
|
|
|
* <special-option .value=${option}></special-option>
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @overridable
|
|
|
|
* @param {SelectOption} option
|
|
|
|
* @returns {TemplateResult}
|
|
|
|
*/
|
2023-09-13 19:55:33 +02:00
|
|
|
protected _optionTemplate(option : SelectOption) : TemplateResult
|
2022-03-01 12:35:33 +01:00
|
|
|
{
|
2022-03-03 18:38:56 +01:00
|
|
|
return html`
|
|
|
|
<span>Override _optionTemplate(). ${option.value} => ${option.label}</span>`;
|
2022-03-01 12:35:33 +01:00
|
|
|
}
|
|
|
|
|
2022-12-14 22:32:20 +01:00
|
|
|
_groupTemplate(option) : TemplateResult
|
|
|
|
{
|
|
|
|
if(!Array.isArray(option.value))
|
|
|
|
{
|
|
|
|
return this._optionTemplate(option);
|
|
|
|
}
|
|
|
|
return html`
|
2023-02-09 21:35:40 +01:00
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
<small>${this.noLang ? option.label : this.egw().lang(option.label)}</small>
|
2022-12-14 22:32:20 +01:00
|
|
|
${option.value.map(this._optionTemplate.bind(this))}
|
|
|
|
<sl-divider></sl-divider>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2022-03-01 12:35:33 +01:00
|
|
|
/**
|
|
|
|
* Load extra stuff from the template node. In particular, we're looking for any <option/> tags added.
|
|
|
|
*
|
|
|
|
* @param {Element} _node
|
|
|
|
*/
|
|
|
|
loadFromXML(_node : Element)
|
|
|
|
{
|
|
|
|
let new_options = [];
|
2022-05-31 17:58:49 +02:00
|
|
|
|
|
|
|
// Read the option-tags, but if not rendered there won't be any yet so check existing options
|
|
|
|
let options = _node.querySelectorAll("option");
|
2022-03-01 12:35:33 +01:00
|
|
|
for(let i = 0; i < options.length; i++)
|
|
|
|
{
|
|
|
|
new_options.push({
|
|
|
|
value: et2_readAttrWithDefault(options[i], "value", options[i].textContent),
|
|
|
|
// allow options to contain multiple translated sub-strings eg: {Firstname}.{Lastname}
|
2022-06-17 22:42:42 +02:00
|
|
|
label: options[i].textContent.replace(/{([^}]+)}/g, (str, p1) =>
|
2022-03-01 12:35:33 +01:00
|
|
|
{
|
|
|
|
return this.egw().lang(p1);
|
|
|
|
}),
|
|
|
|
title: et2_readAttrWithDefault(options[i], "title", "")
|
|
|
|
});
|
|
|
|
}
|
2022-06-08 22:57:26 +02:00
|
|
|
this._xmlOptions = new_options;
|
2022-05-31 17:58:49 +02:00
|
|
|
if(options.length == 0 && this.__select_options.length)
|
|
|
|
{
|
|
|
|
// Start with any existing options, (static options from type)
|
|
|
|
// Use a copy since we'll probably be modifying it, and we don't want to change for any other
|
|
|
|
// widget of the same static type
|
|
|
|
new_options = [...this.__select_options];
|
|
|
|
}
|
2022-03-01 12:35:33 +01:00
|
|
|
|
|
|
|
if(this.id)
|
|
|
|
{
|
|
|
|
new_options = find_select_options(this, {}, new_options);
|
|
|
|
}
|
2022-05-31 17:58:49 +02:00
|
|
|
if(new_options.length)
|
2022-03-16 21:36:23 +01:00
|
|
|
{
|
|
|
|
this.select_options = new_options;
|
|
|
|
}
|
2024-08-15 18:20:51 +02:00
|
|
|
let others = _node.querySelectorAll(":scope > :not(option)");
|
2024-02-20 16:26:55 +01:00
|
|
|
// Load the child nodes.
|
|
|
|
others.forEach((node) =>
|
|
|
|
{
|
|
|
|
let widgetType = node.nodeName.toLowerCase();
|
|
|
|
|
|
|
|
if(widgetType == "#comment" || widgetType == "#text")
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the new element
|
|
|
|
this.createElementFromNode(node);
|
|
|
|
});
|
2022-03-01 12:35:33 +01:00
|
|
|
}
|
|
|
|
}
|
2022-06-10 18:25:31 +02:00
|
|
|
|
2024-02-22 22:25:12 +01:00
|
|
|
return Et2WidgetWithSelect as unknown as Constructor<SearchMixinInterface> & Et2InputWidgetInterface & LitElement & T;
|
2022-06-10 18:25:31 +02:00
|
|
|
}
|