Skip to content

Commit

Permalink
feat: add target API to SideNavItem (#5962)
Browse files Browse the repository at this point in the history
* feat: add target API to SideNavItem

* test: add tests for new target API in SideNavItem

* chore: run formatter
  • Loading branch information
DiegoCardoso committed Jan 22, 2024
1 parent 0e1c4a1 commit 1951cf1
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,67 @@ public void setPathAliases(Set<String> pathAliases) {
}
}

/**
* Gets the target of this item.
*
* @return the target of this item
*/
public String getTarget() {
return getElement().getProperty("target");
}

/**
* Where to display the linked URL, as the name for a browsing context.
* <p>
* The following keywords have special meanings for where to load the URL:
* <ul>
* <li><code>_self</code>: the current browsing context. (Default)</li>
* <li><code>_blank</code>: usually a new tab, but users can configure
* browsers to open a new window instead.</li>
* <li><code>_parent</code>: the parent browsing context of the current one.
* If no parent, behaves as <code>_self</code>.</li>
* <li><code>_top</code>: the topmost browsing context (the "highest"
* context that’s an ancestor of the current one). If no ancestors, behaves
* as <code>_self</code>.</li>
* </ul>
* </p>
*
* @param target
* the target of this item
*/
public void setTarget(String target) {
if (target == null) {
getElement().removeProperty("target");
} else {
getElement().setProperty("target", target);
}
}

/**
* Sets whether the target URL should be opened in a new browser tab.
* <p>
* This is a convenience method for setting the target to
* <code>_blank</code>. See {@link #setTarget(String)} for more information.
* </p>
*
* @param openInNewBrowserTab
* true if the target URL should be opened in a new browser tab,
* false otherwise
*/
public void setOpenInNewBrowserTab(boolean openInNewBrowserTab) {
setTarget(openInNewBrowserTab ? "_blank" : null);
}

/**
* Gets whether the target URL should be opened in a new browser tab.
*
* @return true if the target URL should be opened in a new browser tab,
* false otherwise
*/
public boolean isOpenInNewBrowserTab() {
return "_blank".equals(getTarget());
}

private Set<String> getPathAliasesFromView(Class<? extends Component> view,
RouteParameters routeParameters) {
RouteAlias[] routeAliases = view.getAnnotationsByType(RouteAlias.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,39 @@ public void setPathAsComponent_aliasWithMissingParameterNotAdded() {
}, TestRouteWithAliases.class);
}

@Test
public void setTarget_hasTarget() {
sideNavItem.setTarget("_blank");
Assert.assertEquals("_blank",
sideNavItem.getElement().getProperty("target"));
Assert.assertEquals("_blank", sideNavItem.getTarget());
}

@Test
public void targetDefined_setToNull_noTarget() {
sideNavItem.setTarget("_blank");
sideNavItem.setTarget(null);
Assert.assertFalse(sideNavItem.getElement().hasProperty("target"));
Assert.assertNull(sideNavItem.getTarget());
}

@Test
public void setOpenInNewBrowserTab_targetBlankDefinedOnProperty() {
// call setOpenInNewTab and check that getTarget returns "_blank"
sideNavItem.setOpenInNewBrowserTab(true);
Assert.assertEquals("_blank",
sideNavItem.getElement().getProperty("target"));
Assert.assertTrue(sideNavItem.isOpenInNewBrowserTab());
}

@Test
public void openInNewBrowserTabDefined_setOpenInNewBrowserTabToFalse() {
sideNavItem.setOpenInNewBrowserTab(true);
sideNavItem.setOpenInNewBrowserTab(false);
Assert.assertFalse(sideNavItem.getElement().hasProperty("target"));
Assert.assertFalse(sideNavItem.isOpenInNewBrowserTab());
}

private boolean sideNavItemHasLabelElement() {
return sideNavItem.getElement().getChildren()
.anyMatch(this::isLabelElement);
Expand Down

0 comments on commit 1951cf1

Please sign in to comment.