2022-05-12 23:22:49 +02:00
|
|
|
/**
|
|
|
|
* EGroupware eTemplate2 - Dropdown 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
import {SlButtonGroup, SlDropdown} from "@shoelace-style/shoelace";
|
2023-09-13 19:55:33 +02:00
|
|
|
import {css, html, LitElement, TemplateResult} from "lit";
|
2023-08-18 16:47:37 +02:00
|
|
|
import {Et2WidgetWithSelectMixin} from "../Et2Select/Et2WidgetWithSelectMixin";
|
2022-05-12 23:22:49 +02:00
|
|
|
import {SelectOption} from "../Et2Select/FindSelectOptions";
|
2023-09-13 19:55:33 +02:00
|
|
|
import shoelace from "../Styles/shoelace";
|
2022-05-12 23:22:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A split button - a button with a dropdown list
|
|
|
|
*
|
|
|
|
* There are several parts to the button UI:
|
|
|
|
* - Container: This is what is percieved as the dropdown button, the whole package together
|
|
|
|
* - Button: The part on the left that can be clicked
|
|
|
|
* - Arrow: The button to display the choices
|
|
|
|
* - Menu: The list of choices
|
|
|
|
*
|
|
|
|
* Menu options are passed via the select_options. They are normally the same
|
|
|
|
* as for a select box, but the title can also be full HTML if needed.
|
|
|
|
*
|
|
|
|
*/
|
2023-09-13 19:55:33 +02:00
|
|
|
export class Et2DropdownButton extends Et2WidgetWithSelectMixin(LitElement)
|
2022-05-12 23:22:49 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
static get styles()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
...super.styles,
|
2023-09-13 19:55:33 +02:00
|
|
|
shoelace,
|
2022-05-12 23:22:49 +02:00
|
|
|
css`
|
|
|
|
:host {
|
2022-05-17 17:40:34 +02:00
|
|
|
/* Avoid unwanted style overlap from button */
|
|
|
|
border: none;
|
|
|
|
background-color: none;
|
2022-05-18 19:01:27 +02:00
|
|
|
}
|
|
|
|
:host, sl-menu {
|
2022-05-13 19:51:29 +02:00
|
|
|
/**
|
|
|
|
Adapt shoelace color variables to what we want
|
|
|
|
Maybe some logical variables from etemplate2.css here?
|
|
|
|
*/
|
|
|
|
--sl-color-primary-50: rgb(244, 246, 247);
|
|
|
|
--sl-color-primary-100: var(--gray-10);
|
|
|
|
--sl-color-primary-300: var(--input-border-color);
|
|
|
|
--sl-color-primary-400: var(--input-border-color);
|
2022-05-18 19:01:27 +02:00
|
|
|
--sl-color-primary-600: var(--primary-background-color);
|
2022-05-13 19:51:29 +02:00
|
|
|
--sl-color-primary-700: #505050;
|
2022-05-12 23:22:49 +02:00
|
|
|
}
|
2022-05-17 17:40:34 +02:00
|
|
|
:host(:active), :host([active]) {
|
|
|
|
background-color: initial;
|
|
|
|
}
|
2022-05-18 22:39:25 +02:00
|
|
|
sl-button-group {
|
|
|
|
display: initial;
|
|
|
|
}
|
|
|
|
#main {
|
|
|
|
flex: 1 1 auto;
|
|
|
|
}
|
2022-05-12 23:22:49 +02:00
|
|
|
`,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-05-13 19:51:29 +02:00
|
|
|
static get properties()
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
...super.properties
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-05-12 23:22:49 +02:00
|
|
|
// Make sure imports stay
|
|
|
|
private _group : SlButtonGroup;
|
|
|
|
private _dropdow : SlDropdown;
|
|
|
|
|
2022-05-13 19:51:29 +02:00
|
|
|
constructor()
|
|
|
|
{
|
|
|
|
super();
|
|
|
|
|
|
|
|
// Bind handlers - parent already got click
|
|
|
|
this._handleSelect = this._handleSelect.bind(this);
|
|
|
|
}
|
2022-05-12 23:22:49 +02:00
|
|
|
|
|
|
|
connectedCallback()
|
|
|
|
{
|
|
|
|
super.connectedCallback();
|
|
|
|
|
2022-05-13 19:51:29 +02:00
|
|
|
// Rebind click to just the main button, not the whole thing
|
2022-05-12 23:22:49 +02:00
|
|
|
this.removeEventListener("click", this._handleClick);
|
2022-05-13 19:51:29 +02:00
|
|
|
}
|
|
|
|
|
2023-05-16 23:00:43 +02:00
|
|
|
protected _renderOptions()
|
2022-05-13 19:51:29 +02:00
|
|
|
{
|
2023-05-16 23:00:43 +02:00
|
|
|
// We have our own render, so we can handle it internally
|
2022-05-12 23:22:49 +02:00
|
|
|
}
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
|
2023-05-16 23:00:43 +02:00
|
|
|
render() : TemplateResult
|
2022-05-12 23:22:49 +02:00
|
|
|
{
|
2023-02-28 19:12:50 +01:00
|
|
|
if(this.readonly)
|
|
|
|
{
|
2023-05-16 23:00:43 +02:00
|
|
|
return html``;
|
2023-02-28 19:12:50 +01:00
|
|
|
}
|
2022-05-12 23:22:49 +02:00
|
|
|
return html`
|
|
|
|
<sl-button-group>
|
2023-05-16 23:00:43 +02:00
|
|
|
<sl-button size="${egwIsMobile() ? "large" : "medium"}" id="main"
|
|
|
|
?disabled=${this.disabled}
|
|
|
|
@click=${this._handleClick}
|
|
|
|
>
|
2022-11-14 19:22:56 +01:00
|
|
|
${this.label}
|
|
|
|
</sl-button>
|
2022-05-13 22:12:42 +02:00
|
|
|
<sl-dropdown placement="bottom-end" hoist>
|
2022-11-14 19:22:56 +01:00
|
|
|
<sl-button size="${egwIsMobile() ? "large" : "medium"}" slot="trigger" caret
|
|
|
|
?disabled=${this.disabled}></sl-button>
|
2023-05-16 23:00:43 +02:00
|
|
|
<sl-menu @sl-select=${this._handleSelect}>
|
|
|
|
${(this.select_options || []).map((option : SelectOption) => this._optionTemplate(option))}
|
2022-05-12 23:22:49 +02:00
|
|
|
</sl-menu>
|
|
|
|
</sl-dropdown>
|
|
|
|
</sl-button-group>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2022-05-18 19:01:27 +02:00
|
|
|
_optionTemplate(option : SelectOption) : TemplateResult
|
2022-05-12 23:22:49 +02:00
|
|
|
{
|
|
|
|
let icon = option.icon ? html`
|
|
|
|
<et2-image slot="prefix" src=${option.icon} icon></et2-image>` : '';
|
|
|
|
|
|
|
|
return html`
|
2023-09-18 21:57:34 +02:00
|
|
|
<sl-menu-item value="${option.value}">
|
2022-05-12 23:22:49 +02:00
|
|
|
${icon}
|
2023-02-09 21:35:40 +01:00
|
|
|
${this.noLang ? option.label : this.egw().lang(option.label)}
|
2023-09-18 21:57:34 +02:00
|
|
|
</sl-menu-item>`;
|
2022-05-12 23:22:49 +02:00
|
|
|
}
|
|
|
|
|
2022-05-13 19:51:29 +02:00
|
|
|
protected _handleSelect(ev)
|
|
|
|
{
|
|
|
|
this._value = ev.detail.item.value;
|
|
|
|
|
|
|
|
// Trigger a change event
|
|
|
|
this.dispatchEvent(new Event("change"));
|
|
|
|
|
|
|
|
// Let it bubble, if anyone else is interested
|
|
|
|
}
|
|
|
|
|
|
|
|
get value() : string
|
|
|
|
{
|
|
|
|
return this._value;
|
|
|
|
}
|
|
|
|
|
|
|
|
set value(new_value)
|
|
|
|
{
|
|
|
|
let oldValue = this.value;
|
|
|
|
this._value = new_value;
|
|
|
|
this.requestUpdate("value", oldValue);
|
|
|
|
}
|
|
|
|
|
2022-05-18 19:01:27 +02:00
|
|
|
get _optionTargetNode() : HTMLElement
|
2022-05-17 18:04:39 +02:00
|
|
|
{
|
|
|
|
return this.shadowRoot.querySelector("sl-menu");
|
|
|
|
}
|
|
|
|
|
2022-05-12 23:22:49 +02:00
|
|
|
get buttonNode()
|
|
|
|
{
|
2022-05-13 19:51:29 +02:00
|
|
|
return this.shadowRoot.querySelector("#main");
|
|
|
|
}
|
|
|
|
|
|
|
|
get triggerButtonNode()
|
|
|
|
{
|
|
|
|
return this.shadowRoot.querySelector("[slot='trigger']");
|
|
|
|
}
|
|
|
|
|
|
|
|
get dropdownNode()
|
|
|
|
{
|
|
|
|
return this.shadowRoot.querySelector("sl-dropdown");
|
2022-05-12 23:22:49 +02:00
|
|
|
}
|
2023-02-22 17:55:43 +01:00
|
|
|
|
|
|
|
blur()
|
|
|
|
{
|
|
|
|
this.shadowRoot.querySelector("sl-button-group")?.dispatchEvent(new Event('blur'));
|
|
|
|
}
|
|
|
|
|
|
|
|
focus()
|
|
|
|
{
|
|
|
|
this.shadowRoot.querySelector("sl-button-group")?.dispatchEvent(new Event('focus'));
|
|
|
|
}
|
2022-05-12 23:22:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// @ts-ignore TypeScript is not recognizing that Et2Button is a LitElement
|
|
|
|
customElements.define("et2-dropdown-button", Et2DropdownButton);
|