Skip to content

Commit

Permalink
fix all existing relative markdown links that point to #! places. Als…
Browse files Browse the repository at this point in the history
…o, reliably find the GWT place token from other URLs (used for refreshing the page using the placechanger)
  • Loading branch information
jay-hodgson committed May 15, 2024
1 parent 4956042 commit b8dc40b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,17 +190,19 @@ public PlaceChanger getPlaceChanger() {
new PlaceChanger() {
@Override
public void goTo(Place place) {
synapseJSNIUtils.setIsInnerProgrammaticHistoryChange();
// If we are not already on this page, go there.
if (!placeController.getWhere().equals(place)) {
try {
placeController.goTo(place);
} catch (Exception e) {
synapseJSNIUtils.consoleError(e.getMessage());
if (place != null) {
synapseJSNIUtils.setIsInnerProgrammaticHistoryChange();
// If we are not already on this page, go there.
if (!placeController.getWhere().equals(place)) {
try {
placeController.goTo(place);
} catch (Exception e) {
synapseJSNIUtils.consoleError(e.getMessage());
}
} else {
// We are already on this page but we want to force it to reload.
eventBus.fireEvent(new PlaceChangeEvent(place));
}
} else {
// We are already on this page but we want to force it to reload.
eventBus.fireEvent(new PlaceChangeEvent(place));
}
}
};
Expand Down Expand Up @@ -409,15 +411,13 @@ public String getSynapseVersion() {
public void refreshPage() {
// get the place associated to the current url
AppPlaceHistoryMapper appPlaceHistoryMapper = getAppPlaceHistoryMapper();
String path = synapseJSNIUtils.getLocationPath();

String place = DEFAULT_REFRESH_PLACE;
String[] pathItems = path.split("/");
String url = synapseJSNIUtils.getCurrentURL();

if (pathItems.length > 1) {
place = pathItems[1];
String placeToken = DEFAULT_REFRESH_PLACE;
if (url.contains(":")) {
placeToken = StringUtils.getGWTPlaceTokenFromURL(url);
}
Place currentPlace = appPlaceHistoryMapper.getPlace(place);
Place currentPlace = appPlaceHistoryMapper.getPlace(placeToken);
getPlaceChanger().goTo(currentPlace);
}

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/sagebionetworks/web/client/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,16 @@ public static String toTitleCase(String s) {

return output.toString();
}

public static String getGWTPlaceTokenFromURL(String url) {
String placeToken = null;
if (url.contains(":")) {
// optimization - use place changer in this case as well
int colonIndex = url.lastIndexOf(':');
// Search backwards from colonIndex to find the last '/'
int lastSlashIndex = url.lastIndexOf('/', colonIndex);
placeToken = url.substring(lastSlashIndex + 1);
}
return placeToken;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.gwtbootstrap3.client.ui.html.Italic;
import org.sagebionetworks.web.client.GWTWrapper;
import org.sagebionetworks.web.client.GlobalApplicationState;
import org.sagebionetworks.web.client.StringUtils;
import org.sagebionetworks.web.client.SynapseJSNIUtils;
import org.sagebionetworks.web.client.mvp.AppPlaceHistoryMapper;
import org.sagebionetworks.web.client.utils.Callback;
Expand Down Expand Up @@ -48,11 +49,19 @@ public interface Binder extends UiBinder<Widget, MarkdownWidgetViewImpl> {}
if (Event.ONCLICK == event.getTypeInt()) {
AnchorElement el = (AnchorElement) event.getCurrentTarget();
String href = el.getHref();
String placeToken = href.substring(href.indexOf('!'));
AppPlaceHistoryMapper appPlaceHistoryMapper =
globalAppState.getAppPlaceHistoryMapper();
Place newPlace = appPlaceHistoryMapper.getPlace(placeToken);
globalAppState.getPlaceChanger().goTo(newPlace);
String placeToken = null;
// Detect old #!, and direct to the right place
if (href.contains("#!")) {
placeToken = href.substring(href.indexOf('!') + 1);
} else {
placeToken = StringUtils.getGWTPlaceTokenFromURL(href);
}
if (placeToken != null) {
AppPlaceHistoryMapper appPlaceHistoryMapper =
globalAppState.getAppPlaceHistoryMapper();
Place newPlace = appPlaceHistoryMapper.getPlace(placeToken);
globalAppState.getPlaceChanger().goTo(newPlace);
}
}
};

Expand Down Expand Up @@ -91,10 +100,10 @@ private void addPlaceChangerEventHandlerToAnchors() {
NodeList<Element> anchors = contentPanel
.getElement()
.getElementsByTagName("a");
String hostPageURL = gwt.getHostPageBaseURL();
String hostPrefix = gwt.getHostPrefix();
for (int i = 0; i < anchors.getLength(); i++) {
AnchorElement anchorElement = (AnchorElement) anchors.getItem(i);
if (anchorElement.getHref().startsWith(hostPageURL)) {
if (anchorElement.getHref().startsWith(hostPrefix)) {
DOM.sinkEvents(
anchorElement,
Event.ONCLICK | Event.ONMOUSEOUT | Event.ONMOUSEOVER
Expand Down

0 comments on commit b8dc40b

Please sign in to comment.