Skip to content

Commit

Permalink
refactor: use individual setters instead of enum
Browse files Browse the repository at this point in the history
  • Loading branch information
web-padawan committed Jul 9, 2024
1 parent 866a872 commit ed9ee38
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import elemental.json.Json;
Expand All @@ -44,7 +41,6 @@
import com.vaadin.flow.dom.ElementDetachEvent;
import com.vaadin.flow.dom.ElementDetachListener;
import com.vaadin.flow.dom.Style;
import com.vaadin.flow.internal.JsonSerializer;
import com.vaadin.flow.shared.Registration;

/**
Expand All @@ -63,53 +59,17 @@ public class Popover extends Component implements HasComponents {
private Component target;
private Registration targetAttachRegistration;

private boolean openOnClick = true;
private boolean openOnHover = false;
private boolean openOnFocus = false;

/**
* Constructs an empty popover.
*/
public Popover() {
getElement().getNode().addAttachListener(this::attachComponentRenderer);

setTrigger(Set.of(PopoverTrigger.CLICK));
}

/**
* Sets the popover trigger mode describing how the overlay is opened or
* closed on user interaction with the target.
*
* When passing null or an empty set, the popover can only be opened and
* closed programmatically.
*
* @param trigger
* the set of trigger values to set
*/
public void setTrigger(Set<PopoverTrigger> trigger) {
JsonArray triggerAsJson = Json.createArray();

if (!(trigger == null || trigger.isEmpty())) {
triggerAsJson = JsonSerializer.toJson(trigger.stream()
.map(PopoverTrigger::toString).collect(Collectors.toSet()));
}

getElement().setPropertyJson("trigger", triggerAsJson);
}

/**
* Gets the popover trigger mode describing how the overlay is opened or
* closed on user interaction with the target.
*
* @return the trigger
*/
public Set<PopoverTrigger> getTrigger() {
List<String> trigger = JsonSerializer.toObjects(String.class,
(JsonArray) getElement().getPropertyRaw("trigger"));

if (trigger.isEmpty()) {
return Collections.emptySet();
}

return trigger.stream()
.map((item) -> PopoverTrigger.valueOf(item.toUpperCase()))
.collect(Collectors.toSet());
updateTrigger();
}

/**
Expand Down Expand Up @@ -221,6 +181,93 @@ public int getHideDelay() {
return getElement().getProperty("hideDelay", 0);
}

/**
* Sets whether the popover can be opened via target click.
*
* @param openOnClick
* {@code true} to allow opening the popover via target click,
* {@code false} to disallow it.
*/
public void setOpenOnClick(boolean openOnClick) {
this.openOnClick = openOnClick;
updateTrigger();
}

/**
* Gets whether the popover can be opened via target click, which is
* {@code true} by default.
*
* @return {@code true} if the popover can be opened with target click,
* {@code false} otherwise.
*/
public boolean isOpenOnClick() {
return this.openOnClick;
}

/**
* Sets whether the popover can be opened via target focus.
*
* @param openOnFocus
* {@code true} to allow opening the popover via target focus,
* {@code false} to disallow it.
*/
public void setOpenOnFocus(boolean openOnFocus) {
this.openOnFocus = openOnFocus;
updateTrigger();
}

/**
* Gets whether the popover can be opened via target focus, which is
* {@code false} by default.
*
* @return {@code true} if the popover can be opened with target focus,
* {@code false} otherwise.
*/
public boolean isOpenOnFocus() {
return this.openOnFocus;
}

/**
* Sets whether the popover can be opened via target hover.
*
* @param openOnHover
* {@code true} to allow opening the popover via target hover,
* {@code false} to disallow it.
*/
public void setOpenOnHover(boolean openOnHover) {
this.openOnHover = openOnHover;
updateTrigger();
}

/**
* Gets whether the popover can be opened via target hover, which is
* {@code false} by default.
*
* @return {@code true} if the popover can be opened with target hover,
* {@code false} otherwise.
*/
public boolean isOpenOnHover() {
return this.openOnHover;
}

private void updateTrigger() {
JsonArray trigger = Json.createArray();

if (isOpenOnClick()) {
trigger.set(trigger.length(), "click");
}

if (isOpenOnHover()) {
trigger.set(trigger.length(), "hover");
}

if (isOpenOnFocus()) {
trigger.set(trigger.length(), "focus");
}

getElement().setPropertyJson("trigger", trigger);
}

/**
* Sets the target component for this popover.
* <p>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -101,60 +101,83 @@ public void setHideDelay_getHideDelay() {
}

@Test
public void getTrigger_defaultValue_click() {
Assert.assertEquals(Set.of(PopoverTrigger.CLICK), popover.getTrigger());
public void isOpenOnClick_trueByDefault() {
Assert.assertTrue(popover.isOpenOnClick());
}

@Test
public void getTriggerProperty_defaultValue_click() {
JsonArray jsonArray = (JsonArray) popover.getElement()
.getPropertyRaw("trigger");
Assert.assertEquals(1, jsonArray.length());
Assert.assertEquals("click", jsonArray.get(0).asString());
public void isOpenOnFocus_falseByDefault() {
Assert.assertFalse(popover.isOpenOnFocus());
}

@Test
public void setTrigger_getTrigger() {
popover.setTrigger(Set.of(PopoverTrigger.HOVER, PopoverTrigger.FOCUS));
Assert.assertEquals(Set.of(PopoverTrigger.HOVER, PopoverTrigger.FOCUS),
popover.getTrigger());
public void isOpenOnHover_falseByDefault() {
Assert.assertFalse(popover.isOpenOnHover());
}

@Test
public void setTrigger_getTriggerProperty() {
popover.setTrigger(Set.of(PopoverTrigger.HOVER, PopoverTrigger.FOCUS));
JsonArray jsonArray = (JsonArray) popover.getElement()
.getPropertyRaw("trigger");
Assert.assertEquals(2, jsonArray.length());
Assert.assertEquals("hover", jsonArray.get(0).asString());
Assert.assertEquals("focus", jsonArray.get(1).asString());
public void setOpenOnClick_isOpenOnClick() {
popover.setOpenOnClick(false);
Assert.assertFalse(popover.isOpenOnClick());

popover.setOpenOnClick(true);
Assert.assertTrue(popover.isOpenOnClick());
}

@Test
public void setOpenOnFocus_isOpenOnFocus() {
popover.setOpenOnFocus(true);
Assert.assertTrue(popover.isOpenOnFocus());

popover.setOpenOnFocus(false);
Assert.assertFalse(popover.isOpenOnFocus());
}

@Test
public void setOpenOnHover_isOpenOnHover() {
popover.setOpenOnHover(true);
Assert.assertTrue(popover.isOpenOnHover());

popover.setOpenOnHover(false);
Assert.assertFalse(popover.isOpenOnHover());
}

@Test
public void setTrigger_null_getTrigger_emptySet() {
popover.setTrigger(null);
Assert.assertEquals(Collections.emptySet(), popover.getTrigger());
public void getTriggerProperty_defaultValue_click() {
JsonArray jsonArray = (JsonArray) popover.getElement()
.getPropertyRaw("trigger");
Assert.assertEquals(1, jsonArray.length());
Assert.assertEquals("click", jsonArray.get(0).asString());
}

@Test
public void setTrigger_null_getTriggerProperty_emptyArray() {
popover.setTrigger(null);
public void setOpenOnClick_triggerPropertyUpdated() {
popover.setOpenOnClick(false);

JsonArray jsonArray = (JsonArray) popover.getElement()
.getPropertyRaw("trigger");
Assert.assertEquals(0, jsonArray.length());
}

@Test
public void setTrigger_empty_getTrigger_emptySet() {
popover.setTrigger(Collections.emptySet());
Assert.assertEquals(Collections.emptySet(), popover.getTrigger());
public void setOpenOnFocus_triggerPropertyUpdated() {
popover.setOpenOnFocus(true);

JsonArray jsonArray = (JsonArray) popover.getElement()
.getPropertyRaw("trigger");
Assert.assertEquals(2, jsonArray.length());
Assert.assertEquals("click", jsonArray.get(0).asString());
Assert.assertEquals("focus", jsonArray.get(1).asString());
}

@Test
public void setTrigger_empty_getTriggerProperty_emptyArray() {
popover.setTrigger(Collections.emptySet());
public void setOpenOnHover_triggerPropertyUpdated() {
popover.setOpenOnHover(true);

JsonArray jsonArray = (JsonArray) popover.getElement()
.getPropertyRaw("trigger");
Assert.assertEquals(0, jsonArray.length());
Assert.assertEquals(2, jsonArray.length());
Assert.assertEquals("click", jsonArray.get(0).asString());
Assert.assertEquals("hover", jsonArray.get(1).asString());
}
}

0 comments on commit ed9ee38

Please sign in to comment.