egroupware_official/api/js/etemplate/Et2Link/Et2LinkEntry.ts

424 lines
9.2 KiB
TypeScript
Raw Normal View History

/**
* EGroupware eTemplate2 - Search & select link entry WebComponent
*
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package api
* @link https://www.egroupware.org
* @author Nathan Gray
*/
2024-05-07 22:46:44 +02:00
import {css, html, LitElement, nothing} from "lit";
import {classMap} from "lit/directives/class-map.js";
2022-05-31 21:40:31 +02:00
import {Et2LinkAppSelect} from "./Et2LinkAppSelect";
import {Et2InputWidget} from "../Et2InputWidget/Et2InputWidget";
import {Et2LinkSearch} from "./Et2LinkSearch";
2022-06-17 22:06:12 +02:00
import {Et2Link, LinkInfo} from "./Et2Link";
2024-05-07 22:46:44 +02:00
import {HasSlotController} from "../Et2Widget/slot";
2022-05-31 21:40:31 +02:00
/**
* Find and select a single entry using the link system.
*
2022-05-31 21:40:31 +02:00
*
*/
2024-05-07 22:46:44 +02:00
export class Et2LinkEntry extends Et2InputWidget(LitElement)
2022-05-31 21:40:31 +02:00
{
static get styles()
{
return [
...super.styles,
css`
2024-05-07 22:46:44 +02:00
:host {
2022-05-31 21:40:31 +02:00
display: block;
2024-05-07 22:46:44 +02:00
}
2024-05-07 22:46:44 +02:00
:host(.hideApp) ::slotted([slot="app"]) {
display: none;
2024-05-07 22:46:44 +02:00
}
2024-05-07 22:46:44 +02:00
.form-control-input {
display: flex;
gap: 0.5rem;
}
2022-05-31 21:40:31 +02:00
`
];
}
static get properties()
{
return {
...super.properties,
value: {type: Object},
/**
* Limit to just this application - hides app selection
*/
onlyApp: {type: String},
2022-05-31 21:40:31 +02:00
/**
* Limit to the listed applications (comma seperated)
*/
applicationList: {type: String},
2022-05-31 21:40:31 +02:00
/**
* Show just application icons instead of names
*/
appIcons: {type: Boolean},
2022-05-31 21:40:31 +02:00
/**
* Callback before query to server.
* It will be passed the request & et2_link_entry objects. Must return true, or false to abort query.
*/
query: {type: Function},
/**
* Callback when user selects an option. Must return true, or false to abort normal action.
*/
select: {type: Function},
/**
* Displayed in the search / select when no value is selected
*/
placeholder: {type: String},
/**
* Additional search parameters that are passed to the server
* when we query searchUrl
*/
searchOptions: {type: Object}
2022-05-31 21:40:31 +02:00
}
}
2022-06-02 17:13:21 +02:00
/**
* We only care about this value until render. After the sub-nodes are created,
* we take their "live" values for our value.
*
* @type {LinkInfo}
* @private
*/
2024-05-07 22:46:44 +02:00
private __value : LinkInfo = {app: "", id: ""};
2022-06-02 17:13:21 +02:00
protected __onlyApp : string;
2024-05-07 22:46:44 +02:00
protected readonly hasSlotController = new HasSlotController(this, 'help-text', 'label');
2022-05-31 21:40:31 +02:00
constructor()
{
super();
}
connectedCallback()
{
super.connectedCallback();
this._handleShow = this._handleShow.bind(this);
this._handleHide = this._handleHide.bind(this);
2022-06-02 17:13:21 +02:00
2022-06-17 22:06:12 +02:00
if(!this.readonly)
{
2024-05-07 22:46:44 +02:00
this.updateComplete.then(() =>
{
this._bindListeners();
});
2022-06-17 22:06:12 +02:00
}
2022-05-31 21:40:31 +02:00
}
disconnectedCallback()
{
super.disconnectedCallback();
this._unbindListeners();
}
2022-06-01 16:05:34 +02:00
set onlyApp(app)
2022-05-31 21:40:31 +02:00
{
this.__onlyApp = app || "";
// If initial value got set before onlyApp, it still needs app in pre-render value
2024-05-07 22:46:44 +02:00
if(this.__value && app)
{
2024-05-07 22:46:44 +02:00
this.__value.app = this.__onlyApp;
}
if(app)
{
this.app = app;
}
if(this._appNode)
{
this._appNode.onlyApp = app;
}
2022-05-31 21:40:31 +02:00
}
get onlyApp()
2022-05-31 21:40:31 +02:00
{
return this.__onlyApp;
2022-06-01 16:05:34 +02:00
}
set app(app)
{
2024-05-07 22:46:44 +02:00
if(typeof this.__value !== "object" || this.__value == null)
{
2024-05-07 22:46:44 +02:00
this.__value = <LinkInfo>{app: app}
}
else
{
this.__value.app = app;
}
this.requestUpdate("value");
2022-06-01 16:05:34 +02:00
}
get app()
{
2024-05-07 22:46:44 +02:00
return this.__value?.app || "";
2022-05-31 21:40:31 +02:00
}
set searchOptions(options)
2022-06-01 16:05:34 +02:00
{
2022-06-02 17:13:21 +02:00
this.updateComplete.then(() =>
{
this._searchNode.searchOptions = options;
2022-06-02 17:13:21 +02:00
});
2022-06-01 16:05:34 +02:00
}
get searchOptions()
2022-06-01 16:05:34 +02:00
{
return this._searchNode?.searchOptions;
2022-05-31 21:40:31 +02:00
}
set searchOptions(options)
{
this.updateComplete.then(() =>
{
this._searchNode.searchOptions = options;
});
}
get searchOptions()
{
return this._searchNode?.searchOptions;
}
2022-05-31 21:40:31 +02:00
get _appNode() : Et2LinkAppSelect
{
2024-05-07 22:46:44 +02:00
return this.shadowRoot?.querySelector("et2-link-apps");
2022-05-31 21:40:31 +02:00
}
get _searchNode() : Et2LinkSearch
{
2024-05-07 22:46:44 +02:00
return <Et2LinkSearch>this.shadowRoot?.querySelector("et2-link-search");
2022-05-31 21:40:31 +02:00
}
get placeholder() : string
{
return this._searchNode?.placeholder;
}
set placeholder(new_value)
{
2022-07-08 17:30:41 +02:00
if(this._searchNode)
{
this._searchNode.placeholder = new_value;
}
}
2022-05-31 21:40:31 +02:00
protected _bindListeners()
{
this.addEventListener("sl-show", this._handleShow);
this.addEventListener("sl-hide", this._handleHide);
2022-05-31 21:40:31 +02:00
}
protected _unbindListeners()
{
this.removeEventListener("sl-show", this._handleShow);
this.removeEventListener("sl-hide", this._handleHide);
2022-05-31 21:40:31 +02:00
}
/**
* Hide app selection when there's an entry
* @param event
* @protected
*/
2024-05-07 22:46:44 +02:00
protected handleEntrySelect(event)
{
event.stopPropagation();
2024-05-07 22:46:44 +02:00
this.value = <string>this._searchNode.value ?? "";
this.classList.toggle("hideApp", Boolean(typeof this.value == "object" ? this.value?.id : this.value));
2024-05-07 22:46:44 +02:00
this.updateComplete.then(() =>
{
this.dispatchEvent(new Event("change"));
});
this.requestUpdate('value');
this.validate();
}
/**
* Show app selection when there's no entry
* @param event
* @protected
*/
2024-05-07 22:46:44 +02:00
protected handleEntryClear(event)
{
2024-05-07 22:46:44 +02:00
this.value = ""
this.classList.remove("hideApp")
this._searchNode.value = "";
this._searchNode.focus();
2024-05-07 22:46:44 +02:00
this.updateComplete.then(() =>
{
this.dispatchEvent(new Event("change"));
});
this.requestUpdate('value');
this.validate();
}
/**
* Option select dropdown opened
* Show app selection (Et2LinkAppSelect controls own visibility according to onlyApp)
* @param event
* @protected
*/
protected _handleShow(event)
{
this.classList.remove("hideApp");
}
/**
* Option select dropdown closed
* Hide app selection (Et2LinkAppSelect controls own visibility according to onlyApp)
* only if there's a value selected
*
* @param event
* @protected
*/
protected _handleHide(event)
{
if(this._searchNode.value)
{
this.classList.add("hideApp");
}
}
get value() : LinkInfo | string | number
2022-06-01 16:05:34 +02:00
{
if(this.onlyApp)
2022-06-01 16:05:34 +02:00
{
2023-12-05 16:58:41 +01:00
return <string>this._searchNode?.value ?? "";
2022-06-01 16:05:34 +02:00
}
2024-05-07 22:46:44 +02:00
return this.__value;
2022-06-01 16:05:34 +02:00
}
set value(val : LinkInfo | string | number)
2022-06-01 16:05:34 +02:00
{
2024-05-07 22:46:44 +02:00
let value : LinkInfo = {app: this.onlyApp || (this.app || this._appNode?.value), id: ""};
2022-06-02 17:13:21 +02:00
if(typeof val === 'string' && val.length > 0)
2022-06-01 16:05:34 +02:00
{
2022-06-02 17:13:21 +02:00
if(val.indexOf(',') > 0)
2022-06-01 16:05:34 +02:00
{
2022-06-02 17:13:21 +02:00
val = val.replace(",", ":");
2022-06-01 16:05:34 +02:00
}
if (val.indexOf(':') > 0)
{
const vals = val.split(':');
value.app = vals[0];
value.id = vals[1];
}
else
{
value.id = val;
}
2022-06-02 17:13:21 +02:00
}
else if(typeof val === "number" && val)
2022-06-02 17:13:21 +02:00
{
value.id = String(val);
2022-06-01 16:05:34 +02:00
}
else if(typeof val === "object" && val !== null) // object with attributes: app, id, title
2022-06-01 16:05:34 +02:00
{
2022-06-02 17:13:21 +02:00
value = (<LinkInfo>val);
}
2024-05-07 22:46:44 +02:00
const oldValue = this.__value;
this.__value = value;
this.classList.toggle("hideApp", Boolean(this.__value.id));
this.requestUpdate("value", oldValue);
}
protected handleLabelClick()
{
this._searchNode.focus();
2022-06-01 16:05:34 +02:00
}
2024-05-07 22:46:44 +02:00
2022-05-31 21:40:31 +02:00
/**
2024-05-07 22:46:44 +02:00
* Update the search node's app & clear selected value when
* selected app changes.
* @param event
2022-05-31 21:40:31 +02:00
* @protected
*/
2024-05-07 22:46:44 +02:00
protected handleAppChange(e)
2022-05-31 21:40:31 +02:00
{
2024-05-07 22:46:44 +02:00
this.app = this._appNode.value;
this._searchNode.app = this._appNode.value;
this._searchNode.value = "";
this._searchNode.clearSearch();
this._searchNode.focus();
this.requestUpdate('value');
}
render()
2022-05-31 21:40:31 +02:00
{
2024-05-07 22:46:44 +02:00
const labelTemplate = this._labelTemplate();
const helpTemplate = this._helpTextTemplate();
2022-05-31 21:40:31 +02:00
return html`
2024-05-07 22:46:44 +02:00
<div
part="form-control"
class=${classMap({
'form-control': true,
'form-control--medium': true,
'form-control--has-label': labelTemplate !== nothing,
'form-control--has-help-text': helpTemplate !== nothing
})}
>
${labelTemplate}
<div part="form-control-input" class="form-control-input">
<et2-link-apps
onlyApp=${this.onlyApp ? this.onlyApp : nothing}
?appIcons=${this.appIcons}
?applicationList=${this.applicationList}
?disabled=${this.disabled}
?readonly=${this.disabled}
.value=${this.__value?.app ? this.__value.app : nothing}
@change=${this.handleAppChange}
></et2-link-apps>
<et2-link-search
?placeholder=${this.placeholder}
?required=${this.required}
?disabled=${this.disabled}
?readonly=${this.readonly}
.app=${this.__value?.app || nothing}
.value=${this.__value?.id || nothing}
@change=${this.handleEntrySelect}
@sl-clear=${this.handleEntryClear}
>
${(this.__value?.title) ? html`
<option value=${this.__value.id}>${this.__value.title}</option>
` : nothing}
</et2-link-search>
</div>
${helpTemplate}
2022-05-31 21:40:31 +02:00
</div>
`;
}
}
2022-06-17 22:06:12 +02:00
customElements.define("et2-link-entry", Et2LinkEntry);
export class Et2LinkEntryReadonly extends Et2Link
{
}
customElements.define("et2-link-entry_ro", Et2LinkEntryReadonly);