2

I have a GWT Places/Activities web application. I use Anchor's click event to move user in a new "place", so there's no way user can open any "link" in a new window or tab. The question is - how do I make all these links real?

Andrey Agibalov
  • 7,624
  • 8
  • 66
  • 111

2 Answers2

7

While using a Hyperlink (or InlineHyperlink) works, I'd rather use a ClickHandler on an Anchor, because if any activity returns a non-null value in mayStop the URL wouldn't have changed yet (whereas with a Hyperlink, it would have, even if the user chooses to cancel the navigation).

To set the target (href) on the Anchor, simply use your PlaceHistoryMapper. And to correctly handle the ctrl+click, middle-click and right-click, reuse the HyperlinkImpl:

Anchor anchor = new Anchor("text", "#" + placeHistoryMapper.getToken(targetPlace));
anchor.addClickHandler(new ClickHandler() {
   private static final HyperlinkImpl IMPL = GWT.create(HyperlinkImpl.class);

   @Override
   public void onClick(ClickEvent event) {
      if (IMPL.handleAsClick(event)) {
         placeController.goTo(targetPlace);
         event.preventDefault();
      }
   }
});
Thomas Broyer
  • 64,353
  • 7
  • 91
  • 164
  • +1 - nice. When using activities and places this is almost always the behaviour you want. It would be nice if this was built into GWT so we don't have to resort to "secret plans and clever tricks" to get something as fundamental as this working as expected. – pillingworth Oct 20 '11 at 15:16
  • @Thomas Broyer: I've create a playground app to check this idea and the behavior is pretty strange. When I load the page for the very first time, all three "Left click", "Right-Menu-Open in new tab" and "Ctrl click" work fine. But when I left click and then go back "Ctrl click" doesn't work anymore - it now does the same what non-ctrl left click does. Reloading the whole page fixes this. Thoughts? (I use Chrome if it matters) – Andrey Agibalov Oct 21 '11 at 14:27
3

Maybe instead of using Anchor and listening to the ClickEvent use Hyperlink with the targetHistoryToken set to the Place name.

e.g.

<g:Hyperlink text="Home" targetHistoryToken="home" />

pillingworth
  • 3,238
  • 2
  • 24
  • 49