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
|
|
|
|
*/
|
|
|
|
|
2022-06-17 22:06:12 +02:00
|
|
|
import {css, html, LitElement, PropertyValues, SlotMixin} from "@lion/core";
|
2022-05-31 21:40:31 +02:00
|
|
|
import {Et2LinkAppSelect} from "./Et2LinkAppSelect";
|
|
|
|
import {Et2InputWidget} from "../Et2InputWidget/Et2InputWidget";
|
|
|
|
import {FormControlMixin, ValidateMixin} from "@lion/form-core";
|
|
|
|
import {Et2LinkSearch} from "./Et2LinkSearch";
|
2022-06-17 22:06:12 +02:00
|
|
|
import {Et2Link, LinkInfo} from "./Et2Link";
|
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
|
|
|
*
|
|
|
|
*/
|
|
|
|
export class Et2LinkEntry extends Et2InputWidget(FormControlMixin(ValidateMixin(SlotMixin(LitElement))))
|
|
|
|
{
|
|
|
|
static get styles()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
...super.styles,
|
|
|
|
css`
|
|
|
|
:host {
|
|
|
|
display: block;
|
|
|
|
}
|
2022-06-02 19:52:27 +02:00
|
|
|
:host(.hideApp) ::slotted([slot="app"]) {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
placeholder: {type: String}
|
2022-05-31 21:40:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get slots()
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
...super.slots,
|
|
|
|
app: () =>
|
|
|
|
{
|
2022-06-01 16:05:34 +02:00
|
|
|
const app = <Et2LinkAppSelect>document.createElement("et2-link-apps")
|
2022-07-21 17:57:50 +02:00
|
|
|
if(this.__onlyApp)
|
2022-06-01 17:23:07 +02:00
|
|
|
{
|
2022-07-21 17:57:50 +02:00
|
|
|
app.onlyApp = this.__onlyApp;
|
2022-06-01 17:23:07 +02:00
|
|
|
}
|
2022-06-06 21:31:33 +02:00
|
|
|
else if(typeof this._value !== "undefined" && this._value.app)
|
2022-06-02 17:13:21 +02:00
|
|
|
{
|
|
|
|
app.value = this._value.app;
|
|
|
|
}
|
2022-05-31 21:40:31 +02:00
|
|
|
return app;
|
|
|
|
},
|
|
|
|
select: () =>
|
|
|
|
{
|
|
|
|
const select = <Et2LinkSearch><unknown>document.createElement("et2-link-search");
|
2022-06-06 21:31:33 +02:00
|
|
|
if(typeof this._value !== "undefined" && this._value.id)
|
2022-06-01 16:05:34 +02:00
|
|
|
{
|
2022-06-02 18:18:46 +02:00
|
|
|
if(this._value.title)
|
|
|
|
{
|
|
|
|
select.select_options = [{value: this._value.id, label: this._value.title}]
|
|
|
|
}
|
2022-06-02 17:13:21 +02:00
|
|
|
select.app = this._value.app;
|
|
|
|
select.value = this._value.id;
|
2022-06-01 16:05:34 +02:00
|
|
|
}
|
2022-05-31 21:40:31 +02:00
|
|
|
return select;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* N.B.: Single underscore! Otherwise we conflict with parent __value
|
|
|
|
*
|
|
|
|
* @type {LinkInfo}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
private _value : LinkInfo;
|
|
|
|
|
2022-07-21 17:57:50 +02:00
|
|
|
protected __onlyApp : string;
|
2022-06-02 19:52:27 +02:00
|
|
|
|
2022-05-31 21:40:31 +02:00
|
|
|
constructor()
|
|
|
|
{
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
connectedCallback()
|
|
|
|
{
|
|
|
|
super.connectedCallback();
|
|
|
|
|
|
|
|
this._handleAppChange = this._handleAppChange.bind(this);
|
2022-06-02 19:52:27 +02:00
|
|
|
this._handleEntrySelect = this._handleEntrySelect.bind(this);
|
|
|
|
this._handleEntryClear = this._handleEntryClear.bind(this);
|
|
|
|
this._handleShow = this._handleShow.bind(this);
|
|
|
|
this._handleHide = this._handleHide.bind(this);
|
2022-06-02 17:13:21 +02:00
|
|
|
|
|
|
|
// Clear initial value
|
|
|
|
this._value = undefined;
|
2022-06-02 19:52:27 +02:00
|
|
|
|
2022-06-17 22:06:12 +02:00
|
|
|
if(!this.readonly)
|
|
|
|
{
|
|
|
|
this._bindListeners();
|
|
|
|
}
|
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-06-17 22:06:12 +02:00
|
|
|
updated(changedProperties : PropertyValues)
|
|
|
|
{
|
|
|
|
super.updated(changedProperties);
|
|
|
|
if(changedProperties.has("readonly"))
|
|
|
|
{
|
|
|
|
this._appNode.readonly = this.readonly;
|
|
|
|
this._searchNode.readonly = this.readonly;
|
|
|
|
}
|
2022-06-20 23:19:57 +02:00
|
|
|
// Pass some properties on to app selection
|
2022-07-21 17:57:50 +02:00
|
|
|
if(changedProperties.has("onlyApp"))
|
2022-06-20 23:19:57 +02:00
|
|
|
{
|
2022-07-21 17:57:50 +02:00
|
|
|
this._appNode.onlyApp = this.onlyApp;
|
2022-06-20 23:19:57 +02:00
|
|
|
}
|
2022-07-21 17:57:50 +02:00
|
|
|
if(changedProperties.has("applicationList"))
|
2022-06-20 23:19:57 +02:00
|
|
|
{
|
2022-07-21 17:57:50 +02:00
|
|
|
this._appNode.applicationList = this.applicationList;
|
2022-06-20 23:19:57 +02:00
|
|
|
}
|
2022-07-21 17:57:50 +02:00
|
|
|
if(changedProperties.has("appIcons"))
|
2022-06-20 23:19:57 +02:00
|
|
|
{
|
2022-07-21 17:57:50 +02:00
|
|
|
this._appNode.appIcons = this.appIcons;
|
2022-06-20 23:19:57 +02:00
|
|
|
}
|
2022-06-17 22:06:12 +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
|
2022-07-18 19:42:26 +02:00
|
|
|
if(this._value && app)
|
|
|
|
{
|
2022-07-21 17:57:50 +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)
|
|
|
|
{
|
2022-06-02 17:13:21 +02:00
|
|
|
this.updateComplete.then(() =>
|
2022-06-01 17:23:07 +02:00
|
|
|
{
|
|
|
|
this._appNode.value = app;
|
|
|
|
this._searchNode.app = app;
|
2022-06-02 17:13:21 +02:00
|
|
|
});
|
2022-06-01 16:05:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get app()
|
|
|
|
{
|
2022-06-06 21:31:33 +02:00
|
|
|
return this._appNode?.value || "";
|
2022-05-31 21:40:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get _appNode() : Et2LinkAppSelect
|
|
|
|
{
|
|
|
|
return this.querySelector("[slot='app']");
|
|
|
|
}
|
|
|
|
|
|
|
|
get _searchNode() : Et2LinkSearch
|
|
|
|
{
|
|
|
|
return this.querySelector("[slot='select']");
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
this._appNode.addEventListener("change", this._handleAppChange);
|
2022-06-06 21:31:33 +02:00
|
|
|
this._searchNode.addEventListener("sl-select", this._handleEntrySelect);
|
|
|
|
this._searchNode.addEventListener("sl-clear", this._handleEntryClear);
|
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()
|
|
|
|
{
|
|
|
|
this._appNode.removeEventListener("change", this._handleAppChange);
|
2022-06-02 19:52:27 +02:00
|
|
|
this.removeEventListener("sl-select", this._handleEntrySelect);
|
|
|
|
this.removeEventListener("sl-clear", this._handleEntryClear);
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Update the search node's app & clear selected value when
|
|
|
|
* selected app changes.
|
|
|
|
* @param event
|
|
|
|
* @protected
|
|
|
|
*/
|
2022-05-31 21:40:31 +02:00
|
|
|
protected _handleAppChange(event)
|
|
|
|
{
|
|
|
|
this._searchNode.app = this._appNode.value;
|
2022-06-02 19:52:27 +02:00
|
|
|
this._searchNode.value = "";
|
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
|
|
|
|
*/
|
|
|
|
protected _handleEntrySelect(event)
|
|
|
|
{
|
|
|
|
this.classList.add("hideApp");
|
2022-07-07 17:54:18 +02:00
|
|
|
this.dispatchEvent(new Event("change"));
|
2022-06-02 19:52:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show app selection when there's no entry
|
|
|
|
* @param event
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected _handleEntryClear(event)
|
|
|
|
{
|
|
|
|
this.classList.remove("hideApp")
|
2022-06-06 22:42:43 +02:00
|
|
|
this._searchNode.focus();
|
2022-07-07 17:54:18 +02:00
|
|
|
|
|
|
|
this.dispatchEvent(new Event("change"));
|
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
|
|
|
{
|
2022-07-07 17:54:18 +02:00
|
|
|
return <string>this._searchNode?.value;
|
2022-06-01 16:05:34 +02:00
|
|
|
}
|
2022-06-17 22:06:12 +02:00
|
|
|
return this._searchNode ? <LinkInfo>{
|
2022-06-02 17:13:21 +02:00
|
|
|
id: this._searchNode.value,
|
|
|
|
app: this.app,
|
2022-06-01 16:05:34 +02:00
|
|
|
//search: this._searchNode... // content of search field
|
2022-06-02 17:13:21 +02:00
|
|
|
} : 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
|
|
|
{
|
2022-07-21 17:57:50 +02:00
|
|
|
let value : LinkInfo = {app: this.onlyApp || this.app, 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-06-06 21:31:33 +02:00
|
|
|
else if(typeof val === "object") // 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the searchNode is not there yet, hold value. We'll use these values when we create the
|
|
|
|
// slotted searchNode.
|
|
|
|
if(this._searchNode == null)
|
|
|
|
{
|
|
|
|
this._value = value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.app = value.app;
|
|
|
|
this._searchNode.value = value.id;
|
2022-06-01 16:05:34 +02:00
|
|
|
}
|
2022-06-02 19:52:27 +02:00
|
|
|
if(value.id)
|
|
|
|
{
|
|
|
|
this.classList.add("hideApp");
|
|
|
|
}
|
2022-06-01 16:05:34 +02:00
|
|
|
}
|
|
|
|
|
2022-05-31 21:40:31 +02:00
|
|
|
/**
|
|
|
|
* @return {TemplateResult}
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
_inputGroupInputTemplate()
|
|
|
|
{
|
|
|
|
return html`
|
|
|
|
<div class="input-group__input">
|
|
|
|
<slot name="app"></slot>
|
|
|
|
<slot name="select"></slot>
|
|
|
|
</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);
|