org.dominokit.domino.ui.forms.SwitchButton Maven / Gradle / Ivy
package org.dominokit.domino.ui.forms;
import elemental2.dom.HTMLDivElement;
import elemental2.dom.HTMLElement;
import elemental2.dom.HTMLInputElement;
import elemental2.dom.HTMLLabelElement;
import org.dominokit.domino.ui.style.Color;
import org.dominokit.domino.ui.style.Style;
import org.dominokit.domino.ui.style.Styles;
import org.dominokit.domino.ui.utils.Checkable;
import org.dominokit.domino.ui.utils.DominoElement;
import java.util.ArrayList;
import java.util.List;
import static java.util.Objects.isNull;
import static org.jboss.gwt.elemento.core.Elements.*;
public class SwitchButton extends BasicFormElement implements Checkable {
public static final String READONLY = "readonly";
private HTMLDivElement container = div().css("switch").css("form-group").css(Styles.no_wrap).asElement();
private HTMLDivElement formLine = div().css("form-line").asElement();
private HTMLDivElement formControl = div().css("form-control").asElement();
private HTMLLabelElement onOffLabelElement = label().asElement();
private HTMLLabelElement labelElement = label().css("form-label", "focused").asElement();
private HTMLInputElement inputElement = input("checkbox").asElement();
private DominoElement lever = DominoElement.of(span().css("lever"));
private List> changeHandlers = new ArrayList<>();
private Color color;
private DominoElement onTitleTextRoot = DominoElement.of(span());
private DominoElement offTitleTextRoot = DominoElement.of(span());
private String checkedReadonlyLabel = "Yes";
private String unCheckedReadonlyLabel = "No";
private boolean autoValidation;
private String offTitle;
private String onTitle;
public SwitchButton(String label, String offTitle, String onTitle) {
this(label);
setOffTitle(offTitle);
setOnTitle(onTitle);
}
public SwitchButton(String label, String onOffTitle) {
this(label);
setOffTitle(onOffTitle);
}
public SwitchButton(String label) {
this();
setLabel(label);
}
public SwitchButton() {
Style.of(formControl).setProperty("border-bottom", "0px");
formControl.appendChild(onOffLabelElement);
onOffLabelElement.appendChild(offTitleTextRoot.asElement());
onOffLabelElement.appendChild(inputElement);
onOffLabelElement.appendChild(lever.asElement());
onOffLabelElement.appendChild(onTitleTextRoot.asElement());
inputElement.addEventListener("change", evt -> {
evt.stopPropagation();
if (!isReadOnly()) {
onCheck();
if (autoValidation)
validate();
}
});
formLine.appendChild(formControl);
formLine.appendChild(labelElement);
container.appendChild(formLine);
init(this);
}
public static SwitchButton create(String label, String offTitle, String onTitle) {
return new SwitchButton(label, offTitle, onTitle);
}
public static SwitchButton create(String label, String onOffTitle) {
return new SwitchButton(label, onOffTitle);
}
public static SwitchButton create() {
return new SwitchButton();
}
private void onCheck() {
for (ChangeHandler super Boolean> checkHandler : changeHandlers) {
checkHandler.onValueChanged(isChecked());
}
}
public DominoElement getLever() {
return lever;
}
@Override
public SwitchButton value(Boolean value) {
if (value != null && value) {
check();
} else {
uncheck();
}
return this;
}
@Override
public Boolean getValue() {
return inputElement.checked;
}
@Override
public boolean isEmpty() {
return !isChecked();
}
@Override
public SwitchButton clear() {
value(false);
return this;
}
@Override
public SwitchButton check() {
return check(false);
}
@Override
public SwitchButton uncheck() {
return uncheck(false);
}
@Override
public SwitchButton check(boolean silent) {
inputElement.checked = true;
if (!silent) {
onCheck();
validate();
}
updateLabel();
return this;
}
@Override
public SwitchButton uncheck(boolean silent) {
inputElement.checked = false;
if (!silent) {
onCheck();
validate();
}
updateLabel();
return this;
}
@Override
public boolean isChecked() {
return inputElement.checked;
}
@Override
public SwitchButton addChangeHandler(ChangeHandler super Boolean> changeHandler) {
changeHandlers.add(changeHandler);
return this;
}
@Override
public SwitchButton removeChangeHandler(ChangeHandler super Boolean> changeHandler) {
if (changeHandler != null)
changeHandlers.remove(changeHandler);
return this;
}
@Override
public boolean hasChangeHandler(ChangeHandler super Boolean> changeHandler) {
return changeHandlers.contains(changeHandler);
}
public SwitchButton setColor(Color color) {
if (this.color != null)
lever.style().remove(this.color.getStyle());
lever.style().add(color.getStyle());
this.color = color;
return this;
}
@Override
public SwitchButton setLabel(String label) {
this.labelElement.textContent = label;
return this;
}
@Override
public String getLabel() {
return labelElement.textContent;
}
@Override
public DominoElement getLabelElement() {
return DominoElement.of(labelElement);
}
public SwitchButton setOnTitle(String onTitle) {
onTitleTextRoot
.clearElement()
.appendChild(span().textContent(onTitle));
this.onTitle = onTitle;
return this;
}
public SwitchButton setOffTitle(String offTitle) {
offTitleTextRoot
.clearElement()
.appendChild(span().textContent(offTitle));
this.offTitle = offTitle;
return this;
}
public DominoElement getOnOffLabelElement() {
return DominoElement.of(onOffLabelElement);
}
@Override
public DominoElement getInputElement() {
return DominoElement.of(inputElement);
}
@Override
public DominoElement getFieldContainer() {
return DominoElement.of(formLine);
}
@Override
public SwitchButton setAutoValidation(boolean autoValidation) {
this.autoValidation = autoValidation;
return this;
}
@Override
public boolean isAutoValidation() {
return autoValidation;
}
@Override
public HTMLElement asElement() {
return container;
}
public Style style() {
return Style.of(this);
}
@Override
public SwitchButton setReadOnly(boolean readOnly) {
inputElement.disabled = readOnly;
if (readOnly) {
setAttribute(READONLY, READONLY);
updateLabel();
} else {
removeAttribute(READONLY);
setOffTitle(offTitle);
setOnTitle(onTitle);
}
return this;
}
private void updateLabel() {
setOffTitle(offTitle);
setOnTitle(onTitle);
if (isReadOnly()) {
if (isChecked()) {
if (isOnTitleEmpty()) {
offTitleTextRoot
.clearElement()
.appendChild(span().textContent(offTitle))
.appendChild(span().textContent(getCheckedReadonlyLabel()));
} else {
offTitleTextRoot.clearElement();
}
} else {
if (isOnTitleEmpty()) {
offTitleTextRoot
.clearElement()
.appendChild(span().textContent(offTitle))
.appendChild(span().textContent(getUnCheckedReadonlyLabel()));
} else {
onTitleTextRoot.clearElement();
}
}
}
}
private boolean isOnTitleEmpty() {
return isNull(onTitle) || onTitle.isEmpty();
}
private String getCheckedReadonlyLabel() {
return isNull(checkedReadonlyLabel) || checkedReadonlyLabel.isEmpty() ? "" : ": " + checkedReadonlyLabel;
}
private String getUnCheckedReadonlyLabel() {
return isNull(unCheckedReadonlyLabel) || unCheckedReadonlyLabel.isEmpty() ? "" : ": " + unCheckedReadonlyLabel;
}
public SwitchButton setCheckedReadonlyLabel(String checkedReadonlyLabel) {
this.checkedReadonlyLabel = checkedReadonlyLabel;
return this;
}
public SwitchButton setUnCheckedReadonlyLabel(String unCheckedReadonlyLabel) {
this.unCheckedReadonlyLabel = unCheckedReadonlyLabel;
return this;
}
@Override
public boolean isReadOnly() {
return hasAttribute(READONLY);
}
@Override
public String getStringValue() {
return Boolean.toString(getValue());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy