2022-06-02 19:52:27 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
|
|
|
/**
|
2022-06-02 19:52:27 +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
|
|
|
}
|
2023-01-13 19:28:02 +01:00
|
|
|
|
2024-05-07 22:46:44 +02:00
|
|
|
:host(.hideApp) ::slotted([slot="app"]) {
|
2022-06-02 19:52:27 +02:00
|
|
|
display: none;
|
2024-05-07 22:46:44 +02:00
|
|
|
}
|
2023-01-13 19:28:02 +01:00
|
|
|
|
2024-05-07 22:46:44 +02:00
|
|
|
.form-control-input {
|
|
|
|
display: flex;
|
|
|
|
gap: 0.5rem;
|
|
|
|
}
|
2023-01-13 19:28:02 +01:00
|
|
|
|
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
|
|
|
|
*/
|
2022-07-21 17:57:50 +02:00
|
|
|
onlyApp: {type: String},
|
2022-05-31 21:40:31 +02:00
|
|
|
/**
|
|
|
|
* Limit to the listed applications (comma seperated)
|
|
|
|
*/
|
2022-07-21 17:57:50 +02:00
|
|
|
applicationList: {type: String},
|
2022-05-31 21:40:31 +02:00
|
|
|
/**
|
|
|
|
* Show just application icons instead of names
|
|
|
|
*/
|
2022-07-21 17:57:50 +02:00
|
|
|
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.
|
|
|
|
*/
|
2022-07-07 17:54:18 +02:00
|
|
|
select: {type: Function},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displayed in the search / select when no value is selected
|
|
|
|
*/
|
2024-02-08 21:26:07 +01:00
|
|
|
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
|
|
|
|
2022-07-21 17:57:50 +02:00
|
|
|
protected __onlyApp : string;
|
2024-05-07 22:46:44 +02:00
|
|
|
protected readonly hasSlotController = new HasSlotController(this, 'help-text', 'label');
|
2022-06-02 19:52:27 +02:00
|
|
|
|
2022-05-31 21:40:31 +02:00
|
|
|
constructor()
|
|
|
|
{
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
connectedCallback()
|
|
|
|
{
|
|
|
|
super.connectedCallback();
|
|
|
|
|
2022-06-02 19:52:27 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-06-02 19:52:27 +02:00
|
|
|
disconnectedCallback()
|
|
|
|
{
|
|
|
|
super.disconnectedCallback();
|
|
|
|
this._unbindListeners();
|
|
|
|
}
|
2022-06-01 16:05:34 +02:00
|
|
|
|
2022-07-21 17:57:50 +02:00
|
|
|
set onlyApp(app)
|
2022-05-31 21:40:31 +02:00
|
|
|
{
|
2022-07-21 17:57:50 +02:00
|
|
|
this.__onlyApp = app || "";
|
2022-07-18 19:42:26 +02:00
|
|
|
|
2022-07-21 17:57:50 +02:00
|
|
|
// 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)
|
2022-07-18 19:42:26 +02:00
|
|
|
{
|
2024-05-07 22:46:44 +02:00
|
|
|
this.__value.app = this.__onlyApp;
|
2022-07-18 19:42:26 +02:00
|
|
|
}
|
2022-06-06 21:31:33 +02:00
|
|
|
if(app)
|
|
|
|
{
|
|
|
|
this.app = app;
|
|
|
|
}
|
2022-07-07 17:54:18 +02:00
|
|
|
if(this._appNode)
|
|
|
|
{
|
2022-07-21 17:57:50 +02:00
|
|
|
this._appNode.onlyApp = app;
|
2022-07-07 17:54:18 +02:00
|
|
|
}
|
2022-05-31 21:40:31 +02:00
|
|
|
}
|
|
|
|
|
2022-07-21 17:57:50 +02:00
|
|
|
get onlyApp()
|
2022-05-31 21:40:31 +02:00
|
|
|
{
|
2022-07-21 17:57:50 +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)
|
2022-06-01 17:23:07 +02:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2024-02-08 21:26:07 +01:00
|
|
|
set searchOptions(options)
|
2022-06-01 16:05:34 +02:00
|
|
|
{
|
2022-06-02 17:13:21 +02:00
|
|
|
this.updateComplete.then(() =>
|
2022-06-01 17:23:07 +02:00
|
|
|
{
|
2024-02-08 21:26:07 +01:00
|
|
|
this._searchNode.searchOptions = options;
|
2022-06-02 17:13:21 +02:00
|
|
|
});
|
2022-06-01 16:05:34 +02:00
|
|
|
}
|
|
|
|
|
2024-02-08 21:26:07 +01:00
|
|
|
get searchOptions()
|
2022-06-01 16:05:34 +02:00
|
|
|
{
|
2024-02-09 08:19:00 +01:00
|
|
|
return this._searchNode?.searchOptions;
|
2022-05-31 21:40:31 +02:00
|
|
|
}
|
|
|
|
|
2024-02-08 21:26:07 +01:00
|
|
|
set searchOptions(options)
|
|
|
|
{
|
|
|
|
this.updateComplete.then(() =>
|
|
|
|
{
|
|
|
|
this._searchNode.searchOptions = options;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
get searchOptions()
|
|
|
|
{
|
2024-02-09 08:19:00 +01:00
|
|
|
return this._searchNode?.searchOptions;
|
2024-02-08 21:26:07 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-07-07 17:54:18 +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-07-07 17:54:18 +02:00
|
|
|
}
|
2022-05-31 21:40:31 +02:00
|
|
|
|
|
|
|
protected _bindListeners()
|
|
|
|
{
|
2022-06-02 19:52:27 +02:00
|
|
|
this.addEventListener("sl-show", this._handleShow);
|
|
|
|
this.addEventListener("sl-hide", this._handleHide);
|
2022-05-31 21:40:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected _unbindListeners()
|
|
|
|
{
|
2022-06-02 19:52:27 +02:00
|
|
|
this.removeEventListener("sl-show", this._handleShow);
|
|
|
|
this.removeEventListener("sl-hide", this._handleHide);
|
2022-05-31 21:40:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-06-02 19:52:27 +02:00
|
|
|
/**
|
|
|
|
* Hide app selection when there's an entry
|
|
|
|
* @param event
|
|
|
|
* @protected
|
|
|
|
*/
|
2024-05-07 22:46:44 +02:00
|
|
|
protected handleEntrySelect(event)
|
2022-06-02 19:52:27 +02:00
|
|
|
{
|
2023-04-28 17:27:17 +02:00
|
|
|
event.stopPropagation();
|
2024-05-07 22:46:44 +02:00
|
|
|
this.value = <string>this._searchNode.value ?? "";
|
2023-07-25 18:26:22 +02:00
|
|
|
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"));
|
|
|
|
});
|
2023-01-13 19:28:02 +01:00
|
|
|
this.requestUpdate('value');
|
|
|
|
|
|
|
|
this.validate();
|
2022-06-02 19:52:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show app selection when there's no entry
|
|
|
|
* @param event
|
|
|
|
* @protected
|
|
|
|
*/
|
2024-05-07 22:46:44 +02:00
|
|
|
protected handleEntryClear(event)
|
2022-06-02 19:52:27 +02:00
|
|
|
{
|
2024-05-07 22:46:44 +02:00
|
|
|
this.value = ""
|
2022-06-02 19:52:27 +02:00
|
|
|
this.classList.remove("hideApp")
|
2023-12-06 22:46:07 +01:00
|
|
|
this._searchNode.value = "";
|
2022-06-06 22:42:43 +02:00
|
|
|
this._searchNode.focus();
|
2022-07-07 17:54:18 +02:00
|
|
|
|
2024-05-07 22:46:44 +02:00
|
|
|
this.updateComplete.then(() =>
|
|
|
|
{
|
|
|
|
this.dispatchEvent(new Event("change"));
|
|
|
|
});
|
2023-01-13 19:28:02 +01:00
|
|
|
this.requestUpdate('value');
|
|
|
|
|
|
|
|
this.validate();
|
2022-06-02 19:52:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Option select dropdown opened
|
2022-07-21 17:57:50 +02:00
|
|
|
* Show app selection (Et2LinkAppSelect controls own visibility according to onlyApp)
|
2022-06-02 19:52:27 +02:00
|
|
|
* @param event
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected _handleShow(event)
|
|
|
|
{
|
|
|
|
this.classList.remove("hideApp");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Option select dropdown closed
|
2022-07-21 17:57:50 +02:00
|
|
|
* Hide app selection (Et2LinkAppSelect controls own visibility according to onlyApp)
|
2022-06-02 19:52:27 +02:00
|
|
|
* only if there's a value selected
|
|
|
|
*
|
|
|
|
* @param event
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected _handleHide(event)
|
|
|
|
{
|
|
|
|
if(this._searchNode.value)
|
|
|
|
{
|
|
|
|
this.classList.add("hideApp");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-06 21:31:33 +02:00
|
|
|
get value() : LinkInfo | string | number
|
2022-06-01 16:05:34 +02:00
|
|
|
{
|
2022-07-21 17:57:50 +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
|
|
|
}
|
|
|
|
|
2022-06-06 21:31:33 +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
|
|
|
|
2022-06-06 21:31:33 +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
|
|
|
}
|
2022-06-22 15:29:21 +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
|
|
|
}
|
2022-06-06 21:31:33 +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
|
|
|
}
|
2022-10-13 18:37:29 +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}
|
2024-07-24 17:16:14 +02:00
|
|
|
applicationList=${this.applicationList ? this.applicationList : nothing}
|
2024-05-07 22:46:44 +02:00
|
|
|
?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
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-06-22 15:29:21 +02:00
|
|
|
customElements.define("et2-link-entry_ro", Et2LinkEntryReadonly);
|