Skip to content

Commit

Permalink
Inverted link selector for place manager
Browse files Browse the repository at this point in the history
  • Loading branch information
hpehl committed Mar 5, 2024
1 parent fa313ff commit 075d930
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- Inverted link selector for place manager

## [1.3.2] - 2024-03-05

### Fixed
Expand Down
58 changes: 58 additions & 0 deletions router/src/main/java/org/jboss/elemento/router/LinkSelector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2023 Red Hat
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.elemento.router;

import org.jboss.elemento.By;

import elemental2.dom.HTMLAnchorElement;

class LinkSelector {

private final By selector;
private final boolean not;

LinkSelector() {
this((By) null, false);
}

LinkSelector(String selector) {
this(By.selector(selector), false);
}

LinkSelector(String selector, boolean not) {
this(By.selector(selector), not);
}

LinkSelector(By selector) {
this(selector, false);
}

LinkSelector(By selector, boolean not) {
this.selector = selector;
this.not = not;
}

boolean matches(HTMLAnchorElement a) {
if (selector == null) {
return true;
}
if (not) {
return !a.matches(selector.toString());
} else {
return a.matches(selector.toString());
}
}
}
20 changes: 13 additions & 7 deletions router/src/main/java/org/jboss/elemento/router/PlaceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class PlaceManager {
private Supplier<HTMLElement> root;
private Function<String, String> title;
private Function<Place, Page> notFound;
private By linkSelector;
private LinkSelector linkSelector;
private String failedRoute;

public PlaceManager() {
Expand All @@ -87,7 +87,7 @@ public PlaceManager() {
this.root = () -> document.body;
this.title = Function.identity();
this.notFound = place -> new DefaultNotFound();
this.linkSelector = null;
this.linkSelector = new LinkSelector();
this.failedRoute = null;
}

Expand Down Expand Up @@ -136,11 +136,19 @@ public PlaceManager afterPlace(AfterPlaceHandler afterPlace) {
}

public PlaceManager linkSelector(String selector) {
return linkSelector(By.selector(selector));
return linkSelector(By.selector(selector), false);
}

public PlaceManager linkSelector(String selector, boolean not) {
return linkSelector(By.selector(selector), not);
}

public PlaceManager linkSelector(By selector) {
this.linkSelector = selector;
return linkSelector(selector, false);
}

public PlaceManager linkSelector(By selector, boolean not) {
this.linkSelector = new LinkSelector(selector, not);
return this;
}

Expand Down Expand Up @@ -223,9 +231,7 @@ private HTMLAnchorElement anchorElement(Event event) {

private boolean shouldHandleLink(HTMLAnchorElement a, URL url) {
if (url.origin.equals(location.origin) && url.hash.isEmpty()) { // only links of this origin w/o a hash
if (base.isRelative(url.pathname)) { // and the same base relative path
return linkSelector == null || a.matches(linkSelector.toString());
}
return linkSelector.matches(a);
}
return false;
}
Expand Down

0 comments on commit 075d930

Please sign in to comment.