2022-07-27 23:51:32 +02:00
|
|
|
/**
|
|
|
|
* EGroupware eTemplate2 - Checkbox 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
import {css} from "lit";
|
2022-07-27 23:51:32 +02:00
|
|
|
import {Et2InputWidget} from "../Et2InputWidget/Et2InputWidget";
|
|
|
|
import '../Et2Image/Et2Image';
|
|
|
|
import {SlCheckbox} from "@shoelace-style/shoelace";
|
|
|
|
import shoelace from "../Styles/shoelace";
|
|
|
|
|
|
|
|
export class Et2Checkbox extends Et2InputWidget(SlCheckbox)
|
|
|
|
{
|
2022-08-07 10:10:33 +02:00
|
|
|
/**
|
|
|
|
* Value to set checkbox in (third) indeterminate state
|
|
|
|
*/
|
|
|
|
static readonly INDETERMINATE = '***undefined***';
|
|
|
|
|
2022-07-27 23:51:32 +02:00
|
|
|
static get styles()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
...shoelace,
|
|
|
|
...super.styles,
|
|
|
|
css`
|
2023-03-22 18:18:56 +01:00
|
|
|
:host {
|
|
|
|
/* Make it line up with the middle of surroundings */
|
|
|
|
margin: auto 0px;
|
|
|
|
vertical-align: baseline;
|
|
|
|
}
|
|
|
|
|
|
|
|
:host([disabled]) {
|
|
|
|
display: initial;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fix positioning */
|
|
|
|
|
|
|
|
.checkbox {
|
|
|
|
position: relative;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extend hover highlight to label */
|
|
|
|
|
|
|
|
.checkbox:not(.checkbox--disabled):hover {
|
|
|
|
color: var(--sl-input-border-color-hover);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Use normal color even when required */
|
|
|
|
|
|
|
|
:host([required]) .checkbox__control {
|
|
|
|
color: var(--input-text-color);
|
|
|
|
}
|
2022-12-09 18:55:19 +01:00
|
|
|
`,
|
2022-07-27 23:51:32 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
static get properties()
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
...super.properties,
|
|
|
|
/* Different value when checked */
|
|
|
|
selectedValue: {type: String},
|
|
|
|
/* Different value when unchecked */
|
|
|
|
unselectedValue: {type: String}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor()
|
|
|
|
{
|
|
|
|
super();
|
2022-08-07 10:10:33 +02:00
|
|
|
|
2022-08-08 18:32:43 +02:00
|
|
|
this.isSlComponent = true;
|
|
|
|
|
2022-08-07 10:10:33 +02:00
|
|
|
this.selectedValue = 'true';
|
|
|
|
this.unselectedValue = '';
|
2022-07-27 23:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
connectedCallback()
|
|
|
|
{
|
|
|
|
super.connectedCallback();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
get label()
|
|
|
|
{
|
|
|
|
return this._labelNode?.textContent || "";
|
|
|
|
}
|
|
|
|
|
|
|
|
set label(label_text)
|
|
|
|
{
|
|
|
|
if(this._labelNode)
|
|
|
|
{
|
|
|
|
this._labelNode.textContent = label_text;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.updateComplete.then(() =>
|
|
|
|
{
|
|
|
|
this.label = label_text;
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 10:10:33 +02:00
|
|
|
get value() : string | boolean
|
2022-07-27 23:51:32 +02:00
|
|
|
{
|
2022-08-07 10:10:33 +02:00
|
|
|
return this.indeterminate ? undefined :
|
|
|
|
(this.checked ? this.selectedValue : this.unselectedValue);
|
2022-07-27 23:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
set value(new_value : string | boolean)
|
|
|
|
{
|
|
|
|
this.requestUpdate("checked");
|
|
|
|
this.indeterminate = false;
|
2022-08-07 10:10:33 +02:00
|
|
|
|
|
|
|
if(typeof new_value === "boolean")
|
2022-07-27 23:51:32 +02:00
|
|
|
{
|
2022-08-07 10:10:33 +02:00
|
|
|
this.checked = new_value;
|
2022-07-27 23:51:32 +02:00
|
|
|
}
|
2022-08-07 10:10:33 +02:00
|
|
|
else if(new_value == this.selectedValue)
|
2022-07-27 23:51:32 +02:00
|
|
|
{
|
|
|
|
this.checked = true;
|
|
|
|
}
|
2022-08-07 10:10:33 +02:00
|
|
|
else if(new_value == this.unselectedValue)
|
2022-07-27 23:51:32 +02:00
|
|
|
{
|
|
|
|
this.checked = false;
|
|
|
|
}
|
2022-08-07 10:10:33 +02:00
|
|
|
// concept of an indeterminate value did not exist in eT2 and set value gets called with all kind of truthy of falsy values
|
|
|
|
// therefore we can NOT set everything not matching our (un)selectedValue to indeterminate!
|
|
|
|
// For now, we only do that for an explicit Et2Checkbox.INDETERMINATE value
|
|
|
|
else if (new_value === Et2Checkbox.INDETERMINATE)
|
2022-07-27 23:51:32 +02:00
|
|
|
{
|
|
|
|
this.indeterminate = true;
|
|
|
|
}
|
2022-08-07 10:10:33 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
this.checked = !!new_value;
|
|
|
|
}
|
2022-07-27 23:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private get _labelNode()
|
|
|
|
{
|
|
|
|
return this.shadowRoot?.querySelector("slot:not([name])");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
customElements.define("et2-checkbox", Et2Checkbox);
|