2021-06-23 23:39:58 +02:00
|
|
|
/**
|
|
|
|
* EGroupware eTemplate2 - Button widget
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage api
|
|
|
|
* @link https://www.egroupware.org
|
|
|
|
* @author Nathan Gray
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2022-07-28 21:06:18 +02:00
|
|
|
import {css, PropertyValues} from "@lion/core";
|
2021-08-25 19:32:15 +02:00
|
|
|
import {Et2InputWidget} from "../Et2InputWidget/Et2InputWidget";
|
2022-07-27 21:03:44 +02:00
|
|
|
import '../Et2Image/Et2Image';
|
2022-07-27 00:23:27 +02:00
|
|
|
import {SlButton} from "@shoelace-style/shoelace";
|
|
|
|
import shoelace from "../Styles/shoelace";
|
2021-07-14 17:49:36 +02:00
|
|
|
|
2022-07-27 00:23:27 +02:00
|
|
|
export class Et2Button extends Et2InputWidget(SlButton)
|
2021-06-23 23:39:58 +02:00
|
|
|
{
|
2021-08-19 01:41:23 +02:00
|
|
|
protected clicked : boolean = false;
|
2021-08-13 23:26:18 +02:00
|
|
|
|
2021-09-15 19:08:44 +02:00
|
|
|
/**
|
|
|
|
* images to be used as background-image, if none is explicitly applied and id matches given regular expression
|
|
|
|
*/
|
|
|
|
static readonly default_background_images : object = {
|
|
|
|
save: /save(&|\]|$)/,
|
|
|
|
apply: /apply(&|\]|$)/,
|
|
|
|
cancel: /cancel(&|\]|$)/,
|
|
|
|
delete: /delete(&|\]|$)/,
|
|
|
|
discard: /discard(&|\]|$)/,
|
|
|
|
edit: /edit(&|\[\]|$)/,
|
|
|
|
next: /(next|continue)(&|\]|$)/,
|
|
|
|
finish: /finish(&|\]|$)/,
|
|
|
|
back: /(back|previous)(&|\]|$)/,
|
|
|
|
copy: /copy(&|\]|$)/,
|
|
|
|
more: /more(&|\]|$)/,
|
|
|
|
check: /(yes|check)(&|\]|$)/,
|
|
|
|
cancelled: /no(&|\]|$)/,
|
|
|
|
ok: /ok(&|\]|$)/,
|
|
|
|
close: /close(&|\]|$)/,
|
|
|
|
add: /(add(&|\]|$)|create)/ // customfields use create*
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Classnames added automatically to buttons to set certain hover background colors
|
|
|
|
*/
|
|
|
|
static readonly default_classes : object = {
|
|
|
|
et2_button_cancel: /cancel(&|\]|$)/, // yellow
|
|
|
|
et2_button_question: /(yes|no)(&|\]|$)/, // yellow
|
|
|
|
et2_button_delete: /delete(&|\]|$)/ // red
|
|
|
|
};
|
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
static get styles()
|
|
|
|
{
|
|
|
|
return [
|
2022-07-27 00:23:27 +02:00
|
|
|
...shoelace,
|
2021-08-13 23:26:18 +02:00
|
|
|
...super.styles,
|
|
|
|
css`
|
2021-07-19 19:57:06 +02:00
|
|
|
:host {
|
2022-01-19 18:30:45 +01:00
|
|
|
padding: 0;
|
2021-07-19 19:57:06 +02:00
|
|
|
/* These should probably come from somewhere else */
|
2022-01-18 15:13:48 +01:00
|
|
|
max-width: 125px;
|
2022-01-20 13:13:04 +01:00
|
|
|
min-width: fit-content;
|
2021-07-19 19:57:06 +02:00
|
|
|
}
|
2022-06-16 21:59:31 +02:00
|
|
|
:host([hideonreadonly][disabled]) {
|
|
|
|
display:none;
|
|
|
|
}
|
2021-07-19 19:57:06 +02:00
|
|
|
/* Set size for icon */
|
2022-01-20 16:36:51 +01:00
|
|
|
::slotted(img.imageOnly) {
|
|
|
|
padding-right: 0px !important;
|
|
|
|
width: 16px !important;
|
|
|
|
}
|
2022-07-27 00:23:27 +02:00
|
|
|
::slotted(et2-image) {
|
2022-07-27 21:03:44 +02:00
|
|
|
max-width: 20px;
|
2022-07-27 00:23:27 +02:00
|
|
|
display: flex;
|
2021-09-14 19:43:43 +02:00
|
|
|
}
|
|
|
|
::slotted([slot="icon"][src='']) {
|
|
|
|
display: none;
|
|
|
|
}
|
2022-01-19 18:30:45 +01:00
|
|
|
.imageOnly {
|
2022-01-20 13:13:04 +01:00
|
|
|
width:18px;
|
|
|
|
height: 18px;
|
2022-01-19 18:30:45 +01:00
|
|
|
}
|
2022-07-27 00:23:27 +02:00
|
|
|
/* Make hover border match other widgets (select) */
|
|
|
|
.button--standard.button--default:hover:not(.button--disabled) {
|
|
|
|
background-color: var(--sl-input-background-color-hover);
|
|
|
|
border-color: var(--sl-input-border-color-hover);
|
|
|
|
color: var(--sl-input-color-hover);
|
|
|
|
}
|
|
|
|
.button {
|
|
|
|
justify-content: left;
|
|
|
|
}
|
|
|
|
.button--has-label.button--medium .button__label {
|
|
|
|
padding: 0 var(--sl-spacing-medium);
|
|
|
|
}
|
|
|
|
.button__label {
|
|
|
|
}
|
2022-07-28 10:08:17 +02:00
|
|
|
.button__prefix {
|
|
|
|
padding-left: 1px;
|
|
|
|
}
|
2022-08-03 00:38:52 +02:00
|
|
|
|
|
|
|
/* Only image, no label */
|
|
|
|
.button--has-prefix:not(.button--has-label) {
|
2022-08-03 18:10:28 +02:00
|
|
|
justify-content: center;
|
2022-08-08 11:57:02 +02:00
|
|
|
width: var(--sl-input-height-medium);
|
|
|
|
padding-inline-start: var(--sl-spacing-small);
|
2022-08-03 00:38:52 +02:00
|
|
|
}
|
2022-08-08 11:57:02 +02:00
|
|
|
|
2021-09-14 19:43:43 +02:00
|
|
|
`,
|
2021-08-13 23:26:18 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
static get properties()
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
...super.properties,
|
2022-03-15 20:48:05 +01:00
|
|
|
// LionButton doesn't have a label property & Et2Widget avoids re-defining it
|
|
|
|
label: {type: String},
|
2022-03-25 16:55:02 +01:00
|
|
|
image: {type: String},
|
|
|
|
|
2022-04-05 00:24:01 +02:00
|
|
|
/**
|
|
|
|
* If button is set to readonly, do we want to hide it completely (old behaviour) or show it as disabled
|
|
|
|
* (default)
|
2022-06-16 21:59:31 +02:00
|
|
|
* Something's not quite right here, as the attribute shows up as "hideonreadonly" instead of "hide" but
|
|
|
|
* it does not show up without the "attribute", and attribute:"hideonreadonly" does not show as an attribute
|
2022-04-05 00:24:01 +02:00
|
|
|
*/
|
2022-06-16 21:59:31 +02:00
|
|
|
hideOnReadonly: {type: Boolean, reflect: true, attribute: "hide"},
|
2022-04-05 00:24:01 +02:00
|
|
|
|
2022-03-25 16:55:02 +01:00
|
|
|
/**
|
|
|
|
* Button should submit the etemplate
|
2022-04-06 22:48:57 +02:00
|
|
|
* Return false from the click handler to cancel the submit, or set noSubmit to true to skip submitting.
|
2022-03-25 16:55:02 +01:00
|
|
|
*/
|
2022-04-06 22:48:57 +02:00
|
|
|
noSubmit: {type: Boolean, reflect: false},
|
2022-04-05 00:24:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* When submitting, skip the validation step. Allows to submit etemplates directly to the server.
|
|
|
|
*/
|
|
|
|
noValidation: {type: Boolean}
|
2021-08-10 23:02:52 +02:00
|
|
|
}
|
2021-08-13 23:26:18 +02:00
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2022-08-03 15:48:23 +02:00
|
|
|
constructor(...args : any[])
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
2022-08-03 15:48:23 +02:00
|
|
|
super(...args);
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
// Property default values
|
2022-03-11 22:36:50 +01:00
|
|
|
this.__image = '';
|
2022-04-06 22:48:57 +02:00
|
|
|
this.noSubmit = false;
|
2022-04-05 00:24:01 +02:00
|
|
|
this.hideOnReadonly = false;
|
|
|
|
this.noValidation = false;
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-23 19:49:17 +02:00
|
|
|
// Do not add icon here, no children can be added in constructor
|
2021-08-13 23:26:18 +02:00
|
|
|
|
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2022-08-03 19:33:37 +02:00
|
|
|
protected firstUpdated(_changedProperties : PropertyValues)
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
2022-08-03 19:33:37 +02:00
|
|
|
super.firstUpdated(_changedProperties);
|
|
|
|
|
|
|
|
if(!this.label && this.__image)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Label / no label should get special classes set, but they're missing without this extra requestUpdate()
|
|
|
|
This is a work-around for button--has-prefix & button--has-label not being set, something to do
|
|
|
|
with how we're setting them.
|
|
|
|
*/
|
|
|
|
this.updateComplete.then(() =>
|
|
|
|
{
|
|
|
|
this.requestUpdate();
|
|
|
|
});
|
|
|
|
}
|
2021-08-25 23:24:27 +02:00
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2022-07-27 00:23:27 +02:00
|
|
|
set label(new_label : string)
|
|
|
|
{
|
2022-07-28 21:06:18 +02:00
|
|
|
this.updateComplete.then(() =>
|
2022-07-27 00:23:27 +02:00
|
|
|
{
|
2022-07-28 21:06:18 +02:00
|
|
|
if(!this._labelNode)
|
|
|
|
{
|
|
|
|
const textNode = document.createTextNode(new_label);
|
|
|
|
this.appendChild(textNode);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._labelNode.textContent = new_label;
|
|
|
|
}
|
|
|
|
});
|
2022-07-27 00:23:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get label()
|
|
|
|
{
|
2022-07-27 12:10:39 +02:00
|
|
|
return this._labelNode?.textContent?.trim();
|
2022-07-27 00:23:27 +02:00
|
|
|
}
|
|
|
|
|
2021-08-25 23:24:27 +02:00
|
|
|
set image(new_image : string)
|
|
|
|
{
|
2022-07-28 21:06:18 +02:00
|
|
|
let oldValue = this.__image;
|
2022-08-01 16:02:50 +02:00
|
|
|
if(new_image.indexOf("http") >= 0 || new_image.indexOf(egw.webserverUrl) >=0)
|
2021-08-25 23:24:27 +02:00
|
|
|
{
|
2022-03-11 22:36:50 +01:00
|
|
|
this.__image = new_image
|
2021-08-25 23:24:27 +02:00
|
|
|
}
|
|
|
|
else
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
2022-03-11 22:36:50 +01:00
|
|
|
this.__image = this.egw().image(new_image);
|
2021-08-10 23:02:52 +02:00
|
|
|
}
|
2021-08-25 23:24:27 +02:00
|
|
|
this.requestUpdate("image", oldValue);
|
2021-08-13 23:26:18 +02:00
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2022-03-11 14:22:04 +01:00
|
|
|
get image ()
|
|
|
|
{
|
2022-05-09 17:59:13 +02:00
|
|
|
return this.__image;
|
2022-03-11 14:22:04 +01:00
|
|
|
}
|
|
|
|
|
2021-08-19 01:41:23 +02:00
|
|
|
_handleClick(event : MouseEvent) : boolean
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
// ignore click on readonly button
|
2021-08-25 19:32:15 +02:00
|
|
|
if(this.disabled || this.readonly)
|
|
|
|
{
|
2022-03-02 00:56:58 +01:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopImmediatePropagation();
|
2021-08-25 19:32:15 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
this.clicked = true;
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
// Cancel buttons don't trigger the close confirmation prompt
|
2021-08-19 01:41:23 +02:00
|
|
|
if(this.classList.contains("et2_button_cancel"))
|
2021-08-10 23:02:52 +02:00
|
|
|
{
|
2021-08-13 23:26:18 +02:00
|
|
|
this.getInstanceManager()?.skip_close_prompt();
|
2021-08-10 23:02:52 +02:00
|
|
|
}
|
|
|
|
|
2021-08-19 01:41:23 +02:00
|
|
|
if(!super._handleClick(event))
|
2021-08-10 23:02:52 +02:00
|
|
|
{
|
2021-08-13 23:26:18 +02:00
|
|
|
this.clicked = false;
|
|
|
|
return false;
|
2021-08-10 23:02:52 +02:00
|
|
|
}
|
|
|
|
|
2021-09-15 19:08:44 +02:00
|
|
|
// Submit the form
|
2022-04-06 22:48:57 +02:00
|
|
|
if(!this.noSubmit)
|
2021-09-15 19:08:44 +02:00
|
|
|
{
|
2022-04-05 00:24:01 +02:00
|
|
|
return this.getInstanceManager().submit(this, undefined, this.noValidation);
|
2021-09-15 19:08:44 +02:00
|
|
|
}
|
2021-08-13 23:26:18 +02:00
|
|
|
this.clicked = false;
|
|
|
|
this.getInstanceManager()?.skip_close_prompt(false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-15 19:08:44 +02:00
|
|
|
/**
|
|
|
|
* Handle changes that have to happen based on changes to properties
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
requestUpdate(name : PropertyKey, oldValue)
|
|
|
|
{
|
|
|
|
super.requestUpdate(name, oldValue);
|
|
|
|
|
2022-04-05 00:24:01 +02:00
|
|
|
// "disabled" is the attribute from the spec
|
|
|
|
if(name == 'readonly')
|
|
|
|
{
|
|
|
|
if(this.readonly)
|
|
|
|
{
|
|
|
|
this.setAttribute('disabled', "");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.removeAttribute("disabled");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-15 19:08:44 +02:00
|
|
|
// Default image & class are determined based on ID
|
|
|
|
if(name == "id" && this._widget_id)
|
|
|
|
{
|
|
|
|
// Check against current value to avoid triggering another update
|
|
|
|
if(!this.image)
|
|
|
|
{
|
|
|
|
let image = this._get_default_image(this._widget_id);
|
2022-08-03 15:48:23 +02:00
|
|
|
if(image && image != this.__image)
|
2021-09-15 19:08:44 +02:00
|
|
|
{
|
2021-09-16 21:35:41 +02:00
|
|
|
this.image = image;
|
2021-09-15 19:08:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
let default_class = this._get_default_class(this._widget_id);
|
|
|
|
if(default_class && !this.classList.contains(default_class))
|
|
|
|
{
|
2021-09-16 21:35:41 +02:00
|
|
|
this.classList.add(default_class);
|
2021-09-15 19:08:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 21:06:18 +02:00
|
|
|
updated(changedProperties : PropertyValues)
|
|
|
|
{
|
|
|
|
super.updated(changedProperties);
|
|
|
|
|
|
|
|
if(changedProperties.has("image"))
|
|
|
|
{
|
|
|
|
if(this.image && !this._iconNode)
|
|
|
|
{
|
|
|
|
const image = document.createElement("et2-image");
|
|
|
|
image.slot = "prefix";
|
|
|
|
this.prepend(image);
|
2022-08-03 16:27:23 +02:00
|
|
|
image.src = this.__image;
|
|
|
|
}
|
|
|
|
else if (this._iconNode)
|
|
|
|
{
|
|
|
|
this._iconNode.src = this.__image;
|
2022-07-28 21:06:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-15 19:08:44 +02:00
|
|
|
/**
|
|
|
|
* Get a default image for the button based on ID
|
|
|
|
*
|
|
|
|
* @param {string} check_id
|
|
|
|
*/
|
|
|
|
_get_default_image(check_id : string) : string
|
|
|
|
{
|
|
|
|
if(!check_id)
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2022-05-13 16:54:02 +02:00
|
|
|
if(!this.image)
|
2021-09-15 19:08:44 +02:00
|
|
|
{
|
|
|
|
for(const image in Et2Button.default_background_images)
|
|
|
|
{
|
|
|
|
if(check_id.match(Et2Button.default_background_images[image]))
|
|
|
|
{
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a default class for the button based on ID
|
|
|
|
*
|
|
|
|
* @param check_id
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
_get_default_class(check_id)
|
|
|
|
{
|
|
|
|
if(!check_id)
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
for(var name in Et2Button.default_classes)
|
|
|
|
{
|
|
|
|
if(check_id.match(Et2Button.default_classes[name]))
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2021-08-23 19:49:17 +02:00
|
|
|
get _iconNode() : HTMLImageElement
|
|
|
|
{
|
|
|
|
return <HTMLImageElement>(Array.from(this.children)).find(
|
2022-07-27 00:23:27 +02:00
|
|
|
el => (<HTMLElement>el).slot === "prefix",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
get _labelNode() : HTMLElement
|
|
|
|
{
|
|
|
|
return <HTMLImageElement>(Array.from(this.childNodes)).find(
|
|
|
|
el => (<HTMLElement>el).nodeType === 3,
|
2021-08-23 19:49:17 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
/**
|
|
|
|
* Implementation of the et2_IInput interface
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Always return false as a button is never dirty
|
|
|
|
*/
|
|
|
|
isDirty()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
resetDirty()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
getValue()
|
|
|
|
{
|
2021-08-19 01:41:23 +02:00
|
|
|
if(this.clicked)
|
2021-08-10 23:02:52 +02:00
|
|
|
{
|
2021-08-13 23:26:18 +02:00
|
|
|
return true;
|
2021-08-10 23:02:52 +02:00
|
|
|
}
|
2021-08-13 23:26:18 +02:00
|
|
|
|
|
|
|
// If "null" is returned, the result is not added to the submitted
|
|
|
|
// array.
|
|
|
|
return null;
|
|
|
|
}
|
2021-06-23 23:39:58 +02:00
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-27 19:21:40 +02:00
|
|
|
// @ts-ignore TypeScript is not recognizing that Et2Button is a LitElement
|
2022-05-13 16:54:02 +02:00
|
|
|
customElements.define("et2-button", Et2Button);
|