Skip to content

Commit

Permalink
fix #578 Clearing the tree search leads to a wrong expanded tree afte…
Browse files Browse the repository at this point in the history
…rwards
  • Loading branch information
vegegoku committed Oct 10, 2021
1 parent 7e7067e commit 7e11eba
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ public Card setCollapsible() {

private void switchVisibility() {
if (collapsible) {
if (body.getCollapsible().isHidden()) {
if (body.getCollapsible().isCollapsed()) {
expand();
collapseAnchor.element().setAttribute("aria-expanded", "true");
} else {
Expand All @@ -427,7 +427,7 @@ private void switchVisibility() {
* @return same instance
*/
public Card toggle() {
if (body.getCollapsible().isHidden()) {
if (body.getCollapsible().isCollapsed()) {
expand();
} else {
collapse();
Expand Down Expand Up @@ -461,7 +461,7 @@ public Card collapse() {
* @return true if the body is hidden, false otherwise
*/
public boolean isCollapsed() {
return body.getCollapsible().isHidden();
return body.getCollapsible().isCollapsed();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ public void togglePanel(AccordionPanel panel) {
List<AccordionPanel> accordionPanels = otherPanels(panel);
accordionPanels.forEach(
accordionPanel -> {
if (!accordionPanel.isHidden()) {
if (!accordionPanel.isCollapsed()) {
accordionPanel.hide();
}
});
if (panel.isHidden()) {
if (panel.isCollapsed()) {
panel.show();
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public boolean isForceHidden() {
* @return same instance
*/
public Collapsible setForceHidden(boolean forceHidden) {
if (!isHidden()) {
if (!isCollapsed()) {
hide();
}
this.forceHidden = forceHidden;
Expand Down Expand Up @@ -151,7 +151,18 @@ private void onShowCompleted() {
* @return boolean, true if the element is hidden.
*/
@Override
@Deprecated
public boolean isHidden() {
return isCollapsed();
}

/**
* checks if the wrapped element is collapsed
*
* @return boolean, true if the element is collapsed.
*/
@Override
public boolean isCollapsed() {
return this.collapsed || DominoElement.of(element).hasAttribute("d-collapsed");
}

Expand All @@ -162,7 +173,7 @@ public boolean isHidden() {
*/
@Override
public Collapsible toggleDisplay() {
if (isHidden()) {
if (isCollapsed()) {
show();
} else {
hide();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.dominokit.domino.ui.utils.BaseDominoElement;
import org.dominokit.domino.ui.utils.DominoElement;
import org.dominokit.domino.ui.utils.HasBackground;
import org.dominokit.domino.ui.utils.IsCollapsible;
import org.jboss.elemento.EventType;
import org.jboss.elemento.IsElement;

Expand Down Expand Up @@ -190,7 +191,7 @@ private void selectFirstSearchResult() {

private int firstVisibleActionIndex() {
for (int i = 0; i < actions.size(); i++) {
if (!actions.get(i).isHidden()) {
if (actions.get(i).isExpanded()) {
return i;
}
}
Expand All @@ -199,7 +200,7 @@ private int firstVisibleActionIndex() {

private int lastVisibleActionIndex() {
for (int i = actions.size() - 1; i >= 0; i--) {
if (!actions.get(i).isHidden()) {
if (actions.get(i).isExpanded()) {
return i;
}
}
Expand Down Expand Up @@ -249,7 +250,7 @@ private void addMenuNavigationListener() {
menuNavigation =
MenuNavigation.create(actions)
.onSelect(DropdownAction::select)
.focusCondition(item -> !item.isHidden())
.focusCondition(IsCollapsible::isExpanded)
.onFocus(
item -> {
if (isOpened()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public HTMLLIElement element() {
}

boolean isAllHidden() {
return actions.stream().allMatch(DropdownAction::isHidden);
return actions.stream().allMatch(DropdownAction::isCollapsed);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public HTMLLIElement element() {

/** @return boolean, true if all the options in the group are hidden */
boolean isAllHidden() {
return options.stream().allMatch(SelectOption::isHidden);
return options.stream().allMatch(SelectOption::isCollapsed);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private double calculateRangeOffset() {
int width = slider.element().offsetWidth - 15;
double percent = (getValue() - getMin()) / (getMax() - getMin());
double rangeOffset = percent * width;
if (!leftAddonContainer.isHidden()) {
if (leftAddonContainer.isExpanded()) {
rangeOffset += leftAddonContainer.element().offsetWidth + 9;
}
return rangeOffset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ public Tree<T> setAutoCollapse(boolean autoCollapse) {
*/
public Tree<T> setTitle(String title) {
getTitle().setTextContent(title);
if (getHeader().isHidden()) {
if (getHeader().isCollapsed()) {
getHeader().show();
}
return this;
Expand Down
48 changes: 41 additions & 7 deletions domino-ui/src/main/java/org/dominokit/domino/ui/tree/TreeItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static org.jboss.elemento.Elements.span;
import static org.jboss.elemento.Elements.ul;

import elemental2.dom.DomGlobal;
import elemental2.dom.EventListener;
import elemental2.dom.HTMLAnchorElement;
import elemental2.dom.HTMLElement;
Expand Down Expand Up @@ -89,6 +90,7 @@ public class TreeItem<T> extends WavesElement<HTMLLIElement, TreeItem<T>>
private final DominoElement<HTMLElement> indicatorContainer =
DominoElement.of(span().css("tree-indicator"));
private HTMLElement titleElement;
private OriginalState originalState;

public TreeItem(String title, BaseIcon<?> icon) {
this.title = title;
Expand Down Expand Up @@ -386,10 +388,17 @@ public TreeItem<T> toggleDisplay() {
return this;
}

/** {@inheritDoc} */
/** @deprecated use {@link #isCollapsed()} {@inheritDoc} */
@Override
@Deprecated
public boolean isHidden() {
return collapsible.isHidden();
return collapsible.isCollapsed();
}

/** {@inheritDoc} */
@Override
public boolean isCollapsed() {
return collapsible.isCollapsed();
}

/** {@inheritDoc} */
Expand Down Expand Up @@ -512,7 +521,7 @@ public void activate() {
@Override
public void activate(boolean activateParent) {
Style.of(element()).addCss("active");
if (isNull(expandIcon) || collapsible.isHidden() || !isParent()) {
if (isNull(expandIcon) || collapsible.isCollapsed() || !isParent()) {
replaceIcon(this.activeIcon);
}

Expand All @@ -535,7 +544,7 @@ private void replaceIcon(BaseIcon<?> newIcon) {
@Override
public void deactivate() {
Style.of(element()).removeCss("active");
if (isNull(expandIcon) || collapsible.isHidden() || !isParent()) {
if (isNull(expandIcon) || collapsible.isCollapsed() || !isParent()) {
restoreIcon();
}
if (isParent()) {
Expand Down Expand Up @@ -636,6 +645,10 @@ public String getTitle() {
*/
public boolean filter(String searchToken) {
boolean found;
if (isNull(this.originalState)) {
this.originalState = new OriginalState(collapsible.isExpanded());
}

if (isParent()) {
found = getFilter().filter(this, searchToken) | filterChildren(searchToken);
} else {
Expand All @@ -644,7 +657,7 @@ public boolean filter(String searchToken) {

if (found) {
Style.of(element).removeCssProperty("display");
if (isParent() && isAutoExpandFound() && collapsible.isHidden()) {
if (isParent() && isAutoExpandFound() && collapsible.isCollapsed()) {
collapsible.show();
}
return true;
Expand All @@ -662,6 +675,19 @@ public boolean isAutoExpandFound() {

/** Clears the filter applied */
public void clearFilter() {
if (nonNull(originalState)) {
DomGlobal.requestAnimationFrame(
timestamp -> {
if (collapsible.isExpanded() != originalState.expanded) {
if (this.equals(this.getTreeRoot().getActiveItem())) {
collapsible.show();
} else {
collapsible.toggleDisplay(originalState.expanded);
}
}
this.originalState = null;
});
}
Style.of(element).removeCssProperty("display");
subItems.forEach(TreeItem::clearFilter);
}
Expand All @@ -680,15 +706,15 @@ public boolean filterChildren(String searchToken) {

/** Collapse all children */
public void collapseAll() {
if (isParent() && !collapsible.isHidden()) {
if (isParent() && !collapsible.isCollapsed()) {
hide();
subItems.forEach(TreeItem::collapseAll);
}
}

/** Expand all children */
public void expandAll() {
if (isParent() && collapsible.isHidden()) {
if (isParent() && collapsible.isExpanded()) {
show();
subItems.forEach(TreeItem::expandAll);
}
Expand Down Expand Up @@ -861,4 +887,12 @@ public TreeItem<T> setTitle(String title) {
public HTMLUListElement getChildrenContainer() {
return childrenContainer;
}

private static class OriginalState {
private boolean expanded;

public OriginalState(boolean expanded) {
this.expanded = expanded;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,25 @@ public T clearElement() {
return element;
}

/** @return boolean, true if the component is not visible */
/**
* @deprecated use {@link #isCollapsed()}
* @return boolean, true if the component is not visible
*/
@Override
@Editor.Ignore
@Deprecated
public boolean isHidden() {
return isCollapsed();
}

/** @return boolean, true if the component is not visible */
@Override
@Editor.Ignore
public boolean isCollapsed() {
if (isNull(collapsible)) {
return false;
}
return collapsible.isHidden();
return collapsible.isCollapsed();
}

/** @return the HTML element of type E which is the root element of the component */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ public interface IsCollapsible<T> {
*/
T toggleDisplay(boolean state);

/** @return boolean, true if the component is hidden */
/**
* @deprecated use {@link #isCollapsed()}
* @return boolean, true if the component is hidden
*/
@Deprecated
boolean isHidden();

/** @return boolean, true if the component is collapsed */
boolean isCollapsed();

/** @return boolean, true if the component is expanded */
default boolean isExpanded() {
return !isCollapsed();
}
}

0 comments on commit 7e11eba

Please sign in to comment.