Fix history issues

This commit is contained in:
Arne Keller 2021-03-05 10:29:54 +01:00
parent 029b088b38
commit 37b3be5066
3 changed files with 29 additions and 19 deletions

View File

@ -38,6 +38,7 @@ public class InputBar extends HorizontalLayout implements LocaleChangeObserver {
private static final short MAX_INPUT_LENGTH = 1000; private static final short MAX_INPUT_LENGTH = 1000;
private final transient Consumer<Pair<String, Map<String, String>>> callback;
private final TextField inputField; private final TextField inputField;
private TypeAssumptionsArea typeAssumptionsArea; private TypeAssumptionsArea typeAssumptionsArea;
private final Button inferTypeButton; private final Button inferTypeButton;
@ -50,6 +51,8 @@ public class InputBar extends HorizontalLayout implements LocaleChangeObserver {
* @param callback Consumer to call the inferType()-method in UpperBar * @param callback Consumer to call the inferType()-method in UpperBar
*/ */
protected InputBar(Consumer<Pair<String, Map<String, String>>> callback) { protected InputBar(Consumer<Pair<String, Map<String, String>>> callback) {
this.callback = callback;
Button infoIcon = new Button(new Icon(VaadinIcon.INFO_CIRCLE)); Button infoIcon = new Button(new Icon(VaadinIcon.INFO_CIRCLE));
infoIcon.addClickListener(event -> onInfoIconClick()); infoIcon.addClickListener(event -> onInfoIconClick());
@ -78,7 +81,7 @@ public class InputBar extends HorizontalLayout implements LocaleChangeObserver {
typeAssumptionsArea = new TypeAssumptionsArea(); typeAssumptionsArea = new TypeAssumptionsArea();
Button exampleButton = new Button(VaadinIcon.PAPERCLIP.create(), event -> onExampleButtonClick()); Button exampleButton = new Button(VaadinIcon.PAPERCLIP.create(), event -> onExampleButtonClick());
exampleButton.setId(EXAMPLE_BUTTON_ID); exampleButton.setId(EXAMPLE_BUTTON_ID);
inferTypeButton = new Button("", event -> onTypeInferButtonClick(callback)); inferTypeButton = new Button("", event -> onTypeInferButtonClick());
inferTypeButton.addClickShortcut(Key.ENTER).listenOn(this); inferTypeButton.addClickShortcut(Key.ENTER).listenOn(this);
inferTypeButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY); inferTypeButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
inferTypeButton.setId(INFER_BUTTON_ID); inferTypeButton.setId(INFER_BUTTON_ID);
@ -88,13 +91,12 @@ public class InputBar extends HorizontalLayout implements LocaleChangeObserver {
} }
/** /**
* Sets the provided string as the value of the inputField and starts the type inference algorithm. * Sets the provided string as the value of the inputField.
* *
* @param term the provided string * @param term the provided string
*/ */
protected void inferTerm(String term) { protected void setTerm(String term) {
inputField.setValue(term); inputField.setValue(term);
UI.getCurrent().getPage().executeJs("document.getElementById($0).click()", INFER_BUTTON_ID);
} }
/** /**
@ -106,15 +108,10 @@ public class InputBar extends HorizontalLayout implements LocaleChangeObserver {
typeAssumptionsArea = new TypeAssumptionsArea(typeAssumptions); typeAssumptionsArea = new TypeAssumptionsArea(typeAssumptions);
} }
private void onTypeInferButtonClick(Consumer<Pair<String, Map<String, String>>> callback) { private void onTypeInferButtonClick() {
String currentInput = inputField.getOptionalValue().orElse(StringUtils.EMPTY); String currentInput = inputField.getOptionalValue().orElse(StringUtils.EMPTY);
inputField.blur(); inputField.blur();
if ("".equals(currentInput)) {
UI.getCurrent().getPage().setTitle(getTranslation("root.typicalc"));
} else {
UI.getCurrent().getPage().setTitle(getTranslation("root.typicalc") + " - " + currentInput);
}
callback.accept(Pair.of(currentInput, typeAssumptionsArea.getTypeAssumptions())); callback.accept(Pair.of(currentInput, typeAssumptionsArea.getTypeAssumptions()));
} }
@ -123,7 +120,7 @@ public class InputBar extends HorizontalLayout implements LocaleChangeObserver {
} }
private void onExampleButtonClick() { private void onExampleButtonClick() {
Dialog exampleDialog = new ExampleDialog(this::inferTerm); Dialog exampleDialog = new ExampleDialog(this::setTerm);
exampleDialog.open(); exampleDialog.open();
} }

View File

@ -20,6 +20,7 @@ import java.nio.charset.StandardCharsets;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -41,6 +42,7 @@ public class MainViewImpl extends AppLayout
public static final String PAGE_TITLE = "Typicalc"; public static final String PAGE_TITLE = "Typicalc";
private final UpperBar upperBar; private final UpperBar upperBar;
private transient Optional<TypeInferenceView> tiv = Optional.empty();
/** /**
* Creates a new MainViewImpl. * Creates a new MainViewImpl.
@ -55,8 +57,8 @@ public class MainViewImpl extends AppLayout
@Override @Override
public void setTypeInferenceView(TypeInfererInterface typeInferer) { public void setTypeInferenceView(TypeInfererInterface typeInferer) {
TypeInferenceView tiv = new TypeInferenceView(typeInferer); tiv = Optional.of(new TypeInferenceView(typeInferer));
setContent(tiv); setContent(tiv.get());
} }
@Override @Override
@ -67,6 +69,7 @@ public class MainViewImpl extends AppLayout
@Override @Override
public void beforeEnter(BeforeEnterEvent event) { public void beforeEnter(BeforeEnterEvent event) {
tiv = Optional.empty();
if (event.getLocation().getPath().matches(TypeInferenceView.ROUTE + "/.*")) { if (event.getLocation().getPath().matches(TypeInferenceView.ROUTE + "/.*")) {
Location url = event.getLocation(); Location url = event.getLocation();
List<String> segments = url.getSegments(); List<String> segments = url.getSegments();
@ -83,6 +86,11 @@ public class MainViewImpl extends AppLayout
} }
} }
@Override
protected void afterNavigation() {
// this method ensures that the content is visible after navigation
tiv.ifPresent(this::setContent);
}
private void setTermInURL(Pair<String, Map<String, String>> lambdaTermAndAssumptions) { private void setTermInURL(Pair<String, Map<String, String>> lambdaTermAndAssumptions) {
String lambdaTerm = lambdaTermAndAssumptions.getLeft(); String lambdaTerm = lambdaTermAndAssumptions.getLeft();
@ -99,7 +107,7 @@ public class MainViewImpl extends AppLayout
if (types.length() > 0) { if (types.length() > 0) {
typeAssumptions = "?" + types.toString(); typeAssumptions = "?" + types.toString();
} }
UI.getCurrent().getPage().getHistory().replaceState(null, UI.getCurrent().getPage().getHistory().pushState(null,
new Location(TypeInferenceView.ROUTE + "/" + lambdaTerm + typeAssumptions)); new Location(TypeInferenceView.ROUTE + "/" + lambdaTerm + typeAssumptions));
} }

View File

@ -62,25 +62,30 @@ public class UpperBar extends HorizontalLayout {
} }
/** /**
* Starts the type inference algorithm by passing the required arguments to the MainViewListener. * Starts the type inference algorithm by passing the required arguments to the MainViewListener
* and updating the URL.
* *
* @param termAndAssumptions the lambda term to be type-inferred and the type assumptions to use * @param termAndAssumptions the lambda term to be type-inferred and the type assumptions to use
*/ */
protected void typeInfer(Pair<String, Map<String, String>> termAndAssumptions) { protected void typeInfer(Pair<String, Map<String, String>> termAndAssumptions) {
setTermInURL.accept(termAndAssumptions); setTermInURL.accept(termAndAssumptions);
presenter.typeInferLambdaString(termAndAssumptions.getLeft(), termAndAssumptions.getRight()); startInfer(termAndAssumptions.getLeft(), termAndAssumptions.getRight());
}
private void startInfer(String term, Map<String, String> typeAssumptions) {
presenter.typeInferLambdaString(term, typeAssumptions);
} }
/** /**
* Calls the inferTerm method in {@link InputBar} with the provided * Sets the lambda term and type assumptions in the InputBar and starts the inference.
* string as the argument.
* *
* @param term the provided string * @param term the provided string
* @param typeAssumptions type assumptions to use * @param typeAssumptions type assumptions to use
*/ */
protected void inferTerm(String term, Map<String, String> typeAssumptions) { protected void inferTerm(String term, Map<String, String> typeAssumptions) {
inputBar.setTypeAssumptions(typeAssumptions); inputBar.setTypeAssumptions(typeAssumptions);
inputBar.inferTerm(term); inputBar.setTerm(term);
startInfer(term, typeAssumptions);
} }
private void onHelpIconClick() { private void onHelpIconClick() {