Skip to content

Commit

Permalink
#201 - Add stealth routing - done (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankHossfeld authored Jun 17, 2021
1 parent 9b305b1 commit 0d910b0
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 24 deletions.
8 changes: 8 additions & 0 deletions etc/wiki/11 Router.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ Starting with version 2.0.1 Nalu offers a new method to route. The name of the n

This might be useful in case of an error and routing to a central error page or a start-page.

### Stealth Routing without calling the mayStop-method (since 2.5.0)
Starting with version 2.5.0 Nalu offers a new method to route. The name of the new method is `forceStealthRoute`. You can use the `forceStealthRoute`-method similar to the `route-method. The main difference is, that the `stealthRoute`-method will not update the hash and will not call the `mayStop`-method of currently active controllers and composites.

### Stealth Routing (since 2.5.0)
Starting with version 2.5.0 Nalu offers a new method to route. The name of the new method is `stealthRoute`. You can use the `stealthRoute`-method similar to the `route-method. The main difference is, that the `stealthRoute`-method will not update the hash.

This might be useful in case you have to implement a dispatcher controller and don't want to the dispatcher hash inside the history..

### Note on routes
Defining routes in your application needs some attention. Otherwise you will be surprised by some unexpected results.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ private static void handleChange(RouteChangeHandler handler,
.getStartRoute(),
!PropertyFactory.get()
.isStayOnSide(),
false,
handler);
} else {
handler.onRouteChange(newUrl);
Expand All @@ -219,6 +220,7 @@ private static void handleChange(RouteChangeHandler handler,

public static void route(String newRoute,
boolean replace,
boolean stealthMode,
RouteChangeHandler handler) {
String newRouteToken;
if (PropertyFactory.get()
Expand All @@ -238,14 +240,16 @@ public static void route(String newRoute,
}
if (PropertyFactory.get()
.hasHistory()) {
if (replace) {
DomGlobal.window.history.replaceState(newRouteToken,
null,
newRouteToken);
} else {
DomGlobal.window.history.pushState(newRouteToken,
null,
newRouteToken);
if (!stealthMode) {
if (replace) {
DomGlobal.window.history.replaceState(newRouteToken,
null,
newRouteToken);
} else {
DomGlobal.window.history.pushState(newRouteToken,
null,
newRouteToken);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,11 @@ public void remove(String selector) {

@Override
public void route(String newRoute,
boolean replace) {
boolean replace,
boolean stealthMode) {
NaluPluginCoreWeb.route(newRoute,
replace,
stealthMode,
this.routeChangeHandler);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,11 @@ public void remove(String selector) {

@Override
public void route(String newRoute,
boolean replace) {
boolean replace,
boolean stealthMode) {
NaluPluginCoreWeb.route(newRoute,
replace,
stealthMode,
this.routeChangeHandler);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,11 @@ public void remove(String selector) {

@Override
public void route(String newRoute,
boolean replace) {
boolean replace,
boolean stealthMode) {
NaluPluginCoreWeb.route(newRoute,
replace,
stealthMode,
this.routeChangeHandler);
}

Expand Down
21 changes: 20 additions & 1 deletion nalu/src/main/java/com/github/nalukit/nalu/client/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,26 @@ void route(String route,
*/
void forceRoute(String route,
String... parameter);


/**
* Route to a new page without confirmation
* and updating the hash.
*
* @param route new route
* @param parameter parameters of the route
*/
void forceStealthRoute(String route,
String... parameter);

/**
* Route to a new page without updating the hash.
*
* @param route new route
* @param parameter parameters of the route
*/
void stealthRoute(String route,
String... parameter);

/**
* Removes a controller from the cache
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ public class RoutingInterceptionException
extends Exception {

private String controllerClassName;

private String route;

private String[] parameter;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ public void route(String newRoute,
}
// let's do the routing!
this.route(newRoute,
false,
false,
false,
params);
Expand Down Expand Up @@ -201,6 +202,75 @@ public void forceRoute(String newRoute,
this.route(newRoute,
true,
false,
false,
params);
}

/**
* The method routes to another screen. In case it is called,
* it will:
* <ul>
* <li>not create a new hash</li>
* <li>not update the url (in case history is desired)</li>
* </ul>
* Once the url gets updated, it triggers the onhashchange event
* and Nalu starts to work
* <p>
* in opposite to the route-method, the forceStealthRoute-method
* does not confirm the new route!
*
* @param newRoute routing goal
* @param params list of parameters [0 - n]
*/
@Override
public void forceStealthRoute(String newRoute,
String... params) {
// fire souring event ...
this.fireRouterStateEvent(RouterState.START_ROUTING,
newRoute,
params);
// first, we track the new route (if there is a tracker!)
if (!Objects.isNull(this.tracker)) {
this.tracker.track(newRoute,
params);
}
// let's do the routing!
this.route(newRoute,
true,
false,
true,
params);
}

/**
* The method routes to another screen. In case it is called,
* it will:
* <ul>
* <li>not create a new hash</li>
* <li>not update the url (in case history is desired)</li>
* </ul>
* Once the url gets updated, it triggers the onhashchange event and Nalu starts to work
*
* @param newRoute routing goal
* @param params list of parameters [0 - n]
*/
@Override
public void stealthRoute(String newRoute,
String... params) {
// fire souring event ...
this.fireRouterStateEvent(RouterState.START_ROUTING,
newRoute,
params);
// first, we track the new route (if there is a tracker!)
if (!Objects.isNull(this.tracker)) {
this.tracker.track(newRoute,
params);
}
// let's do the routing!
this.route(newRoute,
false,
false,
true,
params);
}

Expand Down Expand Up @@ -411,6 +481,7 @@ void handleRouting(String hash,
this.route(redirectTo,
true,
true,
false,
parms);
return;
}
Expand Down Expand Up @@ -440,6 +511,7 @@ public void onOk() {
@Override
public void onCancel() {
plugin.route(lastExecutedHash,
false,
false);
// clear loop detection list ...
loopDetectionList.clear();
Expand Down Expand Up @@ -790,6 +862,7 @@ public void onRoutingInterceptionException(RoutingInterceptionException e) {
route(e.getRoute(),
true,
true,
false,
e.getParameter());
}

Expand Down Expand Up @@ -882,6 +955,7 @@ private void doRouting(String hash,
this.route(e.getRoute(),
true,
true,
false,
e.getParameter());
}
});
Expand Down Expand Up @@ -960,6 +1034,7 @@ private void doRouting(String hash,
this.route(e.getRoute(),
true,
true,
false,
e.getParameter());
return;
}
Expand Down Expand Up @@ -990,6 +1065,7 @@ private void doRouting(String hash,
route(e.getRoute(),
true,
true,
false,
e.getParameter());
return;
}
Expand Down Expand Up @@ -1104,16 +1180,13 @@ private List<CompositeControllerReference> getCompositeForController(String cont
private void route(String newRoute,
boolean forceRouting,
boolean replaceState,
boolean stealthMode,
String... params) {
String newRouteWithParams = this.generate(newRoute,
params);
if (replaceState) {
this.plugin.route(newRouteWithParams,
true);
} else {
this.plugin.route(newRouteWithParams,
false);
}
this.plugin.route(newRouteWithParams,
replaceState,
stealthMode);
this.handleRouting(newRouteWithParams,
forceRouting);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ void confirm(String message,
void remove(String selector);

void route(String newRoute,
boolean replace);
boolean replace,
boolean stealthMode);

void initialize(ShellConfiguration shellConfiguration);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ public void remove(String selector) {

@Override
public void route(String newRoute,
boolean replace) {
boolean replace,
boolean stealthMode) {
Assertions.assertTrue(routeHandler.compare(newRoute,
replace),
"route mismatch!");
Expand Down
3 changes: 2 additions & 1 deletion nalu/src/test/java/com/github/nalukit/nalu/client/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public void remove(String selector) {

@Override
public void route(String newRoute,
boolean replace) {
boolean replace,
boolean stealthMode) {
}

@Override
Expand Down

0 comments on commit 0d910b0

Please sign in to comment.