2022-03-08 19:19:33 +01:00
|
|
|
/**
|
|
|
|
* EGroupware eTemplate2 - InvokerMixing
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package api
|
|
|
|
* @link https://www.egroupware.org
|
|
|
|
* @author Ralf Becker
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* eslint-disable import/no-extraneous-dependencies */
|
2022-07-21 22:05:53 +02:00
|
|
|
import {css, dedupeMixin, html, LitElement, SlotMixin} from '@lion/core';
|
2022-06-10 18:25:31 +02:00
|
|
|
import {Et2InputWidget, Et2InputWidgetInterface} from "../Et2InputWidget/Et2InputWidget";
|
2022-03-10 15:39:45 +01:00
|
|
|
import {colorsDefStyles} from "../Styles/colorsDefStyles";
|
2022-03-08 19:19:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoker mixing adds an invoker button to a widget to trigger some action, e.g.:
|
|
|
|
* - searchbox to delete input
|
|
|
|
* - url to open url
|
|
|
|
* - url-email to open mail compose
|
|
|
|
*
|
|
|
|
* Inspired by Lion date-picker.
|
|
|
|
*/
|
2022-06-10 18:25:31 +02:00
|
|
|
|
|
|
|
type Constructor<T = Et2InputWidgetInterface> = new (...args : any[]) => T;
|
|
|
|
export const Et2InvokerMixin = dedupeMixin(<T extends Constructor<LitElement>>(superclass : T) =>
|
2022-03-08 19:19:33 +01:00
|
|
|
{
|
2022-07-21 22:05:53 +02:00
|
|
|
class Et2Invoker extends SlotMixin(Et2InputWidget(superclass))
|
2022-03-08 19:19:33 +01:00
|
|
|
{
|
|
|
|
/** @type {any} */
|
|
|
|
static get properties()
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
_invokerLabel: {
|
|
|
|
type: String,
|
|
|
|
},
|
|
|
|
_invokerTitle: {
|
|
|
|
type: String,
|
|
|
|
},
|
|
|
|
_invokerAction: {
|
|
|
|
type: Function,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2022-03-10 17:13:43 +01:00
|
|
|
|
2022-03-10 15:39:45 +01:00
|
|
|
static get styles()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
...super.styles,
|
|
|
|
colorsDefStyles,
|
|
|
|
css`
|
2022-03-12 13:49:25 +01:00
|
|
|
::slotted(input), input, ::slotted(select) {
|
2022-03-10 15:39:45 +01:00
|
|
|
background-color: transparent;
|
|
|
|
border: none !important;
|
|
|
|
}
|
|
|
|
.input-group {
|
|
|
|
border: 1px solid var(--input-border-color);
|
|
|
|
}
|
|
|
|
.input-group__suffix{
|
2022-04-22 23:21:46 +02:00
|
|
|
text-align: center;
|
2022-03-10 15:39:45 +01:00
|
|
|
}
|
|
|
|
.input-group__container {
|
|
|
|
align-items: center
|
|
|
|
}
|
|
|
|
::slotted([slot="suffix"]) {
|
2022-04-22 23:21:46 +02:00
|
|
|
width: 14px;
|
2022-03-10 15:39:45 +01:00
|
|
|
border: none !important;
|
|
|
|
background-color: transparent !important;
|
|
|
|
}
|
|
|
|
::slotted(:disabled) {cursor: default !important;}
|
|
|
|
:host(:hover) ::slotted([slot="suffix"]) {
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
];
|
|
|
|
}
|
2022-03-10 17:13:43 +01:00
|
|
|
|
2022-03-08 19:19:33 +01:00
|
|
|
get slots()
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
...super.slots,
|
|
|
|
suffix: () =>
|
|
|
|
{
|
2022-07-21 22:05:53 +02:00
|
|
|
return this._invokerTemplate();
|
2022-03-08 19:19:33 +01:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
get _invokerNode()
|
|
|
|
{
|
|
|
|
return /** @type {HTMLElement} */ (this.querySelector(`#${this.__invokerId}`));
|
|
|
|
}
|
|
|
|
|
2022-06-10 18:25:31 +02:00
|
|
|
constructor(...args : any[])
|
2022-03-08 19:19:33 +01:00
|
|
|
{
|
2022-06-10 18:25:31 +02:00
|
|
|
super(...args);
|
2022-03-08 19:19:33 +01:00
|
|
|
/** @private */
|
|
|
|
this.__invokerId = this.__createUniqueIdForA11y();
|
|
|
|
// default for properties
|
|
|
|
this._invokerTitle = 'Click to open';
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @private */
|
|
|
|
__createUniqueIdForA11y()
|
|
|
|
{
|
|
|
|
return `${this.localName}-${Math.random().toString(36).substr(2, 10)}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {PropertyKey} name
|
|
|
|
* @param {?} oldValue
|
|
|
|
*/
|
|
|
|
requestUpdate(name, oldValue)
|
|
|
|
{
|
|
|
|
super.requestUpdate(name, oldValue);
|
|
|
|
|
|
|
|
if (name === 'disabled' || name === 'showsFeedbackFor' || name === 'modelValue')
|
|
|
|
{
|
|
|
|
this._toggleInvokerDisabled();
|
|
|
|
}
|
2022-03-12 13:49:25 +01:00
|
|
|
|
|
|
|
if (name === '_invokerLabel' || name === '_invokerTitle')
|
|
|
|
{
|
|
|
|
this._toggleInvoker();
|
|
|
|
}
|
|
|
|
if (name === '_invokerAction')
|
|
|
|
{
|
|
|
|
if (oldValue) this._invokerNode?.removeEventListener('click', oldValue);
|
|
|
|
this._invokerNode?.addEventListener('click', this._invokerAction.bind(this), true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* (Un)Hide invoker, if no label or action defined
|
|
|
|
*
|
|
|
|
* @protected
|
|
|
|
* */
|
|
|
|
_toggleInvoker()
|
|
|
|
{
|
|
|
|
if (this._invokerNode)
|
|
|
|
{
|
|
|
|
this._invokerNode.style.display = !this._invokerLabel ? 'none' : 'inline-block';
|
|
|
|
this._invokerNode.innerHTML = this._invokerLabel || '';
|
|
|
|
this._invokerNode.title = this._invokerTitle || '';
|
|
|
|
}
|
2022-03-08 19:19:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to check if invoker can be activated: not disabled, empty or invalid
|
|
|
|
*
|
|
|
|
* @protected
|
|
|
|
* */
|
|
|
|
_toggleInvokerDisabled()
|
|
|
|
{
|
|
|
|
if (this._invokerNode)
|
|
|
|
{
|
|
|
|
const invokerNode = /** @type {HTMLElement & {disabled: boolean}} */ (this._invokerNode);
|
2022-07-21 22:05:53 +02:00
|
|
|
invokerNode.disabled = this.disabled || !this.value || (this.input && !this.input.reportValidity())
|
2022-03-08 19:19:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param {import('@lion/core').PropertyValues } changedProperties */
|
|
|
|
firstUpdated(changedProperties)
|
|
|
|
{
|
|
|
|
super.firstUpdated(changedProperties);
|
|
|
|
this._toggleInvokerDisabled();
|
2022-03-12 13:49:25 +01:00
|
|
|
this._toggleInvoker();
|
2022-03-08 19:19:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subclassers can replace this with their custom extension invoker,
|
|
|
|
* like `<my-button><calendar-icon></calendar-icon></my-button>`
|
|
|
|
*/
|
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
_invokerTemplate()
|
|
|
|
{
|
|
|
|
return html`
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
@click="${this._invokerAction}"
|
|
|
|
id="${this.__invokerId}"
|
2022-04-01 20:00:40 +02:00
|
|
|
aria-label="${this._invokerTitle}"
|
|
|
|
title="${this._invokerTitle}"
|
2022-03-08 19:19:33 +01:00
|
|
|
>
|
|
|
|
${this._invokerLabel}
|
|
|
|
</button>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
}
|
2022-06-10 18:25:31 +02:00
|
|
|
|
|
|
|
return Et2Invoker as unknown as Constructor<Et2Invoker> & T;
|
2022-03-08 19:19:33 +01:00
|
|
|
})
|