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
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
import {css, html} from "../../../node_modules/@lion/core/index.js";
|
|
|
|
import {LionButton} from "../../../node_modules/@lion/button/index.js";
|
2021-07-14 17:49:36 +02:00
|
|
|
import {Et2InputWidget} from "./et2_core_inputWidget";
|
2021-08-12 18:32:05 +02:00
|
|
|
import {Et2Widget} from "./Et2Widget";
|
2021-07-14 17:49:36 +02:00
|
|
|
|
2021-08-10 23:02:52 +02:00
|
|
|
export class Et2Button extends Et2InputWidget(Et2Widget(LionButton))
|
2021-06-23 23:39:58 +02:00
|
|
|
{
|
2021-08-13 23:26:18 +02:00
|
|
|
protected _created_icon_node: HTMLImageElement;
|
|
|
|
protected clicked: boolean = false;
|
|
|
|
private image: string;
|
|
|
|
|
|
|
|
static get styles()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
...super.styles,
|
|
|
|
css`
|
2021-07-19 19:57:06 +02:00
|
|
|
:host {
|
|
|
|
padding: 1px 8px;
|
|
|
|
/* These should probably come from somewhere else */
|
|
|
|
border-radius: 3px;
|
|
|
|
background-color: #e6e6e6;
|
|
|
|
}
|
|
|
|
/* Set size for icon */
|
|
|
|
::slotted([slot="icon"]) {
|
|
|
|
width: 20px;
|
|
|
|
padding-right: 3px;
|
|
|
|
}`,
|
2021-08-13 23:26:18 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
static get properties()
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
...super.properties,
|
|
|
|
image: {type: String},
|
|
|
|
onclick: {type: Function}
|
2021-08-10 23:02:52 +02:00
|
|
|
}
|
2021-08-13 23:26:18 +02:00
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
constructor()
|
|
|
|
{
|
|
|
|
super();
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
// Property default values
|
|
|
|
this.image = '';
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
// Create icon Element since BXButton puts it as child, but we put it as attribute
|
|
|
|
this._created_icon_node = document.createElement("img");
|
|
|
|
this._created_icon_node.slot = "icon";
|
|
|
|
// Do not add this._icon here, no children can be added in constructor
|
|
|
|
|
|
|
|
// Define a default click handler
|
|
|
|
// If a different one gets set via attribute, it will be used instead
|
|
|
|
this.onclick = (typeof this.onclick === "function") ? this.onclick : () =>
|
2021-08-10 23:02:52 +02:00
|
|
|
{
|
2021-08-13 23:26:18 +02:00
|
|
|
return this.getInstanceManager().submit();
|
|
|
|
};
|
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
connectedCallback()
|
|
|
|
{
|
|
|
|
super.connectedCallback();
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
//this.classList.add("et2_button")
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
if (this.image)
|
|
|
|
{
|
|
|
|
this._created_icon_node.src = egw.image(this.image);
|
|
|
|
this.appendChild(this._created_icon_node);
|
2021-08-10 23:02:52 +02:00
|
|
|
}
|
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
this.addEventListener("click", this._handleClick.bind(this));
|
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
_handleClick(event: MouseEvent): boolean
|
|
|
|
{
|
|
|
|
debugger;
|
|
|
|
// ignore click on readonly button
|
|
|
|
if (this.disabled) 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
|
|
|
|
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-13 23:26:18 +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-08-13 23:26:18 +02:00
|
|
|
this.clicked = false;
|
|
|
|
this.getInstanceManager()?.skip_close_prompt(false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
render()
|
|
|
|
{
|
|
|
|
return html`
|
|
|
|
<div class="button-content et2_button" id="${this._buttonId}">
|
|
|
|
<slot name="icon"></slot>
|
|
|
|
<slot></slot>
|
|
|
|
</div> `;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation of the et2_IInput interface
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Always return false as a button is never dirty
|
|
|
|
*/
|
|
|
|
isDirty()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
resetDirty()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
getValue()
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
customElements.define("et2-button", Et2Button);
|