You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PushStateAndReplaceState.java 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package com.vaadin.tests.components.ui;
  2. import java.net.URI;
  3. import com.vaadin.annotations.Title;
  4. import com.vaadin.server.Page;
  5. import com.vaadin.server.VaadinRequest;
  6. import com.vaadin.tests.components.AbstractReindeerTestUI;
  7. import com.vaadin.ui.Button;
  8. import com.vaadin.ui.CheckBox;
  9. import com.vaadin.ui.Label;
  10. import com.vaadin.ui.Notification;
  11. @Title("Original title")
  12. public class PushStateAndReplaceState extends AbstractReindeerTestUI {
  13. private final Label locationLabel = new Label();
  14. private CheckBox replace;
  15. @Override
  16. protected void setup(VaadinRequest request) {
  17. locationLabel.setId("locationLabel");
  18. addComponent(locationLabel);
  19. updateLabel();
  20. getPage().addPopStateListener(event -> {
  21. Notification.show("Popstate event");
  22. updateLabel();
  23. });
  24. replace = new CheckBox("replace");
  25. replace.setId("replace");
  26. addComponent(replace);
  27. addComponent(createButton("test", "Move to ./test",
  28. Page.getCurrent().getLocation() + "/test"));
  29. addComponent(createButton("X", "Move to X", "X"));
  30. addComponent(createButton("root_X", "Move to /X", "/X"));
  31. }
  32. private Button createButton(String id, String caption,
  33. final String newUri) {
  34. Button button = new Button(caption, event -> {
  35. getPage().setTitle(caption);
  36. if (replace.getValue()) {
  37. getPage().replaceState(newUri);
  38. } else {
  39. getPage().pushState(newUri);
  40. }
  41. updateLabel();
  42. });
  43. button.setId(id);
  44. return button;
  45. }
  46. private void updateLabel() {
  47. URI location = getPage().getLocation();
  48. locationLabel.setValue("Current Location: " + location);
  49. }
  50. @Override
  51. public String getTestDescription() {
  52. return "Modern web framework shouldn't force you to use hashbang style urls for deep linking";
  53. }
  54. @Override
  55. protected Integer getTicketNumber() {
  56. return null;
  57. }
  58. }