Skip to content
This repository has been archived by the owner on Nov 29, 2018. It is now read-only.

added flexible path prefix #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/main/java/de/barop/gwt/client/HistoryImplPushState.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import java.util.logging.Logger;

import com.google.gwt.core.client.GWT;
import com.google.gwt.logging.client.LogConfiguration;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
Expand All @@ -40,8 +41,13 @@ public class HistoryImplPushState extends HistoryImpl {

private static final Logger LOG = Logger.getLogger(HistoryImplPushState.class.getName());

private String currentPrefix;

@Override
public boolean init() {
// read default path prefix
final PathProviderInterface pathProvider = GWT.create(PathProviderInterface.class);
this.currentPrefix = pathProvider.getPathPrefix();
// initialize HistoryImpl with the current path
updateHistoryToken(Window.Location.getPath() + Window.Location.getQueryString());
// initialize the empty state with the current history token
Expand All @@ -55,8 +61,8 @@ public boolean init() {
@Override
protected void nativeUpdate(final String historyToken) {
String newPushStateToken = CodeServerParameterHelper.append(encodeFragment(historyToken));
if (!newPushStateToken.startsWith("/")) {
newPushStateToken = "/" + newPushStateToken;
if (!newPushStateToken.startsWith(currentPrefix)) {
newPushStateToken = currentPrefix + newPushStateToken;
}

pushState(newPushStateToken);
Expand All @@ -73,7 +79,9 @@ private void updateHistoryToken(String path) {
String[] split = path.split("\\?");
String token = split[0];
token = (token.length() > 0) ? decodeFragment(token) : "";
token = (token.startsWith("/")) ? token.substring(1) : token;
if (token.startsWith(currentPrefix)) {
token = token.substring(currentPrefix.length());
}

String queryString = (split.length == 2) ? split[1] : "";
queryString = CodeServerParameterHelper.remove(queryString);
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/de/barop/gwt/client/PathProviderImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2014 Manfred Tremmel
*
* 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
*
* http://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 de.barop.gwt.client;

/**
* Path provider implementation which defines a path prefix.
*
* @author <a href="mailto:[email protected]">Manfred Tremmel</a>
*
*/
public class PathProviderImpl implements PathProviderInterface
{
@Override
public final String getPathPrefix()
{
return "/";
}
}
29 changes: 29 additions & 0 deletions src/main/java/de/barop/gwt/client/PathProviderInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2014 Manfred Tremmel
*
* 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
*
* http://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 de.barop.gwt.client;

/**
* Interface which defines a path prefix provider.
*
* @author <a href="mailto:[email protected]">Manfred Tremmel</a>
*
*/
public interface PathProviderInterface
{
/**
* get the path prefix.
*/
String getPathPrefix();
}
7 changes: 7 additions & 0 deletions src/main/resources/de/barop/gwt/PushState.gwt.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@
<when-type-is class="com.google.gwt.user.client.ui.InlineHyperlink" />
</replace-with>

<!--
Path provider.
-->
<replace-with class="de.barop.gwt.client.PathProviderImpl">
<when-type-is class="de.barop.gwt.client.PathProviderInterface" />
</replace-with>

<!--
Ensures a suitable format of the history token for the current browser.
-->
Expand Down